-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse table 2d.cpp
More file actions
54 lines (45 loc) · 1.21 KB
/
sparse table 2d.cpp
File metadata and controls
54 lines (45 loc) · 1.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
//sparse table O(1) query
gcd,mx,mn,etc
#include<bits/stdc++.h>
using namespace std;
const int N = 505, LG = 10;
int st[N][N][LG][LG];
int a[N][N], lg2[N];
int yo(int x1, int y1, int x2, int y2) {
x2++;
y2++;
int a = lg2[x2 - x1], b = lg2[y2 - y1];
return max(
max(st[x1][y1][a][b], st[x2 - (1 << a)][y1][a][b]),
max(st[x1][y2 - (1 << b)][a][b], st[x2 - (1 << a)][y2 - (1 << b)][a][b])
);
}
void build(int n, int m) { // 0 indexed
for (int i = 2; i < N; i++) lg2[i] = lg2[i >> 1] + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
st[i][j][0][0] = a[i][j];
}
}
for (int a = 0; a < LG; a++) {
for (int b = 0; b < LG; b++) {
if (a + b == 0) continue;
for (int i = 0; i + (1 << a) <= n; i++) {
for (int j = 0; j + (1 << b) <= m; j++) {
if (!a) {
st[i][j][a][b] = max(st[i][j][a][b - 1], st[i][j + (1 << (b - 1))][a][b - 1]);
} else {
st[i][j][a][b] = max(st[i][j][a - 1][b], st[i + (1 << (a - 1))][j][a - 1][b]);
}
}
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
build(n, m);
return 0;
}
// https://www.codechef.com/problems/CENS20B