POJ 2019 Cornfields(二维RMQ)
相比以前的RMQ不同的是,这是一个二维的ST算法
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 300
int minbest[][][N][N];
int maxbest[][][N][N];
///int lg[N];
int getmax(int a,int b,int c,int d)
{
return max(max(a,b),max(c,d));
}
int getmin(int a,int b,int c,int d)
{
return min(min(a,b),min(c,d));
}
int lg(int x)
{
if(x == ) return ;
return lg(x>>) + ;
}
int main()
{
int n,b,k;
while(EOF != scanf("%d%d%d",&n,&b,&k))
{
//lg[0] = -1;
/* for(int i = 1; i <= n; i++)
if(i&(i-1) == 0)
lg[i] = lg[i-1] + 1;
else lg[i] = lg[i-1];*/
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n ; j++)
{
int num;
scanf("%d",&num);
minbest[][][i][j] = maxbest[][][i][j] = num;
}
}
for(int i = ; (<<i) <= n ; i++)
for(int j = ; (<<j) <= n ; j++)
{
if(i + j)
{
for(int x = ; x <= n+-(<<i); x++)
for(int y = ; y <= n+-(<<j); y++)
{
if(i)
{
minbest[i][j][x][y] = min(minbest[i-][j][x][y],minbest[i-][j][x+ (<<i-)][y]);
maxbest[i][j][x][y] = max(maxbest[i-][j][x][y],maxbest[i-][j][x+ (<<i-)][y]);
}
if(j)
{
minbest[i][j][x][y] = min(minbest[i][j-][x][y],minbest[i][j-][x][y + (<<j-)]);
maxbest[i][j][x][y] = max(maxbest[i][j-][x][y],maxbest[i][j-][x][y + (<<j-)]);
}
}
}
} int tmp = lg(b) ;
///cout<<tmp<<endl;
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
int mi = getmin(minbest[tmp][tmp][x][y],minbest[tmp][tmp][x+b-(<<tmp)][y],
minbest[tmp][tmp][x][y+b-(<<tmp)],minbest[tmp][tmp][x+b-(<<tmp)][y+b-(<<tmp)]
);
int ma = getmax(maxbest[tmp][tmp][x][y],maxbest[tmp][tmp][x+b-(<<tmp)][y],
maxbest[tmp][tmp][x][y+b-(<<tmp)],maxbest[tmp][tmp][x+b-(<<tmp)][y+b-(<<tmp)]
);
///cout<<ma<<" "<<mi<<endl;
printf("%d\n",ma - mi);
}
}
return ;
}
POJ 2019 Cornfields(二维RMQ)的更多相关文章
- POJ 2019 Cornfields [二维RMQ]
题目传送门 Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7963 Accepted: 3822 ...
- POJ 2019 Cornfields 二维线段树的初始化与最值查询
模板到不行.. 连更新都没有.. .存个模板. 理解留到小结的时候再写. #include <algorithm> #include <iostream> #include & ...
- [poj2019]Cornfields(二维RMQ)
题意:给你一个n*n的矩阵,让你从中圈定一个小矩阵,其大小为b*b,有q个询问,每次询问告诉你小矩阵的左上角,求小矩阵内的最大值和最小值的差. 解题关键:二维st表模板题. 预处理复杂度:$O({n^ ...
- POJ 2019 Cornfields (二维RMQ)
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4911 Accepted: 2392 Descri ...
- [POJ 2019] Cornfields
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5516 Accepted: 2714 Descri ...
- poj2019 二维RMQ裸题
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions:8623 Accepted: 4100 Descrip ...
- hdu2888 二维RMQ
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 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次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
随机推荐
- Python识别网站验证码
http://drops.wooyun.org/tips/6313 Python识别网站验证码 Manning · 2015/05/28 10:57 0x00 识别涉及技术 验证码识别涉及很多方面的内 ...
- Codeforces Round #364 (Div. 2) D. As Fast As Possible
D. As Fast As Possible time limit per test 1 second memory limit per test 256 megabytes input stand ...
- MT5基础知识
获取账户相关信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 printf("ACCOUNT_BALANCE ...
- cdn是什么
CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构, 将网站的内容发布到最接近用户的网络”边缘”,使用户可以就近 ...
- Winsock SPI-Socks5-SSL
- C# 引用参数
最近经常和同事讨论引用参数的问题,为了搞清楚,查了些资料,其中CLR via C#中讲的比较清楚,整理了下 ----摘自(CLR via C#) 在默认情况下,CLR假设所有的方法参数都是按值传递的. ...
- JSTL标签库---SUN公司开发的标签库
JSTL里的标签包含五大类标签: 核心标签库 国际化标签 JSTL函数(EL函数) 数据库标签// 操作数据库的,用不到了 XML标签//操作XML的,用不到了 都在jstl.jar,standar. ...
- oracle_一次移动数据库dbf文件的操作
oracle数据库的dbf路径下面磁盘不足,需要把原始路径下面的dbf文件移动到另外一个磁盘路径下, 具体的操作有四步. 1.把整个表空间offline. 2.copy原始路径下的dbf文件到新的路径 ...
- linux(视频学习)2
第二部分(javaee的开发环境的搭建): 1. 安装jdk的过程: 安装ios的镜像文件,挂载到/mnt目录下.挂载: mount /mnt/cdrom卸载: umount /mnt/cdrom ...
- json对象的简单介绍
1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任 ...