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. cannot load such file -- openssl

    [test@localhost usr]$ /usr/local/ruby/bin/gem install bundler ERROR: Loading command: install (LoadE ...

  2. codeforces 624A Save Luke(水题)

    A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. HTML5实现动画三种方式

    编者注:作者以一个运动的小车为例子,讲述了三种实现HTML5动画的方式,思路清晰,动画不仅仅是canvas,还有css3和javascript.通过合理的选择,来实现最优的实现. PS:由于显卡.录制 ...

  4. UVaLive 7362 Farey (数学,欧拉函数)

    题意:给定一个数 n,问你0<= a <=n, 0 <= b <= n,有多少个不同的最简分数. 析:这是一个欧拉函数题,由于当时背不过模板,又不让看书,我就暴力了一下,竟然A ...

  5. Caused by: Cannot locate the chosen ObjectFactory implementation: spring - [unknown location] 的解决方式

    1.添加网上所说的struts2 plugin jar包 2. <!-- Struts2配置 --> <filter> <filter-name>struts2&l ...

  6. setbuf和freopen

    看memcached代码的时候学习了一个api,setbuf,可以设置文件流的缓冲区.        #include <stdio.h> void setbuf(FILE *stream ...

  7. 剑指OFFER之重建二叉树(九度OJ1385)

    题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7 ...

  8. Win7激活后添加grub引导Linux最简单方法

    因为Win7(Vista同理)的激活方式是通过grub摸你OEM的Slic信息,所以主引导分区MBR被这个grub占用,以此才能激活WIn7.但是如果想同时安装Linux在别的分区,就会产生问题:gr ...

  9. contiki-main.c 中的process系列函数学习笔记 <contiki学习笔记之六>

    说明:本文依然依赖于 contiki/platform/native/contiki-main.c 文件. ---------------------------------------------- ...

  10. mac上java开发环境

    刚刚入手 macbook 还是 按照window 的方式,下载java,安装,配置环境变量,下载maven安装配置 等等....非常繁琐.. but 在mac上不用这么复杂...利用 brew 命令去 ...