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. Messenger 进程间通信

    Messenger 使用 Messenger 可以在进程间传递数据, 实现一对多的处理. 其内部实现, 也是基于 aidl 文件, 这个aidl位于: frameworks/base/core/jav ...

  2. JS数字计算精度问题解决

    add(a, b) {//相加 var c, d, e; try { c = a.toString().split(".")[1].length; } catch (f) { c ...

  3. UITableView .grouped 类型去除顶部间距

    在设置 UITableView 的 style 为 .grouped 类型的时候,发现第一个 cell 的顶部存在大段的间距,而改为 .plain 类型则没有这个间距,效果如下: 设置了 conten ...

  4. Java 注解全面解析

    1.基本语法 注解定义看起来很像接口的定义.事实上,与其他任何接口一样,注解也将会编译成class文件. @Target(ElementType.Method) @Retention(Retentio ...

  5. https://segmentfault.com/bookmark/1230000008276077

    https://segmentfault.com/bookmark/1230000008276077

  6. 使用wifite破解路由器密码

    使用wifite破解路由器密码 发表于 2016-02-06   |   分类于 wifite  |   暂无评论  |   10次阅读 简介 wifite是一款自动化wep.wpa破解工具,不支持w ...

  7. Vuex-Action

    Action 类似于 mutation,不同在于: Action 提交的是 mutation,而不是直接变更状态. Action 可以包含任意异步操作. 让我们来注册一个简单的 action: con ...

  8. tera term通过ttl脚本 自动连接服务器(转自http://www.cnblogs.com/wxb0328/p/teraterm.html)

    转自http://www.cnblogs.com/wxb0328/p/teraterm.html 在现在的这个公司一直使用tera term来远程连接服务器,感觉很方便,特别是它的ttl脚本配置的自动 ...

  9. cpu几种架构区别

    转自:http://smilejay.com/2012/07/intel-procssor-architecture/ (1)x86 (IA-32,i386,x86-32,x32) x86是指基于In ...

  10. @PathVariable @RequestParam @RequestBody 的区别

    转载自:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...