#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> #define PLI pair<LL, int> #define ull unsigned long long using namespace std; const int N = 1000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; int a[N][N], b[N][N], Log[N], n, m; struct ST2 { int dp[N][N][10][10], ty; void build(int n, int m, int b[N][N], int _ty) { ty = _ty; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) dp[i][j][0][0] = ty * b[i][j]; for(int u = 0; u <= Log[n]; u++) { for(int v = 0; v <= Log[m]; v++) { if(!u && !v) continue; for(int i = 1; i+(1<<u)-1 <= n; i++) { for(int j = 1; j+(1<<v)-1 <= m; j++) { if(u) dp[i][j][u][v] = max(dp[i][j][u-1][v], dp[i+(1<<(u-1))][j][u-1][v]); else dp[i][j][u][v] = max(dp[i][j][u][v-1], dp[i][j+(1<<(v-1))][u][v-1]); } } } } } int query(int x1, int y1, int x2, int y2) { int k1 = Log[x2-x1+1], k2 = Log[y2-y1+1]; x2 = x2-(1<<k1)+1; y2 = y2-(1<<k2)+1; return max(max(dp[x1][y1][k1][k2], dp[x2][y1][k1][k2]), max(dp[x1][y2][k1][k2], dp[x2][y2][k1][k2])); } } rmq; int main() { for(int i = -(Log[0]=-1); i < N; i++) Log[i] = Log[i - 1] + ((i & (i - 1)) == 0); scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &a[i][j]); for(int i = n; i >= 1; i--) { for(int j = m; j >= 1; j--) { if(!a[i][j]) continue; b[i][j] = min(b[i+1][j+1], min(b[i][j+1], b[i+1][j])) + 1; } } rmq.build(n, m, b, 1); int q; scanf("%d", &q); while(q--) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); int l = 1, r = min(x2-x1, y2-y1) + 1, ans = 0; while(l <= r) { int mid = l + r >> 1; int x3 = x2 - mid + 1, y3 = y2 - mid + 1; if(rmq.query(x1, y1, x3, y3) >= mid) l = mid + 1, ans = mid; else r = mid - 1; } printf("%d\n", ans); } return 0; } /* */
原文地址:https://www.cnblogs.com/CJLHY/p/9727247.html
时间: 2024-10-06 12:17:52