-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathsparse_table.cc
62 lines (56 loc) · 1.5 KB
/
sparse_table.cc
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
//
// Sparse Table for Range Minimum Query
//
// Description:
// The sparse table stores
// table[h][i] = min(x[i], ..., x[i+2^h-1])
// to solve
// RMQ(i,j) = min { x[i], ..., x[j-1] }.
//
// Algorithm:
// table[h+1][i] = min(table[h][i], table[h][i+2^h]).
// RMQ(i,j) = min(table[h][i], table[h][j-2^h-1].
//
// Complexity:
// O(n log n) for construction,
// O(1) for query.
//
#include <iostream>
#include <vector>
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
template <class T>
struct sparse_table {
const vector<T> &x;
vector<vector<int>> table;
int argmin(int i, int j) { return x[i] < x[j] ? i : j; }
sparse_table(const vector<T> &x) : x(x) {
int logn = sizeof(int)*__CHAR_BIT__-1-__builtin_clz(x.size());
table.assign(logn+1, vector<int>(x.size()));
iota(all(table[0]), 0);
for (int h = 0; h+1 <= logn; ++h)
for (int i = 0; i+(1<<h) < x.size(); ++i)
table[h+1][i] = argmin(table[h][i], table[h][i+(1<<h)]);
}
T range_min(int i, int j) { // = min x[i,j)
int h = sizeof(int)*__CHAR_BIT__-1-__builtin_clz(j-i); // = log2
return x[argmin(table[h][i], table[h][j-(1<<h)])];
}
};
int main() {
vector<int> a = {5,3,8,2,1,5,6};
int n = a.size();
sparse_table<int> ST(a);
for (int i = 0; i < n; ++i) {
for (int j = i+1; j <= n; ++j) {
cout << ST.range_min(i, j) << " ";
}
cout << endl;
}
}