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

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
 
二维RMQ问题 
矩形解法:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define N 255 int n,b,k;
int val[N][N];
int mx[N][N][][];
int mi[N][N][][]; void ST(int n,int m)
{
int i,j,r,c;
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
mx[i][j][][]=mi[i][j][][]=val[i][j];
}
}
int kn=(int)(log(double(n))/log(2.0));
int km=(int)(log(double(m))/log(2.0));
for(i=;i<=kn;i++)
{
for(j=;j<=km;j++)
{
if(i== && j==) continue;
for(r=;r+(<<i)-<=n;r++)
{
for(c=;c+(<<j)-<=m;c++)
{
if(i==)
{
mx[r][c][i][j]=max(mx[r][c][i][j-],mx[r][c+(<<(j-))][i][j-]);
mi[r][c][i][j]=min(mi[r][c][i][j-],mi[r][c+(<<(j-))][i][j-]);
}
else
{
mx[r][c][i][j]=max(mx[r][c][i-][j],mx[r+(<<(i-))][c][i-][j]);
mi[r][c][i][j]=min(mi[r][c][i-][j],mi[r+(<<(i-))][c][i-][j]);
}
}
}
}
}
} int RMQ(int r1,int c1,int r2,int c2)
{
int kr=(int)(log(double(r2-r1+))/log(2.0));
int kc=(int)(log(double(c2-c1+))/log(2.0)); int t1=mx[r1][c1][kr][kc];
int t2=mx[r2-(<<kr)+][c1][kr][kc];
int t3=mx[r1][c2-(<<kc)+][kr][kc];
int t4=mx[r2-(<<kr)+][c2-(<<kc)+][kr][kc]; int m1=mi[r1][c1][kr][kc];
int m2=mi[r2-(<<kr)+][c1][kr][kc];
int m3=mi[r1][c2-(<<kc)+][kr][kc];
int m4=mi[r2-(<<kr)+][c2-(<<kc)+][kr][kc]; return max(max(t1,t2),max(t3,t4))-min(min(m1,m2),min(m3,m4));
} int main()
{
int i,j;
scanf("%d%d%d",&n,&b,&k);
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
scanf("%d",&val[i][j]);
}
}
ST(n,n);
while(k--)
{
int r1,c1,r2,c2;
scanf("%d%d",&r1,&c1);
r2=r1+b-;
c2=c1+b-;
printf("%d\n",RMQ(r1,c1,r2,c2));
}
return ;
}

正方形解法:

