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 中 compareTo方法问题

    compareTo方法原理:先读取出字符串的第一个“字母”进行比较,比较的方法是ascii码表的值(字符所对应的十进制值),如果前面的大那么返回1,后面的大返回-1:此位置相同,继续比较下一位,直到最 ...

  2. pop 一个viewController时候会有键盘闪现出来又消失

    原因是alertview关闭影响了系统其他的动画导致的.要么延迟调用,要么自己做一个alertview. iOS 8.3,dismiss alert view时系统会尝试恢复之前的keyboard i ...

  3. 深入分析Java Web中的编码问题

    编码问题一直困扰着我,每次遇到乱码或者编码问题,网上一查,问题解决了,但是实际的原理并没有搞懂,每次遇到,都是什么头疼. 决定彻彻底底的一次性解决编码问题. 1.为什么要编码 计算机的基本单元是字节, ...

  4. EXT3文件系统误删除导致文件系统中的邮件丢失恢复方法

    一.故障描述 由8块盘组成的RAID5, 上层是EXT3文件系统,由于误删除导致文件系统中的邮件丢失 二.镜像磁盘为防止数据恢复过程中由于误操作对原始磁盘造成二次破坏, 使用winhex软件为每块磁盘 ...

  5. recompose mapProps

    mapProps介绍 mapProps函数接收一个函数参数,这个函数参数会返回一个对象用作为接下来的组件的props.组件接收到的props只能是通过mapProps函数参数返回的对象,其他的prop ...

  6. postcss的安装与使用

    我是经过公司另外一个同事推荐的这个 他是一个资深的大哥哥  我觉得我确实需要跟多的学习和成长 而且我觉得我应该听他的话 多学学新知识 最近一直在做适配的网站 会出现很多媒体查询 我发现用这个写媒体查询 ...

  7. windows 7 netsh wlan命令连接wifi

    显示本机保存的profiles,配置文件是以wifi的ssid命名的. netsh wlan show profiles 用netsh wlan connect name=00_1111 连接其中一个 ...

  8. 剑指offer-第一个只出现一次的字符

    题目描述 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置   解题思路 由于char类型一共有256种可能,所以开辟一个数组ha ...

  9. 新概念英语(1-41)Penny's bag

    新概念英语(1-41)Penny's bag Who is the tin of tobacco for? A:Is that bag heavy, Penny? B:Not very. A:Here ...

  10. 新概念英语(1-69)The car race

    新概念英语(1-69)The car race Which car was the winner in 1995 ? There is  car race near our town every ye ...