codeforces 731D (DP 二分 二维RMQ)

26 篇文章 0 订阅
6 篇文章 0 订阅

题目链接:点击这里

题意:给出一个01矩阵,每次询问一个矩形中的最大全1正方形的边长。

dp[i][j] 存储以 (i,j) 为右下角的最大全1正方形,转移方程就是

dp[i][j]={min{dp[i1][j],dp[i][j1],dp[i1][j1]}+10(val[i][j]=1)(val[i][j]=0)
.然后用二维RMQ记录子矩阵的最大正方形的边长。 然后对于询问 (x1,y1),(x2,y2) ,二分边长mid,如果询问的矩形中存边长为mid的全1正方形那么必然是以矩形 (x1+mid1,y1+mid1)(x2,y2) 中的某一个点为右下角,所以直接矩形的最大值check即可。

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值