POJ 3321:Apple Tree 树状数组
2015-10-04 14:12
375 查看
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
题意是有一个树,树根始终是1。最开始每一个节点上都上有苹果,然后就开始折腾了。
操作为Q时是询问,该分叉上现在一共有多少个苹果。
操作为C时,如果当前该节点上有苹果,摘掉。如果当前该节点没有苹果,长出一个来。
先dfs出每一个节点的起始时间和结束时间,然后根据起始时间和结束时间使用树状数组求和的思想就能得到结果。
代码:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22131 | Accepted: 6715 |
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
题意是有一个树,树根始终是1。最开始每一个节点上都上有苹果,然后就开始折腾了。
操作为Q时是询问,该分叉上现在一共有多少个苹果。
操作为C时,如果当前该节点上有苹果,摘掉。如果当前该节点没有苹果,长出一个来。
先dfs出每一个节点的起始时间和结束时间,然后根据起始时间和结束时间使用树状数组求和的思想就能得到结果。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #pragma warning(disable:4996) using namespace std; #define MY_MAX 220000 int C[MY_MAX]; vector<vector<int>> G(MY_MAX/2); int HasApple[MY_MAX/2]; int sta[MY_MAX]; int en[MY_MAX]; int ncount=0; int n,m; void dfs(int v) { sta[v] = ++ncount; for(int i=0;i<G[v].size();i++) { dfs(G[v][i]); } en[v] = ++ncount; } int lowbit(int x) { return x&(-x); } void add(int x,int val) { while(x<=en[1]+1) { C[x] = C[x] + val; x=x+lowbit(x); } } int sum(int x) { int res=0; while(x>0) { res+=C[x]; x=x-lowbit(x); } return res; } int main() { //freopen("i.txt","r",stdin); //freopen("o.txt","w",stdout); int i,temp1,temp2; char oper[5]; scanf("%d",&n); for(i=1;i<=n-1;i++) { scanf("%d%d",&temp1,&temp2); G[temp1].push_back(temp2); HasApple[i]=1; } HasApple =1; dfs(1); scanf("%d",&m); memset(C,0,sizeof(C)); for(i=1;i<=en[1]+1;i++) { add(i,1); } for(i=1;i<=m;i++) { scanf("%s%d",oper,&temp1); if(oper[0]=='C') { if(HasApple[temp1]) { HasApple[temp1]=0; add(sta[temp1]+1,-1); add(en[temp1]+1,-1); } else { HasApple[temp1]=1; add(sta[temp1]+1,1); add(en[temp1]+1,1); } } else if(oper[0]=='Q') { printf("%d\n",(sum(en[temp1])-sum(sta[temp1]))/2+HasApple[temp1]); } } //system("pause"); return 0; }
相关文章推荐
- php开发微信支付获取用户地址
- 浅谈使用PHP开发微信支付的流程
- BIOS入门之我见-输入
- 联想+A916+原版官方稳定精简ROM
- BIOS入门之我见-界面
- Android开发之基础新特性-------------Fragment&帧动画(一知识点)
- Android四大基本组件——BroadcastReceive
- Swift学习笔记11——方法(Method)
- BIOS入门之我见-布局
- Inside the C++ Object Model | Outline
- iOS_NSAttributeString
- BIOS入门之我见-预热
- BIOS入门之我见-开篇
- ios-技术总结
- Android WebView 支持H5 DOM Storage
- Android四大基本组件——Activity
- App不得不用的控件
- iOS和OSX的核心
- Android SDK 在线更新国内镜像服务器地址
- Android OpenGLES 实现结构