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. 如何在O(n)的时间复杂度内找出数组中出现次数超过了一半的数

    方法一:每次取出两个不同的数,剩下的数字中重复出现次数超过一半的数字肯定,将规模缩小化.如果每次删除两个不同的数,这里当然不是真的把它们踢出数组,而是对于候选数来说,出现次数减一,对于其他数来说,循环 ...

  2. 反射简介—C#特性和反射

    .NET编译器的任务之一就是为所有定义和引用的类型生成元数据描述.除了程序集中标准的元数据外,.NET平台还支持特定(attribute)把更多的元数据嵌入到程序集中. .NET特性扩展了抽象的Sys ...

  3. IDEA maven项目下测试mybatis例子,使用mappper class或package引入mapper映射文件,总是报错Invalid bound statement(所有配置完全正确)

    困扰几个小时,终于查到解决办法及原因(可以直接到最后看解决方案) 环境就是用IDEA搭建的maven项目,主要jar包引入配置如下 <dependencies> <dependenc ...

  4. Nginx 防盗链

    “盗链”的定义是:此内容不在自己服务器上,而通过技术手段,绕过别人放广告有利益的最终页,直接在自己的有广告有利益的页面上向最终用户提供此内容. 常常是一些小网站来盗取一些有实力的大网站的地址(比如一些 ...

  5. 如何使Ubuntu Linux12.04 LTS版可以用root用户登陆

    如何使Ubuntu Linux12.04 LTS版可以用root用户登陆 1.  用普通用户登录2.  在终端执行sudo -s,然后输入当前登录的普通用户密码,进入到root用户模式3.  执行ge ...

  6. [Command] lrzsz - 文件传输工具包

    lrzsz 是一个支持 XMODEM.YMODEM.ZMODEM 文件传输协议的 Unix 程序包.它是 Omen Technologies 公司所有的 rzsz 程序包的公开发行增强版,遵守 GNU ...

  7. AngularJS的初步学习(1)

    AngularJS 是一个Javascript框架.它可通过 <script> 标签添加到 HTML 页面.AngularJS 通过 指令 扩展了 HTML,且通过 表达式绑定数据到 HT ...

  8. 【python】解决No module named _sqlite3的问题

    环境版本说明: centos 7 python3.6.4 django 2.0 在创建django项目时候.报错以下信息 详细错误信息如下: 原因: 未安装sqlite 模块 解决: 1 安装 sql ...

  9. IIS7 windows 下安装PHP

    首先要下载 php for windows 这个软件,百度一搜一大把. 1.我安装的是5.3.22版,就以这个为例给大家解说一下. 安装就是下一步,下一步,到一个选项 IIS FastCGI 选这个! ...

  10. cmake openssl ios

    1 下载源代码 git clone https://github.com/pol51/OpenSSL-CMake.git cd OpenSSL-CMake mkdir build && ...