-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrmq.cpp
49 lines (46 loc) · 1.29 KB
/
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
#include <vector>
struct RMQ {
vector<int> M, R;
int N, I, F;
RMQ(vector<int>& _R) {
R = _R; N = R.size();
M.resize(8 * N + 10);
_makeTree(1, 0, N);
}
// Returns minimum of interval [a,b) in O(log N)
int getMin(int a, int b) {
I = a; F = b;
return _find(1, 0, N);
}
// makes V[pos] = num in O(log N)
void update(int pos, int num) {
R[pos] = num;
I = pos; F = pos+1;
_update(1, 0, N);
}
int _find(int node, int a, int b) {
if (a >= I && b <= F) return M[node];
if (a >= F || b <= I) return -1;
int left = _find(2*node, a, (a+b)/2);
int right = _find(2*node+1, (a+b)/2, b);
if (left == -1 || right == -1) return max(left, right);
if (R[left] <= R[right]) return left; // (*)
return right;
}
void _makeTree(int node, int a, int b) {
if (a+1 == b) { M[node] = a; return; }
_makeTree(2*node, a, (a+b)/2);
_makeTree(2*node+1, (a+b)/2, b);
if (R[M[2*node]] <= R[M[2*node+1]]) M[node] = M[2*node]; // (*)
else M[node] = M[2*node+1];
}
void _update(int node, int a, int b) {
if (a == I && b == F) { M[node] = a; return; }
if (a >= F || b <= I) return;
_update(2*node, a, (a+b)/2);
_update(2*node+1, (a+b)/2, b);
if (R[M[2*node]] <= R[M[2*node+1]]) M[node] = M[2*node]; // (*)
else M[node] = M[2*node+1];
}
// (*) change <= to >= to have MAX query instead
};