相比以前的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. 其他应用和技巧-用JS实现的抽奖程序

    -------------------------------- <script type="text/javascript">            var data ...

  2. windows 装 centos

    windows下压缩一下空间 直接装centos 找到分配给linux的/boot目录,然后修改grub/menu.lst文件,更换一下位置即可

  3. AngularJs多重视图和路由的使用

    使用AngularJs来做多重视图和路由是在方便不过了,在开发过程中,都有许多的页面,而这些页面都有相同的部分,比如页面的头部和尾部通常都是一样的,变化的都是主体部分,还有就是一些后端管理的一些项目, ...

  4. 更改Xcode的缺省公司名

    更改前: //  testAppDelegate.m //  test // //  Created by gaohf on 11-5-24. //  Copyright 2011 __MyCompa ...

  5. PHP: 异常exception

    异常最常见于SDK调用中,函数执行失败时抛出异常,顺带错误码和错误信息. 先来看下PHP的异常处理相关函数: public Exception::__construct() ([ string $me ...

  6. 在Linux中设置共享目录

    1.  是否安装了samba服务 sudo apt-get install samba sudo apt-get install smbfs 2.  创建需要共享的目录/ home/usr/share ...

  7. js实现td排序及分组分类

    如题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  8. js里写网页结构, 传函数参数

    如题 "<td align='center' height='30px' width='80px'><a href='javascript:sort(\"&quo ...

  9. list组件

    <?xml version="1.0"?> <!-- Simple example to demonstrate the Spark List component ...

  10. Android按钮单击事件的四种常用写法总结

    很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.因此本文就把这些写法总结一下,比较下各种写法的优劣,希望对大家灵活地选择编码方式可 ...