# POJ——3264Balanced Lineup（RMQ模版水题）

Balanced Lineup
 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 44112 Accepted: 20713 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<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
const int N=50010;
int n,q;
int maxm[N][18],minm[N][18];
void rmq()
{
int i,j;
for (j=1; (1<<j)<=n; j++)
{
for (i=1; i+(1<<j)-1<=n; i++)
{
minm[i][j]=min(minm[i][j-1],minm[i+(1<<(j-1))][j-1]);
maxm[i][j]=max(maxm[i][j-1],maxm[i+(1<<(j-1))][j-1]);
}
}
}
int main(void)
{
int i,j,l,r;
while (~scanf("%d%d",&n,&q))
{
MM(maxm);MM(minm);
for (i=1; i<=n; i++)
{
scanf("%d",&maxm[i][0]);
minm[i][0]=maxm[i][0];
}
rmq();
for (i=0; i<q; i++)
{
scanf("%d%d",&l,&r);
int k=(int)log2(r-l+1);
int ans=max(maxm[l][k],maxm[r-(1<<k)+1][k])-min(minm[l][k],minm[r-(1<<k)+1][k]);
printf("%d\n",ans);
}
}
return 0;
}

• 本文已收录于以下专栏：

## RMQ经典题：【POJ3264】 Balanced Lineup

http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K ...
• NOI2015
• 2015年01月31日 21:36
• 238

## POJ 3264 Balanced Lineup 【RMQ求区间最值模板题】

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

## POJ 3264 Balanced Lineup 查询区间最大最小值 基础线状树水题

POJ 3264 Balanced Lineup 查询区间最大最小值 水题

## POJ 3264 Balanced Lineup（最大最小差值 线段树水题）

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Tota...

## POJ 3264 Balanced Lineup （区间最值 RMQ模板/线段树）

• CillyB
• 2017年03月15日 23:29
• 158

## poj 3264 Balanced Lineup（RMQ求区间最值）

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 29631   Accepted:...
• WEYuLi
• 2013年09月27日 14:11
• 501

## RMQ的ST算法学习小记 Poj 3264 Balanced Lineup

RMQ (Range Minimum/Maximum Query)问题是指：对于长度为n的数列A，回答若干询问RMQ(A,i,j)(i,j 主要方法及复杂度(处理复杂度和查询复杂度)如下: 1.线...

举报原因： 您举报文章：POJ——3264Balanced Lineup（RMQ模版水题） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)