【POJ - 2019】Cornfields(二维st表,模板)

题干:

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*/ 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439895.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

虚拟机安装Linux(vmware + ubuntu)

VMWare 提取码&#xff1a;7zph 建议官网下载比较新&#xff0c;还快一点 https://www.vmware.com/products/workstation-pro.htmlubantu 下载地址 安装过程都差不多可以参考 VMware下安装Ubuntu系统图文详细教程_master-CSDN博客_vmware安装ubuntu系统 出现蓝屏问题可以参考…

Apollo进阶课程⑱丨Apollo感知之旅——传感器标定

目录 传感器标定 标定的目的 传感器标定算法 标定案例解析 3D标定间制作 Cmaera-to-Camera外参标定 Lidar-to-Camera外参标定 Lidar-to-Lidar外参标定 Lidar内参标定 Lidar-to-GPS外参标定 自然场景的Lidar-to-Camera外参标定 自然场景的Bifocal Camera外参标定 C…

一步步编写操作系统 15 CPU与外设通信——IO接口,下

既然都说到IO接口了&#xff0c;不知道各位有没有疑问&#xff0c;cpu是怎样访问到IO接口呢&#xff1f;肯定得有个链路吧&#xff1f;什么&#xff1f;有隐约听到有同学开玩笑说&#xff1a;cpu用无线访问其它设备。哈哈&#xff0c;不知道各位听说过没有&#xff0c;无线的终…

Telnet端口连接Linux服务器失败

在ubuntu写了个服务器端口号是666 &#xff0c;ip地址是192.168.96.129 在windows用telnet无法连接上 首先检查windows telnet服务是否打开 Windows 10操作系统上使用telnet命令&#xff08;图文&#xff09;_时间-CSDN博客_windows使用telnet命令 测试网络是否通&#xff1a;…

*【2019牛客暑期多校训练营(第三场)- G】Removing Stones(分治)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/883/G 来源&#xff1a;牛客网 Summer vacation is coming and Mark has returned home from his university having successfully survived the exam week. Today, he is very bored. So his frien…

重磅 | 完备的 AI 学习路线,最详细的资源整理!

本文转自微信公众号&#xff1a;Datawhale&#xff08;强烈推荐&#xff09; 原创&#xff1a; AIUnion Datawhale 今天 【导读】 本文由知名开源平台&#xff0c;AI技术平台以及领域专家&#xff1a;Datawhale&#xff0c;ApacheCN&#xff0c;AI有道和黄海广博士联合整理贡献…

一步步编写操作系统 16 显卡概述

之前我们的mbr中我们刚刚向屏幕输出了“1 MBR”这几个字符&#xff0c;这种喜悦还没有过去&#xff0c;我就要给大家泼冷水了&#xff1a;这种打印字符的方法马上就用不了啦。 mbr是运行在实模式下&#xff0c;所以在实模式下也可以用bios的0x10中断打印字符串&#xff0c;这是…

Windows/Linux 下使用telnet发送消息

Windows下使用telnet 1.首先打开cmd命令行连接上服务器端口 连不上可以参考这篇 Telnet端口连接Linux服务器失败_m0_46480482的博客-CSDN博客 telnnt <ip地址> <端口号> 2. 连接成功后&#xff0c;会发现是一片黑的 按住 ctrl ] 可以招出提示 输入 &#x…

【2019牛客暑期多校训练营(第六场)- D】Move(随机化二分)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/886/D 来源&#xff1a;牛客网 After the struggle of graduating from college, TangTang is about to move from a student apartment to his new home. TangTang has n items to move, the i-th …

Apollo进阶课程⑲丨Apollo感知之旅——感知算法

目录 点云感知 启发式方法&#xff1a;NCut 深度学习方法&#xff1a;CNNSeg 视觉感知 CNN检测 CNN分割 后处理 红绿灯感知 基于深度学习的红绿灯感知模块 Radar感知 超声波感知 原文链接&#xff1a;进阶课程⑲丨Apollo感知之旅——感知算法 感知是自动驾驶的第一环…

