Test instructions
Give you a tree, the initial nodes have an apple, give two kinds of operation,C x means that if the X node has an apple to take away, no apple will grow one.
Q x queries How many apples are in the subtree with the root x.
Analysis:
Start the problem, the ancestors by the child's label can not be determined, I would like to be able to renumber
And a tree we numbered in the first order this ensures that a subtree is in a contiguous interval, and then it is a bit.
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<11#defineAll 1,n,1#defineN 100010#defineRead Freopen ("In.txt", "R", stdin)Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;structedge{intV,next;} E[n*2];intused[n],bit[n],l[n],r[n],id,f[n],n,head[n*2];intLen;voidAdd_edge (intUintv) {E[LEN].V=v; E[len].next=Head[u]; Head[u]=len++;}voidAddintXintd) { while(x<=N) {Bit[x]+=D; X+=x& (-x); }}intSumintx) { intnum=0; while(x>0) {num+=Bit[x]; X-=x& (-x); } returnnum;}voidDfsinti) {L[i]=++id;//The root node number is the left edge of the subtree.used[i]=1; for(intj=head[i];j!=-1; j=E[j].next) { if(!USED[E[J].V]) {DFS (E[J].V); }} R[i]=id;//the last descendant's numbered right border}intMain () { while(~SCANF ("%d",&N)) {memset (bit,0,sizeof(bit)); Memset (F,0,sizeof(f)); memset (Used,0,sizeof(used)); memset (Head,-1,sizeof(head)); intu,v; Len=0; for(intI=0; i<n-1;++i) {scanf ("%d%d",&u,&v); Add_edge (U,V); Add_edge (V,u); } ID=0; DFS (1); for(intI=1; i<=n;++i) Add (i,1); intm,fork; Charop[3]; scanf ("%d",&m); while(m--) {scanf ("%s%d",op,&fork); if(op[0]=='C'){ if(F[fork]) {Add (L[fork],1); F[fork]=0; } Else{Add (L[fork],-1); F[fork]=1; } } Else if(op[0]=='Q') {printf ("%d\n", SUM (R[fork])-sum (l[fork]-1)); } } }return 0;}
POJ 3321-apple Tree (designator +bit)