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
 

Source

2009 Multi-University Training Contest 9 - Host by HIT

二维RMQ练习题。

一维RMQ二分维护一个区间,而二维RMQ通过维护四个等分的矩形区间维护了一个区间的最值,基本原理差不多

理解的还不是很透彻,代码基本靠抄。

这题内存限制范围很小,数组稍微开大就MLE

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int f[][][][];
//f[i][j][x][y]记录以(i,j)为左上角,(i+(1<<x),j+(1<<y))为右下角的矩形内的最大值
int mp[][];
int n,m;
void init(){
int i,j;
int k,l;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
f[i][j][][]=mp[i][j];
int kn=(int)(log((double)n)/log(2.0));
int km=(int)(log((double)m)/log(2.0));
for(i=;i<=kn;i++)
for(j=;j<=km;j++){
if(i== && j==)continue;
for(k=;k+(<<i)-<=n;k++)
for(l=;l+(<<j)-<=m;l++){
if(!i)//i==0 && j!=0
f[k][l][i][j]=max(f[k][l][i][j-],f[k][l+(<<(j-))][i][j-]);
else //i!=0
f[k][l][i][j]=max(f[k][l][i-][j],f[k+(<<(i-))][l][i-][j]);
}
}
return;
}
int RMQ(int x1,int y1,int x2,int y2){
int kn=(int)(log(double(x2-x1+))/log(2.0));
int km=(int)(log(double(y2-y1+))/log(2.0));
int a=max(f[x1][y1][kn][km],f[x2-(<<kn)+][y1][kn][km]);
int b=max(f[x1][y2-(<<km)+][kn][km],f[x2-(<<kn)+][y2-(<<km)+][kn][km]);
return max(a,b);
}
int main(){
int i,j;
int k;
while(scanf("%d%d",&n,&m)!=-){ for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&mp[i][j]);
init();
int x1,x2,y1,y2;
scanf("%d",&k);
while(k--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans=RMQ(x1,y1,x2,y2);
printf("%d ",ans);
if(ans==mp[x1][y1] || ans==mp[x2][y2] || ans==mp[x1][y2] || ans==mp[x2][y1])
puts("yes");
else puts("no");
}
}
return ;
}

HDU2888 Check Corners的更多相关文章

  1. HDU-2888 Check Corners 二维RMQ

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题.解题思路如下(转载别人写的): dp[row][col][i][j] 表示[row,ro ...

  2. HDU2888 Check Corners(二维RMQ)

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

  3. 【HDOJ 2888】Check Corners(裸二维RMQ)

    Problem Description Paul draw a big m*n matrix A last month, whose entries Ai,j are all integer numb ...

  4. hdu2188 Check Corners

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

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

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

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

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

  7. 【HDOJ】2888 Check Corners

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

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

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

  9. Check Corners HDU - 2888(二维RMQ)

    就是板题.. 查询子矩阵中最大的元素...然后看看是不是四个角落的  是就是yes  不是就是no  判断一下就好了 #include <iostream> #include <cs ...

随机推荐

  1. Beaufort密码

    博福特密码,是一种类似于维吉尼亚密码的替代密码,由弗朗西斯·蒲福(Francis Beaufort)发明.它最知名的应用是M-209密码机.博福特密码属于对等加密,即加密演算法与解密演算法相同 博福特 ...

  2. mysqli预处理和事务处理

    1 应用环境 mysqli预处理功能(大量数据处理时使用) 2 步骤 a)mysqli连接数据库 $mysqli = new mysqli('localhost','root','root','chu ...

  3. Could not publish server configuration for MyEclipse Tomcat v7.0. Multiple Contexts have a path

    Could not publish server configuration for Tomcat v6.0 Server at localhost. 经常在使用tomcat服务器的时候 总会发生一些 ...

  4. 项目管理和缺陷跟踪工具Redmine

    官网: http://www.redmine.org/ http://demo.redmine.org/ 下载: http://www.redmine.org/projects/redmine/wik ...

  5. usb驱动开发4之总线设备驱动模型

    在上文说usb_init函数,却给我们留下了很多岔路口.这次就来好好聊聊关于总线设备驱动模型.这节只讲理论,不讲其中的函数方法,关于函数方法使用参考其他资料. 总线.设备.驱动对应内核结构体分别为bu ...

  6. 【转】【C#】【Thread】【Task】多线程

    多线程 多线程在4.0中被简化了很多,仅仅只需要用到System.Threading.Tasks.::.Task类,下面就来详细介绍下Task类的使用. 一.简单使用 开启一个线程,执行循环方法,返回 ...

  7. 给vps设置ssh供爬墙使用

    在服务器上建一个 username : 添加用户:useradd -s /bin/false username,将用户的shell设置成/bin/false.这样用户就无法与系统进行交互. 设置密码: ...

  8. C# == equals 本质理解

    using System; using System.Diagnostics; using System.Text; using System.Collections; using System.Co ...

  9. ZooKeeper学习第八期——ZooKeeper伸缩性

    一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都已经知道了在ZooKeeper集群当中有两种角色Leader和Follower.Leader可以接受 ...

  10. UIPasteboard的使用

    剪贴板的使用以及自定义剪贴板. 系统剪贴板的直接调用 其实整个过程非常的简单,我就用我写的一个自定义UILable来说明调用系统剪贴板. 首先,因为苹果只放出来了 UITextView,UITextF ...