Codeforces713D. Animals and Puzzle
$n<=1000,m<=1000$,$n*m$的01矩阵,给$t<=1000000$个询问,每次问一个矩形中最大的1正方形的边长。
先想想不考虑“一个矩形中”的限制,那记$f(i,j)$--以$(i,j)$为右下角的最大的正方形,那
很好,那现在加入一个边界限制,由于边长r的正方形同时也是边长r-1,r-2……的,那来二分答案吧,现在对二分的答案$x$就检查一个区域里的$f$数组中最大的那一个是否大于等于$x$。查静态区间最大,用ST表啦!拓展到二维情况即可。
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
//#include<math.h>
//#include<assert.h>
#include<algorithm>
//#include<iostream>
//#include<bitset>
using namespace std; int n,m,t;
#define maxn 1011
int a[maxn][maxn],Log[maxn];short rmq[maxn][maxn][][]; int x1,x2,y1,y2;
int rmqquery(int x1,int y1,int x2,int y2)
{
int p=Log[x2-x1+],q=Log[y2-y1+];
return max(max(rmq[x1][y1][p][q],rmq[x2-(<<p)+][y1][p][q])
,max(rmq[x1][y2-(<<q)+][p][q],rmq[x2-(<<p)+][y2-(<<q)+][p][q]));
}
bool check(int len) {return rmqquery(x1+len-,y1+len-,x2,y2)>=len;}
int main()
{
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=;i<=n;i++)
for (int j=;j<=m;j++)
if (a[i][j]==) rmq[i][j][][]=;
else rmq[i][j][][]=min(rmq[i][j-][][],min(rmq[i-][j][][],rmq[i-][j-][][]))+;
Log[]=-; for (int i=,to=max(n,m);i<=to;i++) Log[i]=Log[i>>]+;
for (int q=;(<<q)<=m;q++)
for (int i=;i<=n;i++)
for (int j=,to=m-(<<q)+;j<=to;j++)
rmq[i][j][][q]=max(rmq[i][j][][q-],rmq[i][j+(<<(q-))][][q-]);
for (int p=;(<<p)<=n;p++)
{
for (int i=,to=n-(<<p)+;i<=to;i++)
for (int j=;j<=m;j++)
rmq[i][j][p][]=max(rmq[i][j][p-][],rmq[i+(<<(p-))][j][p-][]);
for (int q=;(<<q)<=m;q++)
for (int i=,to=n-(<<p)+;i<=to;i++)
for (int j=,to=m-(<<q)+;j<=to;j++)
rmq[i][j][p][q]=max(rmq[i][j][p][q-],rmq[i][j+(<<(q-))][p][q-]);
}
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int L=,R=min(x2-x1+,y2-y1+);
while (L<R)
{
const int mid=(L+R+)>>;
if (check(mid)) L=mid;
else R=mid-;
}
printf("%d\n",L);
}
return ;
}
Codeforces713D. Animals and Puzzle的更多相关文章
- Animals and Puzzle
Animals and Puzzle time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- 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】713 D. Animals and Puzzle 动态规划+二维ST表
[题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...
- 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 713D Animals and Puzzle(二维ST表+二分答案)
题目链接 Animals and Puzzle 题意 给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...
- Codeforces 713D Animals and Puzzle
题意:一个n*m的01矩阵,Q个询问,每次询问一个矩形区域内,最大的全1正方形的边长是多少? 题解:dp[0][0][i][j]表示以(i, j)为右下角的正方形的最长边长.RMQ后,二分答案即可. ...
- codeforces 713D D. Animals and Puzzle 二分+二维rmq
题目链接 给一个01矩阵, 然后每个询问给出两个坐标(x1, y1), (x2, y2). 问你这个范围内的最大全1正方形的边长是多少. 我们dp算出以i, j为右下角的正方形边长最大值. 然后用二维 ...
- BUPT2017 wintertraining(16) #9
龟速补题.目前基本弃坑.已暂时放弃 D.I 两题. 下面不再写题意了直接说解法注意事项之类,直接放contest链接. https://vjudge.net/contest/151537 A.The ...
- Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net
Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...
随机推荐
- spring tool suite开发环境搭建
先把是构建工具maven: maven里面有一个conf文件夹,然后里面有个setting.xml配置文件,先要把项目要的setting.xml覆盖这个原来的配置文件. 这个maven配置文件有一个作 ...
- React全家桶之一 react-router之高级
使用query获取URL中的参数 //引入相关的依赖 const Page = props => <div> <h1>{props.location.query.mess ...
- linux下redis安装访问
下载编译安装 wget http://download.redis.io/releases/redis-3.0.1.tar.gz tar xvf redis-3.0.1.tar.gz mv redis ...
- Mybatis 分页插件 PageHelper
话不多说,直接导入.部署流程. 1. 引入插件依赖包: maven工程中,pom.xml文件下,添加插件配置项: 2. 配置插件拦截器: webapp -> WEB-INF 下添加 .xml配置 ...
- Farseer.net轻量级开源框架说明及链接索引
Farseer.net是什么? 基于.net framework 4 开发的一系列解决方案. 完全开源在GitHub中托管.并发布到NuGet中. Farseer.Net由最初的关系数据库ORM框架后 ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库上下文 下一篇:Farseer.net轻量级ORM开源框 ...
- asterisk-java ami5 分机状态,挂机原因之类的
这些东西网上随便一找一大堆,也只是记录下自己找的.方便以后自己复制粘贴用. 最后为啦实现分机状态在web的实时更新,我选择啦使用websocket. //获得分机状态 public static St ...
- schtasks /create 计划任务 中文路径 名字都要加“” 子命令 /tn /tr 前面要空格 否则会出错
echo off echo. 清空连接 net use * /del /y echo. 连接 net use \\192.168.1.2\人力资源部\考勤\考勤数据-小莫提供 "密码&quo ...
- (二)Redis for 阿里云公网连接
目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis 阿里云目前仅支持内网连接Redi ...
- laravel学习:模块化caffeinated
# Modules Extract and modularize your code for maintainability. Essentially creates "mini-larav ...