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. JSON 理解

    转自: http://blog.csdn.net/qyf_5445/article/details/8635578 (json很全面的理解) http://www.cnblogs.com/haitao ...

  2. HTTP协议知多少-关于http1.x、http2、SPDY的相关知识

    作为网站开发的基础协议,我们知道浏览器上都有输出http这四个字母,这意味着什么呢? 这就是最基础的HTTP协议. 逐浪君今天为各位大人准备了一些HTTP技术的知识,来和大家分享. 以下图为例: 这一 ...

  3. mac系统webstorm快捷键

    WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为"Web前端开发神器"."最强大的HTML5编辑器". ...

  4. Java filter拦截器的使用

    1.web.xml配置 <!-- 验证是否登录 拦截功能 --> <filter> <filter-name>isLogin</filter-name> ...

  5. 探索 Java 热部署

    在 JAVA 开发领域,热部署一直是一个难以解决的问题,目前的 JAVA 虚拟机只能实现方法体的修改热部署,对于整个类的结构修改,仍然需要重启虚拟机,对类重新加载才能完成更新操作.对于某些大型的应用来 ...

  6. 如何将ASP.NET-WebApi发布到IIS6.0上(转)

    关于"如何将ASP.NET-WebApi发布到IIS6.0上"的这方面的学习,一开始项目组长让我们接触的时候,我的心情是这样的 哇呜.jpg 当时真的是一脸懵逼啊,对于刚接触asp ...

  7. makefile使用.lds链接脚本以及 $@ ,$^, $,< 解析

    先来分析一个简单的.lds链接脚本 例1,假如现在有head.c init.c nand.c main.c这4个文件: 1.1 首先创建链接脚本nand.lds: SECTIONS { firtst ...

  8. easyUI创建人员树

    最近做了一个树状的下拉列表,在这里记录一下,以后可以直接使用 项目中的树状下拉列表是用来选择人员用的,具体实现展示如下: 先说一说功能,左边的人员数是提供选人的,当点击中间的按钮,选中的人员会直接移到 ...

  9. setTimeout,setInterval你不知道的…

    javascript线程解释(setTimeout,setInterval你不知道的事) 标签: javascript引擎任务浏览器functionxmlhttprequest 2011-11-21 ...

  10. Java运算符和流程控制

    1 运算符 1.1  比较运算符 比较运算符的结果都是boolean类型,也即是要么是true,要么是false. 比较运算符"=="不能写成"=". > ...