Check Corners

Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2377    Accepted Submission(s): 859

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

题意:

每次查询求解一个矩阵中的最大值,并判断是否与这个矩阵的四角相等。

/*
二维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模板题的更多相关文章

  1. hdu 2888 二维RMQ

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

  2. poj2019 二维RMQ模板题

    和hdu2888基本上一样的,也是求一个矩阵内的极值 #include<iostream> #include<cstring> #include<cstdio> # ...

  3. hduacm 2888 ----二维rmq

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

  4. poj2019 二维RMQ裸题

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

  5. 二维RMQ模板

    int main(){ ; i <= n; i++) ; j <= m; j++) { scanf("%d", &val[i][j]); dp[i][j][][ ...

  6. Cornfields POJ - 2019(二维RMQ板题)

    就是求子矩阵中最大值与最小值的差... 板子都套不对的人.... #include <iostream> #include <cstdio> #include <sstr ...

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

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

  8. POJ 2019 Cornfields [二维RMQ]

    题目传送门 Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7963   Accepted: 3822 ...

  9. Zeratul的完美区间(线段树||RMQ模板题)

    原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...

随机推荐

  1. Java中RuntimeException和Exception的区别

    [TOC] 1. 引入RuntimeException public class RuntimeException { public static void main(String[] args) { ...

  2. Linux下高效指令

    Linux管理磁盘 资本指令 查看当前磁盘使用情况:df -h fdisk -l (查看所有的硬盘) 服务器添加硬盘:在系统设置添加 分区: fdisk /dev/sdb (sdb, sdc, sde ...

  3. JAVA_SE基础——编码规范&代码编写规则

    这次我来给大家说明下编码规范&代码编写规则  ↓ 编码规范可以帮助程序员在编程时注意一些细节问题,提高程序的可读性,让程序员能够尽快地理解新的代码,并帮助大家编写出规范的利于维护的Java代码 ...

  4. api-gateway实践(13)新服务网关 - 断路保护/熔断机制

    参考链接:SpringCloud的Hystrix(五) Hystrix机制 新需求列表 1.在线测试 根据定义,生成输入界面, 点击测试, 验证参数,发起调用,返回执行结果 2.熔断保护 两个实现类: ...

  5. api-gateway实践(10)新服务网关 - OpenID Connect

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  6. Asp.NET Core2.0 项目实战入门视频课程_完整版

    END OR START? 看到这个标题,你开不开心,激不激动呢? 没错,.net core的入门课程已经完毕了.52ABP.School项目从11月19日,第一章视频的试录制,到今天完整版出炉,离不 ...

  7. 输入法searchLookUpEditd的使用

    输入法是DevExpress系列控件比较常用的一个控件.searchLookUpEditd控件集成了快捷输入法,可以使用拼音,五笔等方式快捷输入. 先展示一下输入法的效果

  8. Python入门之函数的嵌套/名称空间/作用域/函数对象/闭包函数

    本篇目录: 一.函数嵌套 二.函数名称空间与作用域 三.函数对象 四.闭包函数 ============================================================ ...

  9. uvalive 3213 Ancient Cipher

    https://vjudge.net/problem/UVALive-3213 题意: 输入两个字符串,问是否可以由第一个字符串的每个字符一一映射得到第二个字符串,字符是可以随意移动的. 思路: 统计 ...

  10. Hibernate(三): org.hibernate.HibernateException: No CurrentSessionContext configured!

    Hibernate版本5.2.9 获取Session的方式是sessionFactory.getCurrentSession(); 比较老一些的版本使用的是sessionFactory.openSes ...