HDU2888 Check Corners
Description
Input
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
Sample Input
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
13 no
20 yes
4 yes
Source
二维RMQ练习题。
一维RMQ二分维护一个区间,而二维RMQ通过维护四个等分的矩形区间维护了一个区间的最值,基本原理差不多
理解的还不是很透彻,代码基本靠抄。
这题内存限制范围很小,数组稍微开大就MLE
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int f[][][][];
//f[i][j][x][y]记录以(i,j)为左上角,(i+(1<<x),j+(1<<y))为右下角的矩形内的最大值
int mp[][];
int n,m;
void init(){
int i,j;
int k,l;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
f[i][j][][]=mp[i][j];
int kn=(int)(log((double)n)/log(2.0));
int km=(int)(log((double)m)/log(2.0));
for(i=;i<=kn;i++)
for(j=;j<=km;j++){
if(i== && j==)continue;
for(k=;k+(<<i)-<=n;k++)
for(l=;l+(<<j)-<=m;l++){
if(!i)//i==0 && j!=0
f[k][l][i][j]=max(f[k][l][i][j-],f[k][l+(<<(j-))][i][j-]);
else //i!=0
f[k][l][i][j]=max(f[k][l][i-][j],f[k+(<<(i-))][l][i-][j]);
}
}
return;
}
int RMQ(int x1,int y1,int x2,int y2){
int kn=(int)(log(double(x2-x1+))/log(2.0));
int km=(int)(log(double(y2-y1+))/log(2.0));
int a=max(f[x1][y1][kn][km],f[x2-(<<kn)+][y1][kn][km]);
int b=max(f[x1][y2-(<<km)+][kn][km],f[x2-(<<kn)+][y2-(<<km)+][kn][km]);
return max(a,b);
}
int main(){
int i,j;
int k;
while(scanf("%d%d",&n,&m)!=-){ for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&mp[i][j]);
init();
int x1,x2,y1,y2;
scanf("%d",&k);
while(k--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans=RMQ(x1,y1,x2,y2);
printf("%d ",ans);
if(ans==mp[x1][y1] || ans==mp[x2][y2] || ans==mp[x1][y2] || ans==mp[x2][y1])
puts("yes");
else puts("no");
}
}
return ;
}
HDU2888 Check Corners的更多相关文章
- HDU-2888 Check Corners 二维RMQ
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题.解题思路如下(转载别人写的): dp[row][col][i][j] 表示[row,ro ...
- HDU2888 Check Corners(二维RMQ)
有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...
- 【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 ...
- hdu2188 Check Corners
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Hdu 2888 Check Corners (二维RMQ (ST))
题目链接: Hdu 2888 Check Corners 题目描述: 给出一个n*m的矩阵,问以(r1,c1)为左上角,(r2,c2)为右下角的子矩阵中最大的元素值是否为子矩阵的顶点? 解题思路: 二 ...
- HDU 2888:Check Corners(二维RMQ)
http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中 ...
- 【HDOJ】2888 Check Corners
二维RMQ. /* 2888 */ #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- HDU 2888 Check Corners (模板题)【二维RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
- Check Corners HDU - 2888(二维RMQ)
就是板题.. 查询子矩阵中最大的元素...然后看看是不是四个角落的 是就是yes 不是就是no 判断一下就好了 #include <iostream> #include <cs ...
随机推荐
- Unity抗锯齿
Unity抗锯齿设置是针对模型,对模型的阴影的锯齿设置无效,不知道我这样的理解是否正确. 遇到的问题 而我是要对灯光照射在模型上产生的阴影进行抗锯齿,暂时还未研究出解决方案,希望知道的朋友告知一声. ...
- Eclipse c++头文件问题(未完)
http://stackoverflow.com/questions/7905025/string-could-not-resolved-error-in-eclipse-for-c-eclipse- ...
- hydra爆破用法
-R 根据上一次进度继续破解 -S 使用SSL协议连接 -s 指定端口 -l 指定用户名 -L 指定用户名字典(文件) -p 指定密码破解 -P 指定密码字典(文件) -e 空密码探测和指定用户密码探 ...
- Android的Style的使用
Style个人理解就是view的一些属性的集合,那么一系列view(例如TextVIew),只要是要该style那么就都有相同的内容,如 文字的大少,颜色等,方便修改 首先最基本的使用,多个textV ...
- Android应用性能优化之使用SparseArray替代HashMap
HashMap是java里比较常用的一个集合类,我比较习惯用来缓存一些处理后的结果.最近在做一个Android项目,在代码中定义这样一个变量,实例化时,Eclipse却给出了一个 performanc ...
- 在页面上以消息束的形式同时抛出多个DialogMessage
com.sun.java.util.collections.ArrayList exceptions = new com.sun.java.util.collections.ArrayList(); ...
- JQuery 如何选择带有多个class的元素
Q: 比如下面代码需要选择同时带有这几个class的元素,怎么写? 1 <div class="modal fade in"></div> A: 1. 依次 ...
- Linux下目录的合并以及文件的覆盖
有两个目录test和new,test目录下有目录和文件,new目录下有更改过的一些test下的目录和文件,以及一些新增的文件,现在对两个目录进行合并以及覆盖test下的旧文件. cp -frap ne ...
- Linux各目录作用
- NoSQL数据库之国产开源产品:SequoiaDB 分析前言
随着互联网技术的发展,面对海量数据的存储和分析,传统关系型数据库已经无法满足,由此衍生出一种与关系型数据库区别开的数据库NoSQL(Not Only SQL). 国外做的比较成熟的NoSQL有Mong ...