-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoj3321.cpp
More file actions
83 lines (75 loc) · 1.53 KB
/
poj3321.cpp
File metadata and controls
83 lines (75 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <cstdio>
#include <cstring>
const int MAXN = 200000;
struct Edge {
int val, next;
} edge[(MAXN << 1) + 100];
int edgeHead[MAXN + 100];
struct TreeNode {
int low, high, sum;
} tnode[MAXN + 100];
int cidx = 0;
bool pick[MAXN + 100];
bool visited[MAXN + 100]; // avoid it give repeat edge in different direction
int n;
inline int lowbit(int x) {
return x & (-x);
}
void dfs(int root) {
tnode[root].low = ++cidx;
visited[root] = true;
for(int i = edgeHead[root]; i; i = edge[i].next)
if(!visited[edge[i].val])
dfs(edge[i].val);
tnode[root].high = cidx;
}
// from bottom to top
void addToTree(int i, int v) {
while(i <= n) {
tnode[i].sum += v;
i += lowbit(i);
}
}
// from top to bottom
int sumFromTree(int i) {
int sum = 0;
while(i > 0) {
sum += tnode[i].sum;
i -= lowbit(i);
}
return sum;
}
void buildTree() {
for(int i = 1; i <= n; ++i)
addToTree(i, 1);
}
int main() {
int a, b;
scanf("%d", &n);
buildTree();
for(int i = 1; i < n; ++i) {
scanf("%d%d", &a, &b);
edge[i].val = b;
edge[i].next = edgeHead[a];
edgeHead[a] = i;
}
dfs(1);
char s[3];
int t, v;
scanf("%d", &t);
for(int i = 0; i < t; ++i) {
scanf("%s%d", s, &v);
if(s[0] == 'C') {
if(pick[v]) {
addToTree(tnode[v].low, 1);
pick[v] = false;
} else {
addToTree(tnode[v].low, -1);
pick[v] = true;
}
} else {
printf("%d\n", sumFromTree(tnode[v].high) - sumFromTree(tnode[v].low - 1));
}
}
return 0;
}