POJ---3321-Apple tree(树状数组)
2015-08-21 16:05
483 查看
A - Apple Tree(8.3.1)
Crawling in process...
Crawling failed
Time Limit:2000MS
Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
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?
![](http://poj.org/images/3321_1.gif)
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
Hint
Crawling in process...
Crawling failed
Time Limit:2000MS
Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
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?
![](http://poj.org/images/3321_1.gif)
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
Hint
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <stack> #include <list> #include <vector> #include <map> using namespace std; #define MAX 100002 int s[MAX],cnt,c[MAX],a[MAX]; struct node1 { int next; int tail; }edge[MAX]; struct node2 { int r,l; }apple[MAX]; void DFS(int u) { int i; apple[u].l=cnt; for(i=s[u]; i!=-1; i=edge[i].next) DFS(edge[i].tail); apple[u].r=cnt++; } inline int lowbit(int x) { return x&(-x); } void change(int x) { int i; if(a[x]) for(i=x; i<cnt; i+=lowbit(i)) ++c[i]; else for(i=x; i<cnt; i+=lowbit(i)) --c[i]; } int sum(int x) { int i,res=0; for(i=x; i>0; i-=lowbit(i)) res+=c[i]; return res; } int main() { int i,n,m,t1,t2,t; char str[3]; scanf("%d",&n); memset(s,-1,sizeof(s[0])*(n+1)); memset(c,0,sizeof(c[0])*(n+1)); memset(apple,0,sizeof(apple[0])*(n+1)); for(i=0; i<n-1; ++i) { scanf("%d%d",&t1,&t2); edge[i].tail=t2; edge[i].next=s[t1]; s[t1]=i; } cnt=1; DFS(1); scanf("%d",&m); for(i=1; i<=n; ++i) { a[i]=1; change(i); } while(m--) { scanf("%s %d",str,&t); if(str[0]=='Q') printf("%d\n",sum(apple[t].r)-sum(apple[t].l-1)); else { a[ apple[t].r ] = (a[ apple[t].r ] + 1)%2; change(apple[t].r); } } return 0; }
相关文章推荐
- Windows环境下Android Studio v1.0安装教程
- Windows环境下Android Studio v1.0安装教程
- Android Day01笔记
- Android 点击两次返回退出应用
- android 组件DatePicker实例
- Android应用:View.VISIBLE、INVISIBLE、GONE的区别
- Android大图片裁剪终极解决方案 原理分析
- 五、触摸事件的吞噬和传递
- android app Application全局变量获取
- iOS-Animation学习2-CABasicAnimation以及CAAnimationGroup的基本用法
- OC基础13:数字、字符串和集合2
- Android常用adb命令
- iOS POP到指定的界面的方法
- ios新技术文章转载链接汇总
- android studio 配置
- Android PopupWindow的简单使用
- Android Studio使用Lint进行代码检查
- Android Studio使用Lint进行代码检查
- Android PopupWindow的使用和分析
- Android中的md5加密