POJ3264(KB7-G RMQ)
Balanced Lineup
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
//2017-05-17
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int N = ;
int a[N], DPmin[N][], DPmax[N][]; void init(int n)
{
for(int j = ; j<=(int)log2(n); j++)
for(int i = ; i<=n; i++){
DPmax[i][j] = DPmax[i][j-];
DPmin[i][j] = DPmin[i][j-];
if(i+(<<j)- <= n){
DPmax[i][j] = max(DPmax[i][j-], DPmax[i+(<<(j-))][j-]);
DPmin[i][j] = min(DPmin[i][j-], DPmin[i+(<<(j-))][j-]);
}
}
} int query(int l, int r)
{
int k = (int)log2(r-l+);
return max(DPmax[l][k], DPmax[r-(<<k)+][k])-min(DPmin[l][k], DPmin[r-(<<k)+][k]);
} int main()
{
int n, q;
while(scanf("%d%d", &n, &q)!=EOF)
{
for(int i = ; i <= n; i++){
scanf("%d", &DPmax[i][]);
DPmin[i][] = DPmax[i][];
}
init(n);
int l, r;
while(q--)
{
scanf("%d%d", &l, &r);
printf("%d\n", query(l, r));
}
} return ;
}
POJ3264(KB7-G RMQ)的更多相关文章
- POJ3264 Balanced Lineup [RMQ模板]
题意:有n头牛,输入他们的身高,求某区间身高的极值的差(max-min), 用RMQ模板,同时构造求极大值和极小值的两个数组. //poj3264 #include <iostream> ...
- [POJ3264]Balanced Lineup(RMQ, ST算法)
题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Give y ...
- HDU 5869 Different GCD Subarray Query
离线操作,树状数组,$RMQ$. 这个题的本质和$HDU$ $3333$是一样的,$HDU$ $3333$要求计算区间内不同的数字有几个. 这题稍微变了一下,相当于原来扫描到$i$的之后是更新$a[i ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- 【POJ3264】Balanced Lineup(RMQ)
题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...
- RMQ、POJ3264
这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh ...
- (WAWAWAWAWAWAW) G. Periodic RMQ Problem
没有联通门 : Codeforces G. Periodic RMQ Problem /* Codeforces G. Periodic RMQ Problem MMP 什么动态开点线段树啊 ... ...
- POJ3264/RMQ
题目链接 /* 询问一段区间内的元素差值最大是多少,用RMQ维护一个最大值和一个最小值,相减即可. */ #include<cstdio> #include<cstring> ...
随机推荐
- Spring学习笔记3——消息队列(rabbitmq), 发送邮件
本节的内容是用户注册时,将邮箱地址先存入rabbitmq队列,之后返回给用户注册成功:之后消息队列的接收者从队列中获取消息,发送邮件给用户. 一.RabbitMQ介绍 如果之前对rabbitm ...
- MyBatis全局配置文件标签详解
一.全局配置文件结构 configuration 配置 properties 属性:可以加载properties配置文件的信息 settings 设置:可以设置mybatis的全局属性 typeAli ...
- iOS数据持久化--数据库
一.简介 1.iOS常用的5中存储方式 (1)plist (2)preference(用户属性) (3)归档 (4)数据库 (5)core data 其中(1)(2) (3) 都只能存储小型的数据,因 ...
- WebDriver高级应用实例(4)
4.1操作web页面的滚动条 被测网页的网址: http://v.sogou.com Java语言版本的API实例代码 import org.testng.annotations.Test; impo ...
- Spring Boot 中使用 Jedis 及 Lettuce的对比
首先,同样的程序,采用不同方式的Redis连接方式. defautl : 默认,0配置 ,也就是走的是 lettuce 单通道方式. 端口:8081 jedis : 使用Jedis 连接池. ...
- One difference between AngularJS' $location and window.location
Recenently, I encountered a problem. Client side code is: $http({ url: "/api/runtimelicense&quo ...
- LaTeX技巧:如何高效地将LaTeX代码转为Word公式
LaTeX转换为word这么刺激的做法是很多国内用户咨询的问题,有些用户只得把LaTeX排版好的转换为word,这里给大家推荐几个用法,或许能够帮助到诸位. 第一种方法:MathType的Toggle ...
- 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 0、学习目标
Learn to build a neural network with one hidden layer, using forward propagation and backpropagation ...
- 【VC版】如何获取其他进程中ListView控件中的内容
如果需要C#版的,可以看下我之前写的:C#如何获取其他程序ListView控件中的内容 获取其他进程的数据需要使用到以下几个函数: VirtualAllocEx() VirtualFreeEx() W ...
- 全网最全的Windows下Anaconda2 / Anaconda3里正确下载安装用来向微信好友发送消息的itchat库(图文详解)
不多说,直接上干货! Anaconda2 里 PS C:\Anaconda2\Scripts> PS C:\Anaconda2\Scripts> pip.exe install itch ...