POJ 3321 Apple Tree(树状数组)
2015-08-17 21:34
344 查看
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
Source
POJ Monthly--2007.08.05, Huang, Jinsong
题意:给定一棵树,某些节点上有苹果,多次询问各子树上的节点数,并且在
询问的中途随时可能新增和删除苹果。
思路:这个可以使用树状数组来做,不过和一般的树状数组不一样,因为这是
一棵树上进行分叉,所以区间不是按照1-n进行排列的,所以要找到一种方法通过两个点
之间的关系把1-n进行重新排列,存在begin和end中,这样就可以用树状数组了。
不可以用vector做,我用了直接超时,改成边表就AC了......Orz
点击打开链接
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21635 | Accepted: 6573 |
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
Source
POJ Monthly--2007.08.05, Huang, Jinsong
题意:给定一棵树,某些节点上有苹果,多次询问各子树上的节点数,并且在
询问的中途随时可能新增和删除苹果。
思路:这个可以使用树状数组来做,不过和一般的树状数组不一样,因为这是
一棵树上进行分叉,所以区间不是按照1-n进行排列的,所以要找到一种方法通过两个点
之间的关系把1-n进行重新排列,存在begin和end中,这样就可以用树状数组了。
不可以用vector做,我用了直接超时,改成边表就AC了......Orz
点击打开链接
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<stack> #include<queue> #include<vector> #define N 100005 using namespace std; int n; int cnt; int m; int head ,c ; int begin ,end ; bool v ,du ; struct node{ int xx; int next; }q[N<<1]; void add(int x,int y){ q[cnt].xx = y; q[cnt].next = head[x]; head[x] = cnt++; } int lowbit(int x){ return x&(-x); } int getsum(int x){ int s = 0; while(x>0){ s += c[x]; x -= lowbit(x); } return s; } void updata(int x,int t){ while(x<=n){ c[x] += t; x += lowbit(x); } } void DFS(int x){ cnt++; begin[x] = cnt; v[x] = 1; for(int i=head[x];i!=-1;i=q[i].next){ if(v[q[i].xx] == 0){ DFS(q[i].xx); } } end[x] = cnt; } int main(){ while(scanf("%d",&n)!=EOF){ memset(head,-1,sizeof(head)); memset(v,0,sizeof(v)); memset(du,1,sizeof(du)); cnt = 0; int x,y; for(int i=1;i<n;i++){ scanf("%d%d",&x,&y); add(x,y); add(y,x); } cnt = 0; DFS(1); for(int i=1;i<=n;i++){ updata(i,1); } scanf("%d",&m); char str[10]; while(m--){ scanf("%s%d",str,&x); if(str[0] == 'C'){ if(du[x]){ updata(begin[x],-1); du[x] = 0; }else{ updata(begin[x],1); du[x] = 1; } }else{ printf("%d\n",getsum(end[x]) - getsum(begin[x]-1)); } } } return 0; }
相关文章推荐
- android 手机屏幕横屏竖屏切换
- Light OJ 1339 Strongest Community(分块暴力)
- 【Android】Json解析初步
- iOS事件处理
- ios中的代理与协议(delegate)
- Android Api Demos登顶之路(三十四)Device Admin
- Android的发展历史
- cocos 绑定到lua时指定命名空间 程序崩溃
- 高仿知乎日报(四)
- Android中onClick的四种实现方法
- Android百度地图SDK 导航初始化和地图初始化引起的冲突
- Cocos2d-x学习笔记(十二)—— Box2d物理引擎(未完)
- 【Android】四大组件(3)Service
- iOS开发——多线程篇——GCD
- IOS-笔记5 (swift和OC,View大小和位置)
- 【转】android 开发 命名规范
- IOS 设置TableViewCell背景透明
- Activity的进入,退出动画设置
- Android开发(1)——项目结构
- 6.ios-类存管理与分类