描述

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.

输入

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.

输出

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.

样例输入

6 3
1
7
3
4
2
5
1 5
4 6
2 2

样例输出

6
3
0

题目大意:

给定一个序列,每次查询一个区间,求这个区间最大值与最小值的差。

#include <bits/stdc++.h>
using namespace std;
const int N=5e4+;
const int INF=0x3f3f3f3f;
int st1[N][],st2[N][],a[N];///st1[i][j]代表以第i个开始长度为2^j的区间内的最大值
int query(int l,int r)
{
int k=log2(r-l+);///需要查询的区间长度
return max(st1[l][k],st1[r-(<<k)+][k])-min(st2[l][k],st2[r-(<<k)+][k]);///[l][k]以l开始,[r-(1<<k)+1][k]到r结束
}
int main()
{
int n,m;
memset(st2,INF,sizeof st2);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
st1[i][]=st2[i][]=a[i];
for(int j=;j<;j++)
for(int i=;i+(<<(j-))<=n;i++)
st1[i][j]=max(st1[i][j-],st1[i+(<<(j-))][j-]),st2[i][j]=min(st2[i][j-],st2[i+(<<(j-))][j-]);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r));
}
return ;
}

 

Balanced Lineup(ST)的更多相关文章

  1. 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)

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

  2. Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup (ST表模板)

    传送门(ST表裸题) ST表是一种很优雅的算法,用于求静态RMQ 数组l[i][j]表示从i开始,长度为2^j的序列中的最大值 注意事项: 1.核心部分: ; (<<j) <= n; ...

  3. POJ 3264 Balanced Lineup(ST模板)

    链接:http://poj.org/problem?id=3264 题意:给n个数,求一段区间L,R的最大值 - 最小值,Q次询问 思路:ST表模板,预处理区间最值,O(1)复杂度询问 AC代码: # ...

  4. poj3264 - Balanced Lineup(RMQ_ST)

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

  5. Balanced Lineup(RMQ)

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

  6. POJ 题目3264 Balanced Lineup(RMQ)

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

  7. POJ 3264 Balanced Lineup(RMQ_ST)

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

  8. POJ 3264 Balanced Lineup(RMQ)

    点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...

  9. 【POJ3264】Balanced Lineup(RMQ)

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

随机推荐

  1. Outlook 0x800CCC1A 错误

    使用POP3帐户时,您可能在Outlook 2013/2016中看到以下错误.我在Exchange Server 2013环境中遇到此问题,在Windows 8.1上运行的Microsoft Outl ...

  2. LINQ新添知识

    linquser.ExecuteCommand("TRUNCATE TABLE Board");       linquser.ExecuteCommand("DELET ...

  3. BZOJ 1137: [POI2009]Wsp 岛屿 半平面交

    1137: [POI2009]Wsp 岛屿 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 165  Solved: ...

  4. 手机屏幕材质详解(TFT,TPS,OLED,AMOLED等)

    手机屏幕概括起来就是两种,一个是LCD,一个是OLED屏幕,这两个是屏幕显示技术的两大基础. 一 . LCD:Liquid Crystal Display,这是一种介于固态和液态之间的物质,称为液晶技 ...

  5. pip和apt-get换源

    pip换源 一下方法对pip和pip3同时起作用 永久换源 运行一下命令: cd ~/.pip 如果提示目录不存在的话,我们要自行创建一个,再进入目录 mkdir ~/.pip cd ~/.pip 在 ...

  6. 2019全套Java视频 免费赠送

    本人今年刚看完这套课程找到工作了 待遇还不错 现在送给大家 网盘链接:https://pan.baidu.com/s/1cEK6WoXS4F9SRgj1bZclqg提取码:bjl8希望对大家有用 一起 ...

  7. JavaScript -- 内置对象数组

    数组 创建数组的基本方式有两种: 1.使用 Array构造函数 语法:new Array() 小括号( )说明: (1)预先知道数组要保存的项目数量 (2)向Array构造函数中传递数组应包含的项 2 ...

  8. 【二分 最小割】cf808F. Card Game

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  9. MySQL 查询优化之 Block Nested-Loop 与 Batched Key Access Joins

    MySQL 查询优化之 Block Nested-Loop 与 Batched Key Access Joins 在MySQL中,可以使用批量密钥访问(BKA)连接算法,该算法使用对连接表的索引访问和 ...

  10. sphinx增量索引使用

    sphinx在使用过程中如果表的数据量很大,新增加的内容在sphinx索引没有重建之前都是搜索不到的. 这时可以通过建立sphinx增量索引,通过定时更新增量索引,合并主索引的方式,来实现伪实时更新. ...