-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsegment_tree_rmq.cpp
executable file
·123 lines (92 loc) · 2.49 KB
/
segment_tree_rmq.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
/** resources:
1. https://codeforces.com/blog/entry/15729
2. https://codeforces.com/blog/entry/15890
3. https://codeforces.com/blog/entry/18051
4. https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/range-minimum-query/description/
**/
template <class T, class F = function <T(const T &, const T &)> >
class SegmentTree {
vector <T> _A;
vector <T> ST;
vector <T> LT;
F func;
int _N;
public :
template <class iter>
SegmentTree(iter _begin, iter _end, const F _func = [](T a, T b) {return a <= b ? a : b;}) : func(_func) {
_N = distance(_begin, _end);
_N = (1 << (int)ceil(log2(_N)));
_A.assign(_N + 1, 0);
ST.assign(_N << 1, 0);
LT.assign(_N << 1, 0);
__typeof(_begin) i = _begin;
for(int j = 1; i != _end; ++i, ++j)
_A[j] = *i;
build(1, 1, _N);
}
void build(int p, int l, int r) {
if(l == r) {
ST[p] = _A[l];
return;
}
int mid = (l + r) >> 1;
build(p + p, l, mid);
build(p + p + 1, mid + 1, r);
const T & x = ST[p + p];
const T & y = ST[p + p + 1];
ST[p] = func(x, y);
}
void update_range(int ul, int ur, int delta) {
update_range(ul, ur, delta, 1, 1, _N);
}
T query(int ql, int qr) {
return query(ql, qr, 1, 1, _N);
}
void update_point(int inx, int delta) {
inx += _N - 1;
ST[inx] = delta;
while(inx > 1) {
inx >>= 1;
const T & x = ST[inx + inx];
const T & y = ST[inx + inx + 1];
ST[inx] = func(x, y);
}
}
private :
void update_range(int ul, int ur, int delta, int p, int l, int r) {
if(r < ul || ur < l)
return;
if(ul <= l && r <= ur) {
ST[p] += delta;
LT[p] += delta;
return;
}
propagate(p);
int mid = (l + r) >> 1;
update_range(ul, ur, delta, p + p, l, mid);
update_range(ul, ur, delta, p + p + 1, mid + 1, r);
const T & x = ST[p + p];
const T & y = ST[p + p + 1];
ST[p] = func(x, y);
}
T query(int ql, int qr, int p, int l, int r) {
if(r < ql || qr < l)
return INT_MAX;
if(ql <= l && r <= qr)
return ST[p];
propagate(p);
int mid = (l + r) >> 1;
const T & x = query(ql, qr, p + p, l, mid);
const T & y = query(ql, qr, p + p + 1, mid + 1, r);
return func(x, y);
}
void propagate(int p) {
if(LT[p]) {
ST[p + p] += LT[p];
ST[p + p + 1] += LT[p];
LT[p + p] += LT[p];
LT[p + p + 1] += LT[p];
LT[p] = 0;
}
}
};