Check Corners

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

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[row][col][i][j] 表示[row,row+2^i-1]x[col,col+2^j-1] 二维区间内的最小值
=  max{dp[row][col][i][j-1],dp[row][col][i-1][j],dp[row][col+2^(j-1)][i][j-1],dp[row+2^(i-1)][col][i-1][j]}
 
查询结果为
      max{dp[sx][sy][kx][ky],dp[sx][ey-2^ky+1][kx][ky],dp[ex-2^kx+1][sy][kx][ky],dp[ex-2^kx+1][ey-2^ky+1][kx][ky]}
 
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int a[MAXN][MAXN],n,m,dp[MAXN][MAXN][][];
void Init()
{
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
dp[i][j][][] = a[i][j];
}
}
for(int pi = ; pi < ; pi++){
for(int pj = ; pj < ; pj++){
if(pi == && pj == )continue;
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
if(i + ( << pi) - > n || j + ( << pj) - > m)continue;
if(pi == ){
dp[i][j][pi][pj] = max(dp[i][j][pi][pj-],dp[i][j+(<<(pj-))][pi][pj-]);
}
else {
dp[i][j][pi][pj] = max(dp[i][j][pi-][pj],dp[i+(<<(pi-))][j][pi-][pj]);
}
}
}
}
}
}
void getans(int x1,int y1,int x2,int y2)
{
int kx,ky;
kx = (int)(log((double)(x2 - x1)) / log(2.0));
ky = (int)(log((double)(y2 - y1)) / log(2.0));
int ans = -INF;
ans = max(ans,dp[x1][y1][kx][ky]);
ans = max(ans,dp[x2 - ( << kx) + ][y1][kx][ky]);
ans = max(ans,dp[x1][y2 - ( << ky) + ][kx][ky]);
ans = max(ans,dp[x2 - ( << kx) + ][y2 - ( << ky) + ][kx][ky]);
printf("%d ",ans);
if(a[x1][y1] == ans || a[x1][y2] == ans || a[x2][y1] == ans || a[x2][y2] == ans)printf("yes\n");
else printf("no\n");
}
void solve()
{
int q;
scanf("%d",&q);
int x1,y1,x2,y2;
while(q--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
getans(x1,y1,x2,y2);
}
}
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
scanf("%d",&a[i][j]);
}
}
Init();
solve();
}
return ;
}

hdu2888 二维RMQ的更多相关文章

  1. hdu2888 二维ST表(RMQ)

    二维RMQ其实和一维差不太多,但是dp时要用四维 /* 二维rmq */ #include<iostream> #include<cstring> #include<cs ...

  2. HDU2888 Check Corners(二维RMQ)

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

  3. hduacm 2888 ----二维rmq

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

  4. hdu 2888 二维RMQ模板题

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

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

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

  6. POJ 2019 Cornfields [二维RMQ]

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

  7. 【LightOJ 1081】Square Queries(二维RMQ降维)

    Little Tommy is playing a game. The game is played on a 2D N x N grid. There is an integer in each c ...

  8. 【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 ...

  9. POJ 2019 Cornfields (二维RMQ)

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4911   Accepted: 2392 Descri ...

随机推荐

  1. BeanShell Assertion in Jmeter

    以下为几个beanshell assertion的栗子: if (ResponseCode != null && ResponseCode.equals ("200" ...

  2. JSP第5次测试---测试分析

    1. (选择一项) A: B: C: D:  解析:构造方法方法名与类名必须相同,并且没有返回值,在对象创建时被调用:可以有带参和无参的构造方法同时出现于一个类. 2. (选择一项) A: B: C: ...

  3. poj2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17953   Accepted: 8688   Special ...

  4. python-数据类型补充及文件处理操作

    ___数据类型____ 一.列表的复制 浅复制和深复制 浅复制只复制一层,深复制完全克隆,慎用 1.实现浅复制的三种方式: name=['song','xiao','nan'] import copy ...

  5. @OBJC 和 DYNAMIC

    原文转载自:@OBJC 和 DYNAMIC 虽然说 Swift 语言的初衷是希望能摆脱 Objective-C 的沉重的历史包袱和约束,但是不可否认的是经过了二十多年的洗礼,Cocoa 框架早就烙上了 ...

  6. Hibernate总结4之HQL

    一,HQL特点 与SQL相似,SQL中的语法基本上都可以直接使用. SQL查询的是表和表中的列:HQL查询的是对象与对象中的属性. HQL的关键字不区分大小写,类名与属性名是区分大小写的. SELEC ...

  7. php配置rewrite模块

    转 (1)    启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_r ...

  8. JQuery学习

    首先要明白一点,JQuery是一个JS的封装库,目的是为了关注点分离,让前端更加侧重于界面显示,而不是各个浏览器不同的差异性,下面是JQuery的一些常用的基本用法 一,JQuery语法 window ...

  9. codevs1501 二叉树最大宽度和高度

    难度等级:白银    1501 二叉树最大宽度和高度 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整数n. 下面 ...

  10. Python之线程、进程和协程

    python之线程.进程和协程 目录: 引言 一.线程 1.1 普通的多线程 1.2 自定义线程类 1.3 线程锁 1.3.1 未使用锁 1.3.2 普通锁Lock和RLock 1.3.3 信号量(S ...