Assign the task
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 636 Accepted Submission(s): 322
Problem Description
There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody's boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.
The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.
Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.
The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.
Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.
Input
The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.
For each test case:
The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.
The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).
The next line contains an integer M (M ≤ 50,000).
The following M lines each contain a message which is either
"C x" which means an inquiry for the current task of employee x
or
"T x y"which means the company assign task y to employee x.
(1<=x<=N,0<=y<=10^9)
For each test case:
The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.
The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).
The next line contains an integer M (M ≤ 50,000).
The following M lines each contain a message which is either
"C x" which means an inquiry for the current task of employee x
or
"T x y"which means the company assign task y to employee x.
(1<=x<=N,0<=y<=10^9)
Output
For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.
Sample Input
1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3
Sample Output
Case #1:
-1
1
2
Source
用线段树修改区间值,查询单点值。
好久没写线段树了,这都写挫。。。
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 19:50:24 4 File Name :E:2013ACM比赛练习2013-11-17C.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 21 const int MAXN = 50010; 22 struct Edge 23 { 24 int to,next; 25 }edge[MAXN]; 26 int head[MAXN],tot; 27 int cnt; 28 int start[MAXN],end[MAXN]; 29 void init() 30 { 31 cnt = 0; 32 tot = 0; 33 memset(head,-1,sizeof(head)); 34 } 35 void addedge(int u,int v) 36 { 37 edge[tot].to = v; 38 edge[tot].next = head[u]; 39 head[u] = tot++; 40 } 41 void dfs(int u) 42 { 43 ++cnt; 44 start[u] = cnt; 45 for(int i = head[u];i != -1;i = edge[i].next) 46 { 47 dfs(edge[i].to); 48 } 49 end[u] = cnt; 50 } 51 struct Node 52 { 53 int l,r; 54 int val; 55 int lazy; 56 }segTree[MAXN*4]; 57 void Update_Same(int r,int v) 58 { 59 if(r) 60 { 61 segTree[r].val = v; 62 segTree[r].lazy = 1; 63 } 64 } 65 void push_down(int r) 66 { 67 if(segTree[r].lazy) 68 { 69 Update_Same(r<<1,segTree[r].val); 70 Update_Same((r<<1)|1,segTree[r].val); 71 segTree[r].lazy = 0; 72 } 73 } 74 void Build(int i,int l,int r) 75 { 76 segTree[i].l = l; 77 segTree[i].r = r; 78 segTree[i].val = -1; 79 segTree[i].lazy = 0; 80 if(l == r)return; 81 int mid = (l+r)/2; 82 Build(i<<1,l,mid); 83 Build((i<<1)|1,mid+1,r); 84 } 85 void update(int i,int l,int r,int v) 86 { 87 if(segTree[i].l == l && segTree[i].r == r) 88 { 89 Update_Same(i,v); 90 return; 91 } 92 push_down(i); 93 int mid = (segTree[i].l + segTree[i].r)/2; 94 if(r <= mid)update(i<<1,l,r,v); 95 else if(l > mid)update((i<<1)|1,l,r,v); 96 else 97 { 98 update(i<<1,l,mid,v); 99 update((i<<1)|1,mid+1,r,v); 100 } 101 } 102 int query(int i,int u) 103 { 104 if(segTree[i].l == u && segTree[i].r == u) 105 return segTree[i].val; 106 push_down(i); 107 int mid = (segTree[i].l + segTree[i].r)/2; 108 if(u <= mid)return query(i<<1,u); 109 else return query((i<<1)|1,u); 110 } 111 bool used[MAXN]; 112 int main() 113 { 114 //freopen("in.txt","r",stdin); 115 //freopen("out.txt","w",stdout); 116 int n; 117 int T; 118 scanf("%d",&T); 119 int iCase = 0; 120 while(T--) 121 { 122 iCase++; 123 printf("Case #%d: ",iCase); 124 int u,v; 125 memset(used,false,sizeof(used)); 126 init(); 127 scanf("%d",&n); 128 for(int i = 1;i < n;i++) 129 { 130 scanf("%d%d",&u,&v); 131 used[u] = true; 132 addedge(v,u); 133 } 134 for(int i = 1;i <= n;i++) 135 if(!used[i]) 136 { 137 dfs(i); 138 break; 139 } 140 Build(1,1,cnt); 141 char op[10]; 142 int m; 143 scanf("%d",&m); 144 while(m--) 145 { 146 scanf("%s",op); 147 if(op[0] == 'C') 148 { 149 scanf("%d",&u); 150 printf("%d ",query(1,start[u])); 151 } 152 else 153 { 154 scanf("%d%d",&u,&v); 155 update(1,start[u],end[u],v); 156 } 157 } 158 } 159 return 0; 160 }