Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16705 Accepted: 5051
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?
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 For every inquiry, output the correspond answer per line.
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
Source POJ Monthly--2007.08.05, Huang, Jinsong
很明顯的一個維護單點查詢區間的題目 但是一開始絲毫看不出來任何區間的意思 明明是棵樹啊。。。
其實 有一種東西叫做 時間戳記 這是什麼呢。 時間戳記就是按dfs序給到過的點打上標記 一個開始的時間 一個結束的時間 那麼從開始時間到結束時間的這段時間就是它和它子節點的區間
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int lim=200011;int sumv[lim<<2],bt[lim],et[lim];struct self{int x,y;}s[lim<<1];int first[lim<<1],nxt[lim<<1];int m,n,a,b,c;char chin;void pushup(int o){sumv[o]=sumv[o<<1]+sumv[o<<1|1];}int x,y;int query(int o,int l,int r){ if(l>=x&&r<=y)return sumv[o]; int m=(l+r)>>1,ret=0; if(x<=m)ret+=query(o<<1,l,m); if(y>m)ret+=query(o<<1|1,m+1,r); return ret;}void update(int o,int l,int r){ if(l==r) { sumv[o]^=1; return; } int m=(l+r)>>1; if(x<=m)update(o<<1,l,m); else update(o<<1|1,m+1,r); pushup(o);}void makeside(int i,int x,int y){ s[i].x=x;s[i].y=y; nxt[i]=first[x]; first[x]=i;}void build(int o,int l,int r){ if(l==r) { sumv[o]=1; return; } int m=(l+r)>>1; build(o<<1,l,m); build(o<<1|1,m+1,r); pushup(o);}bool flag[lim];int t;void maketree(int i){ flag[i]=1; t++; bt[i]=t; for(int e=first[i];e!=-1;e=nxt[e]) if(!flag[s[e].y])maketree(s[e].y); et[i]=t;} int main(){ scanf("%d\n",&m); memset(first,-1,sizeof(first)); memset(nxt,-1,sizeof(nxt)); for(a=1;a<m;a++) { scanf("%d %d\n",&x,&y); makeside(a,x,y); makeside(a+m,y,x); } maketree(1); build(1,1,m); scanf("%d\n",&n); for(a=1;a<=n;a++) { scanf("%c %d\n",&chin,&b); x=bt[b],y=et[b]; if(chin=='Q')printf("%d\n",query(1,1,m)); else update(1,1,m); } return 0;}