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 ≤ n, 1 ≤ 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+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,q,st[][maxn][][maxn],p[maxn],op[];
void init()
{
for(int i=;i<=max(n,m);i++)p[i]=+p[i/];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
for(int k=;k<=m;k++)
{
st[j][i][][k]=max(st[j-][i][][k],st[j-][i+(<<(j-))][][k]);
}
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
for(int t=;(<<t)<=m;t++)
for(int k=;k+(<<t)-<=m;k++)
{
st[j][i][t][k]=max(st[j][i][t-][k],st[j][i][t-][k+(<<(t-))]);
}
}
int query(int ql,int qr,int tl,int tr)
{
int x=p[tl-ql+],y=p[tr-qr+];
return max(max(st[x][ql][y][qr],st[x][tl-(<<x)+][y][qr]),max(st[x][ql][y][tr-(<<y)+],st[x][tl-(<<x)+][y][tr-(<<y)+]));
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)rep(j,,m)
{
scanf("%d",&k);
if(k)st[][i][][j]=min({st[][i-][][j-],st[][i-][][j],st[][i][][j-]})+;
}
init();
scanf("%d",&q);
while(q--)
{
rep(i,,)scanf("%d",&op[i]);
int l=,r=min(op[]-op[],op[]-op[])+,ans;
while(l<=r)
{
int mid=l+r>>;
if(query(op[]+mid-,op[]+mid-,op[],op[])>=mid)ans=mid,l=mid+;
else r=mid-;
}
printf("%d\n",ans);
}
//system("Pause");
return ;
}

Animals and Puzzle的更多相关文章

  1. Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增

    D. Animals and Puzzle 题目连接: http://codeforces.com/contest/713/problem/D Description Owl Sonya gave a ...

  2. 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表

    [题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...

  3. Codeforces Round #371 (Div. 1) D - Animals and Puzzle 二维ST表 + 二分

    D - Animals and Puzzle #include<bits/stdc++.h> #define LL long long #define fi first #define s ...

  4. Codeforces 713D Animals and Puzzle(二维ST表+二分答案)

    题目链接 Animals and Puzzle 题意  给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...

  5. Codeforces 713D Animals and Puzzle

    题意:一个n*m的01矩阵,Q个询问,每次询问一个矩形区域内,最大的全1正方形的边长是多少? 题解:dp[0][0][i][j]表示以(i, j)为右下角的正方形的最长边长.RMQ后,二分答案即可. ...

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

    题目链接 给一个01矩阵, 然后每个询问给出两个坐标(x1, y1), (x2, y2). 问你这个范围内的最大全1正方形的边长是多少. 我们dp算出以i, j为右下角的正方形边长最大值. 然后用二维 ...

  7. Codeforces713D. Animals and Puzzle

    $n<=1000,m<=1000$,$n*m$的01矩阵,给$t<=1000000$个询问,每次问一个矩形中最大的1正方形的边长. 先想想不考虑“一个矩形中”的限制,那记$f(i,j ...

  8. BUPT2017 wintertraining(16) #9

    龟速补题.目前基本弃坑.已暂时放弃 D.I 两题. 下面不再写题意了直接说解法注意事项之类,直接放contest链接. https://vjudge.net/contest/151537 A.The ...

  9. Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net

    Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...

随机推荐

  1. (转)URI与URL的区别

    这两天在写代码的时候,由于涉及到资源的位置,因此,需要在Java Bean中定义一些字段,用来表示资源的位置,比如:imgUrl,logoUri等等.但是,每次定义的时候,心里都很纠结,是该用imgU ...

  2. git 恢复丢失的文件-- 不提交入口文件

    务必进入当前controller下面,才能恢复 git checkout HEAD TestController.class.php 01备份index.php文件 02使用 小乌龟的git 删除 t ...

  3. navicat 连接远程mysql

    01putty中session---远程地址(程序访问的域名) ,端口22--load加载进来 02SSH--Auth-Tunnels(隧道)-- putty端口映射SSH--Auth-Tunnels ...

  4. 异常处理try-catch-finally笔记

    当程序发生异常时,我们期望:返回到一种安全状态,并能够让用户执行一些其他的命令:或者 允许用户保存所有操作的结果,并以适当的方式终止程序. 异常处理机制:程序的执行过程中如果出现异常,会自动生成一个异 ...

  5. Cross Product

    Cross Product These are two vectors: They can be multiplied using the "Cross Product" (als ...

  6. [Jmeter]jemeter启动报错,返回错误码 5,处理方法

    今天在使用jmeter的时候,启动GUI,发现bat文件执行有告警,告警内容如下: java.util.prefs.WindowsPreferences <init>WARNING: Co ...

  7. java基础(1)

    class test { static { a=3; //System.out.println(a); } static int a = 1; String b = "ff"; p ...

  8. ***1133. Fibonacci Sequence(斐波那契数列,二分,数论)

    1133. Fibonacci Sequence Time limit: 1.0 secondMemory limit: 64 MB is an infinite sequence of intege ...

  9. hdu 5493 Queue treap实现将元素快速插入到第i个位置

    input T 1<=T<=1000 n 1<=n<=100000 h1 k1 h2 k2 ... ... hn kn 1<=hi<=1e9  0<=ki&l ...

  10. HDU 5918 SequenceI (2016 CCPC长春站 KMP模版变形)

    这个题目的数据应该是比较弱的,赛场上的时候我们暴力也过了,而且我的kmp居然比暴力还要慢-- 这个变形并不难,跳着选数,把漏掉的位置补上就可以了. 代码如下: #include<iostream ...