#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
#define inf 0x7fffffff
#define N 255
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b int n,b,k;
int val[N][N];
int mx[N][N][];
int mi[N][N][]; int getMax(int x,int y,int p)
{
int res=-inf;
res=max(res,mx[x][y][p]);
if(x+(<<p)<=n) res=max(res,mx[x+(<<p)][y][p]);
if(y+(<<p)<=n) res=max(res,mx[x][y+(<<p)][p]);
if(x+(<<p)<=n && y+(<<p)<=n) res=max(res,mx[x+(<<p)][y+(<<p)][p]);
return res;
}
int getMin(int x,int y,int p)
{
int res=inf;
res=min(res,mi[x][y][p]);
if(x+(<<p)<=n) res=min(res,mi[x+(<<p)][y][p]);
if(y+(<<p)<=n) res=min(res,mi[x][y+(<<p)][p]);
if(x+(<<p)<=n && y+(<<p)<=n) res=min(res,mi[x+(<<p)][y+(<<p)][p]);
return res;
} void ST()
{
int i,j,k;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
mx[i][j][]=mi[i][j][]=val[i][j];
}
}
int kn=(int)(log(n*1.0)/log(2.0)); for(k=;k<=kn;k++)
{
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
mx[i][j][k]=getMax(i,j,k-);
mi[i][j][k]=getMin(i,j,k-);
}
}
}
} int RMQMAX(int x,int y,int b)
{
int p=(int)(log(b*1.0)/log(2.0));
int res=-inf;
res=max(res,mx[x][y][p]);
if(x+b-(<<p)<=n) res=max(res,mx[x+b-(<<p)][y][p]);
if(y+b-(<<p)<=n) res=max(res,mx[x][y+b-(<<p)][p]);
if(x+b-(<<p)<=n && y+b-(<<p)<=n) res=max(res,mx[x+b-(<<p)][y+b-(<<p)][p]);
return res;
} int RMQMIN(int x,int y,int b)
{
int p=(int)(log(b*1.0)/log(2.0));
int res=inf;
res=min(res,mi[x][y][p]);
if(x+b-(<<p)<=n) res=min(res,mi[x+b-(<<p)][y][p]);
if(y+b-(<<p)<=n) res=min(res,mi[x][y+b-(<<p)][p]);
if(x+b-(<<p)<=n && y+b-(<<p)<=n) res=min(res,mi[x+b-(<<p)][y+b-(<<p)][p]);
return res;
} int main()
{
int i,j;
scanf("%d%d%d",&n,&b,&k);
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
scanf("%d",&val[i][j]);
}
}
ST();
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",RMQMAX(x,y,b)-RMQMIN(x,y,b));
}
return ;
}

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

  1. POJ 2019 Cornfields [二维RMQ]

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

  2. POJ 2019 Cornfields (二维RMQ)

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

  3. POJ 2019 Cornfields(二维RMQ)

    相比以前的RMQ不同的是,这是一个二维的ST算法 #include<iostream> #include<cstring> #include<cstdio> #in ...

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

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

  5. Cornfields POJ - 2019(二维RMQ板题)

    就是求子矩阵中最大值与最小值的差... 板子都套不对的人.... #include <iostream> #include <cstdio> #include <sstr ...

  6. poj 2019 二维rmq *

    题目大意:给出一个N*N矩形,每个格子上有一个价值.询问一个b*b的矩形在左上角的位置(x,y),(x+b-1,y+b-1)这一部分的最大值-最小值是多少. 模板题 #include <stdi ...

  7. 二维 ST POJ 2019

    题目大意:给你一个n*n的矩阵,每次给你一个点(x,y),以其为左上角,宽度为b的矩阵中最小的数值和最大数值的差是多少?  一共k个询问. 思路:简单的二维st. 定义dp(i,j,k,L)表示以(i ...

  8. POJ 2019

    简单的RMQ,可我怎么写都WA.不明白,找了一个和我相似的贴过了,要赶着去外婆家. #include <iostream> #include <algorithm> #incl ...

  9. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

随机推荐

  1. mysql学习笔记3

    要用php+mysql 首先要配置环境.现在要先下载wamp(Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python).直接安装就行 可以点下一步的就点下一步,直至 ...

  2. 在Windows下用MingW 4.5.2编译live555

    1.下载live555(http://www.live555.com/liveMedia/public/),解压. 2.进入MingW Shell,输入cd: F:/Qt/live(假定解压到F:/Q ...

  3. C++ 代码性能优化 -- 循环分割提高并行性

    对于一个可结合和可交换的合并操作来说,比如整数的加法或乘法, 我们可以通过将一组合并操作分割成 2 个或更多的部分,并在最后合并结果来提高性能. 原理: 普通代码只能利用 CPU 的一个寄存器,分割后 ...

  4. Understanding Manycore Scalability of File Systems

    多核场景下,不同文件系统,文件操作的性能评估.

  5. QML定时器

    QML中的定时器能够周期性的触发一个事件,其使用非常简单.方便.这里给出一个示例: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQu ...

  6. jxl和poi处理excel之比较

    功能需求是根据客户提供的excel模板,程序动态填充其中的一些数据.该模板包含大量的宏,刚拿到的时候头都要晕了,本人虽天天和电脑打交道,但是excel咱不是高手啊,什么宏,之前光听过,听着都觉得是高级 ...

  7. IE专用CSS,最全的CSS hack方式一览

    http://blog.csdn.net/freshlover/article/details/12132801

  8. 九度OJ1184二叉树

    题目描述: 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储).例如如下的先序遍历字符串:ABC##DE#G##F###其中“#”表示的是空格,空格字符代表空树 ...

  9. Websocket协议之握手连接

    Websocket协议是为了解决web即时应用中服务器与客户端浏览器全双工通信的问题而设计的,是完全意义上的Web应用端的双向通信技术,可以取代之前使用半双工HTTP协议而模拟全双工通信,同时克服了带 ...

  10. platform_driver_register()--如何match之后调用probe

    int platform_driver_register(struct platform_driver *drv) { drv->driver.bus = &platform_bus_t ...