D - Animals and Puzzle

#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表 + 二分的更多相关文章

  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 713D Animals and Puzzle(二维ST表+二分答案)

    题目链接 Animals and Puzzle 题意  给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...

  3. 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 ...

  4. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  5. 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 ...

  6. 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表

    [题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...

  7. Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)

    Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...

  8. Codeforces Round #371 (Div. 2)B. Filya and Homework

    题目链接:http://codeforces.com/problemset/problem/714/B 题目大意: 第一行输入一个n,第二行输入n个数,求是否能找出一个数x,使得n个数中的部分数加上x ...

  9. Codeforces Round #371 (Div. 2) - B

    题目链接:http://codeforces.com/contest/714/problem/B 题意:给定一个长度为N的初始序列,然后问是否能找到一个值x,然后使得序列的每个元素+x/-x/不变,最 ...

随机推荐

  1. 关于GCD的几个结论

    设a和b的最大公约数是d,那么: 1. d是用sa+tb(s和t都是整数)能够表示的最小正整数 证明:设x=sa+tb是sa+tb能够表示出的最小正整数.首先,有d|x,证明如下: 因此有x>= ...

  2. Lua弱表Weak table

    定义:弱表的使用就是使用弱引用,很多程度上是对内存的控制. 1.weak表示一个表,它拥有metatable,并且metatable定义了__mode字段. 2.弱引用不会导致对象的引用计数变化.换言 ...

  3. discuz 删除垃圾帖子

    有时候如果你的论坛被垃圾帖子占满后,会发现使用后台的删除功能还是有些慢, 我们需要先备份自己需要的帖子,然后进行下面的操作: 具体删除帖子的步骤,就是清空数据库里面的两张数据库表:pre_forum_ ...

  4. shell 脚本判断linux 的发行版本

    原文vi ./Get_Dist_Name.sh #!/bin/bash Get_Dist_Name() { if grep -Eqii "CentOS" /etc/issue || ...

  5. clinical significance临床显著性

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  6. CSS文字溢出部分自动用"..."代替

    CSS文字溢出部分自动用"..."代替 如html部分: <h4><马尔代夫双鱼岛Olhuveli4 晚6 日自助游></h4> <p&g ...

  7. Installing kubectl

    Installing kubectl Kubernetes uses a command-line utility called kubectl for communicating with the ...

  8. 前端PHP入门-003-echo离不开它,注释看人品

    不要自以为是,瞪着双眼看是没有用的!因为你不是天才! 永远不要自欺欺人,写不出来代码就是不会. 不要问为什么?我头疼! 我接触的人当中,以为自己已经很很很牛X了,总是问为什么这么写? [心中吐槽]:我 ...

  9. @PathParam 和 @QueryParam

    今天调试一个上传功能,客户端手持机发送数据,在URL中附加一个参数,后台用@PathParam接收,但是报错,无法获取这个参数. url:http://192.168.1.3/web1_service ...

  10. HDU 3977 斐波那契循环节

    这类型的题目其实没什么意思..知道怎么做后,就有固定套路了..而且感觉这东西要出的很难的话,有这种方法解常数会比较大吧..所以一般最多套一些比较简单的直接可以暴力求循环节的题目了.. /** @Dat ...