hdu 2888 二维RMQ模板题
Check Corners
Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2377 Accepted Submission(s): 859
to know the number of them. But soon he find that it is too complex, so he changes his mind, he just want to know whether there is a maximum at the four corners of the sub-matrix, he calls this “Check corners”. It’s a boring job when selecting too many sub-matrices,
so he asks you for help. (For the “Check corners” part: If the sub-matrix has only one row or column just check the two endpoints. If the sub-matrix has only one entry just output “yes”.)
For each test case, the first line contains two integers m, n (1 <= m, n <= 300), which is the size of the row and column of the matrix, respectively. The next m lines with n integers each gives the elements of the matrix which fit in non-negative 32-bit integer.
The next line contains a single integer Q (1 <= Q <= 1,000,000), the number of queries. The next Q lines give one query on each line, with four integers r1, c1, r2, c2 (1 <= r1 <= r2 <= m, 1 <= c1 <= c2 <= n), which are the indices of the upper-left corner
and lower-right corner of the sub-matrix in question.
4 4 10 7
2 13 9 11
5 7 8 20
13 20 8 2
4
1 1 4 4
1 1 3 3
1 3 3 4
1 1 1 1
13 no
20 yes
4 yes
题意:
每次查询求解一个矩阵中的最大值,并判断是否与这个矩阵的四角相等。
/*
二维RMQ的思路与一维的大致相同,都是借助dp先进行预处理,然后快速查询
hhh-2016-01-30 01:59:55
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <vector>
typedef long long ll;
using namespace std; const int maxn = 305;
int dp[maxn][maxn][9][9];
int tmap[maxn][maxn];
int mm[maxn];
void iniRMQ(int n,int m)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[i][j][0][0] = tmap[i][j];
for(int ti = 0; ti <= mm[n]; ti++)
for(int tj = 0; tj <= mm[m]; tj++)
if(ti+tj)
for(int i = 1; i+(1<<ti)-1 <= n; i++)
for(int j = 1; j+(1<<tj)-1 <= m; j++)
{
if(ti)
dp[i][j][ti][tj] =
max(dp[i][j][ti-1][tj],dp[i+(1<<(ti-1))][j][ti-1][tj]);
else
dp[i][j][ti][tj] =
max(dp[i][j][ti][tj-1],dp[i][j+(1<<(tj-1))][ti][tj-1]);
}
} int RMQ(int x1,int y1,int x2,int y2)
{
int k1 = mm[x2-x1+1];
int k2 = mm[y2-y1+1];
x2 = x2 - (1<<k1) +1;
y2 = y2 - (1<<k2) +1;
return
max(max(dp[x1][y1][k1][k2],dp[x1][y2][k1][k2]),
max(dp[x2][y1][k1][k2],dp[x2][y2][k1][k2]));
} int main()
{
int n,m;
mm[0] = -1;
for(int i =1 ; i <= 301; i++)
mm[i] = ((i&(i-1)) == 0)? mm[i-1]+1:mm[i-1];
while(scanf("%d%d",&n,&m)==2)
{
for(int i =1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d",&tmap[i][j]);
iniRMQ(n,m);
int k;
scanf("%d",&k);
while(k--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans = RMQ(x1,y1,x2,y2);
printf("%d ",ans); if(ans == tmap[x1][y1] || ans == tmap[x1][y2]
|| ans == tmap[x2][y1]|| ans == tmap[x2][y2])
printf("yes\n");
else
printf("no\n");
}
}
return 0;
}
hdu 2888 二维RMQ模板题的更多相关文章
- hdu 2888 二维RMQ
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj2019 二维RMQ模板题
和hdu2888基本上一样的,也是求一个矩阵内的极值 #include<iostream> #include<cstring> #include<cstdio> # ...
- hduacm 2888 ----二维rmq
http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题 直接用二维rmq 读入数据时比较坑爹 cin 会超时 #include <cstdio& ...
- poj2019 二维RMQ裸题
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions:8623 Accepted: 4100 Descrip ...
- 二维RMQ模板
int main(){ ; i <= n; i++) ; j <= m; j++) { scanf("%d", &val[i][j]); dp[i][j][][ ...
- Cornfields POJ - 2019(二维RMQ板题)
就是求子矩阵中最大值与最小值的差... 板子都套不对的人.... #include <iostream> #include <cstdio> #include <sstr ...
- HDU 2888:Check Corners(二维RMQ)
http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中 ...
- POJ 2019 Cornfields [二维RMQ]
题目传送门 Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7963 Accepted: 3822 ...
- Zeratul的完美区间(线段树||RMQ模板题)
原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...
随机推荐
- python pdb 调试
命令行 Python -m pdb xxx.py l ----> list 显示当前代码 n ----> next 向下执行一行代码 c ----> continue 继续执行代码 ...
- HTML标签小记文本类标签
文本类标签: <input type="text" name="" value="">文本框 type(方式,方法)name文 ...
- Node入门教程(4)第三章:第一个 Nodejs 程序
第一个 Nodejs 程序 本教程仅适合您已经有一定的JS编程的基础或者是后端语言开发的基础.如果您是零基础,建议您先学一下老马的前端免费视频教程 第一步:创建项目文件夹 首先创建 demos 文件夹 ...
- salesforce零基础学习(八十七)Apex 中Picklist类型通过Control 字段值获取Dependent List 值
注:本篇解决方案内容实现转自:http://mysalesforceescapade.blogspot.com/2015/03/getting-dependent-picklist-values-fr ...
- c#动态加载卸载DLL
前段时间工作的时候遇到一个问题.就是需要每次启动程序的时候动态替换掉某个dll,所以就百度了这方面的资料.这次记录下来让自己以后可以看. 根据自己的理解,动态卸载dll需要有以下条件: 1:dll在加 ...
- AngularJS1.X学习笔记11-服务
如果我没记错的话,spring里边有个service层.什么是服务呢?个人理解就是很多地方要用的,可以跨越控制器甚至是跨越模块的工具.AngularJS也为我们提供了服务这种机制,这让我们可以将一些不 ...
- python入门(6)输入和输出
python入门(6)输入和输出 输出 >>> print 'hello, world' >>> print 'The quick brown fox', 'jum ...
- wmv12下安装centos7
第一步:安装软件: vmw版本是12,并在vmw下安装centos为CentOS-7-x86_64-DVD-1708.iso: 第二步:修改vmw虚拟网络配置 1)配置VMnet8 修改ip等信息 点 ...
- MongoDB的安装和使用指南
什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系 ...
- SQL Server 2014 HADR_DATABASE_WAIT_FOR_TRANSITION_TO_VERSIONING 等待
最近有发现SAP 的MES系统上了AlwaysOn后辅助节点发现无法查询的情况,例如在辅助节点上执行: SELECT TOP 0 * FROM TABLE1; 语句执行正常SELECT TOP 1* ...