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.InputThe 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)OutputFor 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 3Sample Output
Case #1: -1 1 2
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int maxn=50005,INF=200000000; inline int read(){ int out=0,flag=1;char c=getchar(); while(c<48||c>57) {if(c=='-') flag=-1;c=getchar();} while(c>=48&&c<=57) {out=out*10+c-48;c=getchar();} return out*flag; } int N,M,L,R,num[4*maxn],lazy[4*maxn],index[maxn],root=1,siz=0; class node{ public: int lson,rb,siz,f; node() {lson=rb=0;siz=1;f=0;} }e[maxn]; void preorder(int u){ index[u]=++siz; for(int k=e[u].lson;k;k=e[k].rb){ preorder(k); e[u].siz+=e[k].siz; } } void init(){ memset(lazy,-1,sizeof(lazy)); memset(num,-1,sizeof(num)); siz=0; root=1; for(int i=0;i<=N;i++){e[i].lson=e[i].rb=e[i].f=0;e[i].siz=1;} int T=N-1,a,f; while(T--){ a=read(); f=read(); e[a].rb=e[f].lson; e[a].f=f; e[f].lson=a; } while(e[root].f) root=e[root].f; preorder(root); } void pd(int u){ lazy[u<<1]=lazy[u<<1|1]=num[u<<1]=num[u<<1|1]=lazy[u]; lazy[u]=-1; } void Set(int u,int l,int r,int x){ if(l>=L&&r<=R) num[u]=lazy[u]=x; else{ if(lazy[u]!=-1) pd(u); int mid=(l+r)>>1; if(mid>=L) Set(u<<1,l,mid,x); if(mid<R) Set(u<<1|1,mid+1,r,x); num[u]=x; } } int Query(int u,int l,int r){ if(l==r) return num[u]; else{ if(lazy[u]!=-1) pd(u); int mid=(l+r)>>1; if(mid>=L) return Query(u<<1,l,mid); else return Query(u<<1|1,mid+1,r); } } int main() { int T=read(),a,b; char cmd; for(int t=1;t<=T;t++){ printf("Case #%d:\n",t); N=read(); init(); M=read(); while(M--){ cmd=getchar(); while(cmd!='T'&&cmd!='C') cmd=getchar(); if(cmd=='T'){ a=read(); b=read(); L=index[a]; R=L+e[a].siz-1; Set(1,1,N,b); } else{ L=R=index[read()]; printf("%d\n",Query(1,1,N)); } } } return 0; }