forked from ShahjalalShohag/code-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparse Table 2D.cpp
More file actions
97 lines (90 loc) · 2.32 KB
/
Copy pathSparse Table 2D.cpp
File metadata and controls
97 lines (90 loc) · 2.32 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
#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]);
}
}
}
}
}
}
string s[N];
int l[N][N], u[N][N];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!j) l[i][j] = 1;
else l[i][j] = 1 + (s[i][j - 1] <= s[i][j] ? l[i][j - 1] : 0);
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
if (!i) u[i][j] = 1;
else u[i][j] = 1 + (s[i - 1][j] <= s[i][j] ? u[i - 1][j] : 0);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int nw = 1, mnx = u[i][j], mny = l[i][j];
for (int len = 1; len <= min(i, j); len++) {
mnx = min(mnx, u[i][j - len]);
mny = min(mny, l[i - len][j]);
if (min(mnx, mny) >= len + 1) nw++;
else break;
}
a[i][j] = nw;
}
}
build(n, m);
int q;
cin >> q;
while (q--) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--;
x2--;
y2--;
int l = 1, r = min(x2 - x1 + 1, y2 - y1 + 1), ans = 0;
while (l <= r) {
int mid = l + r >> 1;
if (yo(x1 + mid - 1, y1 + mid - 1, x2, y2) >= mid) ans = mid, l = mid + 1;
else r = mid - 1;
}
cout << ans << '\n';
}
return 0;
}
// https://www.codechef.com/problems/CENS20B