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安装telnet

    安装环境:CentOS 6.4    上篇已经讲述了memcached的安装,现在要测试Memcached功能的时候,需要使用到telnet服务.于是就有了本篇.   一.安装telnet 1.检测t ...

  2. jQuery的无new实例化

    我只能说想法很好,设计的巧妙.看代码: var jQuery = function( selector, context ) { //执行了init函数并返回jQuery实例 return new j ...

  3. Wordpress 3.5.1的debug流水账

    一个合作网站是基于Wordpress做的, 不能正常使用FeedWordPress这个插件, 对方没有开发者, 只有一个类似于美工和编辑的人, 因为我在本地使用同样的版本(Wordpress 3.5. ...

  4. Unity架构有点乱

    1,没有合理的将公共的东西归入到基类中,而是分散到子类中,有许多重复. 比如 enbled的变量本应该是所有component所共有的一个属性,应该写在component.然而却发现并非这样,enbl ...

  5. wooyun本地数据抓取

    ---- #-*-coding:utf-8-*- import re import urllib import MySQLdb import time from urllib import unquo ...

  6. iBatis.Net(C#)SQL数据映射

    转载请注明 http://www.cnblogs.com/13590/archive/2013/03/01/2938126.html 摘要:本文探讨了iBatis.Net框架的XML数据映射文件各配置 ...

  7. BibTex参考文献制作

    &1 制作ref.bib文件 在必应学术或者谷歌学术中搜索文章,这里以我硕士的老板和师兄的一篇文章为例: 两种搜索引擎几乎都差不多,一丢丢区别在下面会讲,点击上图中被引数的旁边那个“引用”按钮 ...

  8. Java的动态代理(dynamic proxy)

    什么是动态代理(dynamic proxy) 动态代理(以下称代理),利用Java的反射技术(Java Reflection),在运行时创建一个实现某些给定接口的新类(也称“动态代理类”)及其实例(对 ...

  9. ios 消息推送流程 转载

    iOS开发:推送通知简述及开发实践热度 1已有 706 次阅读 2013-10-15 09:23 |个人分类:经验之谈|系统分类:ios| IOS, 推送一.关于推送通知 推送通知,也被叫做远程通知, ...

  10. Jenkins进阶系列之——14配置Jenkins用户和权限

    今天给大家说说使用Jenkins专有用户数据库的配置,和一些常用的权限配置. 配置用户注册 在已运行的Jenkins主页中,点击左侧的系统管理—>Configure Global Securit ...