题目传送门

Cornfields

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7963   Accepted: 3822

Description

FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find.

FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it.

FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.

Input

* Line 1: Three space-separated integers: N, B, and K.

* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.

* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1.

Output

* Lines 1..K: A single integer per line representing the difference between the max and the min in each query. 

Sample Input

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

Sample Output

5

Source


  分析:

  二维$RMQ$模板题。

  就是模板,但是卡空间是真恶心。。。卡了一个小时。

  Code:

//It is made by HolseLee on 4th Sep 2018
//POJ 2019
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N=;
int mi[N][N][][];
int ma[N][N][][]; void ready(int n)
{
for(int i=; (<<i)<=n; ++i)
for(int j=; (<<j)<=n; ++j) {
if( i== && j== ) continue;
for(int line=; line+(<<i)-<=n; ++line)
for(int ray=; ray+(<<j)-<=n; ++ray) {
if( i ) {
mi[line][ray][i][j]=min(mi[line][ray][i-][j],mi[line+(<<(i-))][ray][i-][j]);
ma[line][ray][i][j]=max(ma[line][ray][i-][j],ma[line+(<<(i-))][ray][i-][j]);
} else {
mi[line][ray][i][j]=min(mi[line][ray][i][j-],mi[line][ray+(<<(j-))][i][j-]);
ma[line][ray][i][j]=max(ma[line][ray][i][j-],ma[line][ray+(<<(j-))][i][j-]);
}
}
}
} int quary(int x,int y,int X,int Y)
{
int kx=,ky=,m1,m2,m3,m4,minn,maxx;
while( (<<(kx+))<=X-x+ ) kx++;
while( (<<(ky+))<=Y-y+ ) ky++; minn=min(min(mi[x][y][kx][ky],mi[X-(<<kx)+][Y-(<<ky)+][kx][ky]),min(mi[X-(<<kx)+][y][kx][ky],mi[x][Y-(<<ky)+][kx][ky])); maxx=max(max(ma[x][y][kx][ky],ma[X-(<<kx)+][Y-(<<ky)+][kx][ky]),max(ma[X-(<<kx)+][y][kx][ky],ma[x][Y-(<<ky)+][kx][ky])); return maxx-minn;
} int main()
{
int n,B,m;
while( scanf("%d%d%d",&n,&B,&m)== && n && B &&m ) {
int x,y;
for(int i=; i<=n; ++i)
for(int j=; j<=n; ++j) {
scanf("%d",&x);
mi[i][j][][]=ma[i][j][][]=x;
}
ready(n);
while( m-- ) {
scanf("%d%d",&x,&y);
int ans=quary(x,y,x+B-,y+B-);
printf("%d\n",ans);
}
}
return ;
}

POJ 2019 Cornfields [二维RMQ]的更多相关文章

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

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

  2. [poj2019]Cornfields(二维RMQ)

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

  3. POJ 2019 Cornfields (二维RMQ)

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

  4. [POJ 2019] Cornfields

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

  5. poj2019 二维RMQ裸题

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

  6. hdu2888 二维RMQ

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

  7. hduacm 2888 ----二维rmq

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

  8. hdu 2888 二维RMQ模板题

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

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

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

随机推荐

  1. time_t与GMT格式互转

    time_t Time::timeFromGMT(string gmt) { char week[4]; memset(week,0,4); char month[4]; memset(month,0 ...

  2. JVM学习十一:JVM之深入分析ClassLoader

    本章节准备写的是对类加载器ClassLoader的剖析,但因为前面已经对类加载器做过一些简单的分析和双亲委派机制的分析:因此本章节的侧重点在于实例演示和自定义加载器. 一.什么是ClassLoader ...

  3. 无废话JavaScript(下)

    五.函数式 这个可不是JavaScript的发明,它的发明人已经死了,而他的这个发明还在困扰着我们……如同爱迪生的灯泡还在照耀着我们. 其实函数式语言很简单,它就是一种与命令式语言同样“完备”的语言实 ...

  4. 应用于网站导航中的 12 个 jQuery 插件

    当考虑到网页设计时,导航被认为是使网页以用户友好方式展现的一个重要部分.在现代的交互网站中,导航起着至关重要的作用,如果没有正确地处理会影响你网站的访问.适当的导航工具能够帮助用户在网站的不同页面内容 ...

  5. [php]修改站点的虚拟目录

    wamp默认的站点的目录是www的目录,可以修改appache的httpd.conf文件来修改目录,修改方法如下: 1. <Directory "D:/SoftWare/wamp/ww ...

  6. 【leetcode 简单】第三十三题 验证回文串

    给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...

  7. HDU 1214 圆桌会议 (找规律)

    题目链接 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家 ...

  8. Attention is all you need 论文详解(转)

    一.背景 自从Attention机制在提出之后,加入Attention的Seq2Seq模型在各个任务上都有了提升,所以现在的seq2seq模型指的都是结合rnn和attention的模型.传统的基于R ...

  9. Linux运维常见问题解决集锦【转】

    作为linux运维,多多少少会碰见这样那样的问题或故障,用点心,平时多注意积累,水平肯定越来越高. 下面就是常见问题解决集锦:   1.shell脚本不执行 问题:某天研发某同事找我说帮他看看他写的s ...

  10. WebClient vs HttpClient vs HttpWebRequest

    转载:http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/ Just when I was startin ...