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

4 4
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

Sample Output

20 no
13 no
20 yes
4 yes

题意:

给定一个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)的更多相关文章

  1. HDU 2888:Check Corners(二维RMQ)

    http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中 ...

  2. HDU2888 Check Corners(二维RMQ)

    有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...

  3. Hdu 2888 Check Corners (二维RMQ (ST))

    题目链接: Hdu 2888 Check Corners 题目描述: 给出一个n*m的矩阵,问以(r1,c1)为左上角,(r2,c2)为右下角的子矩阵中最大的元素值是否为子矩阵的顶点? 解题思路: 二 ...

  4. HDU 2888 Check Corners (模板题)【二维RMQ】

    <题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...

  5. hdu 2888 二维RMQ模板题

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. hdu 2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hduacm 2888 ----二维rmq

    http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题  直接用二维rmq 读入数据时比较坑爹  cin 会超时 #include <cstdio& ...

  8. 【HDOJ】2888 Check Corners

    二维RMQ. /* 2888 */ #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  9. poj2019 二维RMQ裸题

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:8623   Accepted: 4100 Descrip ...

随机推荐

  1. mac下打开hosts文件

    1打开控制台 输入vi(空格)/etc/hosts 进入hosts文件,输入i更改为编辑状态,更改完esc然后shift+:在输入wq保存退出 2打开Finder然后选择上面前往,到前往文件夹,输入/ ...

  2. 02_dubbo实例_多版本号

    [多版本作用] 当一个接口实现,出现不兼容升级时,可以用版本号过渡. 版本号不同的服务之间不能引用. [版本迁移方式] 1.在低压时间段,先升级一半Provider为新版本. 2.再将所有消费者升级为 ...

  3. 21_ConcurrentHashMap和ConcurrentSkipListMap

    [简述] ConcurrentHashMap内部使用段(Segment)来表示这些不用的部分,每个段其实就是一个小的HashTable,他们有自己的锁,只要多个修改操作发生在不同的段上,他们就可以并发 ...

  4. Python爬虫教程-19-数据提取-正则表达式(re)

    本篇主页内容:match的基本使用,search的基本使用,findall,finditer的基本使用,匹配中文,贪婪与非贪婪模式 Python爬虫教程-19-数据提取-正则表达式(re) 正则表达式 ...

  5. C++中long是什么类型

    long long本质上还是整型,只不过是一种超长的整型. int型:32位整型,取值范围为-2^31 ~ (2^31 - 1) .long:在32位系统是32位整型,取值范围为-2^31 ~ (2^ ...

  6. AppDomain配置和卸载

    AppDomain 1.配置AppDomain 使用AppDomainSetup类为新应用程序域提供带有配置信息的公共语言运行时.创建自己的应用程序域时,最重要的ApplicationBase(它是定 ...

  7. iOS文件处理类

    iOS文件处理类 这是一个用来简化iOS中关于文件操作的一个类,所有方法都为类方法. Source File.h // // File.h // FileManager // // http://ho ...

  8. Python学习---Python下[元组]的学习

    元组是不可变的, 用小括号()定义,而且一旦定义 ,不可变[类型是tuple] [元组看做一个整体,不可拆分,不可赋值,但可以全部重新赋值] 通过圆括号,用逗号分隔,常用在使语句或用户定义的函数能够安 ...

  9. UNIX和linux系统性能监控工具oswatcher

    可以在一台机器上运行oswatcher.把运行的结果拷贝到有vnc的机器上进行分析.java -jar oswbba.jar -i /mnt/hgfs/database/oswbb/archive . ...

  10. .NET事务

    概述 事务ACID特性 事务将一系列的工作视为一个工作单元,它具有 ACID 特性: A:Atomicity 不可分性也就是说事务中有多项工作,如果有一项工作失败了,整个事务就算失败了. C:Cons ...