Apple Tree(树状数组)

描述

 

Happy Christmas! Kaka likes apple very much, Santa Claus presents an apple tree for kaka. In this evening, a lot of apples will grow in the 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?

输入

 

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
"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
"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

输出

For every inquiry, output the correspond answer per line.

样例输入

 3

1 2

1 3
3
Q 1
C 2
Q 1

样例输出

3
2

 1 #include <iostream>
 2 #include <vector>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int MAX=100005;
 8 struct Tree
 9 {
10     int left,right,data;
11 }tree[MAX];
12 int n,m,side=1;
13 vector<int> vec[MAX];
14 int cnt[MAX];
15 
16 void DFS(int num)
17 {
18     tree[num].left=side;
19     int len=vec[num].size();
20     for(int i=0;i<len;i++){
21         side++;
22         DFS(vec[num][i]);
23     }
24     tree[num].right=side;
25 }
26 
27 int lowbit(int i)
28 {
29     return i&-i;
30 }
31 
32 void update(int i,int val)
33 {
34     while(i<=n){
35         cnt[i]+=val;
36         i+=lowbit(i);
37     }
38 }
39 
40 int getsum(int i)
41 {
42     int sum=0;
43     while(i>=1){
44         sum+=cnt[i];
45         i-=lowbit(i);
46     }
47     return sum;
48 }
49 
50 int main()
51 {
52     int x,y;
53     char c;
54     memset(cnt,0,sizeof(cnt));
55     scanf("%d",&n);
56     for(int i=1;i<n;i++){
57         scanf("%d %d",&x,&y);
58         vec[x].push_back(y);
59     }
60     DFS(1);
61     for(int i=1;i<=n;i++){
62         tree[i].data=1;
63         update(i,1);
64     }
65     scanf("%d",&m);
66     for(int i=1;i<=m;i++){
67         getchar();
68         scanf("%c %d",&c,&x);
69         if(c=='C'){
70             int val=(tree[x].data==1?-1:1);
71             update(tree[x].left,val);
72             tree[x].data=1-tree[x].data;
73         }
74         else{
75             printf("%d\n",getsum(tree[x].right)-getsum(tree[x].left-1));
76         }
77     }
78     return 0;
79 }

猜你喜欢

转载自www.cnblogs.com/ChangeG1824/p/9493469.html
今日推荐