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> ...
随机推荐
- VS 快捷键设置
工具 --> 选项 --> 环境 --> 键盘
- postgresql 简单入门
安装 https://www.postgresql.org/download/linux/redhat/yum install https://download.postgresql.org/pub/ ...
- 移动端测试接口--Fiddler抓包工具
Fiddler抓包工具是一款免费且功能强大的数据包抓取软件.它通过代理的方式获取程序http通讯的数据,可以用其检测网页和服务器的交互情况,能够记录所有客户端和服务器间的http请求,支持监视.设置断 ...
- pythonweb框架Flask学习笔记05-简单登陆
源代码从下链接引用:https://www.cnblogs.com/felixwang2/p/9261493.html 我使用的是python3.6 在运行代码的时候遇到了以下问题 session[' ...
- Java操作word文档使用JACOB和POI操作word,Excel,PPT需要的jar包
可参考文档: http://wibiline.iteye.com/blog/1725492 下载jar包 http://download.csdn.net/download/javashixiaofe ...
- C# 算法系列一基本数据结构
一.简介 作为一个程序员,算法是一个永远都绕不过去的话题,虽然在大学里参加过ACM的比赛,没记错的话,浙江赛区倒数第二,后来不知怎么的,就不在Care他了,但是现在后悔了,非常的后悔!!!如果当时好好 ...
- 阿里云负载不支持 WebSocket 协议与 WSS 和 Nginx 配置问题
WebSocket 是 HTML5 下一种新的协议.它实现了浏览器与服务器全双工通信,能更好的节省服务器资源和带宽并达到实时通讯的目的.它与HTTP一样通过已建立的TCP连接来传输数据,但是它和HTT ...
- Docker概念学习系列之为什么使用docker?(3)
不多说,直接上干货! 见[博主]撰写的https://mp.weixin.qq.com/s/FFSIOSecVdAr_aSDIFZwSA Docker容器虚拟化的优点: (1)环境隔离: 通过cgro ...
- JavaScript -- URL编码
----- 010-escape.html ----- <!DOCTYPE html> <html> <head> <meta http-equiv=&quo ...
- mycat ER 分片表
<table name="order" dataNode="dn$1-32" rule="mod-long"> <chil ...