poj 3321 Apple Tree dfs序+线段树
Time Limit: 2000MS | Memory Limit: 65536K | |
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
#include<iostream> #include<cstring> #include<cstdio> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; int tree[N<<2]; void build(int l,int r,int pos) { if(l==r) { tree[pos]=1; return; } int mid=(l+r)>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); tree[pos]=tree[pos<<1]+tree[pos<<1|1]; } void update(int l,int r,int pos,int p,int c) { if(l==r&&l==p) { tree[pos]=c; return; } int mid=(l+r)>>1; if(p<=mid) update(l,mid,pos<<1,p,c); if(p>mid) update(mid+1,r,pos<<1|1,p,c); tree[pos]=tree[pos<<1]+tree[pos<<1|1]; } int query(int L,int R,int l,int r,int pos) { if(L<=l&&r<=R) { return tree[pos]; } int mid=(l+r)>>1; int ans=0; if(L<=mid) ans+=query(L,R,l,mid,pos<<1); if(R>mid) ans+=query(L,R,mid+1,r,pos<<1|1); return ans; } struct is { int v,nex; }edge[N<<1]; int head[N<<1],edg; int in[N],out[N],tot; int n,p; void init() { memset(tree,0,sizeof(tree)); memset(head,-1,sizeof(head)); edg=0; tot=0; } void add(int u,int v) { edg++; edge[edg].v=v; edge[edg].nex=head[u]; head[u]=edg; } void dfs(int u,int fa) { in[u]=++tot; for(int i=head[u];i!=-1;i=edge[i].nex) { int v=edge[i].v; if(v==fa)continue; dfs(v,u); } out[u]=tot; } char ch[10]; int main() { int n; while(~scanf("%d",&n)) { init(); for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs(1,-1); build(1,n,1); int q; scanf("%d",&q); while(q--) { int p; scanf("%s%d",ch,&p); if(ch[0]=='Q') { printf("%d\n",query(in[p],out[p],1,n,1)); } else { int x=query(in[p],in[p],1,n,1); if(x) update(1,n,1,in[p],0); else update(1,n,1,in[p],1); } } } return 0; }