## codeforces 713D D. Animals and Puzzle 二分+二维rmq

#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int dp[1001][1001][11][11], mm[1005];
void initRmq(int n, int m)
{
mm[0] = -1;
for(int i = 1; i <= max(n, m); i++) {
mm[i] = ((i&(i-1)) == 0) ? mm[i-1] + 1 : mm[i-1];
}
for (int ii = 0; ii <= mm[n]; ii ++) {
for (int jj = 0; jj <= mm[m]; jj ++) {
if (ii + jj) {
for (int i = 1; i + (1<<ii) - 1 <= n; i ++) {
for(int j = 1; j + (1<<jj) - 1 <= m; j ++) {
if (ii) {
dp[i][j][ii][jj] = max(dp[i][j][ii-1][jj], dp[i+(1<<(ii-1))][j][ii-1][jj]);
} else {
dp[i][j][ii][jj] = max(dp[i][j][ii][jj-1], dp[i][j+(1<<(jj-1))][ii][jj-1]);
}
}
}
}
}
}
}
int rmq(int x1, int y1, int x2, int y2)
{
int k1 = mm[x2-x1+1];
int k2 = mm[y2-y1+1];
x2 = x2 - (1<<k1) + 1;
y2 = y2 - (1<<k2) + 1;
return max(max(dp[x1][y1][k1][k2], dp[x1][y2][k1][k2]), max((dp[x2][y1][k1][k2]), dp[x2][y2][k1][k2]));
}
int main()
{
int n, m, x, q, x1, y1, x2, y2;
cin>>n>>m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
if (x)
dp[i][j][0][0] = min(min(dp[i-1][j][0][0], dp[i][j-1][0][0]), dp[i-1][j-1][0][0]) + 1;
}
}
initRmq(n, m);
cin>>q;
while (q--) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
int l = 0, r = min(x2 - x1, y2 - y1) + 1;
int ans;
while (l <= r) {
int mid = l + r >> 1;
if (rmq(x1 + mid - 1, y1 + mid - 1, x2, y2) >= mid) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
printf("%d\n", ans);
}
return 0;
}

posted on 2016-10-04 10:31  yohaha  阅读(107)  评论(0编辑  收藏

• 随笔 - 305
• 文章 - 0
• 评论 - 6
• 引用 - 0