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. Python之虚拟环境管理

    Python本身有很多个版本,第三方的Python包又有很多可用的版本,所以经常会遇到下面的问题: 运行不同的Python程序,需要使用不同版本的Python(2.x或3.x). 在同一中Python ...

  2. [AX2012]在SSRS报表中获取从Menuitem传入的记录

    在较早版本的AX中我们运行一个报表时会用到类RunBaseReport,从它扩展一个子类,再由它运行报表,一个典型的Axapta3中的例子: class ReportProdInfo extends ...

  3. ios开发之-- tableview/collectionview获取当前点击的cell

    方法如下: 一般collectionView 或者 tableview都有自带的点击函数,如下: , collectionView -(void)collectionView:(UICollectio ...

  4. javascript 资料

    jQuery 语法实例 $(this).hide() 演示 jQuery hide() 函数,隐藏当前的 HTML 元素. $("#test").hide() 演示 jQuery ...

  5. ssh面密码登录配置-hadoop安装准备

    1. 用ssh-keygen创建公钥,一直回车即可 root@kali:~# ssh-keygen -t rsa Generating public/private rsa key pair. Ent ...

  6. es 5.0 拼音分词器 mac

    安装方法和ik中文分词器一样, 先下载: https://github.com/medcl/elasticsearch-analysis-pinyin 执行: mvn package; 打包成功以后, ...

  7. struts1的配置文件详解11111

    要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...

  8. C#实现HTTP请求文件下载,GET、POST请求的数据流接收

    做项目的时候由于插件Phaser请求audio的时候,不允许跨域,具体提示====> 已拦截跨源请求:同源策略禁止读取位于 http://ttyouni.com/1.mp3 的远程资源.(原因: ...

  9. 分布式实时日志系统(二) 环境搭建之 flume 集群搭建/flume ng资料

    最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...

  10. 【错误整理】ora-00054:resource busy and acquire with nowait specified解决方法【转】

    当某个数据库用户在数据库中插入.更新.删除一个表的数据,或者增加一个表的主键时或者表的索引时,常常会出现ora-00054:resource busy and acquire with nowait ...