Check Corners

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

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
 
题目大意:给一个N*M的正整数矩阵,Q条询问:给一个子矩阵的左上角跟右下角的坐标,求这个矩阵的元素最大值,若最大值与四个顶点的值有一个相等,输出max yes,否则输出max no。
 
分析:用二维RMQ离线处理,O(1)查询,手贱的把数组多开大了4跟1就超内存了。。。。。。这游戏真难。

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std; const int maxn=;
const int maxm=; int A[maxn][maxn],flag;
int d[maxn][maxn][maxm][maxm]; inline int max(int a,int b){ return a>b?a:b;} void RMQ_init(int n,int m)
{
int i,j,k,l;
for(i=;i<n;i++)
for(j=;j<m;j++) d[i][j][][]=A[i][j];
for(i=;(<<i)<=n;i++)
{
for(j=;(<<j)<=m;j++)
{
if(i== && j==) continue;
for(k=;k+(<<i)-<n;k++)
{
for(l=;l+(<<j)-<m;l++)
{
if(i== && j!=) d[k][l][i][j]=max(d[k][l][i][j-],d[k][l+(<<(j-))][i][j-]);
else if(i!= && j==) d[k][l][i][j]=max(d[k][l][i-][j],d[k+(<<(i-))][l][i-][j]);
else d[k][l][i][j]=max(d[k][l][i-][j-],max(d[k][l+(<<(j-))][i-][j-],
max(d[k+(<<(i-))][l][i-][j-],d[k+(<<(i-))][l+(<<(j-))][i-][j-])));
}
}
}
}
} int query(int lx,int ly,int rx,int ry)
{
int ri=floor(log(rx-lx+1.0)/log(2.0)+0.000001);
int ci=floor(log(ry-ly+1.0)/log(2.0)+0.000001);
int temp=d[lx][ly][ri][ci];
temp=max(temp,d[lx][ry-(<<ci)+][ri][ci]);
temp=max(temp,d[rx-(<<ri)+][ly][ri][ci]);
temp=max(temp,d[rx-(<<ri)+][ry-(<<ci)+][ri][ci]);
if(temp==A[lx][ly] || temp==A[lx][ry] ||
temp==A[rx][ly] || temp==A[rx][ry])
flag=;
return temp;
} int main()
{
int n,m,i,j,q,lx,ly,rx,ry,ans;
while(~scanf("%d %d",&n,&m))
{
for(i=;i<n;i++)
for(j=;j<m;j++) scanf("%d",&A[i][j]);
RMQ_init(n,m);
scanf("%d",&q);
while(q--)
{
scanf("%d %d %d %d",&lx,&ly,&rx,&ry);
lx--;ly--;rx--,ry--;
flag=;
ans=query(lx,ly,rx,ry);
printf("%d ",ans);
printf(flag?"yes\n":"no\n");
}
}
return ;
}


hdu 2888 二维RMQ的更多相关文章

  1. hdu 2888 二维RMQ模板题

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

  2. hduacm 2888 ----二维rmq

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

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

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

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

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

  5. 二维RMQ hdu 2888

    题目:点这里 题意:给出一个n*m的矩阵,然后又Q个询问:每个询问有x1,y1,x2,y2,x1,y1为子矩阵的左上角坐标,x2,y2为右上角的坐标.求此子矩阵中元素最大值,判断最大值是否在子矩阵四个 ...

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

  7. hdu2888 二维RMQ

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

  8. HDU2888 Check Corners(二维RMQ)

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

  9. POJ 2019 Cornfields [二维RMQ]

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

随机推荐

  1. Linux运维笔记--第四部

    第四部 3. Linux扩展正则表达式实战 扩展的正则表达式:ERE(主要用于egrep或grep  -E) +      重复一个或一个以上前面的字符. (*是0或多个) ?     重复0个或一个 ...

  2. 转 Anaconda启动卡死的解决方案

    https://blog.csdn.net/meng_zhi_xiang/article/details/83651676

  3. Noip 训练指南

    目录 Noip 训练指南 图论 数据结构 位运算 期望 题解 Noip 训练指南 目前完成 \(4 / 72\) 图论 [ ] 跳楼机 [ ] 墨墨的等式 [ ] 最优贸易 [ ] 泥泞的道路 [ ] ...

  4. 初涉斯坦纳树&&bzoj4774: 修路

    斯坦纳树的基础应用 斯坦纳树有什么用 个人一点粗浅理解…… 最基本形式的斯坦纳树问题(以下简称母问题):给定图G和一个关键点集V.求在G中选取一个权值最小(这里权值可以有很多变式)的边集E使V中的点两 ...

  5. (68)zabbix windows性能计数器使用详解

    概述 windows下的性能计数器让zabbix监控更加轻松,直接获取性能计数器的数值即可完成windows监控.性能计数器如下:   1 perf_counter["\Processor( ...

  6. MyBatis的增删改查操作

    搭建好mybatis之后 进行对数据库的操作 添加语句 在映射文件中添加语句 insert into student(name,age,score) values(#{name},#{age},#{s ...

  7. Python PycURL的安装使用

    PycURL中文简介:https://blog.csdn.net/qq_41185868/article/details/80487014 PycURL英文简介(如下):http://pycurl.i ...

  8. Geode 集群搭建,快速上手使用

    Geode 介绍: Geode是一个提供实时.一致访问大型分布式云平台下数据密集型应用的数据管理平台. Geode 通过跨多进程,把内存.CPU.网络资源和可选的本地磁盘汇集起来,来管理应用程序对象及 ...

  9. LeetCode(287)Find the Duplicate Number

    题目 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  10. nrf528xx bootloader 模块介绍(转载)

    转载https://www.cnblogs.com/rfnets/p/8205521.html 1. bootloader 的基本功能: 启动应用 几个应用之间切换 初始化外设 nordic nrf5 ...