Cornfields
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7444   Accepted: 3609

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
  • 用二维ST表即可
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int max4(int a, int b, int c, int d){
return max(max(a,b),max(c,d));
}
int min4(int a, int b, int c, int d){
return min(min(a,b),min(c,d));
}
int mx[][][][], mi[][][][], fac[];
int RMQmx(int x1, int y1, int x2, int y2, int m){
int k=(int)(log((double)m)/log(2.0));
return max4(mx[x1][y1][k][k],mx[x1][y2-fac[k]+][k][k],mx[x2-fac[k]+][y1][k][k],mx[x2-fac[k]+][y2-fac[k]+][k][k]);
}
int RMQmi(int x1, int y1, int x2, int y2, int m){
int k=(int)(log((double)m)/log(2.0));
return min4(mi[x1][y1][k][k],mi[x1][y2-fac[k]+][k][k],mi[x2-fac[k]+][y1][k][k],mi[x2-fac[k]+][y2-fac[k]+][k][k]);
}
int n, m, q, t, u, v;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
for(int i=;i<;i++)
fac[i]=(<<i);
while(~scanf("%d %d %d",&n,&m,&q)){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
scanf("%d",&t);
mx[i][j][][]=t;
mi[i][j][][]=t;
}
int k=(int)(log((double)n)/log(2.0));
// [x][y][1<<e][1<<f]
for(int e=;e<=k;e++)
for(int f=;f<=k;f++)
for(int i=;i+fac[e]-<=n;i++)
for(int j=;j+fac[f]-<=n;j++){
mx[i][j][e][f]=max4(mx[i][j][e-][f-],mx[i+fac[e-]][j][e-][f-],mx[i][j+fac[f-]][e-][f-],mx[i+fac[e-]][j+fac[f-]][e-][f-]);
mi[i][j][e][f]=min4(mi[i][j][e-][f-],mi[i+fac[e-]][j][e-][f-],mi[i][j+fac[f-]][e-][f-],mi[i+fac[e-]][j+fac[f-]][e-][f-]);
}
while(q--){
scanf("%d %d",&u,&v);
printf("%d\n",RMQmx(u,v,u+m-,v+m-,m)-RMQmi(u,v,u+m-,v+m-,m));
}
}
return ;
}

POJ_2019_Cornfields的更多相关文章

随机推荐

  1. 18个不常见的C#关键字,您使用过几个?

    转自:http://www.cnblogs.com/zhuqil/archive/2010/04/09/UnCommon-Csharp-keywords-A-Look.html 1.__arglist ...

  2. ios开发之--复制到剪切板

    仅做记录: UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = @"你好&quo ...

  3. c 网络字节序和本机字节序转换

    将多字节整数类型的数据,从主机的字节顺序转化为网络字节顺序 #include <netinet/in.h> uint32_t htonl(uint32_t hostlong);uint16 ...

  4. 一张图了解SSH端口转发

    ssh和端口转发什么的,我就不想废话了,主要是ssh的命令格式真心不太好理解.网上也搜过相关文章,参差不齐.其实自己也理解怎么用,但我自己也表达不好.这几日无意碰到篇好文章,有图有真相,清楚的很,还有 ...

  5. ConcurrentModificationException 详解

    工作中碰到个ConcurrentModificationException.代码如下: List list = ...;for(Iterator iter = list.iterator(); ite ...

  6. 最简单的GLSL,Shader

    Vertex Shader void main() { gl_FrontColor = gl_Color; gl_Position = ftransform(); } Fragment Shader ...

  7. commons-beanutils的使用

    commons-beanutils是通过内省来完成的. 需要两个包: commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar JavaBean类: ...

  8. 自己搭建CDN服务器静态内容加速-LuManager CDN使用教程

    为什么要自己来搭建一个CDN服务器实现网站访问加速?一是免费CDN服务稳定性和加速效果都不怎么行:二是用国内的付费CDN服务价格贵得要死,一般的草根站长无法承受:三是最现实的问题国内的CDN要求域名B ...

  9. TYAttributedLabel——简单,强大的iOS属性文本控件

    本文转载至 http://www.mobile-open.com/2015/86578.html TYAttributedLabel 简单,强大的属性文本的控件(无需了解CoreText),支持图文混 ...

  10. Java网络编程之查找Internet地址

    一.概述 连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识.由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的I ...