Cornfields

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Submit Status

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

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
unsigned char dpi[][][],dpa[][][];
int main()
{
int n,b,k;
int i,j,ii,jj,x,y;
unsigned char maxa,mina;
scanf("%d%d%d",&n,&b,&k);
memset(dpi,,sizeof(dpi));
memset(dpa,,sizeof(dpa));
for(i=; i<n; i++)
for(j=; j<n; j++)
scanf("%d",&x),dpa[i][j][]=x,dpi[i][j][]=dpa[i][j][];
for(ii=; ii<; ii++)
{
for(i=; i+(<<ii) - <n; i++)
{
for(j=; j+(<<ii)-<n; j++)
{
dpi[i][j][ii]=min(dpi[i][j][ii-],dpi[i+(<<(ii-))][j][ii-]);
dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i][j+(<<(ii-))][ii-]);
dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i+(<<(ii-))][j+(<<(ii-))][ii-]);
dpa[i][j][ii]=max(dpa[i][j][ii-],dpa[i+(<<(ii-))][j][ii-]);
dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i][j+(<<(ii-))][ii-]);
dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i+(<<(ii-))][j+(<<(ii-))][ii-]);
}
}
}
int kk=log(1.0*b)/log(2.0);
for(i=; i<k; i++)
{
scanf("%d%d",&x,&y);
x--,y--;
maxa=dpa[x][y][kk];
maxa=max(maxa,dpa[x][y+b-(<<kk)][kk]);
maxa=max(maxa,dpa[x+b-(<<kk)][y][kk]);
maxa=max(maxa,dpa[x+b-(<<kk)][y+b-(<<kk)][kk]); mina=dpi[x][y][kk];
mina=min(mina,dpi[x][y+b-(<<kk)][kk]);
mina=min(mina,dpi[x+b-(<<kk)][y][kk]);
mina=min(mina,dpi[x+b-(<<kk)][y+b-(<<kk)][kk]);
printf("%d\n",maxa-mina);
}
}

Cornfields poj2019 二维RMQ的更多相关文章

  1. POJ 2019 Cornfields (二维RMQ)

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

  2. poj2019 二维RMQ裸题

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

  3. POJ 2019 Cornfields(二维RMQ)

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

  4. poj2019 二维RMQ模板题

    和hdu2888基本上一样的,也是求一个矩阵内的极值 #include<iostream> #include<cstring> #include<cstdio> # ...

  5. POJ 2019 Cornfields [二维RMQ]

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

  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. [2013-06-05]bat脚本设置DNS

    有时候需要切换本机dns,将网络环境转至测试环境 @echo off netsh interface ip set dns name="本地连接" source=static ad ...

  2. Flink从Kafka 0.8中读取多个Topic时的问题

    Flink提供了FlinkKafkaConsumer08,使用Kafka的High-level接口,从Kafka中读取指定Topic的数据,如果要从多个Topic读取数据,可以如下操作: 1.appl ...

  3. mysql转ElasticSearch的案例分析

    前言 最近工作中在进行一些技术优化,为了减少对数据库的压力,对于只读操作,在程序与db之间加了一层-ElasticSearch.具体实现是db与es通过bin-log进行同步,保证数据一致性,代码调用 ...

  4. PHP发送邮件功能--ThinkPHP3.2.3

    首先第一步   :在网上down了一个PHPMailer插件,插件地址>https://github.com/PHPMailer/PHPMailer下载解压后,这里我们只需要用到其中两个文件,如 ...

  5. 在STEP7 TIA PORTAL中,设置模块的地址和设备名(Device name)

    assign device name, ip address for PROFINET componet in TIA Portal 方法1: PLC --> online & diag ...

  6. 看懂c/c++ 函数、指针、数组定义

    读懂 函数 + 指针 + 数组 c语言运算符机器优先级,看这里 结合运算符优先级,我们试着读懂函数和指针 优先级简单看 表达式提升():一级优先 函数():二级优先 数组[]:二级优先 指针定义*:三 ...

  7. 201521123037 《Java程序设计》第3周学习总结

    1. 本周学习总结 链接:http://naotu.baidu.com/file/026a646bb4031d4238accc69cdf53272 2. 书面作业 1. 代码阅读 public cla ...

  8. 201521123045-----《Java程序设计》第3周学习总结

    ---恢复内容开始--- 1. 本章学习总结 2. 书面作业 1. 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private stati ...

  9. 201521123068《Java程序设计》第2周学习总结

    1.本周学习总结 认识了各种类型与变量的使用 学习运算符的基本使用 变量命名不可以使用数字和一些特殊字符(如*.&.%等)作为开头: 变量名称不可以与Java关键词同名 学习了类与对象的区别 ...

  10. Git与码云(Git@OSC)入门-如何在实验室和宿舍同步你的代码(2)

    4. 处理冲突 4.1 向远程仓库push时无法提交成功,提示在push前应该先pull 如图所示: 有可能是因为远程仓库的版本与本地仓库的版本不一致,所以应先git pull将远程仓库的内容合并到本 ...