Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 44720   Accepted: 20995
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

ST算法求区间内最值

 #include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int mxn=;
int n,Q;
int h[mxn];
int fmx[mxn][],fmi[mxn][];//f[i][j]表示从i开始到i*(1<<j)范围内的目标值
int ST(int a,int b){//ST算法,倍增求区间最值
int i,j;
for(i=;i<=n;i++){//自身
fmx[i][]=fmi[i][]=h[i];
}
int k=(int)(log(n*1.0)/log(2.0));//k=以2为底n的对数
for(i=;i<=k;i++){//第一层为次数
for(j=;j<=n;j++){//第二层为范围
fmx[j][i]=fmx[j][i-];
if(j+(<<(i-)) <=n)
fmx[j][i]=max(fmx[j][i],fmx[j+(<<(i-))][i-]);
fmi[j][i]=fmi[j][i-];
if(j+(<<(i-)) <=n)
fmi[j][i]=min(fmi[j][i],fmi[j+(<<(i-))][i-]);
}
}
return ;
}
int ansmx(int a,int b){//查找最大值
int k=(int)(log(b-a+1.0)/log(2.0));
return max(fmx[a][k],fmx[b-(<<k)+][k]);
}
int ansmi(int a,int b){//查找最小值
int k=(int)(log(b-a+1.0)/log(2.0));
return min(fmi[a][k],fmi[b-(<<k)+][k]);
}
int main(){
scanf("%d%d",&n,&Q);
int i,j;
for(i=;i<=n;i++){
scanf("%d",&h[i]);
}
int a,b;
ST(,n);
for(i=;i<=Q;i++){
scanf("%d%d",&a,&b);
printf("%d\n",ansmx(a,b)-ansmi(a,b));//输出询问区间内最大值和最小值的差
}
return ;
}

POJ3264 Balanced Lineup的更多相关文章

  1. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  2. POJ3264 Balanced Lineup 【线段树】+【单点更新】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 ...

  3. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

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

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

  5. poj3264 balanced lineup【线段树】

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  6. kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  7. POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)

    http://poj.org/problem?id=3264 Time Limit: 5000MS     Memory Limit: 65536K Description For the daily ...

  8. POJ3264 Balanced Lineup [RMQ模板]

    题意:有n头牛,输入他们的身高,求某区间身高的极值的差(max-min), 用RMQ模板,同时构造求极大值和极小值的两个数组. //poj3264 #include <iostream> ...

  9. [POJ3264]Balanced Lineup(RMQ, ST算法)

    题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...

随机推荐

  1. Linux安装Redis

    环境:Centos 6.2 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了memcached的不足,它支持存储的value ...

  2. mysql怎么查询前10条数据?

    mysql 没有top的用法.取而代之的是limit语法为:limit m,n省略n就可以得到你要的效果了. select * from table1 order by column desc  li ...

  3. Apache Shiro(安全框架)

    当前常用流行的安全框架主要有两种:一个是Apache Shiro:另一个是Springsource. 现在介绍一下apache shiro: 既然是安全框架,解决的肯定是权限的 控制.所谓权限是指:用 ...

  4. java 15 - 8 集合框架(并发修改异常的产生原因以及解决方案)

    问题?   我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现.  面试题: Concu ...

  5. linux运维中的命令梳理(一)

    在linux日常运维中,我们平时会用到很多常规的操作命令. 下面对常用命令进行梳理: 命令行日常系快捷键(不分大小写)CTRL + A 移动光标到行首CTRL + E 移动光标到行末CTRL + U ...

  6. Linux下检测IP地址冲突及解决方法

    问题说明:在公司办公网内的一台物理机A上安装了linux系统(ip:192.168.9.120),在上面部署了jenkins,redmine,svn程序.由于是在办公网内,这台机器和同事电脑都是在同一 ...

  7. JavaScript---Ajax和函数回调,异步编程

    一 Ajax 函数的定义  :  Asynchronous JavaScript and XML(异步的 JavaScript 和 XML),无刷新的从服务器读取数据,可以在不重新加载整个网页的情况下 ...

  8. iOS原生地图开发详解

    在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...

  9. Protocol Buffer多态

    java中有多态的概念,protobuf本身没有多态的概念,但是它有一个扩展的概念. 以聊天消息为例,先看下面这个类图,基类是ChatMessage,子类TextMessage和ImageMessag ...

  10. CLR Profiler

    检查c#代码内存泄露工具-CLR Profiler 大家都知道.net有一套自己的内存(垃圾)回收机制,除非有一些数据(方法)长期占有内存不随着垃圾回收功能而释放内存,这样就造成了我们经常说的内存泄露 ...