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. 微信小程序实例--仿豆瓣电影

    最近不想写论文,就想捣鼓点新东西吧,就边看官方文档,花了3天时间写了一个简单的仿豆瓣电影的微信小程序,给大家分享一下教程吧. 源码&效果图 源码点击这里,欢迎star 运行方法: 下载微信we ...

  2. Angular 新手容易碰到的坑

    Q1.<div ng-include="views/user/show.html"></div> 错在哪里? 如果你这么写过,会发现这个位置啥也没有加载出来 ...

  3. 前端开发【第3篇:JavaScript序】

    JavaScript历史 聊聊JavaScript的诞生 JavaScirpt鼻祖:Bremdan Eich(布兰登·艾奇),JavaScript的诞生于浏览器的鼻祖网景公司(Netscape),发布 ...

  4. 微信小程序开发历程

    小程序:    帮助文档:(https://mp.weixin.qq.com/debug/wxadoc/dev/index.html) 优势:    一种无须安装即可运行的程序,与订阅号,服务号是同一 ...

  5. Spring 对缓存的抽象

    Cache vs. Buffer A buffer is used traditionally as an intermediate temporary store for data between ...

  6. 201521123091 《Java程序设计》第5周学习总结

    Java 第五周总结 第五周的作业. 目录 1.本章学习总结 2.Java Q&A 3.使用码云管理Java代码 4.PTA实验 1.本章学习总结 1.1 尝试使用思维导图总结有关多态与接口的 ...

  7. 201521123110《java程序设计》第八周学习总结

    1. 本周学习总结 2. 书面作业 1.List中指定元素的删除 上课就交了,编写时主要用remove方法,然后Iterator的remove()方法可删除当前下标为i的元素后,该元素后的所有元素将往 ...

  8. 201521123089 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 1.代码阅读:Child压缩包内源代码1.1 com.parent包中Child.java文件能否编译通过?哪 ...

  9. 201521123029《java程序设计》第2周学习总结

    1. 本周学习总结 (1)完成了课后习题,对所学知识进一步巩固. (2)学会了JAVA中的选择结构,循环结构,其中条件必须为布尔表达式,与C++非0即真不同. (3)学会java中的break和con ...

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

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 补充上周异常 异常堆栈追踪:获得异常发生的根源 创建自己的异常 自定义异常类不是由Java系统监测到的异常, ...