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 ...
随机推荐
- TestLink学习四:TestLink1.9.13使用说明
前言 测试管理工具,是指用工具对软件的整个测试输入.执行过程和测试结果进行管理的过程.可以提高回归测试的效率.大幅提升测试时间.测试质量.用例复用.需求覆盖等. TestLink用于进行测试过程中的管 ...
- 迷你DVD管理器项目
package chapter5; import java.util.*; public class MiniDVD { public static void main(String[] args){ ...
- Android优化——UI优化(五) Listview 重用convertView
1.重用convertView 我们对convertView添加判断,如果存在我们就直接使用,否则初始化一个convertView的实例.(如下图) 2.使用viewHolder 使用viewHold ...
- php报错日志:PHP Deprecated:Automatically populating $HTTP_RAW_POST_DATA is deprecated
前几天将线上php服务升级到5.6.x版本后,php-error.log报出错误:PHP Deprecated: Automatically populating $HTTP_RAW_POST_DAT ...
- 27Spring_的事务管理_银行转账业务加上事务控制_基于tx.aop进行声明式事务管理
上一篇文章中,银行转账业务没有使用事务,会出现问题,所以这篇文章对上篇文章出现的问题进行修改. 事务 依赖 AOP , AOP需要定义切面, 切面由Advice(通知) 和 PointCut(切点) ...
- “PMS-基础权限管理系统”实施某谱OA系统经验总结
“PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...
- swift 判断输入的字符串是否为数字
// 判断输入的字符串是否为数字,不含其它字符 func isPurnInt(string: String) -> Bool { let scan: Scanner = Scanner(stri ...
- 将Axure用于需求分析工具
http://www.cnblogs.com/hnlong1/p/4113517.html?utm_source=tuicool 今年以来开始接触需求分析工作,uml是必用的建模语言. 一开始是使用最 ...
- Viewpager模仿微信主布局的三种方式 ViewPager,Fragment,ViewPager+FragmentPagerAdapter
效果大概就是这样 很简单 : 1 创建 top 和bottom 2主界面布局 添加top 和bottom 中间添加一个ViewPage 3 给ViewPager 和 底部View设置点击事件 源码下载 ...
- Activiti系列:为什么Activiti 5.18 的REST的api总是返回404错误
REST api可以访问了,如下 1.修改db.properties配置文件,让他访问sql server 2.在浏览器中输入如下地址,注意中间有一个service,这点和之前的不一样,在<Ac ...