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. 前台javascript排序

    <script type="text/javascript"> $(function () { $('.Sorthead-ShowUp').click(function ...

  2. 越狱开发-创建真正的后台程序(Daemon Process)

    在网上搜索了一下如何在IOS上面实现Daemon Process,只有chrisalvares的博客中有过详细的描述,但是其博客中描述的较为复杂, 参考stackoverflow中的一个问答: htt ...

  3. openwrt 路由器变砖后修复方法

    https://wiki.openwrt.org/doc/howto/generic.debrick 变砖后需根据类型进行修复,主要有以下四种: (1)if only something on the ...

  4. java中的Volatile 变量

    Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized”:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少, ...

  5. C++ 类的继承、虚拟继承、隐藏、占用空间

    主函数: #include <iostream> #include "test.h" #include "testfuc.h" using name ...

  6. The Linux Storage Stack Diagram 内核 4.0 版的 I/O 栈

  7. Cenots安装openvpn、pam_mysql进行用户认证和流量控制

    一.安装Centos6.5 二.修改YUM仓库 1. 默认仓库不能访问部分软件包,因此我们需要修改YUM仓库 cd /etc/yum.repos.d #备份 mv ./CentOS-Base.repo ...

  8. Jekyll: .md to .html with self defined themes..

    theme is from here $ gem install jekyll bundler ~ $ jekyll new my-awesome-site ~ $ cd my-awesome-sit ...

  9. FTP、TFTP

      FTP 文件传送协议  (File Transfer Protocol) FTP是因特网上使用得最广泛的文件传送协议. 文件传送协议 FTP (File Transfer Protocol) 是因 ...

  10. Linux系统采用netstat命令查看DDOS攻击的方法

    Linux系统采用netstat命令查看DDOS攻击的方法 来源:互联网 作者:佚名 时间:07-05 15:10:21 [大 中 小] 这篇文章主要为大家介绍了Linux系统采用netstat命令查 ...