2018_2_11_Apple Tree_树状数组
2018-02-11 09:37
162 查看
Apple Tree
DescriptionThere 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?
InputThe 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
OutputFor every inquiry, output the correspond answer per line.Sample Input3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output3
2
SourcePOJ Monthly--2007.08.05, Huang, Jinsong
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 32240 | Accepted: 9707 |
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?
InputThe 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
OutputFor every inquiry, output the correspond answer per line.Sample Input3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output3
2
SourcePOJ Monthly--2007.08.05, Huang, Jinsong
#include<iostream> #include<cstdio> #include<string> #include<vector> #include<cstring> #include<algorithm> #include<map> using namespace std; const int N=1e5+5; struct node1{ int next,tail; }edge ; struct node2{ int r,l; }apple ; int s ,cnt,c ,a ; void dfs(int u){ int i; apple[u].l=cnt; for(int i=s[u];i!=-1;i=edge[i].next) dfs(edge[i].tail); apple[u].r=cnt++; } inline int lowbit(int x){ return x&(-x); } void change(int x){ int i; if(a[x]){ for(i=x;i<cnt;i+=lowbit(i)) c[i]++; }else{ for(i=x;i<cnt;i+=lowbit(i)) c[i]--; } } int sum(int x){ int i,res=0; for(i=x;i>0;i-=lowbit(i)) res+=c[i]; return res; } int main(){ int n; scanf("%d",&n); memset(s,-1,sizeof(s[0])*(n+1)); memset(c,0,sizeof(c[0])*(n+1)); memset(apple,0,sizeof(apple[0])*(n+1)); int t1,t2; for(int i=0;i<n-1;i++){ scanf("%d%d",&t1,&t2); edge[i].tail=t2; edge[i].next=s[t1]; s[t1]=i; } cnt=1; dfs(1); int m; scanf("%d",&m); for(int i=1;i<=n;i++){ a[i]=1; change(i); } char str[3]; int t; while(m--){ scanf("%s%d",str,&t); if(str[0]=='Q'){ sum(apple[t].l-1); printf("%d\n",sum(apple[t].r)-sum(apple[t].l-1)); } else{ a[apple[t].r]=(a[apple[t].r]+1)%2; change(apple[t].r); } } }
相关文章推荐
- 【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)
- Poj 3321 Apple Tree - 树状数组
- 【GDOI2018模拟7.14】小奇的糖果 树状数组+扫描线
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- POJ 3321 Apple Tree DFS序 + 树状数组
- Apple Tree-----树状数组
- poj 3321 Apple Tree (dfs+树状数组)
- 2018_2_11_Creating a Quadtree_四叉树
- [POJ3321] Apple Tree 苹果树 - 树状数组
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ3321 Apple Tree(DFS序 + 树状数组)
- 2018版OCP 11g 052最新考试题库整理(带答案)(11)
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- POJ 3321 Apple Tree (dfs + 树状数组)
- poj 3321 Apple Tree(dfs标号+树状数组)
- poj 3321 Apple Tree DFS时间戳预处理+树状数组
- 02/05/2018 - 02/11/2018
- (利用树状数组统计子树权和8.3.1)POJ 3321 Apple Tree(利用树状数组动态统计子树权和)
- [poj3321]Apple Tree_dfs序_树状数组