-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment-tree.cpp
134 lines (116 loc) · 3.23 KB
/
segment-tree.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
template <typename T>
using t3 = tuple<T,T,T>;
template <typename T>
using t4 = tuple<T,T,T,T>;
typedef vector<ll> vl;
template <typename T>
using vv = vector<vector<T>>;
template <typename T>
using vvv = vector<vv<T>>;
#define MIN -1 // Change this if negative
class SegmentTree {
private:
int n;
vll A, st, lazy; // A -> Original Array
int l(int p) { return (p<<1); }
int r(int p) { return (p<<1)+1; }
int conquer(ll a, ll b) {
if (a==MIN) return b;
if (b==MIN) return a;
return min(a, b); // Change this if RMaxQ
}
void build(int p, int L, int R) {
if (L==R) {
st[p] = A[L];
return;
}
int m = (L+R)/2;
build(l(p), L, m);
build(r(p), m+1, R);
st[p] = conquer(st[l(p)], st[r(p)]);
}
void propagate(int p, int L, int R) {
if (lazy[p]==MIN) return;
st[p] = lazy[p];
if (L==R)
A[L] = lazy[p];
else
lazy[l(p)] = lazy[r(p)] = lazy[p];
lazy[p] = MIN;
}
ll RMQ(int p, int L, int R, int i, int j) {
propagate(p, L, R);
if (i>j) return MIN;
if ((L>=i)&&(R<=j)) return st[p];
int m = (L+R)/2;
return conquer(RMQ(l(p), L, m, i, min(m, j)),
RMQ(r(p), m+1, R, max(m+1, i), j ));
}
void update(int p, int L, int R, int i, int j, ll val) {
propagate(p, L, R);
if (i>j) return;
if ((L>=i)&&(R<=j)) {
lazy[p] = val;
propagate(p, L, R);
} else {
int m = (L+R)/2;
update(l(p), L, m, i, min(m, j), val);
update(r(p), m+1, R, max(m+1, i), j, val);
ll lsubtree = (lazy[l(p)] != MIN)? lazy[l(p)] : st[l(p)];
ll rsubtree = (lazy[r(p)] != MIN)? lazy[r(p)] : st[r(p)];
st[p] = (lsubtree <= rsubtree)? st[l(p)]: st[r(p)]; // Change if RMaxQ
}
}
public:
SegmentTree(int sz): n(sz), st(4*n), lazy(4*n, MIN) {}
SegmentTree(const vll &initialA): SegmentTree((int)initialA.size()) {
A = initialA;
build(1, 0, n-1);
}
void update(int i, int j, ll val) { update(1, 0, n-1, i, j, val); }
int RMQ(int i, int j) { return RMQ(1, 0, n-1, i, j); }
};
/* TESTS
* Uncomment this section to test the code
* Expected output:
* 13
* 11
* 15
* =======
* 13
* 15
* 15
* =======
* 30
* 15
* 15
* =======
* 7
* 15
* 7
*/
int main() {
vll A = {18, 17, 13, 19, 15, 11, 20, 99};
SegmentTree st(A);
printf("%d\n", st.RMQ(1, 3)); // 13
printf("%d\n", st.RMQ(4, 7)); // 11
printf("%d\n", st.RMQ(3, 4)); // 15
st.update(5,5,77);
printf("=======\n%d\n", st.RMQ(1, 3)); // 13
printf("%d\n", st.RMQ(4, 7)); // 15
printf("%d\n", st.RMQ(3, 4)); // 15
st.update(0,3,30);
printf("=======\n%d\n", st.RMQ(1, 3)); // 30
printf("%d\n", st.RMQ(4, 7)); // 15
printf("%d\n", st.RMQ(3, 4)); // 15
st.update(3,3,7);
printf("=======\n%d\n", st.RMQ(1, 3)); // 7
printf("%d\n", st.RMQ(4, 7)); // 15
printf("%d\n", st.RMQ(3, 4)); // 7
}