codeforces 731D (DP 二分 二維RMQ)
阿新 • • 發佈:2019-01-06
題意:給出一個01矩陣,每次詢問一個矩形中的最大全1正方形的邊長。
用
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
#define Clear(x,y) memset (x,y,sizeof(x))
#define Close() ios::sync_with_stdio(0)
#define Open() freopen ("more.in", "r", stdin)
#define get_min(a,b) a = min (a, b)
#define get_max(a,b) a = max (a, b);
#define y0 yzz
#define y1 yzzz
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define pb push_back
#define pl c<<1
#define pr (c<<1)|1
#define lson tree[c].l,tree[c].mid,pl
#define rson tree[c].mid+1,tree[c].r,pr
#define mod 1000000007
typedef unsigned long long ull;
template <class T> inline T lowbit (T x) {return x&(-x);}
template <class T> inline T sqr (T x) {return x*x;}
template <class T>
inline bool scan (T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0; //EOF
while (c != '-' && (c < '0' || c > '9') ) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
const double pi = 3.14159265358979323846264338327950288L;
using namespace std;
#define INF 1e17
#define maxn 1005
#define maxm 1000005
//-----------------morejarphone--------------------//
int n, m;
int val[maxn][maxn];
int dp[maxn][maxn][11][11];
void rmq_init ()
{
for(int row = 1; row <= n; row++)
for(int col = 1; col <=m; col++)
dp[row][col][0][0] = val[row][col];
int mx = log(double(n)) / log(2.0);
int my = log(double(m)) / log(2.0);
for(int i=0; i<= mx; i++)
{
for(int j = 0; j<=my; j++)
{
if(i == 0 && j ==0) continue;
for(int row = 1; row+(1<<i)-1 <= n; row++)
{
for(int col = 1; col+(1<<j)-1 <= m; col++)
{
if(i == 0)//y軸二分
dp[row][col][i][j]=max(dp[row][col][i][j-1],dp[row][col+(1<<(j-1))][i][j-1]);
else//x軸二分
dp[row][col][i][j]=max(dp[row][col][i-1][j],dp[row+(1<<(i-1))][col][i-1][j]);
}
}
}
}
}
int rmq (int x1,int x2,int y1,int y2)
{
int kx = log(double(x2-x1+1)) / log(2.0);
int ky = log(double(y2-y1+1)) / log(2.0);
int m1 = dp[x1][y1][kx][ky];
int m2 = dp[x2-(1<<kx)+1][y1][kx][ky];
int m3 = dp[x1][y2-(1<<ky)+1][kx][ky];
int m4 = dp[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky];
return max( max(m1,m2) , max(m3,m4));
}
int main () {
//freopen ("more.in", "r", stdin);
scanf ("%d%d", &n, &m);
Clear (val, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scan (val[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!val[i][j]) val[i][j] = 0;
else
val[i][j] = min (min (val[i-1][j], val[i][j-1]), val[i-1][j-1])+1;
}
}
rmq_init ();
int q;
scan (q);
while (q--) {
int x1, x2, y1, y2;
scanf ("%d%d%d%d", &x1, &y1, &x2, &y2);
int l = 0, r = min (x2-x1+1, y2-y1+1);
while (r-l > 1) {
int mid = (l+r)>>1;
if (rmq (x1+mid-1, x2, y1+mid-1, y2) >= mid) l = mid;
else r = mid;
}
if (rmq (x1+r-1, x2, y1+r-1, y2) >= r) printf ("%d\n", r);
else printf ("%d\n", l);
}
return 0;
}