Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 45243   Accepted: 21240
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来做
也是我的ST第一道题,不错
这道题我两种方法都写过的,比较简单,当做练熟练度
线段树的解法已经写过的,可以很明显的看出,ST的代码比线段树简洁得多
就直接上ST的代码
 #include<iostream>
#include<cstdio>
#include<cmath>
#define maxx 50005
using namespace std;
int maxsum[maxx][],minsum[maxx][],n,Q;
int _max(int a,int b)
{
return a>b?a:b;
}
int _min(int a,int b)
{
return a<b?a:b;
}
int main()
{ while(~scanf("%d %d\n",&n,&Q))
{
for(int i=;i<=n;i++)
{
scanf("%d\n",&maxsum[i][]);
minsum[i][]=maxsum[i][];
}
for(int j=;j<=;j++)// 预处理
for(int i=;i<=n;i++) //j 循环在 i 循环外
{
if(i+(<<j)-<=n) // 注意左右区间
{
maxsum[i][j]=_max(maxsum[i][j-],maxsum[i+(<<(j-))][j-]);
minsum[i][j]=_min(minsum[i][j-],minsum[i+(<<(j-))][j-]);
}
}
while(Q--)
{int a,b;
scanf("%d %d\n",&a,&b);
int k=(int)((log((double)(b-a+)))/log(2.0)); //注意要包含端点并转换成double
int maxl=_max(maxsum[a][k],maxsum[b-(<<k)+][k]);//不然有些测试要出问题 比如poj
int minl=_min(minsum[a][k],minsum[b-(<<k)+][k]);
printf("%d\n",maxl-minl);
}
}
return ;
}
 

poj3264 - Balanced Lineup(RMQ_ST)的更多相关文章

  1. POJ 3264 Balanced Lineup(RMQ_ST)

    题目链接:http://poj.org/problem? id=3264 Description For the daily milking, Farmer John's N cows (1 ≤ N  ...

  2. poj3264 Balanced Lineup(树状数组)

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

  3. Balanced Lineup(RMQ)

    原题传送门 就是裸RMQ啊.. 求区间最大值和区间最小值,一看就像RMQ,当然线段树貌似也可以. 至于算法嘛.自己学~(好吧,放个传送门...) 然后就是最后把maxsum-minsum就好啦233~ ...

  4. POJ 题目3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39046   Accepted: 18291 ...

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

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

  6. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  7. 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 ...

  8. 【POJ3264】Balanced Lineup(RMQ)

    题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...

  9. [POJ3264]Balanced Lineup(线段树,区间最值差)

    题目链接:http://poj.org/problem?id=3264 一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值. 线段树维护当前节点下属叶节点的两个最值,查询后作差即可. #incl ...

随机推荐

  1. VBA_Excel_教程:变量,数组

    Sub testVar() '变量 Dim strT1 As String strT1 = "A" '常量[加不加类型都可以] Const strT2 As String = &q ...

  2. python3实现简单爬虫功能

    本文参考虫师python2实现简单爬虫功能,并增加自己的感悟. #coding=utf-8 import re import urllib.request def getHtml(url): page ...

  3. 为什么C++中空类和空结构体大小为1?(转载)

    原文链接:http://www.spongeliu.com/260.html 对于结构体和空类大小是1这个问题,首先这是一个C++问题,在C语言下空结构体大小为0(当然这是编译器相关的).这里的空类和 ...

  4. Deep learning with Theano 官方中文教程(翻译)(四)—— 卷积神经网络(CNN)

    供大家相互交流和学习,本人水平有限,若有各种大小错误,还请巨牛大牛小牛微牛们立马拍砖,这样才能共同进步!若引用译文请注明出处http://www.cnblogs.com/charleshuang/. ...

  5. Mybatis按SQL查询字段的顺序返回查询结果

    在SpringMVC+Mybatis的开发过程中,可以通过指定resultType="hashmap"来获得查询结果,但其输出是没有顺序的.如果要按照SQL查询字段的顺序返回查询结 ...

  6. 温习SQL server

    做了好几年的管理工作,技术上有些退步,现在又一一捡起来啦, 以下最近几天看到的好文章, SQL Server约束 http://blog.csdn.net/qq61394323/article/det ...

  7. 深入理解js——prototype原型

    之前(深入理解js--一切皆是对象)中说道,函数也是一种对象.它也是属性的集合,你也可以对函数进行自定义属性.而JavaScript默认的给了函数一个属性--prototype(原型).每个函数都有一 ...

  8. 在eclipse中运行perl代码,需要配置的插件以及方法

    Eclipse配置正则表达式 网址:http://www.cnblogs.com/itech/archive/2010/02/23/1671676.html perl的环境配置以及在Eclipse中安 ...

  9. oracle学习-存储过程返回一个值,和返回一个结果集

    一.返回一个值 --创建存储过程 create or replace procedure sp_hu_test(spcode in varchar2,spname out varchar2)is be ...

  10. elasticsearch Java API汇总

    http://blog.csdn.net/changong28/article/details/38445805#comments 3.1 集群的连接 3.1.1 作为Elasticsearch节点 ...