题干:

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

题目大意:

给出一个N*N (N<=250)的方阵,以及K(<=100000)个询问。每次询问如下:以(Xi,Yi)为左上角,边长为B的子方阵中,最大值和最小值的差是多少?注意对于所有的询问,B都是一个定值。

Input

第一行N,B(<=N),K。含义如上。

接下来N行N列的一个矩阵,每个数<=250。

接下来K行表示询问,每行两个数Xi, Yi 表示询问的方阵的左上角。

解题报告:

首先这题做法贼鸡儿多,因为是练模板嘛所以我就选了不是最快的方式。最快的方式好像是单调队列,也能做到回答O1查询。

注意不能先枚举x再枚举j、、我真是个麻瓜、、倍增这里的问题犯过一次了。(我记得是LCA的时候应该先dfs进去然后再处理而我是先处理了然后再dfs子树的)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 300 + 5;
int Log[MAX];
int F[MAX][MAX][22],f[MAX][MAX][22],a[MAX][MAX];
int n,B,K;
PII Min_Max(int x,int y,int z,int q) {int b[4] = {x,y,z,q};sort(b,b+4);return pm(b[0],b[3]);
}
void ST() {for(int j = 1; (1<<j) <=n; j++) {for(int x = 1; x<=n; x++) {int t = 1<<j-1;for(int y = 1; y+(1<<j) - 1 <= n; y++) {F[x][y][j] = Min_Max(F[x][y][j-1],F[x][y+t][j-1],F[x+t][y][j-1],F[x+t][y+t][j-1]).SS;f[x][y][j] = Min_Max(f[x][y][j-1],f[x][y+t][j-1],f[x+t][y][j-1],f[x+t][y+t][j-1]).FF;}}}
}
int solve(int x,int y) {int k = (int)(log(B)/log(2));//Log[B];int mx = Min_Max(F[x][y][k],F[x+B-(1<<k)][y][k],F[x][y+B-(1<<k)][k],F[x+B-(1<<k)][y+B-(1<<k)][k]).SS;int mn = Min_Max(f[x][y][k],f[x+B-(1<<k)][y][k],f[x][y+B-(1<<k)][k],f[x+B-(1<<k)][y+B-(1<<k)][k]).FF;return mx - mn;
}
int main()
{cin>>n>>B>>K;for(int i = 2; i<=n; i++) Log[i] = Log[i>>1] + 1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) scanf("%d",&a[i][j]),F[i][j][0]=f[i][j][0]=a[i][j]; } ST();int x,y;while(K--) {scanf("%d%d",&x,&y);printf("%d\n",solve(x,y));}return 0 ;
}
/*
3 2 10
1 2 3
4 5 5
7 5 5
1 1
1 2
2 1
2 2*/ 

