相比以前的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. Chapter 2 Open Book——12

    I called him in when dinner was ready, and he sniffed appreciatively as he walked into the room. 当晚饭 ...

  2. poi的各种单元格样式以及一些常用的配置

    之前我做过一个poi到处excel数据的博客,但是,后面使用起来发现,导出的数据单元格样式都不对. 很多没有居中对齐,很多单元格的格式不对,还有就是单元格的大小不对,导致数据显示异常,虽然功能可以使用 ...

  3. js 放大镜用法bug解决

    <img id="zoom_02" src='img/zhang5.jpg' data-zoom-image="img/zhang5p.jpg" /> ...

  4. JS总结之一:字符串的调用方法

    字符串的调用方法:var s="hello, world";document.write(s.charAt(0)); //第一个字符document.write(s.charAt( ...

  5. hdu_5805_NanoApe Loves Sequence(xjb搞)

    题目链接:hdu_5805_NanoApe Loves Sequence 题意: 给你n个数,现在要删一个数,删每个数的概率是一样的,现在问你删一个值后的相邻数绝对值最大差的期望是多少,因为担心精度误 ...

  6. hdu_5778_abs(暴力)

    题目链接:hdu_5778_abs 题意: 给你一个数x,然你找一个y,这个y只能被素数分解,每一个素数恰好出现2次,求y-x的绝对值最小 题解: 给官方的  ps:数据太水,打35个素数也能过 #i ...

  7. LeetCode OJ 235. Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. As3.0 类的【枚举】

    As3.0 类的枚举   “枚举”是您创建的一些自定义数据类型,用于封装一小组值.ActionScript 3.0 并不支持具体的枚举工具,这与 C++ 使用 enum 关键字或 Java 使用 En ...

  9. SDCycleScrollView 添加初始滚动页码

    最近在做一个无限轮播界面,由于自己写的有些卡顿,所以我使用了第三方框架SDCycleScrollView,但是我的初始滚动页不是从第一页开始怎么办,我想从第二页,第三页,或者最后一页,关于这个SDCy ...

  10. 用 gulp.spritesmith 自动化雪碧图

    一.安装nodejs之后,要设置两个环境变量 在 计算机右击属性---高级系统设置---高级---环境变量 打开窗口 新建2个环境变量,它们的值分别是nodejs根目录下的node_modules路径 ...