Apple Tree(需要预处理的树状数组)
2015-03-21 20:49
162 查看
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
For every inquiry, output the correspond answer per line.
Sample Input
Sample Output
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20335 | Accepted: 6182 |
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
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
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <list> #include <iomanip> #include <cstdlib> #include <sstream> using namespace std; typedef long long LL; const int INF=0x5fffffff; const double EXP=1e-6; const int MS=200005; int cnt; vector<vector<int> > edge(MS/2); int flag[MS/2]; int L[MS],R[MS]; int c[MS]; void dfs(int cur) // 用dfs区间划分 { L[cur]=++cnt; for(int i=0;i<edge[cur].size();i++) dfs(edge[cur][i]); R[cur]=++cnt; } int lowbit(int x) { return x&(-x); } void updata(int x,int d) { while(x<=cnt) { c[x]+=d; x+=lowbit(x); } } int getsum(int x) { int ret=0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret; } int main() { int N,M,x,y; scanf("%d",&N); for(int i=0;i<N-1;i++) { scanf("%d%d",&x,&y); edge[x].push_back(y); } cnt=0; dfs(1); memset(c,0,sizeof(c)); for(int i=1;i<=N;i++) { updata(L[i],1); updata(R[i],1); } scanf("%d",&M); char cmd[MS]; for(int i=1;i<=N;i++) flag[i]=1; while(M--) { scanf("%s%d",cmd,&x); if(cmd[0]=='Q') { int t1=getsum(L[x]-1); int t2=getsum(R[x]); printf("%d\n",(t2-t1)/2); } else { if(flag[x]) { updata(L[x],-1); updata(R[x],-1); flag[x]=0; } else { updata(L[x],1); updata(R[x],1); flag[x]=1; } } } return 0; }
相关文章推荐
- POJ 3321—— Apple Tree(树状数组)
- POJ3321:Apple Tree(树状数组)
- 数列还原 2017网易校招(树状数组预处理+全排列)
- POJ 3321-Apple Tree(树状数组)
- POJ 3321 Apple Tree (树状数组)
- poj 3321:Apple Tree(树状数组,提高题)
- POJ 3321 Apple Tree(树状数组)
- 更改点后输出把集合里的点通过树的边连在一起所需要的最小代价 LCA+树状数组 HDU 5296 Annoying problem
- [置顶] HDU 4777 Rabbit Kingdom(离线树状数组 预处理)
- poj-3321-Apple Tree(树状数组)
- POJ 3321 Apple Tree(树状数组)
- POJ3321 Apple Tree (树状数组)
- [区间GCD预处理 树状数组 离线] HDU 5869 Different GCD Subarray Query
- 【POJ 3321】【dfs序(讲解)+(树状数组或者线段树)】Apple Tree【给你一颗树,最初每个节点上都有一个苹果,有两种操作单点修改和查询子树的苹果个数】
- poj 3321 apple tree 树状数组 水
- POJ3321 Apple Tree(树状数组 + dfs + 线性表)
- POJ-3321-Apple Tree,线段树,树状数组。
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
- 【原】 POJ 3321 Apple Tree 深度优先生成树+树状数组 解题报告
- POJ 3321 Apple Tree(树状数组)