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次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
随机推荐
- 获取机器网卡的物理(MAC)地址
<?php /** * 获取机器网卡的物理(MAC)地址* 目前支持WIN/LINUX系统 * 编辑: www.jbxue.com**/ class MacAddInfo { ...
- js 放大镜用法bug解决
<img id="zoom_02" src='img/zhang5.jpg' data-zoom-image="img/zhang5p.jpg" /> ...
- POJ 1611 The Suspects(简单并查集)
( ̄▽ ̄)" #include<iostream> #include<cstdio> using namespace std; ]; void makeSet(int ...
- ebtables
ebtables是以太网桥防火墙,以太网工作在数据链路层,ebtables过滤数据链路层包.2.6内核内置了ebtables,要使用它必须先按装她的用户空间工具(ebtables-V2.0.6),安装 ...
- mongo细节
mongo创建表db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean>, size: < ...
- 页面跳转后样式丢失js失效
2 页面跳转后样式丢失js失效 问题描述: 用ajax跳转的时候,从a.html跳转到b.html后,b.html的css以及js都失效了. 解决办法: 将当前页面需要用到的css以及js放在< ...
- mybatis 做 insert操作的时候返回插入的那条数据的id
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:吃丸子的小鹿链接:http://www.zhihu.com/question/20810321/answer/16843223 ...
- jni开发中的常见错误
* java.lang.UnsatisfiedLinkError: Native method not found: 本地方法没有找到 * 本地函数名写错 * 忘记加载.so文件 没有调用System ...
- SVN-svn path not found: 404 Not Found
报错信息是本地找不到文件 因为我直接移动了项目中的java文件到别的目录,在SVN看来相当于变相的删掉了一个目录的文件,在另外一个目录新增文件, 但是移动文件SVN是不会做删除记录到日志文件中的,所以 ...
- OpenGL红宝书例3.1 -- glBufferSubData使用
代码实现 1.1 C++部分 GLFWwindow *window; GLuint shader_program; GLuint VAO; void init() { static const GLf ...