1228 apple trees
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description
Description
Outside the Kaka's house, there is an apple tree. Every spring, there is always a lot of apples on the tree. Kaka was very fond of apples, so he always cared for the apple tree. We know that there are a lot of fork points in the tree, Apple will be on the fork point of the branches, and no more than two apples knot together. Kaka would like to know the number of apples on the subtree represented by a fork point, so as to study which branches of the apple tree have a stronger result.
What Kaka knows is that some of the apples will be on some forks at some point, but what Kaka doesn't know is that there are always some naughty kids to pick some apples off the tree.
So we define two operations:
C x |
Indicates that the state of the fork point with the number x is changed (the original is an apple, it is removed, the original is not, the knot an apple) |
G x |
How many apples are there in the subtree represented by a fork point that has a number x? |
We assume that at the outset, all the trees were apples, and also included the fork 1 as the root node.
Enter a description
Input Description
First row one number n (n<=100000)
Next n-1 line, 2 number u,v per line, indicates that the fork point U and Fork Point v are directly connected.
Next line a number M, (m<=100000) indicates the number of queries
The next M-line, which represents the query, asks the format as described in the question Q X or C x
Output description
Output Description
For each q x query, output the corresponding result, each line output a
Sample input
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample output
Sample Output
3
2
/*Dfs First, to find the DFS sequence and the subtree size of each point tree becomes a sequence, you can maintain the number of apples with the line segment tree List1[i]=j: The node I of the DFS sequence is J List2[i]=j:dfs sequence I for the nodes of J with X as the root The Dfs order for each node of a subtree of a node is list1[x]~list1[x]+sz[x]-1 if x is the DFS order for the node, then the subtree number DFS sequence is x~x+sz[list2[x]]-1*/#include<iostream>#include<cstdio>using namespacestd;Const intmaxn=100010;intSZ[MAXN],NUM,HEAD[MAXN],N,LIST1[MAXN],P,M,OPX,LIST2[MAXN];structnode1{intPre,to;} E[MAXN];structnode2{intL,r,v;} TR[MAXN<<4];voidInsert (int from,intTo ) {e[++num].to=to ; E[num].pre=head[ from]; head[ from]=num;}voidDfsintNow ) {Sz[now]=1; list1[now]=++p;list2[p]=Now ; for(intI=head[now];i;i=e[i].pre) {DFS (e[i].to); Sz[now]+=Sz[e[i].to]; }}voidBuildintLintRintk) {TR[K].L=l;tr[k].r=r;tr[k].v=1; if(L==R)return; intMid= (l+r) >>1; Build (L,mid,k<<1); Build (Mid+1,r,k<<1|1); TR[K].V=tr[k<<1].v+tr[k<<1|1].v;}Charch[5];voidChangeintk) { if(tr[k].l==TR[K].R) {TR[K].V=!tr[k].v; return; } intMid= (TR[K].L+TR[K].R) >>1; if(opx<=mid) Change (k<<1); ElseChange (k<<1|1); TR[K].V=tr[k<<1].v+tr[k<<1|1].v;}intQuery (intLintRintk) { if(tr[k].l==l&&tr[k].r==R)returntr[k].v; intMid= (TR[K].L+TR[K].R) >>1; if(R<=mid)returnQuery (l,r,k<<1); Else if(L>mid)returnQuery (l,r,k<<1|1); Else returnQuery (l,mid,k<<1) +query (mid+1,r,k<<1|1);}intMain () {scanf ("%d",&N); intx, y; for(intI=1; i<n;i++) {scanf ("%d%d",&x,&y); Insert (x, y); } DFS (1); Build (1N1); scanf ("%d",&m); for(intI=1; i<=m;i++) {scanf ("%s%d",ch,&opx); OPX=LIST1[OPX]; if(ch[0]=='C') Change (1); if(ch[0]=='Q') {printf ("%d\n", Query (opx,opx+sz[list2[opx]]-1,1)); } }}
Codevs 1228 Apple Tree