-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsparse_table.cpp
More file actions
executable file
·148 lines (131 loc) · 4.5 KB
/
Copy pathsparse_table.cpp
File metadata and controls
executable file
·148 lines (131 loc) · 4.5 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
#include <bits/stdc++.h>
using namespace std;
#define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
#define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
#define fixed(n) fixed << setprecision(n)
#define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
#define fill(vec, value) memset(vec, value, sizeof(vec));
#define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
#define add_mod(a, b, m) (((a % m) + (b % m)) % m)
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define sz(x) int(x.size())
#define debug(x) cout << #x << ": " << (x) << "\n";
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define Mod 1'000'000'007
#define OO 2'000'000'000
#define EPS 1e-9
#define PI acos(-1)
template < typename T = int > using Pair = pair < T, T >;
vector < string > RET = {"NO", "YES"};
template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
for (auto &x : v) in >> x;
return in;
}
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
for (const T &x : v) out << x << ' ';
return out;
}
template < typename T >
struct MinOp {
constexpr T operator()(const T& a, const T& b) const { return a < b ? a : b; }
};
/*
* Sparse_Table — Static RMQ / Range Query in O(1) or O(log n)
*
* Template params:
* T = answer/value type (default int)
* Op = binary combine functor (default MinOp<T> = min)
* Base = 0 → 0-indexed, 1 → 1-indexed (default 0)
* numsType = input array type (default T)
*
* Constructor:
* Sparse_Table<T, Op, Base, numsType> st(
* int N,
* vector<numsType>& vec,
* Op op = Op{},
* T def = numeric_limits<T>::max() // identity for Op — change when not using min
* );
*
* Methods:
* query(int L, int R, bool is_overlap = false) → T
* is_overlap = false → O(1), only correct for idempotent ops (min, max, gcd)
* is_overlap = true → O(log n), correct for all ops (sum, etc.)
*
* Example (range min, 0-indexed — default):
* Sparse_Table<int> st(n, arr);
* cout << st.query(1, n);
*
* Example (range min, 1-indexed):
* Sparse_Table<int, MinOp<int>, 1> st(n, arr);
* cout << st.query(1, n);
*
* Example (range sum via lambda, 0-indexed):
* auto sumOp = [](ll a, ll b){ return a + b; };
* Sparse_Table<ll, decltype(sumOp)> st(n, arr, sumOp, 0LL);
* cout << st.query(0, n - 1, true);
*
* Example (range sum via plain function):
* ll mySum(ll a, ll b) { return a + b; }
* Sparse_Table<ll, decltype(&mySum)> st(n, arr, &mySum, 0LL);
* cout << st.query(0, n - 1, true);
*/
template < typename T = int, typename Op = MinOp < T >, int Base = 0, typename numsType = T >
class Sparse_Table {
private:
int n, LOG;
vector < vector < T > > table;
vector < int > Bin_Log;
Op operation;
T DEFAULT;
void Build_Table() {
for (int log = 1; log < LOG; log++)
for (int i = 1; i + (1 << log) - 1 <= n; i++)
table[i][log] = operation(table[i][log - 1], table[i + (1 << (log - 1))][log - 1]);
}
T query_1(int L, int R) {
int log = Bin_Log[R - L + 1];
return operation(table[L][log], table[R - (1 << log) + 1][log]);
}
T query_log_n(int L, int R) {
T answer = DEFAULT;
for (int log = LOG; log >= 0; log--) {
if (L + (1 << log) - 1 <= R) {
answer = operation(answer, table[L][log]);
L += 1 << log;
}
}
return answer;
}
public:
Sparse_Table(
int N = 0,
const vector < numsType >& vec = vector < numsType >(),
Op op = Op{},
T def = numeric_limits < T > ::max()
) : n(N), LOG(__lg(n) + 1), operation(op), DEFAULT(def) {
table = vector < vector < T > > (n + 10, vector < T > (LOG, DEFAULT));
Bin_Log = vector < int > (n + 10);
for (int i = 2; i <= n; i++)
Bin_Log[i] = Bin_Log[i >> 1] + 1;
for (int i = 1; i <= N; i++)
table[i][0] = T(vec[i - !Base]);
Build_Table();
}
T query(int L, int R, bool is_overlap = false) {
return !is_overlap ? query_1(L, R) : query_log_n(L, R);
}
};
void Solve(){
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t = 1;
//cin >> t;
while(t--)
Solve();
return 0;
}