【HDOJ 2888】Check Corners(裸二维RMQ)
Problem Description
Paul draw a big m*n matrix A last month, whose entries Ai,j are all integer numbers ( 1 <= i <= m, 1 <= j <= n ). Now he selects some sub-matrices, hoping to find the maximum number. Then he finds that there may be more than one maximum number, he also wants 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”.)
Input
There are multiple test cases.
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.
Output
For each test case, print Q lines with two numbers on each line, the required maximum integer and the result of the “Check corners” using “yes” or “no”. Separate the two parts with a single space.
Sample Input
Sample Output
题意:
给定一个n*m (1<=m,n<=300)的矩阵,每次询问左上角(r1,c1)到右下角(r2,c2)的子矩形中的最大值并输出。如果每次所询问的四个角有最大值,输出yes,否则输出no。
题解:
裸二维RMQ就直接上板子吧!
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int MAX=;
int val[MAX][MAX];
int dp[MAX][MAX][][];//最大值
int mm[MAX];
void initRMQ(int n,int m)//m*n的矩阵
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
dp[i][j][][]=val[i][j];
for(int ii=;ii<=mm[n];ii++)
for(int jj=;jj<=mm[m];jj++)
if(ii+jj)
for(int i=;i+(<<ii)-<=n;i++)
for(int j=;j+(<<jj)-<=m;j++)
if(ii)dp[i][j][ii][jj]=max(dp[i][j][ii-][jj],dp[i+(<<(ii-))][j][ii-][jj]);
else dp[i][j][ii][jj]=max(dp[i][j][ii][jj-],dp[i][j+(<<(jj-))][ii][jj-]);
}
int rmq(int x1,int y1,int x2,int y2)//所查询矩形区间内的最大值 左上角(x1,y1) -> 右下角(x2,y2)
{
int k1=mm[x2-x1+];
int k2=mm[y2-y1+];
x2=x2-(<<k1)+;
y2=y2-(<<k2)+;
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()
{
mm[]=-;
for(int i=;i<=MAX;i++)
mm[i]=((i&(i-))==)?mm[i-]+:mm[i-];
int n,m,Q;
int r1,c1,r2,c2;
while(scanf("%d%d",&n,&m)==)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&val[i][j]);
initRMQ(n,m);
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);//左上角(r1,c1) -> 右下角(r2,c2)
if(r1>r2)swap(r1,r2);
if(c1>c2)swap(c1,c2);
int tmp=rmq(r1,c1,r2,c2);
printf("%d ",tmp);
if(tmp==val[r1][c1]||tmp==val[r1][c2]||tmp==val[r2][c1]||tmp==val[r2][c2])
printf("yes\n");
else printf("no\n");
}
}
return ;
}
【HDOJ 2888】Check Corners(裸二维RMQ)的更多相关文章
- HDU 2888:Check Corners(二维RMQ)
http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中 ...
- HDU2888 Check Corners(二维RMQ)
有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...
- Hdu 2888 Check Corners (二维RMQ (ST))
题目链接: Hdu 2888 Check Corners 题目描述: 给出一个n*m的矩阵,问以(r1,c1)为左上角,(r2,c2)为右下角的子矩阵中最大的元素值是否为子矩阵的顶点? 解题思路: 二 ...
- HDU 2888 Check Corners (模板题)【二维RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
- hdu 2888 二维RMQ模板题
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 2888 二维RMQ
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hduacm 2888 ----二维rmq
http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题 直接用二维rmq 读入数据时比较坑爹 cin 会超时 #include <cstdio& ...
- 【HDOJ】2888 Check Corners
二维RMQ. /* 2888 */ #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- poj2019 二维RMQ裸题
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions:8623 Accepted: 4100 Descrip ...
随机推荐
- JavaScript的进阶之路(二)函数简介,变量、作用域和内存问题
<h3>ECMAScript中函数不存在函数签名的概念,没有重载</h3><h3>无需指定返回值,可以在任何时候返回任何值.未指定返回值的函数,返回的是一个特殊的u ...
- idea配置maven后提示 commond not found
1.昨天换公司笔记本安装idea后,配置完maven后,发现自己配置的 responsity 里面没有jar包,还是在默认的.m2文件夹里面,之后发现原来是 配置的setting.xml里面的目录 这 ...
- 关于Flume以及Kafka理解
- CASE表达式
一.简介 官方定义CASE是一种表达式,它基于某种格式,按照格式去编写表达式,其中表达式的逻辑是:指定特定的值与条件列表去匹配,返回对应的值. CASE表达式类似我们编程语言中的 if else 和 ...
- 什么是SAP GUI的client
我们用SAPGUI登录某个系统时,除了用户名和密码外,还要指定一个必填字段client: 这个client是什么东东? 看文档: SAP Client is the highest hierarchi ...
- tp 查询数据库时报错 A non well formed numeric value encountered
在database.php中配置或修改 'datetime_format' => false,
- [USACO07OPEN]吃饭Dining
嘟嘟嘟 这应该是网络流入门题之一了,跟教辅的组成这道题很像. 把每一只牛看成书,然后对牛拆点,因为每一只牛只要一份,食物和饮料分别看成练习册和答案. #include<cstdio> #i ...
- Centos6 Ngnix和fastcgi搭建
一.下载Nginx 依赖pcre,zlib,openssl 下载解压包,解压后进入 ./configue make make install 默认安装到/usr/local/ngnix 可执行文件在/ ...
- Windows pycharm Terminal使用Anaconda 的Prompt
从Stack Overflow上找到的方法如下 在Settings->Terminal->Shell path 改成:cmd.exe "/K" "C:\Use ...
- cin,get,getline
一.cin 1.cin使用空白(空格.制表符和换行符)来确定字符串结束的位置,并且对于换行符,cin会把换行符留在输入队列.cin读取字符串放到数组中,并自动在结尾添加空字符. 例如: ]; cin& ...