hdu2888 二维RMQ
Check Corners
Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2513 Accepted Submission(s): 904
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.
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int a[MAXN][MAXN],n,m,dp[MAXN][MAXN][][];
void Init()
{
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
dp[i][j][][] = a[i][j];
}
}
for(int pi = ; pi < ; pi++){
for(int pj = ; pj < ; pj++){
if(pi == && pj == )continue;
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
if(i + ( << pi) - > n || j + ( << pj) - > m)continue;
if(pi == ){
dp[i][j][pi][pj] = max(dp[i][j][pi][pj-],dp[i][j+(<<(pj-))][pi][pj-]);
}
else {
dp[i][j][pi][pj] = max(dp[i][j][pi-][pj],dp[i+(<<(pi-))][j][pi-][pj]);
}
}
}
}
}
}
void getans(int x1,int y1,int x2,int y2)
{
int kx,ky;
kx = (int)(log((double)(x2 - x1)) / log(2.0));
ky = (int)(log((double)(y2 - y1)) / log(2.0));
int ans = -INF;
ans = max(ans,dp[x1][y1][kx][ky]);
ans = max(ans,dp[x2 - ( << kx) + ][y1][kx][ky]);
ans = max(ans,dp[x1][y2 - ( << ky) + ][kx][ky]);
ans = max(ans,dp[x2 - ( << kx) + ][y2 - ( << ky) + ][kx][ky]);
printf("%d ",ans);
if(a[x1][y1] == ans || a[x1][y2] == ans || a[x2][y1] == ans || a[x2][y2] == ans)printf("yes\n");
else printf("no\n");
}
void solve()
{
int q;
scanf("%d",&q);
int x1,y1,x2,y2;
while(q--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
getans(x1,y1,x2,y2);
}
}
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
scanf("%d",&a[i][j]);
}
}
Init();
solve();
}
return ;
}
hdu2888 二维RMQ的更多相关文章
- hdu2888 二维ST表(RMQ)
二维RMQ其实和一维差不太多,但是dp时要用四维 /* 二维rmq */ #include<iostream> #include<cstring> #include<cs ...
- HDU2888 Check Corners(二维RMQ)
有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...
- hduacm 2888 ----二维rmq
http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题 直接用二维rmq 读入数据时比较坑爹 cin 会超时 #include <cstdio& ...
- hdu 2888 二维RMQ模板题
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2888 Check Corners (模板题)【二维RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
- POJ 2019 Cornfields [二维RMQ]
题目传送门 Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7963 Accepted: 3822 ...
- 【LightOJ 1081】Square Queries(二维RMQ降维)
Little Tommy is playing a game. The game is played on a 2D N x N grid. There is an integer in each c ...
- 【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 ...
- POJ 2019 Cornfields (二维RMQ)
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4911 Accepted: 2392 Descri ...
随机推荐
- Maven学习(八)继承和聚合
*聚合(多模块) 在一个项目中 往往有多个模块组成,例如有项目demo下面有a, b两个模块 为了能使用一条命令就能构建demo-a, demo-b两个模块, 需要创建一个额外的聚合模块, 然后通过该 ...
- guava函数式编程
[Google Guava] 4-函数式编程 原文链接 译文链接 译者:沈义扬,校对:丁一 注意事项 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果.预计JDK8中会有所 ...
- Github 安全类Repo收集整理
作者:天谕链接:https://zhuanlan.zhihu.com/p/21380662来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.刚好这两天对之前github上关 ...
- flask表单提交的两种方式
一.通用方式 通用方式就是使用ajax或者$.post来提交. 前端html <form method="post" action="/mockservice&qu ...
- 一个screen的简单配置。。
# Start message startup_message off defencoding utf- encoding utf- utf- shell bash hardstatus always ...
- Linode Centos6.5从零开始装环境...流水账
安装JDK 下载. 先通过oracle网站, 下载, 得到link后, 在linode命令行里wget, 速度飞快, 但是文件名要改下. 其中JDK6是.bin, 其他都是tar.gz, bin直接执 ...
- CentOS RHEL 安装 Tomcat 7
http://www.davidghedini.com/pg/entry/install_tomcat_7_on_centos This post will cover installing and ...
- c语言中%s与%c对读入字符串的区别
对于scanf函数,需求%s类型时,\n是不会影响scanf内容的对于需求%c类型时,\n也是字符,自然会有影响.
- 大学回顾和C与PHP之路
我去年毕业,从事PHP学习和开发一年多. background:medical muti-media electric web; 先讲一下我的背景吧,我大学的学校是一个医科学校,然而专业是计算机动漫设 ...
- bash中变量+=,if大小判断,随机休眠
#!/bin/bash index= while true;do echo "hello" (( index+=)) echo `date "+%H:%M:%S" ...