一步步编写操作系统 17 显存,显卡,显示器 上

为了能够看到图像&#xff0c;我们需要显示器。无论是哪种显示器&#xff0c;它都是由显卡来控制的&#xff0c;我们没必要了解液晶显示器和普通CRT显示器的差别。无底是哪种显卡&#xff0c;它提供给我们的可编程接口都是一样的&#xff1a;IO端口和显存。 显存是由显卡提供的…

C++ socket网络编程笔记(服务端1)

1. 创建一个信箱 int sock; // 创建一个信箱 sock socket(AF_INT,SOCK_STREAM,0) 2. 创建一个标签&#xff0c;写上地址和端口号 struct sockaddr_in server_addr; // 创建一个标签server_addr.sin_family AF_INET; // 标签--协议族 (AF_INET表示IPV4)ser…

动手学PaddlePaddle(0):新版本PaddlePaddle安装

目录 0.引言 1.环境 2.Windows下安装 安装Python 安装PaddlePaddle 0.引言 今天介绍如何安装新版本的PaddlePaddle&#xff0c;现在最新版的PaddlePaddle是指Fluid版&#xff0c;Fluid可以让用户像Pytorch和TensorFlow Eager Execution一样执行程序&#xff0c;也就是说P…

一步步编写操作系统 18 操作显卡,显存,显示器 下

接上回&#xff0c;大家看下显卡各种模式的内存分布。 各外部设备都是通过软件指令的形式与上层接口通信的&#xff0c;显卡&#xff08;显示适配器&#xff09;也不例外&#xff0c;所以它也有自己的bios。位置是0xC0000到0xC7FFF。显卡支持三种模式&#xff0c;文本模式、黑白…

【2019牛客暑期多校训练营(第六场)- J】Upgrading Technology(dp)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/886/J?&headNavacm&headNavacm&headNavacm&headNavacm 来源&#xff1a;牛客网 Rowlet is playing a very popular game in the pokemon world. Recently, he has encountered a p…

VMware 安装VMware Tools

想要在linux和windows之间复制粘贴&#xff0c;把之前一直没有下的vmwaretools的下载过程记录一下。 1.左上角菜单 ->虚拟机 ->安装 vmware tools (我已经点过了所以是取消安装) 2.桌面多了一个VMware tools &#xff0c;点进去看一下位置&#xff0c;复制一下tar.gz的文…

Apollo进阶课程⑳丨Apollo感知之旅——机器学习与感知的未来

目录 1机器学习 可解释性是否需要 其它算法 2感知的未来 Sensor迭代 深度学习仿真数据AI芯片 智能交通设施 3思考 原文链接&#xff1a;进阶课程⑳丨Apollo感知之旅——机器学习与感知的未来 自动驾驶感知中的机器学习最大问题在于系统对模块的要求与普通的机器学习不同…

一步步编写操作系统 19 改进MBR,直接操作显卡

到目前为止&#xff0c;说了一部分有关显存的内容&#xff0c;这对于一般的输出来说已经足够了&#xff0c;下面咱们可以尝试写显存啦。我们将之前MBR改造一下&#xff0c;保留滚屏的操作&#xff0c;只修改有关输出的部分。即把通过bios的输出改为通过显存&#xff0c;你会发现…

C++ socket网络编程笔记(服务端2)

接上篇 C socket网络编程笔记(服务端1)_m0_46480482的博客-CSDN博客 1. 用一个while循环来持续监听信道消息 int done 1; while(done) {.... } 2. 创建一个客户信箱来接受收到的消息 int client_sock;3. 创建一个客户信息的标签记录信息 struct sockaddr_in client; //…

【2019牛客暑期多校训练营(第五场)- E】independent set 1(最大独立集,状压dp)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/885/E 来源&#xff1a;牛客网 Note: For C languages, the memory limit is 100 MB. For other languages, the memory limit is 200 MB. In graph theory, an independent set is a set of nonadj…