Apple Tree
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 20335 |
|
Accepted: 6182 |
Description
There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would grow in the tree. Kaka likes Apple very much, so he has been carefully nurturing the Big Apple tree.
The tree has N forks which is connected by branches. Kaka numbers the forks by 1 to N and the root are always numbered by 1. Apples'll grow on the forks and both Apple won ' t grow on the same fork. Kaka wants to know how many apples is there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka?
Input
The first line contains a integer n (n ≤100,000), which is the number of the forks in the tree.
The following N -1 lines each contain double integers u and v, which means fork u and fo RK v is connected by a branch.
The next line contains an integer m (m ≤100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the Apple on Fork x have been changed. i.e. if there is a apple on the fork and then Kaka pick it; Otherwise a new Apple has grown on the empty fork.
Or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, I Ncluding the apple (if exists) on the fork X
Note The tree is full of apples at the beginning
Output
For every inquiry, the output of the correspond answer per line.
Sample Input
33Q 1C 2Q 1
Sample Output
32
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <stack>9#include <queue>Ten#include <Set> One#include <map> A#include <list> -#include <iomanip> -#include <cstdlib> the#include <sstream> - using namespacestd; -typedefLong LongLL; - Const intinf=0x5fffffff; + Const Doubleexp=1e-6; - Const intms=200005; + intCNT; Avector<vector<int> > Edge (ms/2); at intflag[ms/2]; - intL[ms],r[ms]; - intC[ms]; - - voidDfsintCur//partition with DFS interval - { inl[cur]=++CNT; - for(intI=0; I<edge[cur].size (); i++) to DFS (Edge[cur][i]); +r[cur]=++CNT; - } the * intLowbit (intx) $ {Panax Notoginseng returnx& (-x); - } the + voidUpdata (intXintd) A { the while(x<=CNT) + { -c[x]+=D; $x+=lowbit (x); $ } - } - the intGetsum (intx) - {Wuyi intret=0; the while(x>0) - { Wuret+=C[x]; -x-=lowbit (x); About } $ returnret; - } - - intMain () A { + intn,m,x,y; thescanf"%d",&N); - for(intI=0; i<n-1; i++) $ { thescanf"%d%d",&x,&y); the edge[x].push_back (y); the } theCnt=0; -Dfs1); inMemset (c,0,sizeof(c)); the for(intI=1; i<=n;i++) the { AboutUpdata (L[i],1); theUpdata (R[i],1); the } thescanf"%d",&M); + CharCmd[ms]; - for(intI=1; i<=n;i++) theflag[i]=1;Bayi while(m--) the { thescanf"%s%d",cmd,&x); - if(cmd[0]=='Q') - { the intT1=getsum (l[x]-1); the intT2=getsum (r[x]); theprintf"%d\n", (T2-T1)/2); the } - Else the { the if(Flag[x]) the {94Updata (l[x],-1); theUpdata (r[x],-1); theflag[x]=0; the }98 Else About { -Updata (L[x],1);101Updata (R[x],1);102flag[x]=1;103 }104 } the }106 return 0;107}
Apple Tree (a tree-like array that requires preprocessing)