-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentsTreeUpdates.java
More file actions
45 lines (41 loc) · 872 Bytes
/
SegmentsTreeUpdates.java
File metadata and controls
45 lines (41 loc) · 872 Bytes
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
import java.util.*;
public class SegmentsTreeUpdates{
static int empty = -1;
static int initial = 0;
static int H = 18;
static int M = 1 << (H - 1);
static int[] s = new int[2 * M];
static {
Arrays.fill(s, empty);
s[1] = initial;
}
static int get(int x) {
for (int h = H - 1; h >= 0; h--) {
int y = x >> h;
if (s[y] != empty) {
return s[y];
}
}
throw new RuntimeException();
}
static void update(int v, int from, int to, int value, int h) {
int v1 = v << h;
int v2 = ((v + 1) << h) - 1;
if (from > v2 || to < v1) {
return;
}
from = Math.max(from, v1);
to = Math.min(to, v2);
if (from == v1 && to == v2) {
s[v] = value;
return;
}
if (s[v] != empty) {
s[2 * v] = s[v];
s[2 * v + 1] = s[v];
}
s[v] = empty;
update(2 * v, from, to, value, h - 1);
update(2 * v + 1, from, to, value, h - 1);
}
}