POJ 3321 Apple Tree 【树状数组 dfs记录时间戳】
2015-11-27 18:16
585 查看
Apple Tree
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22366 | Accepted: 6798 |
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
恩,题目大意就是说,每一个树杈都有一个苹果,然后树枝上也有,C表示改变该点的状态,若为0则变为1 ,若为1 则变为0 ,最后问一树杈包括它自己所有的苹果数。看了人家的题解,才了解到要记录树杈所包括的范围/article/7126178.html
#include <iostream> #include<cstdio> #include<cstring> #define maxn 100010 using namespace std; int n,cnt,head[maxn],c[maxn]; bool apple[maxn],vis[maxn]; int begin[maxn],end[maxn]; struct node { int from,to,next; }; node edge[maxn*2]; void add(int f,int t) { edge[cnt].from=f; edge[cnt].to=t; edge[cnt].next=head[f]; head[f]=cnt++; } void dfs(int x) { cnt++; begin[x]=cnt; vis[x]=1; for(int i=head[x];i!=-1;i=edge[i].next) { if(!vis[edge[i].to]) dfs(edge[i].to); } end[x]=cnt; } int lowbit(int t) { return t&(t^(t-1)); } void update(int x,int v) { for(int i=x;i<=n;i+=lowbit(i)) c[i]+=v; } int sum(int x) { int ans=0; for(int i=x;i>0;i-=lowbit(i)) ans+=c[i]; return ans; } int main() { int m,a,b,p; char s[10]; while(~scanf("%d",&n)) { memset(head,-1,sizeof(head)); memset(apple,1,sizeof(apple)); memset(vis,0,sizeof(vis)); memset(c,0,sizeof(c)); memset(begin,0,sizeof(begin)); memset(end,0,sizeof(end)); cnt=0; for(int i=1;i<n;++i) { scanf("%d%d",&a,&b); add(a,b); add(b,a); } cnt=0; dfs(1); for(int i=1;i<=n;++i) update(i,1); scanf("%d",&m); while(m--) { scanf("%s%d",s,&p); if(s[0]=='Q') printf("%d\n",sum(end[p])-sum(begin[p]-1)); else { if(apple[p]) { update(begin[p],-1); apple[p]=0; } else { update(begin[p],1); apple[p]=1; } } } } return 0; }
相关文章推荐
- Android 进阶 Fragment 介绍和使用 (一)
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLi
- Android5.x新特性之ViewDragHelper拖动控件(3种方案)
- Android NDK学习之五、Application.mk简介
- Android studio SDK 文件夹下各个文件的作用(20151127)
- iOS开发>学无止境 - 检测路径下文件夹是否存在
- android图片压缩的完整的解决方案
- Android NDK学习之四、 Android.mk实例和NDK实用技巧
- AndroidStudio中project的model在Android目录中显示
- Android NDK学习之三、 Android.mk的制作
- 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)
- 微信开发jssdk入门
- (as)Android SDK在线更新镜像服务器
- Android NDK学习之 一. Android NDK简介
- Android mMediaRecorder.stop() 报错, 你蛋疼了吗?
- android studio各种版本下载
- iOS入门之page平铺导航,scrollerview滚动计算和pager的切换
- Android ndk 入门5 - opencv实现
- iOS开发 - 处理不等高TableViewCell的招术
- iOS开发 音频播放、录音、视频播放、拍照、视频录制