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. VIM技巧(1)

    VIM技巧(1) 替换 36s/^\(.* = \)entity.\(.*\)$/\1this.GetShowName("\2",\2); 删除空行 %g/^$/d %g/^\s* ...

  2. Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

    题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...

  3. Science上发表的超赞聚类算法(转)

    作者(Alex Rodriguez, Alessandro Laio)提出了一种很简洁优美的聚类算法, 可以识别各种形状的类簇, 并且其超参数很容易确定. 算法思想 该算法的假设是类簇的中心由一些局部 ...

  4. 解决安装SQL Server2008失败的问题

    安装SQL Server2008时遇到"2008安装错误  必须重新启动计算机才能安装 SQL Server". 解决办法:HKEY_LOCAL_MACHINE\SYSTEM\Cu ...

  5. Java中的递归原理分析

    解释:程序调用自身的编程技巧叫做递归.        程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用 ...

  6. CESAsia:英特尔RealSense3D实感技术亮眼

    每年CES展会上都会有许多新奇的.更具创意的产品和创新技术亮相,而作为全球科技盛会之一的CES Asia也不例外.在CES Asia2016展会上,英特尔(Intel)可谓是有备而来,带着旗下支持Re ...

  7. UML图示与代码对照

    本文转载:http://www.cnblogs.com/iamlilinfeng/archive/2012/08/29/2662740.html 一.类继承 1 public class Father ...

  8. 教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值

    (26)Don’t underestimate the value of the UI when tackling performance problems 招数26: 解决性能问题时不要低估UI的价 ...

  9. CCLabelTTF、CCLabelAtlas和CCLabelBMFont的区别

    转自:http://blog.sina.com.cn/s/blog_67a5e47201018tj8.html 在Cocos2d以及Cocos2d-x中,我们经常会用到CCLabelTTF以及CCLa ...

  10. maven for eclipse在线安装

    在线安装 地址变了下面的: http://download.eclipse.org/technology/m2e/releases      Eclipse Indigo安装Maven插件Maven ...