POJ 3321 Apple Tree (树状数组)
来源:互联网 发布:决战武进宠物进阶数据 编辑:程序博客网 时间:2024/05/29 09:45
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 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
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
Source
题目链接: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); } }}
- poj 3321 Apple Tree( 树状数组 )
- POJ 3321 apple tree 树状数组
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- poj 3321 apple tree 树状数组 水
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- POJ 3321 Apple Tree (树状数组)
- POJ 3321 Apple Tree (dfs + 树状数组)
- POJ 3321 Apple Tree (DFS + 树状数组)
- Poj 3321 Apple Tree - 树状数组
- [poj 3321]Apple Tree[树状数组]
- POJ 3321 Apple Tree DFS + 树状数组
- POJ 3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree(dfs+树状数组)
- poj 3321 Apple Tree 树状数组
- poj-3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree【树状数组】
- jdk7新增的常用新特性
- qmake高级概念
- 函数的定义和调用
- 杭电ACM1069——Monkey and Banana~~动态规划
- HDOJ 1016 Prime Ring Problem(dfs+回溯)
- POJ 3321 Apple Tree (树状数组)
- DirectX11 创建顶点缓存
- 生活随笔:关于病人
- GET和POST的区别
- 【Leetcode】Valid Binary Search Tree
- qmake Command Reference
- [codeigniter 四]、模型和辅助函数
- 搬寝室
- Unsupported major.minor version 51.0问题的原因与解决方法