Animals and Puzzle
5 seconds
512 megabytes
standard input
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.
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.
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.
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
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的更多相关文章
- 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 ...
- 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表
[题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...
- 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 ...
- Codeforces 713D Animals and Puzzle(二维ST表+二分答案)
题目链接 Animals and Puzzle 题意 给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...
- Codeforces 713D Animals and Puzzle
题意:一个n*m的01矩阵,Q个询问,每次询问一个矩形区域内,最大的全1正方形的边长是多少? 题解:dp[0][0][i][j]表示以(i, j)为右下角的正方形的最长边长.RMQ后,二分答案即可. ...
- codeforces 713D D. Animals and Puzzle 二分+二维rmq
题目链接 给一个01矩阵, 然后每个询问给出两个坐标(x1, y1), (x2, y2). 问你这个范围内的最大全1正方形的边长是多少. 我们dp算出以i, j为右下角的正方形边长最大值. 然后用二维 ...
- Codeforces713D. Animals and Puzzle
$n<=1000,m<=1000$,$n*m$的01矩阵,给$t<=1000000$个询问,每次问一个矩形中最大的1正方形的边长. 先想想不考虑“一个矩形中”的限制,那记$f(i,j ...
- BUPT2017 wintertraining(16) #9
龟速补题.目前基本弃坑.已暂时放弃 D.I 两题. 下面不再写题意了直接说解法注意事项之类,直接放contest链接. https://vjudge.net/contest/151537 A.The ...
- Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net
Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...
随机推荐
- Spring自动扫描
需要在Springde 配置文件中加入 <context:component-scan base-package="com.annoation"> base-packa ...
- Chapter 14_3 非全局的环境
关于“环境”的一大问题在于它是全局的,任何对它的修改都会影响程序的所有部分. 例如:若安装一个元表用于控制全局变量的访问,那么整个程序都必须遵循这个规范. 当使用某个库时,没有先声明就使用了全局变量, ...
- chapter8_4 错误处理
在Lua中,对于大多数程序都不用作任何错误处理,应用程序本身会负责这类问题. 所有的Lua活动都是由应用程序的一次调用开始的,这类调用要求Lua执行一个程序块. 执行过程中发生了错误,此调用会返回一个 ...
- python 函数/列表的应用
enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a1 b2 c ...
- 7.编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),如果x的值是1,在控制台打印出大写的英文字母表;如果x的值是2,在 控制台打印出小写的英文字母表。其次,再定义一个主类——TestClass,在主类 的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大 小写英文字母表。
package com.bao; public class Print1 { int x; void output() { if(x==1) { System.out.println("AB ...
- 2.Add Two Numbers-两个单链表相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- POJ1613 147/思维题
题目链接[https://www.oj.swust.edu.cn/problem/show/1613] 题意:输出第K小的由1.4.7数字组成的数字. 解题过程:1.4.7.11.14.17.41.4 ...
- winsock编程WSAEventSelect模型
winsock编程WSAEventSelect模型 WSAEventSelect模型和WSAAsyncSelec模型类似,都是用调用WSAXXXXXSelec函数将socket和事件关联并注册到系统, ...
- java编写简单的累加程序
编程思路:1.建立类包demo: 2.在类包中建立CommanParameter类: 3.利用for循环通过强制类型转换将在后台中输入的String类型的字符转换为整型并进进累加操作: package ...
- VO , PO , BO , QO, DAO ,POJO
VO , PO , BO , QO, DAO ,POJO, O/R Mapping 是 Object Relational Mapping (对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定 ...