-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsegment_tree_class.cpp
102 lines (82 loc) · 2.21 KB
/
segment_tree_class.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
// https://leetcode.com/problems/falling-squares
struct segment_tree {
vector<int> tree;
vector<int> lazy;
int n;
segment_tree(int n) {
int size = 1;
while(size < n) {
size *= 2;
}
n = size;
tree.resize(2 * size);
lazy.assign(size, -1);
this->n = n;
}
// [x, y)
int query(int x, int y) {
return query(x, y, 1, 0, n);
}
// point update
void update(int x, int val) {
update(x, val, 1, 0, n);
}
// range update [x, y)
void update(int x, int y, int val) {
update(x, y, val, 1, 0, n);
}
private:
// point update
void update(int x, int val, int id, int l, int r) {
if (l + 1 == r) {
tree[id] = val;
return;
}
int mid = (l + r) / 2;
if (x < mid) update(x, val, left(id), l, mid);
else update(x, val, right(id), mid, r);
tree[id] = combine(tree[left(id)], tree[right(id)]);
}
void update_lazy(int id, int val) {
tree[id] = val;
if (id < n)
lazy[id] = val;
}
void update(int x, int y, int val, int id, int l, int r) {
// fully exclusive
if (y <= l || r <= x) return;
// fully inclusive
if (x <= l && r <= y) {
update_lazy(id, val);
return;
}
propagate(id);
int m = (l + r) / 2;
update(x, y, val, left(id), l, m);
update(x, y, val, right(id), m, r);
tree[id] = combine(tree[left(id)], tree[right(id)]);
}
void propagate(int id) {
if (id < n && ~lazy[id]) {
update_lazy(left(id), lazy[id]);
update_lazy(right(id), lazy[id]);
lazy[id] = -1;
}
}
int left(int id) {
return id * 2;
}
int right(int id) {
return id * 2 + 1;
}
int query( int x, int y, int id, int l, int r) {
if (x <= l && r <= y) return tree[id];
if (y <= l || r <= x) return 0;
propagate(id);
int mid = (l + r) / 2;
return combine(query(x, y, left(id), l, mid), query(x, y, right(id), mid, r));
}
int combine(int a, int b) {
return max(a, b);
}
};