Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 45777   Accepted: 21499
Case Time Limit: 2000MS

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

Line 1: Two space-separated integers, N and Q
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

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

Source

题解:
求区间最大值-最小值
 
RMQ AC代码
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 50010
int n,m,a[N],f[N][],g[N][];
inline int read(){
register int x=,f=;
register char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void RMQ(){
for(int j=;j<=;j++){
for(int i=;i+(<<j)-<=n;i++){
f[i][j]=max(f[i][j-],f[i+(<<j-)][j-]),
g[i][j]=min(g[i][j-],g[i+(<<j-)][j-]);
}
}
}
inline int query(int i,int j){
int k=log(j-i+)/log();
return max(f[i][k],f[j-(<<k)+][k])-min(g[i][k],g[j-(<<k)+][k]);
}
int main(){
n=read();m=read();
for(int i=;i<=n;i++) g[i][]=f[i][]=read();
RMQ();
for(int i=,l,r;i<=m;i++) l=read(),r=read(),printf("%d\n",query(l,r));
return ;
}

线段树代码,自己写吧。

poj3264的更多相关文章

  1. 【poj3264】 Balanced Lineup

    http://poj.org/problem?id=3264 (题目链接) 题意 给出序列,求区间最大值-最小值 Solution 无修改,询问较多,ST表水一发. ST算法(Sparse Table ...

  2. ST表入门学习poj3264 hdu5443 hdu5289 codeforces round #361 div2D

    ST算法介绍:[转自http://blog.csdn.net/insistgogo/article/details/9929103] 作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 方 ...

  3. st表(poj3264)

    st表很像线段树,但线段树既能查询和修改,而st表只能查询. 首先我们先用二维数组建立一个表,st[i][j]表内存的是从第i位开始1<<j范围内的best(st[i][j-1],st[i ...

  4. RMQ、POJ3264

    这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh ...

  5. poj3264(Sparse-Table 算法模板)

    poj3264 题意 询问区间最大值最小值之差. 分析 dp_max[i][j] 表示以 i 为起点,长度为 \(2^j\) 的区间最大值. 利用递推预处理出区间最大值最小值. code #inclu ...

  6. 【POJ3264】Balanced Lineup(RMQ)

    题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...

  7. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  8. poj3264(简单线段树)

    题目链接:https://vjudge.net/problem/POJ-3264 题意:线段树简单应用题,区间查询最大值和最小值的差. 思路:用线段树维护区间的最大值和最小值即可. AC代码: #in ...

  9. Balanced Lineup poj3264 线段树

    Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最 ...

  10. 几道简单的线段树入门题 POJ3264&&POJ3468&&POJ2777

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40687   Accepted: 19137 ...

随机推荐

  1. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  2. nyoj 10 skiing(记忆化搜索)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  3. HL7及PIX相关的测试工具

    最近在开发PIX项目时需要一些工具, 比如PIX各个Actor的测试工具, HL7消息的验证工具等等. 下面列下我找见的几个 必备工具. 1. http://hit-testing.nist.gov: ...

  4. 【Java】多线程冲突解决——同步锁

       转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827547.html    解决并行冲突最有效的方法就是加同步锁,主要有以下几种方法:   1:动态方法 ...

  5. ZOJ 3633 Alice's present 倍增 区间查询最大值

    Alice's present Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...

  6. Lua学习教程之 可变參数数据打包与解包

    利用table的pack与unpack进行数据打包与解包.測试代码例如以下: print("Test table.pack()----------------"); functio ...

  7. iOS开发——动画篇Swift篇&常用动画总结

    UIView动画: UIView动画时最基本的动画,是直接对我们界面上控件进行简单的动画效果实现,如果你只需要用到一些简单的效果,那么这个很适合你,关于UIView动画实现恨简单, UIKit直接将动 ...

  8. Online DDL与pt-online-schema-change

    http://seanlook.com/2016/05/24/mysql-online-ddl-concept/ http://seanlook.com/2016/05/27/mysql-pt-onl ...

  9. oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解

    我们经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用SELECT INTO 和 INSERT INT ...

  10. dsp与sem的互补以及技术实现

    SEM翻译过来叫搜索引擎营销,个人认为是随着搜索引擎竞价排名出现的一个行业,已经有了好多年的历史,做sem的公司这些年里手里应该都积攒着大量的cookie,关键词等与SEM和追踪相关的数据,这些数据现 ...