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

  1. 5 3 1
  2. 5 1 2 6 3
  3. 1 3 5 2 7
  4. 7 2 4 6 1
  5. 9 9 8 6 5
  6. 0 6 9 3 9
  7. 1 2

Sample Output

  1. 5

Source

 
  • 二维区间最值RMQ
  • 用二维ST表即可
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cmath>
  8. #include <vector>
  9. #include <queue>
  10. #include <stack>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. typedef long long LL ;
  15. typedef unsigned long long ULL ;
  16. const int maxn = 1e5 + ;
  17. const int inf = 0x3f3f3f3f ;
  18. const int npos = - ;
  19. const int mod = 1e9 + ;
  20. const int mxx = + ;
  21. const double eps = 1e- ;
  22. const double PI = acos(-1.0) ;
  23.  
  24. int max4(int a, int b, int c, int d){
  25. return max(max(a,b),max(c,d));
  26. }
  27. int min4(int a, int b, int c, int d){
  28. return min(min(a,b),min(c,d));
  29. }
  30. int mx[][][][], mi[][][][], fac[];
  31. int RMQmx(int x1, int y1, int x2, int y2, int m){
  32. int k=(int)(log((double)m)/log(2.0));
  33. 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]);
  34. }
  35. int RMQmi(int x1, int y1, int x2, int y2, int m){
  36. int k=(int)(log((double)m)/log(2.0));
  37. 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]);
  38. }
  39. int n, m, q, t, u, v;
  40. int main(){
  41. // freopen("in.txt","r",stdin);
  42. // freopen("out.txt","w",stdout);
  43. for(int i=;i<;i++)
  44. fac[i]=(<<i);
  45. while(~scanf("%d %d %d",&n,&m,&q)){
  46. for(int i=;i<=n;i++)
  47. for(int j=;j<=n;j++){
  48. scanf("%d",&t);
  49. mx[i][j][][]=t;
  50. mi[i][j][][]=t;
  51. }
  52. int k=(int)(log((double)n)/log(2.0));
  53. // [x][y][1<<e][1<<f]
  54. for(int e=;e<=k;e++)
  55. for(int f=;f<=k;f++)
  56. for(int i=;i+fac[e]-<=n;i++)
  57. for(int j=;j+fac[f]-<=n;j++){
  58. 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-]);
  59. 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-]);
  60. }
  61. while(q--){
  62. scanf("%d %d",&u,&v);
  63. printf("%d\n",RMQmx(u,v,u+m-,v+m-,m)-RMQmi(u,v,u+m-,v+m-,m));
  64. }
  65. }
  66. return ;
  67. }

POJ_2019_Cornfields的更多相关文章

随机推荐

  1. 使用Ajax long polling实现简单的聊天程序

    关于web实时通信,通常使用长轮询或这长连接方式进行实现. 为了能够实际体会长轮询,通过Ajax长轮询实现了一个简单的聊天程序,在此作为笔记. 长轮询 传统的轮询方式是,客户端定时(一般使用setIn ...

  2. weblogic创建域生产模式,输入用户名闪退

    weblogic创建域,生产模式,报错 <2017-12-29 下午04时53分59秒 CST> <Info> <Security> <BEA-090065& ...

  3. Data Guard启动实时日志应用

    1. REDO数据实时应用 启动实时应用的优势在于,REDO数据不需要等待归档完成,接收到即可被应用,这样执行角色切换时,操作能够执行得更快,因为日志是被即时应用的. 要启动实时应用也简单,前提是St ...

  4. Libjingle库 综述

    国内现在很多语音聊天工具都是基于TURN方式实现的,包括YY.AK等等,这种方式对于服务器的性能要求很高,而且在用户量增大的时候,服务器压力也会越来越大,用户的语音质量也会受到很大影响.而基于P2P方 ...

  5. springboot 集成elasticsearch

    In this article, we will discuss about “How to create a Spring Boot + Spring Data + Elasticsearch Ex ...

  6. intellij中使用git插件将项目上传到码云

    参考帖子: git上传本地Intellij idea 项目到码云的git仓库中(评论中有彩蛋,一定要看) IntelliJ-IDEA和Git.GitHub.Gitlab的使用 使用idea上传项目到码 ...

  7. PHP 简易导出excel 类解决Excel 打开乱码

    <?php class exportCsv{ //列名 protected $_column = array(); protected $_reg = array(); public $ret ...

  8. PHP 函数引用传值

    <?php /* * @1 $arr = array_fill(1,100,'bbb'); echo memory_get_usage()."<br>"; fun ...

  9. linux-find【递归搜索文件名】

    1 命令格式: $ find <指定目录> <指定条件> <指定动作>   最常见示例:查找文件名(忽略大小写) $find . -iname sqlquery.j ...

  10. 《转》python学习(10)-集合

    转自 http://www.cnblogs.com/BeginMan/p/3160565.html 一.目录 1.集合概述 2.关于集合的操作符.关系符号 3.集合的一系列操作(添加.更新.访问.删除 ...