-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTreeMinMax.cpp
More file actions
185 lines (181 loc) · 5.21 KB
/
Copy pathSegmentTreeMinMax.cpp
File metadata and controls
185 lines (181 loc) · 5.21 KB
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
/**
* Author: Kevin Li
* Lang: C++
* Description: segment tree for min/max queries
*/
#include <iostream>
#include <vector>
using namespace std;
#define INF 1e9 // change as needed
template<class T>
struct strmq {
#define mp make_pair
#define f first
#define s second
struct node {
T maxv, minv;
T lazy;
bool ch;
T cv;
node *left, *right;
node () : maxv(-INF), minv(INF), lazy(0), ch(false), cv(0), left(0), right(0) {}
};
node *root = 0;
vector<T> a;
int n;
strmq() {
root = new node();
a.clear();
n = 0;
}
void push(node *v, int l, int r) {
if (v->ch) {
if (l != r) {
if (v->left == 0) v->left = new node();
if (v->right == 0) v->right = new node();
v->left->ch = true;
v->right->ch = true;
v->left->lazy = 0;
v->right->lazy = 0;
v->left->cv = v->cv;
v->right->cv = v->cv;
}
v->lazy = 0;
v->ch = false;
v->maxv = v->cv;
v->minv = v->cv;
v->cv = 0;
} else if (v->lazy != 0) {
if (l != r) {
if (v->left == 0) v->left = new node();
if (v->right == 0) v->right = new node();
if (v->left->ch) {
v->left->cv += v->lazy;
} else {
v->left->lazy += v->lazy;
}
if (v->right->ch) {
v->right->cv += v->lazy;
} else {
v->right->lazy += v->lazy;
}
}
v->maxv += v->lazy;
v->minv += v->lazy;
v->lazy = 0;
}
}
void pull(node *v, int l, int r) {
if (v->left == 0) v->left = new node();
if (v->right == 0) v->right = new node();
push(v->left,l,(l+r)/2);
push(v->right,(l+r)/2+1,r);
v->maxv = max(v->left->maxv,v->right->maxv);
v->minv = min(v->left->minv,v->right->minv);
}
void bt(node *v, int l, int r) {
if (l == r) {
v->minv = v->maxv = a[l];
} else {
if (v->left == 0) v->left = new node();
if (v->right == 0) v->right = new node();
bt(v->left,l,(l+r)/2);
bt(v->right,(l+r)/2+1,r);
pull(v,l,r);
}
}
void construct(vector<T> A) {
a = A;
n = (int)a.size();
root = new node(0,n-1);
bt(0,0,n-1);
}
void update(node *v, int l, int r, int i, T val) {
push(v,l,r);
if (l == r) {
v->minv += val;
v->maxv += val;
} else {
if (i <= (l+r)/2) {
if (v->left == 0) v->left = new node();
update(v->left,l,(l+r)/2,i,val);
} else {
if (v->right == 0) v->right = new node();
update(v->right,(l+r)/2+1,r,i,val);
}
pull(v,l,r);
}
}
void ud(int i, T val) {
update(root,0,n-1,i,val);
}
void augment(node *v, int l, int r, int L, int R, T val) {
push(v,l,r);
if (L <= l && r <= R) {
if (v->ch) {
v->cv += val;
} else {
v->lazy += val;
}
} else if (l > R || L > r) {
return;
} else {
if (v->left == 0) v->left = new node();
if (v->right == 0) v->right = new node();
augment(v->left,l,(l+r)/2,L,R,val);
augment(v->right,(l+r)/2+1,r,L,R,val);
pull(v,l,r);
}
}
void ad(int L, int R, T val) {
augment(root,0,n-1,L,R,val);
}
void change(node *v, int l, int r, int L, int R, T val) {
push(v,l,r);
if (L <= l && r <= R) {
v->ch = true;
v->lazy = 0;
v->cv = val;
} else if (l > R || L > r) {
return;
} else {
if (v->left == 0) v->left = new node();
if (v->right == 0) v->right = new node();
change(v->left,l,(l+r)/2,L,R,val);
change(v->right,(l+r)/2+1,r,L,R,val);
pull(v,l,r);
}
}
void cd(int L, int R, T val) {
change(root,0,n-1,L,R,val);
}
T minquery(node *v, int l, int r, int L, int R) {
push(v,l,r);
if (L <= l && r <= R) {
return v->minv;
} else if (l > R || L > r) {
return INF;
} else {
return min(((v->left==0)?INF:minquery(v->left,l,(l+r)/2,L,R)),((v->right==0)?INF:minquery(v->right,(l+r)/2+1,r,L,R)));
}
}
T minqd(int L, int R) {
return minquery(root,0,n-1,L,R);
}
T maxquery(node *v, int l, int r, int L, int R) {
push(v,l,r);
if (L <= l && r <= R) {
return v->maxv;
} else if (l > R || L > r) {
return -INF;
} else {
return max(((v->left==0)?-INF:maxquery(v->left,l,(l+r)/2,L,R)),((v->right==0)?-INF:maxquery(v->right,(l+r)/2+1,r,L,R)));
}
}
T maxqd(int L, int R) {
return maxquery(root,0,n-1,L,R);
}
T getVal(int i) {
return maxqd(i,i);
}
};