「POJ3321」Apple Tree
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
Sample Input
1 2 3 4 5 6 7 |
3 1 2 1 3 3 Q 1 C 2 Q 1 |
Sample Output
1 2 |
3 2 |
题意
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define mod 998244353 #define N 100005 #define pi acos(-1) #define inf 0x7fffffff #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,id,cnt; int q[N],st[N],ed[N],last[N]; int rl[N<<2],rr[N<<2],sum[N<<2]; struct data{ int to,next; }e[N<<1]; void insert(int u,int v) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt; e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt; } void dfs(int x,int fa) { q[++id]=x; st[x]=id; for(int i=last[x];i;i=e[i].next) if(e[i].to!=fa) dfs(e[i].to,x); ed[x]=id; } void build(int k,int l,int r) { int mid=(l+r)>>1; rl[k]=l;rr[k]=r; if(l==r) { sum[k]=1; return; } build(k<<1,l,mid); build(k<<1|1,mid+1,r); sum[k]=sum[k<<1]+sum[k<<1|1]; } int query(int k,int a,int b) { int l=rl[k],r=rr[k],mid=(l+r)>>1; if(l==a&&r==b) return sum[k]; if(b<=mid)return query(k<<1,a,b); else if(a>mid)return query(k<<1|1,a,b); else return query(k<<1,a,mid)+query(k<<1|1,mid+1,b); } void modify(int k,int x) { int l=rl[k],r=rr[k],mid=(l+r)>>1; if(l==r) { sum[k]^=1; return; } if(x<=mid)modify(k<<1,x); else modify(k<<1|1,x); sum[k]=sum[k<<1]+sum[k<<1|1]; } int main() { n=read(); for(int i=1;i<n;i++) { int u=read(),v=read(); insert(u,v); } dfs(1,0); m=read(); char ch[10]; build(1,1,n); for(int i=1;i<=m;i++) { scanf("%s",ch+1); int x=read(); if(ch[1]=='Q') printf("%d\n",query(1,st[x],ed[x])); else modify(1,st[x]); } return 0; } |