Codeforces Round #371 (Div. 1) D - Animals and Puzzle 二维ST表 + 二分
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int a[N][N], b[N][N], Log[N], n, m; struct ST2 {
int dp[N][N][][], ty;
void build(int n, int m, int b[N][N], int _ty) {
ty = _ty;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
dp[i][j][][] = ty * b[i][j];
for(int u = ; u <= Log[n]; u++) {
for(int v = ; v <= Log[m]; v++) {
if(!u && !v) continue;
for(int i = ; i+(<<u)- <= n; i++) {
for(int j = ; j+(<<v)- <= m; j++) {
if(u) dp[i][j][u][v] = max(dp[i][j][u-][v], dp[i+(<<(u-))][j][u-][v]);
else dp[i][j][u][v] = max(dp[i][j][u][v-], dp[i][j+(<<(v-))][u][v-]);
}
}
}
}
}
int query(int x1, int y1, int x2, int y2) {
int k1 = Log[x2-x1+], k2 = Log[y2-y1+];
x2 = x2-(<<k1)+;
y2 = y2-(<<k2)+;
return max(max(dp[x1][y1][k1][k2], dp[x2][y1][k1][k2]),
max(dp[x1][y2][k1][k2], dp[x2][y2][k1][k2]));
}
} rmq; int main() {
for(int i = -(Log[]=-); i < N; i++)
Log[i] = Log[i - ] + ((i & (i - )) == ); scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
scanf("%d", &a[i][j]); for(int i = n; i >= ; i--) {
for(int j = m; j >= ; j--) {
if(!a[i][j]) continue;
b[i][j] = min(b[i+][j+], min(b[i][j+], b[i+][j])) + ;
}
} rmq.build(n, m, b, );
int q; scanf("%d", &q);
while(q--) {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
int l = , r = min(x2-x1, y2-y1) + , ans = ;
while(l <= r) {
int mid = l + r >> ;
int x3 = x2 - mid + , y3 = y2 - mid + ;
if(rmq.query(x1, y1, x3, y3) >= mid) l = mid + , ans = mid;
else r = mid - ;
}
printf("%d\n", ans);
}
return ;
} /*
*/
Codeforces Round #371 (Div. 1) D - Animals and Puzzle 二维ST表 + 二分的更多相关文章
- 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 713D Animals and Puzzle(二维ST表+二分答案)
题目链接 Animals and Puzzle 题意 给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...
- 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 ...
- 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表
[题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...
- 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/不变,最 ...
随机推荐
- HDU 6194 后缀数组
string string string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 「Linux」centos7安装python
•安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqli ...
- svn常见错误
1.svn提交报错:svn: Aborting commit:XXXXXremains in conflict 解决:说明Svn服务器上的对应内容,在你上次Update后已被别人修改了,而你也做了修改 ...
- UVA 1650 Number String
https://vjudge.net/problem/UVA-1650 题意:D表示比前一个数打,I表示比前一个数小,?表示不确定 给出一个长为n由D I?组成的字符串,问满足字符串大小要求的n+1的 ...
- NOIP 2015 提高组 Day2
期望得分:100+10+60=170 实际得分:100+10+35=145 http://www.cogs.pro/cogs/page/page.php?aid=16 T1 跳石头 时间限制:1 s ...
- 字符串:SAM
HDU4622:区间查询不同子串个数 用后缀自动机预处理出所有区间的不同子串个数 建立n次后缀自动机 #include <stdio.h> #include <string.h> ...
- Linux高级编程--04.GDB调试程序(入门概述)
GDB概述 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在UNIX平台下做软件,你会发现GDB这个调试 ...
- Discrete Logging(POJ2417 + BSGS)
题目链接:http://poj.org/problem?id=2417 题目: 题意: 求一个最小的x满足a^x==b(mod p),p为质数. 思路: BSGS板子题,推荐一篇好的BSGS和扩展BS ...
- Go语言 8 反射
文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 8.1概念和作用 Reflection(反射)在计算 ...
- struts获得参数(属性,对象,模型驱动)
0. strutsMVC