POJ 3321 Apple Tree (Tree array)
Apple Tree
| Time Limit:2000 MS |
|
Memory Limit:65536 K |
| Total Submissions:21191 |
|
Accepted:6436 |
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 hasNForks which are connected by branches. Kaka numbers the forks by 1NAnd 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 integerN(N≤ 100,000), which is the number of the forks in the tree.
The followingN-1 lines each contain two integersUAndV, Which means forkUAnd forkVAre connected by a branch.
The next line contains an integerM(M≤ 100,000 ).
The followingMLines each contain a message which is either
CXWhich means the existence of the apple on forkXHas 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
QXWhich 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
31 21 33Q 1C 2Q 1
Sample Output
32
Source
POJ Monthly -- 2007.08.05, Huang, Jinsong
Question link: http://poj.org/problem? Id = 3321
There is an apple tree. At first each node has an apple, and C x indicates that an apple is eaten and not grown, Q x indicates the number of apples on x nodes.
Problem Analysis: The Apple Tree is inverted to form a tree structure, because the result of the subsequent traversal is a continuous interval, and x is the root of the Child root as the right endpoint of the interval, therefore, you can use a tree array to solve this problem and use DFS to pre-process the left and right intervals of each knot tree.
#include
#include
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, cal(nd[x].r) - cal(nd[x].l - 1)); else { a[nd[x].r] = (a[nd[x].r] + 1) % 2; change(nd[x].r); } }}