Check Corners

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

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
 
Source
 
Recommend
gaojie
 
题目大意:求矩阵子矩阵的最大值
题解:二维RMQ
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#define maxn 309
using namespace std; int n,m,dp[maxn][maxn][][],map[maxn][maxn];
int x,y,xx,yy,q; void RMQ_pre(){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
dp[i][j][][]=map[i][j];
int mx=log(double(n))/log(2.0);
int my=log(double(m))/log(2.0);
for(int i=;i<=mx;i++){
for(int j=;j<=my;j++){
if(i==&&j==)continue;
for(int row=;row+(<<i)-<=n;row++){
for(int col=;col+(<<j)-<=m;col++){
if(i==)
dp[row][col][i][j]=max(dp[row][col][i][j-],dp[row][col+(<<(j-))][i][j-]);
else
dp[row][col][i][j]=max(dp[row][col][i-][j],dp[row+(<<(i-))][col][i-][j]);
}
}
}
}
} int RMQ_2D(int x,int y,int xx,int yy){
int kx=log(double(xx-x+))/log(2.0);
int ky=log(double(yy-y+))/log(2.0);
int m1=dp[x][y][kx][ky];
int m2=dp[xx-(<<kx)+][y][kx][ky];
int m3=dp[x][yy-(<<ky)+][kx][ky];
int m4=dp[xx-(<<kx)+][y-(<<ky)+][kx][ky];
return max(max(m1,m2),max(m3,m4));
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&map[i][j]);
RMQ_pre();
scanf("%d",&q);
while(q--){
scanf("%d%d%d%d",&x,&y,&xx,&yy);
int ans=RMQ_2D(x,y,xx,yy);
printf("%d ",ans);
if(ans==map[x][y]||ans==map[xx][y]||ans==map[xx][yy]||ans==map[xx][yy])
printf("yes\n");
else printf("no\n");
}
}
return ;
}
 

hdu2188 Check Corners的更多相关文章

  1. HDU2888 Check Corners

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

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

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

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

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

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

  5. HDU-2888 Check Corners 二维RMQ

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

  6. 【HDOJ】2888 Check Corners

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

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

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

  8. HDU2888 Check Corners(二维RMQ)

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

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

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

随机推荐

  1. selenium模块控制浏览器

    利用selenium模块控制浏览器 导入selenium模块:from selenium import webdriver browserFirefox = webdriver.Firefox()#打 ...

  2. Python3 optparse模块

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  3. OJ 1101 谁是中间的那个

    前言:主要考察排序用法 sort(cow+1,cow+1+n,cmp);//数组按cmp方法排序 Description 一天,农夫乔伊像往常一样来到了他的牧场,他突然对他的奶牛产奶量产生了兴趣.他想 ...

  4. ubuntu没有声音解决办法

    cd /usr/lib/dbus-1.0/ chmod +x dbus-daemon-launch-helper sudo gpasswd -a $USER audio sudo killall pu ...

  5. gstreamer——文档/资源/使用

    http://gstreamer.freedesktop.org/src/ http://gstreamer.freedesktop.org/data/doc/gstreamer/head/qt-gs ...

  6. redis 第一篇

    一.redis简介 redis是一个开源的高性能键值对数据库:本质上是数据结构服务器或者是数据结构数据库 特点: 1.共享内存 如果有自己的线程可以读list,如果外界还有一个程序都上面的list,那 ...

  7. JSP DAO(Model)

    示例代码: 1. Users类 package com.po; public class Users { private String username; private String passwor ...

  8. mongodb index 的background 及集群的索引建立

    在数据库建立索引时,默认时"foreground" 也就是前台建立索引,但是,当你的数据库数据量很大时,在建立索引的时会读取数据文件,大量的文件读写会阻止其他的操作,此时在建立索引 ...

  9. win10系统下载地址

    Win10正式版微软官方原版ISO系统镜像下载: Win10正式版32位简体中文版(含家庭版.专业版) 文件名: cn_windows_10_multiple_editions_x86_dvd_684 ...

  10. 4.JDBC编程

    01.JDBC_Java程序和MySQL的关系:     1).Java程序跟其它MySQL客户端一样,就是一个"客户端",用于"封装SQL语句"并发送给MyS ...