-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsparsetable.cpp
More file actions
52 lines (48 loc) · 1.26 KB
/
sparsetable.cpp
File metadata and controls
52 lines (48 loc) · 1.26 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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define PI acos(-1)
#define pb push_back
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pair<int, pi>>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 10005
#define mod 1000000007
struct rmq
{
bool is_min;
vector<vector<int>> st;
vector<int> log;
int f(int a, int b) { return (is_min) ? min(a, b) : max(a, b); }
int qry(int l, int r) { return f(st[l][log[r - l + 1]], st[r - (1 << log[r - l + 1]) + 1][log[r - l + 1]]); }
rmq(vector<int> &v, bool flag)
{
is_min = flag;
int n = v.size();
log.resize(n + 1);
log[1] = 0;
for (int i = 2; i <= n; i++)
log[i] = log[i / 2] + 1;
int m = log[n] + 2;
st.assign(n + 1, vector<int>(m, 0));
for (int i = 0; i < n; i++)
st[i][0] = v[i];
for (int j = 1; j < m; j++)
{
for (int i = 0; i + (1 << j) <= n; i++)
st[i][j] = f(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
};
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}