Tag: Std algo indicates cin void href space forward ref
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
After getting this topic, we first saw that there will be: there are m inquiries, every time asked 2 kinds of operations. Then you can quickly get to this topic is to use line tree.
But here's the problem, and now he's giving us a tree, and our line tree maintains a sequence, so we'll start by turning this tree into a sequence.
What do we want to do with this operation?? Right! is the DFS order!
We are now running DFS for the tree we have built, then we can find out the DFS sequence for each node, and process the size of each node's subtree, so that all of the nodes in each subtrees tree are connected to each other. A node is a subtree from this node to this node + its subtree size +1, when we ask a node of the number of apples can be converted to ask the size of this interval. When we make an apple change, we make a single point of modification to a point, we make a single point of modification, we can directly reverse it.
#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#defineN 110000using namespacestd;Charch;intN,m,q,x,y,s,tot,ans;intHead[n],size[n],list1[n],list2[n];intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}structedge{intNext,to, from;} Edge[n<<1];intAddintXinty) {Tot++; Edge[tot].to=y; Edge[tot].next=Head[x]; HEAD[X]=tot;}structtree{intL,r,w;} Tree[n*4];intDfsintx) {Size[x]=1, list1[x]=++s,list2[s]=x; for(intI=head[x];i;i=Edge[i].next) { intt=edge[i].to; DFS (t); SIZE[X]+=Size[t]; }}voidBuildintKintLintR) {TREE[K].L=l,tree[k].r=S; if(tree[k].l==TREE[K].R) {TREE[K].W=1; return ; } intMid= (TREE[K].L+TREE[K].R) >>1; Build (k<<1, L,mid); Build (k<<1|1, mid+1, R); TREE[K].W=tree[k<<1].w+tree[k<<1|1].W;}voidChange_point (intk) { if(tree[k].l==TREE[K].R) {TREE[K].W=!TREE[K].W; return ; } intMid= (TREE[K].R+TREE[K].L)/2; if(X<=mid) Change_point (k<<1); ElseChange_point (k<<1|1); TREE[K].W=tree[k<<1].w+tree[k<<1|1].W;}voidAsk_interval (intk) { if(tree[k].l>=x&&tree[k].r<=y) {ans+=TREE[K].W; return ; } intMid= (TREE[K].L+TREE[K].R) >>1; if(X<=mid) Ask_interval (k<<1); if(Y>mid) Ask_interval (k<<1|1);}intMain () {n=read (); for(intI=1; i<n;i++) x=read (), y=read (), add (x, y); DFS (1); Build (1,1, N); M=read (); while(m--) {cin>>ch; Q=read (); X=list1[q];ans=0; if(ch=='C') Change_point (1); Else{y=x+size[q]-1; Ask_interval (1); printf ("%d\n", ans); } } return 0;}
codevs--1228 Apple Tree