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 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
Sample 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
Sample Output
1
1
1
2
2
Hint
题意
给你一个01矩阵,然后Q次询问,每次询问一个矩形区域中,最大的全一正方形的边长是多少。
题解:
首先考虑Dp,dp[i][j]表示以(i,j)位置为右下角,最大的正方形边长是多少,显然dp[i][j]=min(dp[i-1][j],dp[j][i-1],dp[i-1][j-1])+1
然后我们做出这个dp之后,我们怎么做呢?
直接二分答案,假设我们二分的答案为mid,显然在这个矩形区域的左上角的点是废点,然后查询剩下的点的最大值,是否大于等于m就行了。
这个可以用二维线段树,也可以用二维倍增去做就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int maxm = 10;
int n,m,f[maxm][maxm][maxn][maxn],lg[maxn];
void Build_2D_Sparse_Table(int n, int m){
int i, j, k1, k2;
for(i = 2; i < maxn; i++)
lg[i] = 1 + lg[i/2];
for(i = 1; i <= n; i++)
for(k2 = 1; (1 << k2) <= m; k2++)
for(j = 1; j <= m - (1 << k2) + 1; j++)
f[0][k2][i][j] = max(f[0][k2 - 1][i][j], f[0][k2 - 1][i][j + (1 << (k2 - 1))]);
for(k1 = 1; (1 << k1) <= n; k1++)
for(i = 1; i <= n - (1 << k1) + 1; i++)
for(k2 = 0; (1 << k2) <= m; k2++)
for(j = 1; j <= m - (1 << k2) + 1; j++)
f[k1][k2][i][j] = max(f[k1 - 1][k2][i][j], f[k1 - 1][k2][i + (1 << (k1 - 1))][j]);
}
int Query(int x1, int y1, int x2, int y2){
int k1 = lg[x2 - x1 + 1], k2 = lg[y2 - y1 + 1];
x2 = x2 - (1 << k1) + 1;
y2 = y2 - (1 << k2) + 1;
return max(max(f[k1][k2][x1][y1],f[k1][k2][x1][y2]),max(f[k1][k2][x2][y1],f[k1][k2][x2][y2]));
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int x;scanf("%d",&x);
if(x){
f[0][0][i][j]=min(f[0][0][i-1][j],min(f[0][0][i][j-1],f[0][0][i-1][j-1]))+1;
}
}
}
Build_2D_Sparse_Table(n,m);
int q;scanf("%d",&q);
while(q--)
{
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int l=0,r=min(x2-x1,y2-y1)+1,ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(Query(x1+mid-1,y1+mid-1,x2,y2)>=mid)l=mid+1,ans=mid;
else r=mid-1;
}
cout<<ans<<endl;
}
}
Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增的更多相关文章
- 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 Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- Codeforces Round #371 (Div. 2)B. Filya and Homework
题目链接:http://codeforces.com/problemset/problem/714/B 题目大意: 第一行输入一个n,第二行输入n个数,求是否能找出一个数x,使得n个数中的部分数加上x ...
- Codeforces Round #371 (Div. 2) - B
题目链接:http://codeforces.com/contest/714/problem/B 题意:给定一个长度为N的初始序列,然后问是否能找到一个值x,然后使得序列的每个元素+x/-x/不变,最 ...
- Codeforces Round #371 (Div. 2) - A
题目链接:http://codeforces.com/contest/714/problem/A 题意:有两个人A,B 给定A的时间区间[L1,R1], B的时间区间[L2,R2],然后在正好K分钟的 ...
- 严格递增类的dp Codeforces Round #371 (Div. 1) C dp
http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...
随机推荐
- Codeforces 923 D. Picking Strings
http://codeforces.com/contest/923/problem/D 题意: A-->BC , B-->AC , C-->AB , AAA-->empty 问 ...
- [iOS]Xcode+GitHub远程代码托管(GIT, SVN)
先来看看什么是代码远程托管: 其实就是将我们的代码上传到GitHub的服务器上, 供别人下载, 当然了也可以在团队开发的时候, 使用GitHub进行代码合并工作, 下面我们进入正题 (已经有远程仓库的 ...
- java Runnable、Callable、FutureTask 和线程池
一:Runnable.Callable.FutureTask简介 (1)Runnable:其中的run()方法没有返回值. ①.Runnable对象可以直接扔给Thread创建线程实例,并且创建的线程 ...
- 20155210潘滢昊 2016-2017-2 《Java程序设计》第6周学习总结
20155210 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 流(Stream)是对「输入输出」的抽象,注意「输入输出」是相对程序而言的 InputStr ...
- Javascript - Vue - 请求
本地增删查的一个例子 <div id="box"> <div class="panel panel-primary"> ...
- win10下安装MinGW-w64 - for 32 and 64 bit Windows
对于不经常使用c语言的同学来说,只需要安装MinGW-w64 - for 32 and 64 bit Windows,就可以使用GCC在命令行对c源码进行编译. 首先打开命令行检查自己是否已经安装了g ...
- 用《舌尖2》去理解C#中的多态和开闭原则
昨天晚上看了<舌尖上的中国2>第一集,特别的感人,尤其是看到帮别人割麦子的麦客,一亩地开价200,雇主只肯给100,脸上的那种纠结和无可奈何.还有长着大眼睛的跳跳鱼,很可爱,不过最终还是被 ...
- Web框架的原理
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. socket服务端 import ...
- LeetCode(18):四数之和
Medium! 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- laravel5.3之后可以使用withCount()这个方法
比如:文章控制器ArticleController.php查询文章列表数据的时候用withCount连接Comment,Zan模型直接统计每篇文章的评论和点赞数量. 使用之前需要在文章模型文件Arti ...