hdu 2888 二维RMQ

时间:2022-08-30 14:43:59

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 &lpar;模板题&rpar;【二维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 &lbrack;二维RMQ&rsqb;

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

随机推荐

  1. Android技能树

    第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...

  2. Android之开源项目view篇

    本文转自:http://www.trinea.cn/android/android-open-source-projects-view/ 主要介绍Android上那些不错个性化的View,包括List ...

  3. 房租管理小软件(四):对linq的使用

    1.对LInq的封装如下: private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMap ...

  4. c&num; 远程监控(4) 接收端 RTP包重组 分屏显示

    我们在上一期使用RTP协议,并进行了配置,打包了视频数据,这一期我们就对发送的数据进行重组,并显示在接受端上.最后对其进行扩展,支持多客户端视频发送,并在接收端分屏显示.完成远程监控的模拟. 先来个效 ...

  5. knockout computed实例

    function Privilege(options) { var self = this; self.fieldId = options.fieldId; self.readAccessType = ...

  6. Swift - final关键字的介绍,以及使用场景

    final关键字在大多数的编程语言中都存在,表示不允许对其修饰的内容进行继承或者重新操作.Swift中,final关键字可以在class.func和var前修饰. 通常大家都认为使用final可以更好 ...

  7. ZJOI2017 Round&num;2 滚粗记

    在杭州的火车站的KFC餐厅里,独自一人,闲来无事,便写写这篇博客.刚刚的一个礼拜,经历了余姚的省选和杭州的数学集训,感觉有些浪,学校里现在还在上新课,我已经落下一个礼拜的文化课了,回去估计补死:最重要 ...

  8. Maximum Subarray&lpar;最大子数组&rpar;

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. &lbrace;MySQL完整性约束&rcub;一 介绍 二 not null与default 三 unique 四 primary key 五 auto&lowbar;increment 六 foreign key 七 作业

    MySQL完整性约束 阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业 一 ...

  10. Python3学习之路~5&period;2 time &amp&semi; datetime模块

    time模块 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11, ...