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. Eclipse快捷键列表大全

    from: http://hi.baidu.com/lzycsd/item/c6febccceacc173c44941684 from: http://www.open-open.com/bbs/vi ...

  2. 由索引节点(inode)爆满引发的问题

    关于磁盘空间中索引节点爆满的问题还是挺多的,借此跟大家分享一下: 一.发现问题在公司一台配置较低的Linux服务器(内存.硬盘比较小)的/data分区内创建文件时,系统提示磁盘空间不足,用df -h命 ...

  3. 如何将NTFS格式的移动硬盘挂接到Mac OS上进行读写(Read/Write)操作

    现在硬盘便宜,很多同学都有移动硬盘,如果你同时使用Windows与Mac OS的话,移动硬盘最好不要使用NTFS文件系统,否则在Mac OS上,你只能读你的移动硬盘,不能写. 但是实际上的情况是,移动 ...

  4. C#根据IP地址和子网掩码计算广播地址

    using System.Net; /// <summary> /// 获得广播地址 /// </summary> /// <param name="ipAdd ...

  5. 事务的四个特性-ACID

    事务是恢复和并发控制的基本单位.   事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.   原子性(atomicity):一个事务是一个不可分割的工作单位,事务中 ...

  6. JS第一个动画

    描述:让页面中的一个盒子平稳向右移动到指定位置 <div id="div1"> <div> JS代码实现 <script type="tex ...

  7. Asp.net Json数据解析的一种思路

    在日常的编码中,经常会遇到JSON类型的数据,有简单的,也有复杂的.对于简单的,我们可以用正则等匹配,但是一旦遇到复杂的,就比较难办了. 数据分析 目前手头上需要制作一个天气预报功能,现成的接口已经有 ...

  8. [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

    7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...

  9. [转]细说MySQL Explain和Optimizer Trace简介

    在开发过程中,对每个上线的SQL查询指纹(query figerprint)的质量都应有估算:而估算DB查询质量最直接的方法,就是分析其查询执行计划( Query Execution Plan ,即Q ...

  10. python3 入门 (一) 基础语法

    1.编码问题 默认情况下,Python 3源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 也可以为源码文件指定不同的编码,在文件头部加上: # coding=gbk 2.关键字 ...