相比以前的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)的更多相关文章

  1. POJ 2019 Cornfields [二维RMQ]

    题目传送门 Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7963   Accepted: 3822 ...

  2. POJ 2019 Cornfields 二维线段树的初始化与最值查询

    模板到不行.. 连更新都没有.. .存个模板. 理解留到小结的时候再写. #include <algorithm> #include <iostream> #include & ...

  3. [poj2019]Cornfields(二维RMQ)

    题意:给你一个n*n的矩阵,让你从中圈定一个小矩阵,其大小为b*b,有q个询问,每次询问告诉你小矩阵的左上角,求小矩阵内的最大值和最小值的差. 解题关键:二维st表模板题. 预处理复杂度:$O({n^ ...

  4. POJ 2019 Cornfields (二维RMQ)

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4911   Accepted: 2392 Descri ...

  5. [POJ 2019] Cornfields

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5516   Accepted: 2714 Descri ...

  6. poj2019 二维RMQ裸题

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:8623   Accepted: 4100 Descrip ...

  7. hdu2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. hduacm 2888 ----二维rmq

    http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题  直接用二维rmq 读入数据时比较坑爹  cin 会超时 #include <cstdio& ...

  9. hdu 2888 二维RMQ模板题

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  10. HDU 2888 Check Corners (模板题)【二维RMQ】

    <题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...

随机推荐

  1. PE结构之重定位表

    什么是重定位: 重定位就是你本来这个程序理论上要占据这个地址,但是由于某种原因,这个地址现在不能让你占用,你必须转移到别的地址,这就需要基址重定位.你可能会问,不是说过每个进程都有自己独立的虚拟地址空 ...

  2. linux同步windows的时间

    找了很多的资料,都没有windows做时间服务,linux同步windows的时间的,最后自己找了一些软件,终于搞定了,写出来给大家共享,以免大家多走弯路 首先在http://www.meinberg ...

  3. php curl 获取 HTTPS

    注意:谷歌的话开vpn可能才可以,goagent也不行function getHTTPS($url) {  $ch = curl_init();  curl_setopt($ch, CURLOPT_S ...

  4. 第三十二节,datetime时间模块

    首先要引入import datetime时间模块 datetime.date.today()模块函数 功能:输出系统年月日输出格式 2016-01-26[无参] 使用方法:datetime.date. ...

  5. hdu_5742_It's All In The Mind

    题目链接:hdu_5742_It's All In The Mind 题意: 有一个部分的数列,让你找一个满足他给的三个条件的数列,使前两个数的和除这个数列的sum最大 题解: xjb贪心一下就行了. ...

  6. lldpd-0.7.7代码解读(send_pdu部分)

    此文档是经过逆序推到出的,可能有错误之处,敬请指教,谢谢. 1)interfaces_update 更新一些接口信息 2)levent_iface_subscribe 该接口通过socket通信(非阻 ...

  7. JqGrid动态改变列名

    setLabel colname, data, class, properties jqGrid对象 给指定列设置一个新的显示名称.colname:列名称,也可以是列的位置索引,从0开始:data:列 ...

  8. linux 架设 postfix 邮件服务器

    http://www.cnblogs.com/dudu/archive/2012/12/12/linux-postfix-mailserver.html

  9. 远程连接(ssh安装)

    更新源列表打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--& ...

  10. 校验 MD5 值

    Linux 环境下:打开终端,输入命令:"md5sum filename",将结果与网页提供值对比.Windows 环境下:下载 MD5 校验软件并使用.