Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25904 | Accepted: 7682 |
Description
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
![](http://poj.org/images/3321_1.gif)
Input
The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
Source
题目连接:POJ 3321
很久以前就想做,但不知道如何转换,跟另外一题差不多的意思,主要是进行单点修改的应该怎么写要知道据说树状数组更快,然而并不是是很懂树状数组的原理,只会模版,就直接用线段树做了。
代码:
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define INF 0x3f3f3f3f #define MM(x,y) memset(x,y,sizeof(x)) #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) typedef pair<int,int> pii; typedef long long LL; const double PI=acos(-1.0); const int N=100010; struct seg { int l,mid,r; int sum; }; seg T[N<<2]; struct edge { int to; int pre; }E[N]; int head[N],cnt,in[N],out[N],total; void add(int s,int t) { E[cnt].to=t; E[cnt].pre=head[s]; head[s]=cnt++; } void init() { MM(head,-1); cnt=0; MM(in,0); MM(out,0); total=0; } void dfs(int now,int fa) { in[now]=++total; for (int i=head[now]; ~i; i=E[i].pre) { int v=E[i].to; if(v!=fa) { dfs(v,now); } } out[now]=total; } void pushup(int k) { T[k].sum=T[LC(k)].sum+T[RC(k)].sum; } void build(int k,int l,int r) { T[k].l=l; T[k].r=r; T[k].mid=MID(l,r); if(l==r) T[k].sum=1; else { build(LC(k),l,T[k].mid); build(RC(k),T[k].mid+1,r); pushup(k); } } void update(int k,int x) { if(T[k].l==T[k].r&&T[k].l==x) T[k].sum^=1; else { if(x<=T[k].mid) update(LC(k),x); else update(RC(k),x); pushup(k); } } int query(int k,int l,int r) { if(l<=T[k].l&&T[k].r<=r) return T[k].sum; else { if(r<=T[k].mid) return query(LC(k),l,r); else if(l>T[k].mid) return query(RC(k),l,r); else return query(LC(k),l,T[k].mid)+query(RC(k),T[k].mid+1,r); } } int main(void) { int n,m,q,i,x,y,c; char ops[3]; while (~scanf("%d",&n)) { init(); for (i=0; i<n-1; ++i) { scanf("%d%d",&x,&y); add(x,y); } dfs(1,-1); build(1,1,total); scanf("%d",&m); while (m--) { scanf("%s",ops); if(ops[0]=='C') { scanf("%d",&c); update(1,in[c]); } else if(ops[0]=='Q') { scanf("%d",&q); printf("%d ",query(1,in[q],out[q])); } } } return 0; }