forked from Priyansh19077/CP-Templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HLD.cpp
323 lines (317 loc) · 7.74 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// No need to change anything here
struct BinaryLifting {
int n;
int maxLog;
ll maxRequirement;
vector<vector<int>> parent;
vector<int> *edges;
vector<int> logValues;
bool precomputedLogs = false;
BinaryLifting(int n1, vector<int> *edges1, ll requirement, int root) {
n = n1;
edges = edges1;
parent.resize(n);
maxLog = log2(requirement + 1);
maxRequirement = requirement;
for (int i = 0; i < n; i++) {
parent[i].resize(maxLog + 1);
for (int j = 0; j <= maxLog; j++) {
parent[i][j] = -1;
}
}
fillParentTable(root);
if (maxRequirement <= 1000000LL)
precomputeLogs();
}
BinaryLifting() {}
void fillParentTable(int root) {
vector<bool> visited(n);
dfsBinaryLifting(root, visited);
int intermediate = -1;
for (int i = 1; i <= maxLog; i++) {
for (int j = 0; j < n; j++) {
intermediate = parent[j][i - 1];
if (intermediate != -1) {
parent[j][i] = parent[intermediate][i - 1];
}
}
}
}
void dfsBinaryLifting(int root, vector<bool> &visited) {
visited[root] = true;
for (auto i : edges[root]) {
if (!visited[i]) {
parent[i][0] = root;
dfsBinaryLifting(i, visited);
}
}
}
void precomputeLogs() {
precomputedLogs = true;
logValues.resize(maxRequirement + 1);
logValues[1] = 0;
for (int i = 2; i <= maxRequirement; i++) {
logValues[i] = logValues[i / 2] + 1;
}
}
int kthParent(int start, int k) {
int a = start;
while (k > 0) {
int x = getLog(k);
a = parent[a][x];
if (a == -1)
return a;
k -= (1 << x);
}
return a;
}
int getLog(ll x) {
return precomputedLogs ? logValues[x] : log2(x);
}
};
// No need to change anything here
struct LCA {
int n;
BinaryLifting *bl_object;
vector<int> level;
vector<int> *edges;
LCA(int n1, vector<int> *edges1, int root, BinaryLifting *bl) {
n = n1;
bl_object = bl;
edges = edges1;
level.resize(n);
dfsLCA(root, -1);
}
LCA() {}
void dfsLCA(int root, int parent) {
for (auto i : edges[root]) {
if (i != parent) {
level[i] = level[root] + 1;
dfsLCA(i, root);
}
}
}
int getLCA(int a, int b) {
if (level[a] > level[b]) {
swap(a, b);
}
b = bl_object->kthParent(b, level[b] - level[a]);
if (a == b)
return a;
for (int i = bl_object->maxLog; i >= 0; i--) {
int parent1 = bl_object->parent[a][i];
int parent2 = bl_object->parent[b][i];
if (parent2 != parent1 && parent1 != -1 && parent2 != -1) {
a = parent1;
b = parent2;
}
}
return bl_object->parent[a][0];
}
};
template<typename Node, typename Update>
struct SegTree {
vector<Node> tree;
vector<ll> arr; // type may change
int n;
SegTree(int a_len, vector<ll> &a) { // change if type updated
arr = a;
n = a_len;
tree.resize(4 * n); fill(all(tree), Node());
build(0, n - 1, 1);
}
SegTree() {}
void build(int start, int end, int index) // Never change this
{
if (start == end) {
tree[index] = Node(arr[start]);
return;
}
int mid = (start + end) / 2;
build(start, mid, 2 * index);
build(mid + 1, end, 2 * index + 1);
tree[index].merge(tree[2 * index], tree[2 * index + 1]);
}
void update(int start, int end, int index, int query_index, Update &u) // Never Change this
{
if (start == end) {
u.apply(tree[index]);
return;
}
int mid = (start + end) / 2;
if (mid >= query_index)
update(start, mid, 2 * index, query_index, u);
else
update(mid + 1, end, 2 * index + 1, query_index, u);
tree[index].merge(tree[2 * index], tree[2 * index + 1]);
}
Node query(int start, int end, int index, int left, int right) { // Never change this
if (start > right || end < left)
return Node();
if (start >= left && end <= right)
return tree[index];
int mid = (start + end) / 2;
Node l, r, ans;
l = query(start, mid, 2 * index, left, right);
r = query(mid + 1, end, 2 * index + 1, left, right);
ans.merge(l, r);
return ans;
}
void make_update(int index, ll val) { // pass in as many parameters as required
Update new_update = Update(val); // may change
update(0, n - 1, 1, index, new_update);
}
Node make_query(int left, int right) {
return query(0, n - 1, 1, left, right);
}
};
struct Node1 {
ll val; // may change
Node1() { // Identity element
val = -INF; // may change
}
Node1(ll p1) { // Actual Node
val = p1; // may change
}
void merge(Node1 &l, Node1 &r) { // Merge two child nodes
val = max(l.val, r.val); // may change
}
};
struct Update1 {
ll val; // may change
Update1(ll p1) { // Actual Update
val = p1; // may change
}
void apply(Node1 &a) { // apply update to given node
a.val = val; // may change
}
};
template<typename Node, typename Update>
struct HLD {
int n;
int rootHere;
vector<int> *edges;
vector<int> big_child;
vector<int> subtree_sum;
vector<int> chain;
vector<int> label;
vector<ll> values;
SegTree<Node, Update> s1;
LCA *lca_object;
BinaryLifting *bl_object;
HLD(int n1, vector<int> *edges1, int root1, vector<ll> &values1, LCA *lca) {
n = n1;
lca_object = lca;
bl_object = lca->bl_object;
edges = edges1;
rootHere = root1;
big_child.resize(n);
subtree_sum.resize(n);
label.resize(n);
chain.resize(n);
values = values1;
dfsPrecompute(rootHere, -1);
int label_time = 0;
dfsLabels(rootHere, -1, label_time);
for (int i = 0; i < n; i++)
chain[i] = i;
dfsChains(rootHere, -1);
s1 = SegTree<Node, Update>(n, values);
for (int i = 0; i < n; i++) {
s1.make_update(label[i], values[i]);
}
// debugHLD();
}
void dfsPrecompute(int root, int parent) {
subtree_sum[root] = 1;
big_child[root] = -1;
int biggest = -1;
for (auto i : edges[root]) {
if (i != parent) {
dfsPrecompute(i, root);
subtree_sum[root] += subtree_sum[i];
if (subtree_sum[i] > biggest) {
big_child[root] = i;
biggest = subtree_sum[i];
}
}
}
}
void dfsLabels(int root, int parent, int &label_time) {
label[root] = label_time++;
if (big_child[root] != -1)
dfsLabels(big_child[root], root, label_time);
for (auto i : edges[root])
if (i != parent && i != big_child[root])
dfsLabels(i, root, label_time);
}
void dfsChains(int root, int parent) {
if (big_child[root] != -1)
chain[big_child[root]] = chain[root];
for (auto i : edges[root])
if (i != parent)
dfsChains(i, root);
}
void debugHLD() {
debug(big_child);
debug(subtree_sum);
debug(chain);
debug(label);
debug(values);
}
Node queryChain(int here, int toReach) {
Node val = Node(0);
int top;
while (lca_object->level[here] > lca_object->level[toReach]) {
top = chain[here];
if (lca_object->level[top] <= lca_object->level[toReach])
top = bl_object->kthParent(here, lca_object->level[here] - lca_object->level[toReach] - 1);
Node a1 = val;
Node a2 = s1.make_query(label[top], label[here]);
val.merge(a1, a2);
here = bl_object->parent[top][0];
}
return val;
}
ll findAnswer(int u, int v) {
int lca = lca_object->getLCA(u, v);
Node n1 = queryChain(u, lca);
Node n2 = queryChain(v, lca);
Node merged;
merged.merge(n1, n2);
Node n3 = Node(s1.make_query(label[lca], label[lca]));
Node ans;
ans.merge(merged, n3);
return ans.val;
}
void makeUpdateatIndex(int u, ll val) {
s1.make_update(label[u], val);
}
};
// Change accordingly for edge weights instead of node values
void solve() {
int n, q;
cin >> n >> q;
vector<ll> values(n);
for (int i = 0; i < n; i++)
cin >> values[i];
vector<int> *edges = new vector<int>[n];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
edges[a - 1].pb(b - 1);
edges[b - 1].pb(a - 1);
}
BinaryLifting bl_object = BinaryLifting(n, edges, n, 0);
LCA lca_object = LCA(n, edges, 0, &bl_object);
HLD<Node1, Update1> hld = HLD<Node1, Update1>(n, edges, 0, values, &lca_object);
while (q--) {
int a, b, c;
cin >> a >> b >> c;
if (a == 1) {
hld.makeUpdateatIndex(b - 1, c);
} else {
cout << hld.findAnswer(b - 1, c - 1) << " ";
}
}
}