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. instanceof 是如何工作的

    知其然 JavaScript 提供 instanceof 关键字判断一个对象所属的构造函数.在 js 高级编程中讲到 instanceof 的作用: instanceof 操作符,用来测试实例与原型链 ...

  2. Selenium 定位网页元素

    第一 定位元素辅助工具 IE中在元素上右击 ->  “检查元素”,或按F12键打开开发者工具: Chrome中在元素上右击 -> “审查元素”,或按F12键打开开发者工具: Firefox ...

  3. ionic3 自动创建启动背景splash以及图标icon

    在新建的项目文件夹下的 resources下就是我们放置图标以及启动背景图片的位置了. 如果现在我们想生成自己的图片的启动背景以及图片,我们需要把resources下的 icon.png 以及spla ...

  4. mkyaffs2image制作根文件系统、使用NFS挂载虚拟机目录(2)

    1.制作根文件系统及nfs烧写 1.1 先解压文件系统,/wok/nfs_root 目录下是已经构造好的各种文件系统:① fs_mini.tar.bz2 是最小的根文件系统,里面的设备节点是事先建立好 ...

  5. web服务的三大主流架构

    1.远程过程调用 2.服务导向架构 3.表述性状态转移 今天在自学Spring架构时貌似发现 rest的表现.与RPC方式的最大差别,SOA方式更加关注如何去连接服务而不是去特定某个实现的细节.而re ...

  6. css预处理器less和scss之less介绍(一)

    第一次发的标题有误,重发一遍,抱歉了 一.less基础语法 1.声明变量:@变量名:变量值 使用变量:@变量名 例如 @color : #ff0000; @length : 100px; #div1{ ...

  7. 团队作业8——第二次项目冲刺(Beta阶段)--第四天

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 学号 成员 贡献比 201421123001 廖婷婷 17% 201421123002 翁珊 18% 201421123004 ...

  8. 团队作业8——第二次项目冲刺(Beta阶段)Day4--5.21

    展开圆桌式会议: 会议内容:1.团队成员对昨天任务完成情况做一个简单交流,并对昨天工作中存在的问题提出集中讨论解决:2.按照昨天的昨天工作分配表做具体的任务分配:3.简单讨论明天的任务分配每个人的工作 ...

  9. 第03周-Java作业评价

    1.作业存在的问题 几大扣分点: 思维导图敷衍了事 -1 代码格式混乱 -2 无码云提交记录 -2 无PTA实验总结 -2 部分题目未完成. 格式问题:到现在还搞不清楚的,主要是态度问题.相关的教程 ...

  10. 201521123090 《Java程序设计》 第8周学习总结

    本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1) 1.1 ...