Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 47087   Accepted: 22101
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
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=;
int n,m;
int maxm[MAXN][],minm[MAXN][];
void init_st(int size)
{
for(int j=;j<;j++)
{
for(int i=;i<=n;i++)
{
if(i+(<<j)-<=n)
{
maxm[i][j]=max(maxm[i][j-],maxm[i+(<<(j-))][j-]);
minm[i][j]=min(minm[i][j-],minm[i+(<<(j-))][j-]);
}
}
}
}
int rmq_st(int l,int r)
{
int limit=(int)(log(0.0+(r-l+))/log(2.0));
int mn=min(minm[l][limit],minm[r-(<<limit)+][limit]);
int mx=max(maxm[l][limit],maxm[r-(<<limit)+][limit]);
return mx-mn;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",&maxm[i][]);
minm[i][]=maxm[i][];
}
init_st(n);
for(int i=;i<m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
int res=rmq_st(l,r);
printf("%d\n",res);
}
}
return ;
}

POJ3264(RMQ-ST算法)的更多相关文章

  1. [POJ3264]Balanced Lineup(RMQ, ST算法)

    题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...

  2. 求解区间最值 - RMQ - ST 算法介绍

    解析 ST 算法是 RMQ(Range Minimum/Maximum Query)中一个很经典的算法,它天生用来求得一个区间的最值,但却不能维护最值,也就是说,过程中不能改变区间中的某个元素的值.O ...

  3. 【原创】RMQ - ST算法详解

    ST算法: ID数组下标: 1   2   3   4   5   6   7   8   9    ID数组元素: 5   7   3   1   4   8   2   9   8 1.ST算法作 ...

  4. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

  5. POJ 3264 Balanced Lineup RMQ ST算法

    题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...

  6. 关于基础RMQ——ST算法

    RMQ,Range Maximum/Minimum Query,顾名思义,就是询问某个区间内的最大值或最小值,今天我主要记录的是其求解方法--ST算法 相对于线段树,它的运行速度会快很多,可以做到O( ...

  7. POJ3264 (RMQのST解法)

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  8. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  9. RMQ st算法 区间最值模板

    #include<bits/stdc++.h> ; ; int f[N][Logn],a[N],lg[N],n,m; int main(){ cin>>n>>m; ...

  10. RMQ问题(线段树+ST算法)

    转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...

随机推荐

  1. debian内核代码执行流程(三)

    接续<debian内核代码执行流程(二)>未完成部分 下面这行输出信息是启动udevd进程产生的输出信息: [ ]: starting version 175是udevd的版本号. 根据& ...

  2. iOS_网络编程

    网络编程中有以下几种方式向服务器进行提交数据: IOS同步请求.异步请求.GET请求.POST请求 1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可 ...

  3. [Android]开源中国源码分析之二---DrawerLayout

    从启动界面到主界面之后的效果如图所示,采用的是v4包下的DrawerLayout, activity_main.xml文件如下: <!-- A DrawerLayout is intended ...

  4. 【转载】如何简单地理解Python中的if __name__ == '__main__'

    原帖:https://blog.csdn.net/yjk13703623757/article/details/77918633 通俗的理解__name__ == '__main__':假如你叫小明. ...

  5. UVA639 二叉树

    题意:深度为n的二叉树每个节点上有个开关,初始为关闭,每当小球落在节点上都会改变开关的状态,问编号为m的小球最终会落在哪里. 思路:对于二叉树的节点k,左节点右节点的编号为2k,2k+1.只需由最后一 ...

  6. idea创建git分支

        此时只是在本地创建好了分支,修改源代码后add,commit将本地分支提交到远程仓库            分支已创建,其它成员此时就可以从git拉分支

  7. 第四节课-反向传播&&神经网络1

    2017-08-14 这节课的主要内容是反向传播的介绍,非常的详细,还有神经网络的部分介绍,比较简短. 首先是对求导,梯度的求解.反向传播的核心就是将函数进行分解,分段求导,前向计算损失,反向计算各个 ...

  8. Spring插件的安装与卸载---笔记

    Spring插件的安装 1.在eclipse中选择工具菜单Help--->Install New Software选项 2.点击Add, 3.选择插件地址或输入网址,点击  OK  . http ...

  9. QT 使用QSettings读写ini配置文件

    利用Qsettings包一个类 RWIniFile, writeIni方法写文件, readIni方法读文件 rwinifile.h #ifndef RWINIFILE_H #define RWINI ...

  10. linux中动态链接库的创建与使用

    LINUX系统中动态链接库的创建与使用 http://www.cnblogs.com/ardar/articles/357321.html 正常C源文件编写,编译时-shared即可得到SO, gcc ...