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. Ember模板中的操作指向

    模板中的链接操作指向有三个地方,该模板对应的控制器和路由以及视图,默认是先跳转到控制器,如果控制器里没有定义模板中动作的方法,就去该模板对应的路由里找,如果还没找到,就去父级路由找,直到顶级路由,如果 ...

  2. KVM虚拟机网络基础及优化说明

    一个完整的数据包从虚拟机到物理机的路径是: 虚拟机--QEMU虚拟网卡--虚拟化层--内核网桥--物理网卡 KVM的网络优化方案,总的来说,就是让虚拟机访问物理网卡的层数更少,直至对物理网卡的单独占领 ...

  3. javascript从url中获取请求参数

    function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)( ...

  4. wid是一个字符串 必须转化成整型

    wid是一个字符串 必须转化成整型

  5. 06Spring_使用注解配置bean对象

    Spring注解开发需要jar包 和 xml开发 一样的 ! 第一步: 新建项目, 导入jar包(就是前一篇文章里面的那几个核心jar包) 第二步: 在需要spring创建对象类上面 添加@Compo ...

  6. MS-SQL SERVER单列合并的四种常用方法

    /* 原始数据 ID Name ----------- -------------------- 1 吕布 2 张飞 3 赵云 结果 ----------------- 吕布,张飞,赵云 */ )) ...

  7. LINQ to Entities 查询语法

    转自: http://www.cnblogs.com/asingna/archive/2013/01/28/2879595.html 实体框架(Entity Framework )是 ADO.NET  ...

  8. 20145222黄亚奇《Java程序设计》第2周学习总结

    教材学习内容总结 类型: 整数:short(占2字节).int(4).long(8). 浮点数:float(4).double(8) 字符:char(2) 布尔:boolean类型表示true与fal ...

  9. CS:APP2e Y86处理器模拟器∗指南

    CS:APP2e Y86处理器模拟器∗指南 Randal E.Bryant David R. O'Hallaron 2013年7月29日 本文档描述了处理器模拟器,伴随的表示在第4章Y86处理器架构的 ...

  10. CAS自定义登录验证方法

    一.CAS登录认证原理 CAS认证流程如下图: CAS服务器的org.jasig.cas.authentication.AuthenticationManager负责基于提供的凭证信息进行用户认证.与 ...