poj3321 Apple Tree(DFS+树状数组)
2014-08-12 11:16
561 查看
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 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 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 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
Sample Output
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18974 | Accepted: 5767 |
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 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 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
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2 /* 题目大意:有一颗树,刚开始长满苹果,每个树杈上都有一个苹果,修改的时候,若存在,则摘掉,不存在,就+1 基本是copy的pdf代码,有个小错还找不着 dfs标记开始时间和结束时间,利用树状数组求差再除2 Time: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; }
相关文章推荐
- 【POJ3321】Apple Tree(DFS序,树状数组)
- POJ3321 Apple Tree(DFS序 + 树状数组)
- 【POJ3321】Apple Tree-DFS+树状数组维护
- [poj3321]Apple Tree_dfs序_树状数组
- [POJ3321] Apple Tree 苹果树 - 树状数组
- Apple Tree_poj3321_树状数组&dfs
- POJ 3321 Apple Tree(DFS + 树状数组)
- 【树状数组--dfs序转化】poj3321 Apple Tree
- POJ3321(dfs序列+树状数组)
- POJ 3321 Apple Tree (dfs + 树状数组)
- Poj3321 Apple Tree【DFS序】【树状数组】
- [jzoj1016][poj3321]苹果树(dfs序+树状数组维护)
- poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
- POJ 3321 Apple Tree (DFS + 树状数组)
- 【poj3321】Apple Tree——树状数组&DFS
- poj 3321 Apple Tree(dfs标号+树状数组)
- poj - 3321 - Apple Tree(dfs+树状数组)
- 【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)
- POJ3321 Apple Tree(DFS序)
- poj 3321 Apple Tree DFS时间戳预处理+树状数组