-
Notifications
You must be signed in to change notification settings - Fork 0
/
HLD.cpp
163 lines (144 loc) · 3.53 KB
/
HLD.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
#include <bits/stdtr1c++.h>
#define N 30010
#define CLR(ar) memset(ar, 0, sizeof(ar))
#define SET(ar) memset(ar, -1, sizeof(ar))
using namespace std;
vector <int> ar[N];
int t, n, m, val[N], tree[N << 2];
int id, root, subtree[N], depth[N], parent[N], head[N], tail[N], chain[N], num[N];
void dfs(int i, int par){
parent[i] = par;
subtree[i] = 1;
if (i != par) depth[i] = depth[par] + 1;
int len = ar[i].size();
for (int j = 0; j < len; j++){
int x = ar[i][j];
if (x != par){
dfs(x, i);
subtree[i] += subtree[x];
}
}
}
void hld(int i, int par){
int heavy = -1;
int len = ar[i].size();
chain[i] = i;
if (head[i] != -1) chain[i] = chain[par];
for (int j = 0; j < len; j++){
int x = ar[i][j];
if ((x != par) && ((subtree[x] << 1) >= subtree[i])){
heavy = x, head[x] = i, tail[i] = x;
}
}
if (heavy != -1){
num[heavy] = ++id;
hld(heavy, i);
}
for (int j = 0; j < len; j++){
int x = ar[i][j];
if ((x != par) && (x != heavy)) hld(x, i);
}
}
void HeavyLight(){
id = 0, root = 0;
SET(head), SET(tail);
depth[root] = 0;
dfs(root, root);
hld(root, root);
for (int i = 0; i < n; i++){
if (i == root) chain[i] = 0;
else if (chain[i] == i) chain[i] = parent[i];
}
}
int lca(int a, int b){
while (chain[a] != chain[b]){
if (depth[chain[a]] > depth[chain[b]]) a = chain[a];
else b = chain[b];
}
if (a == b) return a;
if (head[a] != -1 && head[b] != -1){
if (depth[a] < depth[b]) return a;
else return b;
}
return chain[a];
}
int query(int idx){
int res = 0;
while (idx > 0){
res += tree[idx];
idx ^= (idx & -idx);
}
return res;
}
void update(int idx, int value){
while (idx <= id){
tree[idx] += value;
idx += (idx & -idx);
}
}
void RMQ(){
CLR(tree);
for (int i = 0; i < n; i++){
if (head[i] != -1){
update(num[i], val[i]);
}
}
}
void update_node(int u, int v){
if (head[u] != -1){
update(num[u], -val[u]);
val[u] = v;
update(num[u], val[u]);
}
else val[u] = v;
}
int F(int v){
int res = 0;
while(1){
if (head[v] != -1){
int x = chain[v];
int d = depth[v] - depth[x];
int idx = num[v];
int r = query(idx) - query(idx - d);
res += r;
v = x;
}
else{
res += val[v];
if (!v) break;
v = parent[v];
}
}
return res;
}
int solve(int a, int b){
int l = lca(a, b);
int x = F(a) + F(b);
int y = F(l) << 1;
int res = (x - y) + val[l];
return res;
}
int main(){
int T = 0, i, j, q, a, b;
scanf("%d", &t);
while (t--){
scanf("%d", &n);
for (i = 0; i <= n; i++) ar[i].clear();
for (i = 0; i < n; i++) scanf("%d", &val[i]);
for (i = 1; i < n; i++){
scanf("%d %d", &a, &b);
ar[a].push_back(b);
ar[b].push_back(a);
}
HeavyLight();
RMQ();
printf("Case %d:\n", ++T);
scanf("%d", &m);
while (m--){
scanf("%d %d %d", &q, &a, &b);
if (!q) printf("%d\n", solve(a, b));
else update_node(a, b);
}
}
return 0;
}