Title 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
Outputs description Output Description
For each q x query, output the corresponding result, each line output a
Sample input to sample
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample output Sample Outputs
3
2
The puzzle: We know that the DFS sequence is continuous in a subtrees tree. So first find the DFS sequence for this tree and record the location of each node in the DFS sequence. Record the maximum number of Dfs sequences in each subtrees tree. A line segment tree is then built according to the DFS sequence. The rest is the same as the bare-wire section tree.
Note that there are line breaks in the data, very deceptive ...
#include <iostream>#include <cstdio>using namespace Std;intt[1000001],mx[100001],POS[1000001],point[1000001],Next[1000001],cnt,sz;struct Use{intSt,en;} b[1000001];intN,u,v,m, Aa,bb;char ch;void Add (int x,int y){Next[++cnt]=point[x];p oint[x]=cnt; b[cnt].st=x; b[cnt].en=y;} void Dfs (int x,intFA) {POS[x]=++sz;mx[x]=sz; for(inti=point[x];i;i=Next[i]) {if(B[I].EN==FA)Continue; DFS (B[i].en,x); mx[x]=max (mx[x],mx[b[i].en]); }}void Build (intKintLintR) {intMidif(L==R) {t[k]=1;return;}; Mid= (L+R)/2; Build2*k, L,mid); Build2*k+1, mid+1, R); t[k]=t[2*k]+t[2*k+1];} void Change (intKintLintRint x){intMidif(l==r&&l==x) {if(t[k]==1) t[k]=0;Elset[k]=1;return; } mid= (L+r)/2;if(x<=mid) Change (2*k, L,mid,x);if(x>mid) Change (2*k+1, mid+1Rx); t[k]=t[2*k]+t[2*k+1];}intQsum (intKintLintRintllintRR) {intMid,ans (0);if(LL<=L&&R<=RR)returnT[K]; Mid= (L+R)/2;if(Ll<=mid) Ans+=qsum (2*k, L,MID,LL,RR);if(MID<RR) Ans+=qsum (2*k+1, mid+1, R,LL,RR);returnAns }intMain () {scanf ("%d", &n); for(intI=1; i<n;i++) {scanf ("%d%d", &u,&v); add (u,v); add (v,u);} Dfs1,0); Build (1,1, N);cin>>m; for(intI=1; i<=m; i++) {scanf ("%*C%*c%c%d", &CH,&AA);if(ch==' C ') {Change (1,1NPOS[AA]); }if(ch==' Q ') {printf("%d\ n", Qsum (1,1NPOS[AA],MX[AA])); } }}
"codevs1228" apple Tree "segment tree +dfs sequence"