-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtree_divide.cpp
172 lines (172 loc) · 5.44 KB
/
tree_divide.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
const int N = 100010;
const int M = 200010;
int pool[N * 40];
int pool_size;
struct Bit
{
void allocate(int size) {
n = size;
c = pool + pool_size;
std::fill(c, c + n, 0);
pool_size += size;
}
void insert(int x, int v) {
for(x++; x < n; x += x&-x) {
c[x] += v;
}
}
int query(int x) {
x = std::min(x + 1, n - 1);
int ret = 0;
for(; x > 0; x -= x &-x) {
ret += c[x];
}
return ret;
}
int n;
int *c;
}bit[2 * N], *node[2 * N];
int bit_size;
struct Center
{
int u, v, d;
Bit* bit;
Bit* sub_bit;
Center(int u, int v, int d, Bit* bit, Bit* sub_bit): u(u), v(v), d(d), bit(bit), sub_bit(sub_bit) {
}
};
std::vector<Center> edge[N];
int first[N], pnt[M], nxt[M], E, mx[N], w[N], n, solved[N], queue[N], parent[N], depth[N], size[N];
void init(int n)
{
E = 0;
std::fill(first, first + n + 1, -1);
std::fill(solved, solved + n + 1, 0);
bit_size = pool_size = 0;
for(int i = 1; i <= n; i++) {
edge[i].clear();
}
}
void add_edge(int a, int b)
{
pnt[E] = b;
nxt[E] = first[a];
first[a] = E++;
}
int bfs(int root, int fa)
{
int head = 0, tail = 0;
queue[tail++] = root;
parent[root] = fa;
while(head < tail) {
int u = queue[head++];
for(int i = first[u]; ~i; i = nxt[i]) {
int v = pnt[i];
if(v != parent[u] && !solved[v]) {
queue[tail++] = v;
parent[v] = u;
depth[v] = depth[u] + 1;
}
}
}
return tail;
}
int get_root(int root, int &sz)
{
int tail = bfs(root, -1);
for(int i = 0; i < tail; i++) {
int u = queue[i];
size[u] = mx[u] = 1;
}
for(int i = tail - 1; i >= 1; i--) {
int u = queue[i];
size[parent[u]] += size[u];
mx[parent[u]] = std::max(mx[parent[u]], size[u]);
}
for(int i = 0; i < tail; i++) {
int u = queue[i];
mx[u] = std::max(mx[u], tail - size[u]);
if(mx[u] < mx[root]) {
root = u;
}
}
sz = tail;
return root;
}
void solve(int root)
{
int sz = -1;
depth[root] = 0;
root = get_root(root, sz);
solved[root] = 1;
Bit &bitu = bit[bit_size];
edge[root].push_back(Center(root, -1, 0, &bitu, &bitu));
bitu.allocate(sz + 1);
bitu.insert(0, w[root]);
bit_size++;
for(int i = first[root]; ~i; i = nxt[i]) {
int v = pnt[i];
if(solved[v]) {
continue;
}
depth[v] = 1;
int tail = bfs(v, root);
bit[bit_size].allocate(tail + 2);
for(int i = 0; i < tail; i++) {
int u = queue[i];
edge[u].push_back(Center(root, v, depth[u], &bitu, &bit[bit_size]));
bitu.insert(depth[u], w[u]);
bit[bit_size].insert(depth[u], w[u]);
}
bit_size++;
solve(v);
}
}
int main()
{
int q, a, b;
while(scanf("%d%d", &n, &q) == 2) {
init(n);
for(int i = 1; i <= n; i++) {
scanf("%d", &w[i]);
}
for(int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
add_edge(a, b);
add_edge(b, a);
}
solve(1);
while(q--) {
char op[5];
scanf("%s%d%d", op, &a, &b);
if(op[0] == '?') {
int ret = 0;
for(int i = 0; i < (int)edge[a].size(); i++) {
Center root = edge[a][i];
if(b >= root.d) {
ret += root.bit->query(b - root.d);
if(~root.v) {
ret -= root.sub_bit->query(b - root.d);
}
}
}
printf("%d\n", ret);
} else {
b -= w[a];
for(int i = 0; i < (int)edge[a].size(); i++) {
Center root = edge[a][i];
root.bit->insert(root.d, b);
if(~root.v) {
root.sub_bit->insert(root.d, b);
}
}
w[a] += b;
}
}
}
return 0;
}