【POJ - 2019】Cornfields(二维st表,模板)相关推荐

  1. 模板 - 数据结构 - ST表 + 二维ST表

    区间最大值,$O(nlogn)$ 预处理,$O(1)$ 查询,不能动态修改.在查询次数M显著大于元素数量N的时候看得出差距. 令 $f[i][j]$ 表示 $[i,i+2^j-1]$ 的最大值. 显然 ...

  2. P2216 [HAOI2007]理想的正方形 ( 二维ST表 )

    题目链接:点击进入 题目 思路 maxx [ i ] [ j ] :左上角坐标 ( i , j ) ,边长为 2 k 2^k 2k 的正方形的最大值 minn [ i ] [ j ] :左上角坐标 ( ...

  3. 视频教程-微信公众平台深度开发v2.0第3季——二维码、模板消息-微信开发

    微信公众平台深度开发v2.0第3季--二维码.模板消息 微信企业号星级会员.10多年软件从业经历,国家级软件项目负责人,主要从事软件研发.软件企业员工技能培训.已经取得计算机技术与软件资格考试(软考) ...

  4. Excel二维交叉表恢复为一维表

    工作中遇到这样的问题,收到Excel二维交叉表(数据透视表的结果,但已经保存为普通Excel),根据业务需要将其再次恢复为一维表. 如果数据量小的话,简单的复制剪切就可以了,如果数据量大的话,那么太繁 ...

  5. 基本概念 - 二维关系表

    文章目录 一.二维关系表 (一)法人表 (二)可能涉及的问题 (三)什么表格才是二维关系表? (四)非规范二维表存在的异常情况 1.异常情况 (1)插入异常 (2)删除异常 (3)更新异常 (4)数据 ...

  6. Wpf DataGrid 绑定Dynamic (二维数据表)

    最近有一个需求,需要根据用户数据动态生成二维数据表,绑定到表格后,用户再次编辑数据,最后再将编辑过的数据,生成类型数据,存入数据库. 仔细分析过后,觉得最适合的方式,莫过于给DataGrid绑定Dyn ...

  7. C#实现二维码打印模板(PDF格式)

    做项目过程中,有一个需求,是要按照模板样式生成某个设备信息的二维码,并打印可以打印出来,于是用了以下代码实现: 1.需要按照模板打印,例如需要打印成这种布局: 可以看到,布局内不仅有文字,对象参数,二 ...

  8. 牛妹吃豆子(二维前缀和模板,修改+求和)

    调了半天忘了要求两次前缀和了. 先对前缀和数组进行修改, 第一次求前缀和得到的是修改后的原矩阵,再求一次前缀和得到二维前缀和,然后根据容斥定理求区间的二维前缀和即可 #include<iostr ...

  9. 7、Power Map—实例:添加二维数据表以及批注

    再次添加场景,这次我们的目的是找出各个产品销量最高的省份,添加批注. 首先复制场景,去掉时间的条件(这个场景不再重复播放). 添加二维图表. 根据最高销量的省份,添加批注. 其他设置雷同.

最新文章

  1. busybox filesystem httpd php-5.5.31 sqlite3 webserver
  2. 经典算法题 -- 判断单链表是否成环及寻找成环节点
  3. numpy基础(part7)--多项式拟合
  4. C# 快速高效率复制对象另一种方式 表达式树
  5. linux脚本登录启动失败,linux-从bash脚本启动进程失败
  6. 未解决:maven:Fatal error compiling: 无效的标记: -arg
  7. java连接数据库电商平台_Java数据库中台项目,电商,CMS轻松实现,包含数据库源文件...
  8. 项目管理 之技术管理
  9. VOC2007-2012数据集
  10. 2020Spatial-Temporal Graph Convolutional Network for Video-based Person Re-identification论文笔记(时空图卷积)
  11. 计算机专业排名2017教育部,软件工程专业大学排名最新版(教育部2017学科排名数据整理)...
  12. iis服务器跳转网页怎么设置,使用IIS管理器实现域名跳转
  13. 计算机丢失opencv_world300.dll文件
  14. 使用idea起服务,起好久都起不起来
  15. 信息化时代,,生产制造管理系统该具备哪些功能?
  16. 多可文档管理迁移说明
  17. Linaro ubuntu for arndale octa烧写步骤
  18. STP格式公仔3D建模,IGS格式模型设计中文讲解视频教程
  19. Python中float类型、float32类型和float64类型的表示精度,所需内存及其之间的转换
  20. There are test failures.Please refer to ……for the individual test results.

热门文章

  1. java 私有变量访问_Java - 访问私有实例变量
  2. 回溯——伯努利装错信封问题
  3. 3485. 最大异或和
  4. python代码编码成jni_python 设置文件编码格式的实现方法
  5. vant实现下拉刷新和上拉加载_微信小程序 - 实现下拉刷新、上拉加载
  6. ubuntu 修改用户名和计算机名称
  7. 关于linux kernel编译的几项关键点:
  8. asterisk语音信箱voicemail.conf
  9. 进程控制2--exec族
  10. linux内核设计与实现 怎么读,《Linux内核设计与实现》读书笔记(一)