POJ 3321 Apple Tree (树状数组)
2015-07-17 23:37
330 查看
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
题目链接:http://poj.org/problem?id=3321
题目大意:有棵苹果树,开始每个结点都有一个苹果,C x表示该点有苹果则吃掉,没有则长出,Q x表示询问x结点上包括x结点的苹果数量
题目分析:将苹果树倒过来形成一个树形结构,因为其后序遍历的结果是一段连续的区间,并且以x为根的子树根为区间的右端点,因此可以通过树状数组来求解此问题,用DFS预处理出每个结点子树的左右区间。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21191 | Accepted: 6436 |
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
题目链接:http://poj.org/problem?id=3321
题目大意:有棵苹果树,开始每个结点都有一个苹果,C x表示该点有苹果则吃掉,没有则长出,Q x表示询问x结点上包括x结点的苹果数量
题目分析:将苹果树倒过来形成一个树形结构,因为其后序遍历的结果是一段连续的区间,并且以x为根的子树根为区间的右端点,因此可以通过树状数组来求解此问题,用DFS预处理出每个结点子树的左右区间。
#include <cstdio> #include <cstring> int const MAX = 1e5 + 5; int head[MAX], c[MAX], a[MAX]; int n, m, cnt, num; struct EDGE { int to, next; }e[MAX]; struct NODE { int l, r; }nd[MAX]; void Add(int x, int y) { e[cnt].to = y; e[cnt].next = head[x]; head[x] = cnt++; } void DFS(int now) { nd[now].l = num; for(int i = head[now]; i != -1; i = e[i].next) DFS(e[i].to); nd[now].r = num ++; } int lowbit(int x) { return x & (-x); } void change(int x) { if(a[x]) { for(int i = x; i < num; i += lowbit(i)) c[i] ++; } else { for(int i = x; i < num; i += lowbit(i)) c[i] --; } } int cal(int x) { int res = 0; for(int i = x; i > 0; i -= lowbit(i)) res += c[i]; return res; } int main() { scanf("%d", &n); memset(head, -1, sizeof(head)); memset(c, 0, sizeof(c)); cnt = 0; for(int i = 0; i < n - 1; i++) { int x, y; scanf("%d %d", &x, &y); Add(x, y); } num = 1; DFS(1); for(int i = 1; i <= n; i++) { a[i] = 1; change(i); } scanf("%d", &m); while(m --) { int x; char s[2]; scanf("%s %d", s, &x); if(s[0] == 'Q') printf("%d\n", cal(nd[x].r) - cal(nd[x].l - 1)); else { a[nd[x].r] = (a[nd[x].r] + 1) % 2; change(nd[x].r); } } }
相关文章推荐
- Android构建打包过程
- iOS8 WebKit库之——WKWebView篇
- Java常用类源码探究(一):Object类
- android 呼入电话的监听(来电监听)转
- Gradle Tips#1-tasks
- Android之判断某个服务是否正在运行的方法
- iOS技术点杂记--有一天我也会成为大牛
- iOS平台游戏安全之IPA破解原理及防御(第三弹)
- ios广告
- Android 语音遥控器的整体分析-主机端语音解码的添加
- Android开发常见问题汇总
- ios内购
- ios换肤思想,及工具类
- Android View中的onMeasure()方法详解
- android开发的全局变量Application
- Unity使用 16bit 压缩 Texture 颜色能均匀过渡
- 一览新的 Android Gradle 构建工具:新的 DSL 结构 和 Gradle 2.5
- android下拉刷新(android.support.v4.widget.SwipeRefreshLayout)
- Android开发实战之Intent传递对象(Serializable和Parcelable)
- Android Studio中使用Android5.0新特性CardView