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 ...
随机推荐
- LoadRunner 11 完整卸载
实现彻底删除LR的步骤 打开任务管理器,关闭所有LR相关的进程 请备份好所有LR脚本 进入控制面板-添加与删除,找到HP LoadRunner并点击删除按钮,若出现提示“shared DLLs”消息框 ...
- java 12-5 StringBuffer的几个案例
1. 把数组拼接成一个字符串 public class StringBufferTest2 { public static void main(String[] args) { //定义一个数组 in ...
- SpringMVC常用接收Json的两种方法
@RequestBody JSONObject requestJson @RequestBody User user 一种是自定义加注解,由Spring负责绑定,一种是使用通用的JSONObject
- CSS3实现10种Loading效果
昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… 第1种效果: 代码如下: <div class="loading"> ...
- 理解SQL Server中的权限体系(下)----安全对象和权限
原文:http://www.cnblogs.com/CareySon/archive/2012/04/12/SQL-Security-SecurableAndPermission.html 在开始阅读 ...
- Linux下Qt的安装与配置
参考资料:http://www.cnblogs.com/emouse/archive/2013/01/28/2880142.html Linux 下编译.安装.配置 QT 下载qt 这里用的是4.7. ...
- iBatis.net入门指南
iBatis.net入门指南 - 1 - 什么是iBatis.net ? - 3 - iBatis.net的原理 - 3 - 新人指路 - 3 - iBatis.net的优缺点 ...
- C#创建Windows Service(Windows 服务)基础教程
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- 1从零开始学习Xamarin.iOS安装篇
安装和配置xamarin.ios 最近.net 开源新闻很火呀,于是想学习xamarin,早1年前就了解过这个东西,但是一直没有时间来学习,我这里装的是MAC上面的版本,废话不多说开始第一步安装. 概 ...
- [MetaHook] R_SparkShower
By hzqst void R_SparkShower(float *pos) { TEMPENTITY *tent; tent = efx.CL_TempEntAllocNoModel(pos); ...