poj3321 Apple Tree(DFS+树状数组)

来源:互联网 发布:vs2015能开发php吗 编辑:程序博客网 时间:2024/05/29 11:08
Apple Tree
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18974 Accepted: 5767

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 toN 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 forku and forkv 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 forkx, 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
/*题目大意:有一颗树,刚开始长满苹果,每个树杈上都有一个苹果,修改的时候,若存在,则摘掉,不存在,就+1基本是copy的pdf代码,有个小错还找不着dfs标记开始时间和结束时间,利用树状数组求差再除2Time:2014-8-12 11:18*/#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int MAX=100000+10;vector<vector<int> >G(MAX);int Start[MAX];int End[MAX];int C[MAX<<1],Lowbit[MAX<<1];int N,Q,nCount;bool HasApple[MAX<<1];void DFS(int x){Start[x]=++nCount;for(int i=0;i<G[x].size();i++){DFS(G[x][i]);}End[x]=++nCount;}void Init(){memset(HasApple,0,sizeof(HasApple));memset(C,0,sizeof(C));for(int i=1;i<=N;i++){G[i].clear();HasApple[i]=1;}for(int i=1;i<=nCount;i++){Lowbit[i]=i&(-i);C[i]=i-(i-Lowbit[i]);}}int QuerynSum(int p){int nSum=0;while(p>0){nSum+=C[p];p-=Lowbit[p];}return nSum;}void Modify(int p,int val){while(p<=nCount){C[p]+=val;p+=Lowbit[p];}}int main(){//freopen("D:\齐帅\poj\in.txt","r",stdin);//freopen("out.txt","w",stdout);while(scanf("%d",&N)!=EOF){for(int i=1;i<N;i++){int a,b;scanf("%d%d",&a,&b);G[a].push_back(b);}nCount=0;DFS(1);Init();scanf("%d",&Q);while(Q--){char cmd[10];scanf("%s",cmd);if(cmd[0]=='Q'){int s,e,pos;scanf("%d",&pos);s=QuerynSum(Start[pos]-1);e=QuerynSum(End[pos]);printf("%d\n",(e-s)/2);}else{int pos;scanf("%d",&pos);if(HasApple[pos]){Modify(Start[pos],-1);//修改的是 C[Start[pos]] 的数值 Modify(End[pos],-1);//HasApple[pos]=0;}else{Modify(Start[pos],1);Modify(End[pos],1);HasApple[pos]=1;}}}}return 0;}

0 0