Apple TreeTime
limit:2000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U SubmitStatus
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 the integers u and v , which means fork u and fork v are connected by a branch.
The next line contains an integer m ( m ≤100,000).
the following M lines each contain a message which be either
" c x strong> "which means the existence of the apple on fork x has 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 F ork x , including the apple (if exists) on the fork x
Note The tree was 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
/*Test instructions: give you a binary tree, the root node is 1, the initial time each node has an Apple, two operations: C:x, on the X-node of the Apple number of different or q:x, query the X node as the root node of the tree of the Apple number of the initial thinking: actually is the map, and then Dfs run over, Record the left and right intervals of each node, note that the points in the interval represented by a node are represented as continuous points, and then record the interval length of the points within the interval, and then use the tree array to add the interval sum of the single point update # timeout: It may be time-out when initializing*/#include<iostream>#include<stdio.h>#include<string.h>#include<vector>using namespacestd;Const intn=220000;intn,m;Charstr[Ten];intCount=1;//the node used to represent the re-taggingintStart[n];//used to mark the starting position of the node intervalintEnd[n];//used to mark the end of a node intervalintLowbit[n];intVal[n];intu,v;intc[n];typedef Vector<int>Vct_int;vector<vct_int>edge (n/2);//for building diagrams (trees)voidDfsintx) {Start[x]=++Count; for(intI=0; I<edge[x].size (); i++) {DFS (edge[x][i]); } End[x]=++Count;}voidAddintXintval) { while(x<=Count) {C[x]+=Val; X+=Lowbit[x]; }}intSumintx) { intres=0; while(x>0) {res+=C[x]; X-=Lowbit[x]; } returnRes;}intMain () {//freopen ("In.txt", "R", stdin);scanf"%d",&N); for(intI=0; i<n-1; i++) {scanf ("%d%d",&u,&v); Edge[u].push_back (v);//Building Map} Count=0; DFS (1);//Search for tags starting with dot 1 for(intI=1; i<=n;i++) {Val[i]=1; } //If you use Add (i,1), it will time out, can only find the value of each, direct initialization//c[i]=i-(I-(lowbit (i))); //Is the number of apples from 1 to I minus the nearest left of the distance I point . for(intI=1; i<=count;i++) {Lowbit[i]=i& (i^ (i-1)); } for(intI=1; i<=count;i++) {C[i]=i-(I-Lowbit[i]); }//In the initial state, each node has an Applescanf"%d",&m); for(intI=0; i<m;i++) {scanf ("%s%d",str,&T); if(str[0]=='C'){ if(Val[u]) {Add (Start[u],-1); Add (End[u],-1); Val[u]=0; }Else{Add (Start[u],1); Add (End[u],1); Val[u]=1; } }Else{ intx1=sum (end[u]); intX2=sum (start[u]); printf ("%d\n", (X1-X2)/2+Val[u]); } } return 0;}
Poj 3321Apple Tree