1228 apple trees
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle 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
/*The problem is a tree. The order of the points is not arranged according to the order of the nodes in the segment tree, so we will try to integrate this node into the line tree. First, save the dots and the edges, and then we think about putting the nodes into a linear interval, so it's the time that DFS allows maintenance to traverse and end, forming an interval. And then each node is the first value of its interval. Segment Tree Complete single-point modification and interval query*/#include<iostream>#include<cstdio>#include<cstring>#defineMAXN 100010using namespacestd;intN,M,X,Y,Z,CNT,TOT,TIME1[MAXN],TIME2[MAXN];intHEAD[MAXN],VIS[MAXN];CharC;structnode0{int from, To,dis,next;} E[MAXN*2];structnode{intL,r,dis,flag;} TRE[MAXN*4];voidAddint from,intTo ) {e[++CNT]. from= from; E[cnt].to=to ; E[cnt].next=head[ from]; head[ from]=CNT;}voidBuildintNowintLintR) {TRE[NOW].L=l;tre[now].r=R; if(l==r) {Tre[now].dis=1; return; } intMid= (l+r) >>1; Build ( Now<<1, L,mid); Build ( Now<<1|1, mid+1, R); Tre[now].dis=tre[now<<1].dis+tre[now<<1|1].dis;}voidDfs (intz) {Time1[z]=++tot; VIS[Z]=1; for(intI=head[z];i;i=E[i].next) { intv=e[i].to; if(!Vis[v]) Dfs (v); } Time2[z]=tot;}voidF_change (intNowintk) { intL=tre[now].l,r=TRE[NOW].R; if(l==r) {if(!tre[now].dis) tre[now].dis=1; Elsetre[now].dis=0; return; } intMid= (l+r) >>1; if(K>mid) F_change (now<<1|1, K); if(K<=mid) F_change (now<<1, K); Tre[now].dis=tre[now<<1].dis+tre[now<<1|1].dis;}intQuery (intNowintLintR) { if(tre[now].l==l&&tre[now].r==r) {returnTre[now].dis; } Tre[now].dis=tre[now<<1].dis+tre[now<<1|1].dis; intMid= (TRE[NOW].L+TRE[NOW].R) >>1; if(R<=mid)returnQuery (now<<1, L,r); Else if(L>mid)returnQuery (now<<1|1, L,r); Else { returnQuery (now<<1, L,mid) +query (now<<1|1, mid+1, R); }}intMain () {scanf ("%d",&N); Build (1,1, N); for(intI=1; i<n;i++) {scanf ("%d%d",&x,&X); Add (x, y); add (y,x); } Dfs (1); scanf ("%d",&m); for(intI=1; i<=m;i++) {cin>>c>>Z; if(c=='Q') printf ("%d\n", Query (1, Time1[z],time2[z])); if(c=='C') F_change (1, Time1[z]); } return 0;}If the heart is sunny, no words sorrow
Apple Tree (line segment tree +dfs sequence)