Balanced Lineup
 

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 ≤ ABN), 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   这题是一个裸的RMQ问题。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int mm[maxn],Min[maxn][],Max[maxn][],a[maxn];
int main(){
#ifndef ONLINE_JUDGE
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
#endif int n,Q;
scanf("%d%d",&n,&Q);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
mm[]=-;
for(int i=;i<=n;i++){
mm[i]=(i&(i-))?mm[i-]:mm[i-]+;
Max[i][]=a[i];
Min[i][]=a[i];
}
for(int k=;k<=mm[n];k++)
for(int i=;i+(<<(k-))<=n;i++){
Max[i][k]=max(Max[i][k-],Max[i+(<<(k-))][k-]);
Min[i][k]=min(Min[i][k-],Min[i+(<<(k-))][k-]);
} int a,b;
while(Q--)
{
scanf("%d%d",&a,&b);
printf("%d\n",max(Max[a][mm[b-a+]],Max[b-(<<mm[b-a+])+][mm[b-a+]])-min(Min[a][mm[b-a+]],Min[b-(<<mm[b-a+])+][mm[b-a+]]));
}
return ;
}

数据结构(RMQ):POJ 3624 Balanced Lineup的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  2. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  3. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  4. poj 3264 Balanced Lineup(RMQ裸题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 43168   Accepted: 20276 ...

  5. POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

    RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...

  6. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  7. [POJ] 3264 Balanced Lineup [ST算法]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  8. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  9. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

随机推荐

  1. SQL Cursor 基本用法

     1 table1结构如下  2 id    int  3 name  varchar(50)  4   5 declare @id int  6 declare @name varchar(50) ...

  2. J2se中的声音---AudioPlayer

    1 package cn.gp.tools; import java.io.FileInputStream; import java.io.FileNotFoundException; import ...

  3. 【CF39E】【博弈论】What Has Dirichlet Got to Do with That?

    Description You all know the Dirichlet principle, the point of which is that if n boxes have no less ...

  4. skip-grant-tables的作用

    skip-grant-tables:非常有用的mysql启动参数(不启动grant-tables授权表) skip-grant-tables:非常有用的mysql启动参数   介绍一个非常有用的mys ...

  5. PHP 用Class构造JSON数据

    header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ...

  6. C#应用程序获取项目路径的方法总结

    一.非Web程序   //基目录,由程序集冲突解决程序用来探测程序集 1.AppDomain.CurrentDomain.BaseDirectory     //当前工作目录的完全限定路径2.Envi ...

  7. jQuery简单的轮播特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 已安装好的tengine编译添加未被安装的模块

    nginx -V 可以查看原来编译时都带了哪些参数 原来的参数: --prefix=/usr/local/tengine 需添加的参数: --with-http_stub_status_module ...

  9. Dao操作的抽取,BaseDao

    Dao操作通用的步骤: 0. 写SQL语句 1. 获取连接 2. 创建stmt 3. 执行sql a) 更新 b) 查询 4. 关闭/异常 代码: BaseDao /** * 通用的dao,自己写的所 ...

  10. 如何编写一个简单的makefile

    一个规则的构成 目标:依赖1,依赖2······ 命令 例子: objs := init.o nand.o head.o main.o nand.bin : $(objs) arm-linux-ld ...