• Animals and Puzzle


    Animals and Puzzle
    time limit per test
    5 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Owl Sonya gave a huge lake puzzle of size n × m to hedgehog Filya as a birthday present. Friends immediately started to assemble the puzzle, but some parts of it turned out to be empty — there was no picture on them. Parts with picture on it are denoted by 1, while empty parts are denoted by 0. Rows of the puzzle are numbered from top to bottom with integers from 1 to n, while columns are numbered from left to right with integers from 1 to m.

    Animals decided to complete the picture and play with it, as it might be even more fun! Owl and hedgehog ask each other some queries. Each query is provided by four integers x1, y1, x2, y2 which define the rectangle, where (x1, y1) stands for the coordinates of the up left cell of the rectangle, while (x2, y2) stands for the coordinates of the bottom right cell. The answer to the query is the size of the maximum square consisting of picture parts only (only parts denoted by 1) and located fully inside the query rectangle.

    Help Sonya and Filya answer t queries.

    Input

    The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — sizes of the puzzle.

    Each of the following n lines contains m integers aij. Each of them is equal to 1 if the corresponding cell contains a picture and 0 if it's empty.

    Next line contains an integer t (1 ≤ t ≤ 1 000 000) — the number of queries.

    Then follow t lines with queries' descriptions. Each of them contains four integers x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n1 ≤ y1 ≤ y2 ≤ m) — coordinates of the up left and bottom right cells of the query rectangle.

    Output

    Print t lines. The i-th of them should contain the maximum size of the square consisting of 1-s and lying fully inside the query rectangle.

    Example
    input
    3 4
    1 1 0 1
    0 1 1 0
    0 1 1 0
    5
    1 1 2 3
    2 1 3 2
    3 2 3 4
    1 1 3 4
    1 2 3 4
    output
    1
    1
    1
    2
    2
    分析:二维倍增;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    const int maxn=1e3+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,q,st[10][maxn][10][maxn],p[maxn],op[4];
    void init()
    {
        for(int i=2;i<=max(n,m);i++)p[i]=1+p[i/2];
        for(int j=1;(1<<j)<=n;j++)
            for(int i=1;i+(1<<j)-1<=n;i++)
                for(int k=1;k<=m;k++)
                {
                    st[j][i][0][k]=max(st[j-1][i][0][k],st[j-1][i+(1<<(j-1))][0][k]);
                }
        for(int j=0;(1<<j)<=n;j++)
            for(int i=1;i+(1<<j)-1<=n;i++)
                for(int t=1;(1<<t)<=m;t++)
                    for(int k=1;k+(1<<t)-1<=m;k++)
                    {
                        st[j][i][t][k]=max(st[j][i][t-1][k],st[j][i][t-1][k+(1<<(t-1))]);
                    }
    }
    int query(int ql,int qr,int tl,int tr)
    {
        int x=p[tl-ql+1],y=p[tr-qr+1];
        return max(max(st[x][ql][y][qr],st[x][tl-(1<<x)+1][y][qr]),max(st[x][ql][y][tr-(1<<y)+1],st[x][tl-(1<<x)+1][y][tr-(1<<y)+1]));
    }
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)rep(j,1,m)
        {
            scanf("%d",&k);
            if(k)st[0][i][0][j]=min({st[0][i-1][0][j-1],st[0][i-1][0][j],st[0][i][0][j-1]})+1;
        }
        init();
        scanf("%d",&q);
        while(q--)
        {
            rep(i,0,3)scanf("%d",&op[i]);
            int l=0,r=min(op[2]-op[0],op[3]-op[1])+1,ans;
            while(l<=r)
            {
                int mid=l+r>>1;
                if(query(op[0]+mid-1,op[1]+mid-1,op[2],op[3])>=mid)ans=mid,l=mid+1;
                else r=mid-1;
            }
            printf("%d
    ",ans);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    kubernetes-handbook 阅读笔记
    kubernetes-notes--阅读笔记
    SpringInAction4笔记——复习
    spring源码解析——2容器的基本实现(第2版笔记)
    把node加入master节点时,日志内容分析
    初始化master节点时,日志内容分析
    Mac OS用minikube安装单节点kubernetes
    Mac OS用vmvare安装多节点kubernetes
    FatMouse's Speed 基础DP
    FatMouse and Cheese 动态化搜索
  • 原文地址:https://www.cnblogs.com/dyzll/p/5933352.html
Copyright © 2020-2023  润新知