-
Notifications
You must be signed in to change notification settings - Fork 1
/
HeavyLight.java
300 lines (272 loc) · 8.5 KB
/
HeavyLight.java
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
package Data_Structures;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
// Heavy-light decomposition with path queries. Query complexity is O(log^2(n)).
// Based on the code from http://codeforces.com/blog/entry/22072
public class HeavyLight {
int getNeutralValue() {
return 0;
}
final List<Integer>[] tree;
final boolean valuesOnVertices; // true - values on vertices, false - values on edges
final SegmentTree segmentTree;
final int[] parent;
final int[] heavy;
final int[] depth;
final int[] pathRoot;
final int[] pos;
public HeavyLight(List<Integer>[] tree, boolean valuesOnVertices) {
this.tree = tree;
this.valuesOnVertices = valuesOnVertices;
int n = tree.length;
segmentTree = new SegmentTree(n);
parent = new int[n];
heavy = new int[n];
depth = new int[n];
pathRoot = new int[n];
pos = new int[n];
Arrays.fill(heavy, -1);
parent[0] = -1;
depth[0] = 0;
dfs(0);
for (int u = 0, p = 0; u < n; u++) {
if (parent[u] == -1 || heavy[parent[u]] != u) {
for (int v = u; v != -1; v = heavy[v]) {
pathRoot[v] = u;
pos[v] = p++;
}
}
}
}
int dfs(int u) {
int size = 1;
int maxSubtree = 0;
for (int v : tree[u]) {
if (v != parent[u]) {
parent[v] = u;
depth[v] = depth[u] + 1;
int subtree = dfs(v);
if (maxSubtree < subtree) {
maxSubtree = subtree;
heavy[u] = v;
}
size += subtree;
}
}
return size;
}
public int query(int u, int v) {
AtomicInteger res = new AtomicInteger(getNeutralValue()); // just mutable integer
processPath(u, v, (a, b) -> res.set(segmentTree.queryOperation(res.get(), segmentTree.query(a, b))));
return res.get();
}
public void modify(int u, int v, int delta) {
processPath(u, v, (a, b) -> segmentTree.modify(a, b, delta));
}
void processPath(int u, int v, BiConsumer<Integer, Integer> op) {
for (; pathRoot[u] != pathRoot[v]; v = parent[pathRoot[v]]) {
if (depth[pathRoot[u]] > depth[pathRoot[v]]) {
int t = u;
u = v;
v = t;
}
op.accept(pos[pathRoot[v]], pos[v]);
}
if (!valuesOnVertices && u == v) return;
op.accept(Math.min(pos[u], pos[v]) + (valuesOnVertices ? 0 : 1), Math.max(pos[u], pos[v]));
}
static class SegmentTree {
// Modify the following 5 methods to implement your custom operations on the tree.
// This example implements Add/Sum operations. Operations like Add/Max, Set/Max can also be implemented.
int modifyOperation(int x, int y) {
return x + y;
}
// query (or combine) operation
int queryOperation(int leftValue, int rightValue) {
return leftValue + rightValue;
}
int deltaEffectOnSegment(int delta, int segmentLength) {
if (delta == getNeutralDelta()) return getNeutralDelta();
// Here you must write a fast equivalent of following slow code:
// int result = delta;
// for (int i = 1; i < segmentLength; i++) result = queryOperation(result, delta);
// return result;
return delta * segmentLength;
}
int getNeutralDelta() {
return 0;
}
int getInitValue() {
return 0;
}
// generic code
int[] value;
int[] delta; // delta[i] affects value[i], delta[2*i+1] and delta[2*i+2]
int joinValueWithDelta(int value, int delta) {
if (delta == getNeutralDelta()) return value;
return modifyOperation(value, delta);
}
int joinDeltas(int delta1, int delta2) {
if (delta1 == getNeutralDelta()) return delta2;
if (delta2 == getNeutralDelta()) return delta1;
return modifyOperation(delta1, delta2);
}
void pushDelta(int i) {
int d = 0;
for (; (i >> d) > 0; d++) {
}
for (d -= 2; d >= 0; d--) {
int x = i >> d;
value[x >> 1] = joinNodeValueWithDelta(x >> 1, 1 << (d + 1));
delta[x] = joinDeltas(delta[x], delta[x >> 1]);
delta[x ^ 1] = joinDeltas(delta[x ^ 1], delta[x >> 1]);
delta[x >> 1] = getNeutralDelta();
}
}
public SegmentTree(int n) {
value = new int[2 * n];
for (int i = 0; i < n; i++) {
value[i + n] = getInitValue();
}
for (int i = 2 * n - 1; i > 1; i -= 2) {
value[i >> 1] = queryOperation(value[i], value[i ^ 1]);
}
delta = new int[2 * n];
Arrays.fill(delta, getNeutralDelta());
}
int joinNodeValueWithDelta(int i, int len) {
return joinValueWithDelta(value[i], deltaEffectOnSegment(delta[i], len));
}
public int query(int from, int to) {
from += value.length >> 1;
to += value.length >> 1;
pushDelta(from);
pushDelta(to);
int res = 0;
boolean found = false;
for (int len = 1; from <= to; from = (from + 1) >> 1, to = (to - 1) >> 1, len <<= 1) {
if ((from & 1) != 0) {
res = found ? queryOperation(res, joinNodeValueWithDelta(from, len)) : joinNodeValueWithDelta(from, len);
found = true;
}
if ((to & 1) == 0) {
res = found ? queryOperation(res, joinNodeValueWithDelta(to, len)) : joinNodeValueWithDelta(to, len);
found = true;
}
}
if (!found) throw new RuntimeException();
return res;
}
public void modify(int from, int to, int delta) {
from += value.length >> 1;
to += value.length >> 1;
pushDelta(from);
pushDelta(to);
int a = from;
int b = to;
for (; from <= to; from = (from + 1) >> 1, to = (to - 1) >> 1) {
if ((from & 1) != 0) {
this.delta[from] = joinDeltas(this.delta[from], delta);
}
if ((to & 1) == 0) {
this.delta[to] = joinDeltas(this.delta[to], delta);
}
}
for (int i = a, len = 1; i > 1; i >>= 1, len <<= 1) {
value[i >> 1] = queryOperation(joinNodeValueWithDelta(i, len), joinNodeValueWithDelta(i ^ 1, len));
}
for (int i = b, len = 1; i > 1; i >>= 1, len <<= 1) {
value[i >> 1] = queryOperation(joinNodeValueWithDelta(i, len), joinNodeValueWithDelta(i ^ 1, len));
}
}
}
// Random test
public static void main(String[] args) {
Random rnd = new Random(1);
for (int step = 0; step < 1000; step++) {
int n = rnd.nextInt(50) + 1;
List<Integer>[] tree = getRandomTree(n, rnd);
HeavyLight hl = new HeavyLight(tree, true);
int[] x = new int[n];
Arrays.fill(x, hl.segmentTree.getInitValue());
for (int i = 0; i < 1000; i++) {
int a = rnd.nextInt(n);
int b = rnd.nextInt(n);
List<Integer> path = new ArrayList<>();
getPathFromAtoB(tree, a, b, -1, path);
if (rnd.nextBoolean()) {
int delta = rnd.nextInt(50) - 100;
hl.modify(a, b, delta);
for (int u : path)
x[u] = hl.segmentTree.joinValueWithDelta(x[u], delta);
} else {
int res1 = hl.query(a, b);
int res2 = hl.getNeutralValue();
for (int u : path)
res2 = hl.segmentTree.queryOperation(res2, x[u]);
if (res1 != res2)
throw new RuntimeException();
}
}
}
for (int step = 0; step < 1000; step++) {
int n = rnd.nextInt(50) + 1;
List<Integer>[] tree = getRandomTree(n, rnd);
HeavyLight hl = new HeavyLight(tree, false);
Map<Long, Integer> x = new HashMap<>();
for (int u = 0; u < tree.length; u++)
for (int v : tree[u])
x.put(edge(u, v), hl.segmentTree.getInitValue());
for (int i = 0; i < 1000; i++) {
int a = rnd.nextInt(n);
int b = rnd.nextInt(n);
List<Integer> path = new ArrayList<>();
getPathFromAtoB(tree, a, b, -1, path);
if (rnd.nextBoolean()) {
int delta = rnd.nextInt(50) - 100;
hl.modify(a, b, delta);
for (int j = 0; j + 1 < path.size(); j++) {
long key = edge(path.get(j), path.get(j + 1));
x.put(key, hl.segmentTree.joinValueWithDelta(x.get(key), delta));
}
} else {
int res1 = hl.query(a, b);
int res2 = hl.getNeutralValue();
for (int j = 0; j + 1 < path.size(); j++) {
long key = edge(path.get(j), path.get(j + 1));
res2 = hl.segmentTree.queryOperation(res2, x.get(key));
}
if (res1 != res2)
throw new RuntimeException();
}
}
}
System.out.println("Test passed");
}
static long edge(int u, int v) {
return ((long) Math.min(u, v) << 16) + Math.max(u, v);
}
static boolean getPathFromAtoB(List<Integer>[] tree, int a, int b, int p, List<Integer> path) {
path.add(a);
if (a == b)
return true;
for (int u : tree[a])
if (u != p && getPathFromAtoB(tree, u, b, a, path))
return true;
path.remove(path.size() - 1);
return false;
}
static List<Integer>[] getRandomTree(int n, Random rnd) {
List<Integer>[] t = Stream.generate(ArrayList::new).limit(n).toArray(List[]::new);
int[] p = new int[n];
for (int i = 0, j; i < n; j = rnd.nextInt(i + 1), p[i] = p[j], p[j] = i, i++) ; // random permutation
for (int i = 1; i < n; i++) {
int parent = p[rnd.nextInt(i)];
t[parent].add(p[i]);
t[p[i]].add(parent);
}
return t;
}
}