poj 3321Apple Tree 树状数组
来源:互联网 发布:高仿买家退货淘宝介入 编辑:程序博客网 时间:2024/06/05 19:00
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
31 21 33Q 1C 2Q 1
Sample Output
32
思路:把树上的点重新dfs,标记到begin[],中,每个点控制的范围是begin[]到end[].其他按树状数组处理。卡时间
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; #define N 100005 struct TNode { int to,next; }nod[2*N]; int n,m,edges; bool apple[N],vis[N]; int f[N],cal[N],begin[N],end[N]; void addedge(int u,int v) { nod[edges].to=v; nod[edges].next=f[u]; f[u]=edges++; } void dfs(int u) { edges++; begin[u]=edges; vis[u]=1; for(int i=f[u];i!=-1;i=nod[i].next) { if(!vis[nod[i].to]) dfs(nod[i].to); } end[u]=edges; } int lowbit(int x) {return x&(-x);} int getsum(int x) { int s=0; for(;x>0;x-=lowbit(x)) s+=cal[x]; return s; } void update(int x,int value) { for(;x<=n;x+=lowbit(x)) cal[x]+=value; } int main() { int i,u,v; char str[5]; scanf("%d",&n); memset(f,-1,sizeof(f)); memset(vis,0,sizeof(vis)); memset(apple,1,sizeof(apple)); edges=0; for(i=1;i<n;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } edges=0; dfs(1); for(i=1;i<=n;i++) update(i,1); scanf("%d",&m); while(m--) { scanf("%s%d",str,&u); if(str[0]=='C') { if(apple[u]) { update(begin[u],-1); apple[u]=0; } else { update(begin[u],1); apple[u]=1; } } else printf("%d\n",getsum(end[u])-getsum(begin[u]-1)); } return 0; }
- poj 3321 Apple Tree( 树状数组 )
- POJ 3321 apple tree 树状数组
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- poj 3321 apple tree 树状数组 水
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- POJ 3321 Apple Tree (树状数组)
- POJ 3321 Apple Tree (dfs + 树状数组)
- POJ 3321 Apple Tree (DFS + 树状数组)
- Poj 3321 Apple Tree - 树状数组
- [poj 3321]Apple Tree[树状数组]
- POJ 3321 Apple Tree DFS + 树状数组
- POJ 3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree(dfs+树状数组)
- poj 3321 Apple Tree 树状数组
- poj-3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree【树状数组】
- 单词选择
- 【Android开发日记】SwipeRefreshLayout添加上拉加载功能
- jQuery Easy UI Tooptip(提示框)组件
- 九数码(康拓展开+BFS)
- 读书笔记---光耀生命
- poj 3321Apple Tree 树状数组
- 数字图像处理领域可以投稿的期刊
- HDU 1171 Big Event in HDU(01背包)
- UVa 11489 - Integer Game (简单博弈 脑筋急转弯)
- uvalive 6396 数论 世界决赛的题
- tinyxml使用
- 模拟退火算法
- 后缀数组模板(倍增法)
- TopCoder-BadNeighbours