Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增
2016-10-03 14:50
399 查看
D. Animals and Puzzle
题目连接:
http://codeforces.com/contest/713/problem/DDescription
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 rectangleSample Input
3 41 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
11
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 D Animals and Puzzle
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- Codeforces Round #395 (Div. 1) E. Timofey and our friends animals(lct动态维护mst)
- Codeforces Round #394 (Div. 2) F. Dasha and Photos(二位前缀和+线段树/二维前缀和,好题)
- Codeforces Round #394 (Div. 2)E. Dasha and Puzzle【思维+Dfs构造】
- Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增 ★ ★ ★
- Codeforces Round #371 (Div. 2) C. Sonya and Queries (map)
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(想法题,构造题)
- Codeforces Round #371 (Div. 2) B. Filya and Homework(水题 分类讨论)
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)
- Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
- Codeforces Round #458 D. Bash and a Tough Math Puzzle(线段树)
- Codeforces Round #354 (Div. 2)676D. Theseus and labyrinth
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)A+B
- Codeforces Round #410 Div2 D Mike and distribution
- Codeforces Round #389 (Div. 2)A. Santa Claus and a Place in a Class(水题)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2) B. Gleb And Pizza
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson [trie]
- Codeforces Round #430 (Div. 2):D. Vitya and Strange Lesson(模拟建树)