POJ——3264Balanced Lineup(RMQ模版水题)

原创 2016年05月30日 13:12:48

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ3264——Balanced Lineup 倍增RMQ裸题

题目链接: 戳我题目大意:给定一个序列,有q次询问,每次询问给定一个区间,每次询问区间最大减去区间最小。输入:第一行两个数 n和q; 下面n行,每行一个数,第i+1行,表示序列的第i项; 接下来...

RMQ经典题:【POJ3264】 Balanced Lineup

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

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

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

POJ - 3264 Balanced Lineup解题报告(RMQ问题 ST算法 魔板题)

题目大意: 给你一串数,50,000个,询问200,000个区间,每次询问输出该区间最大值与最小值的差 分析: 这个因为不用修改,只需要查询,其实用树状数组也是可以的我觉得,但是因为还是有一...

POJ 3264 Balanced Lineup(最大最小差值 线段树水题)

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

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

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

poj 3264 Balanced Lineup(RMQ求区间最值)

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

POJ-3264 Balanced Lineup【RMQ】

题目链接:http://poj.org/problem?id=3264 题目大意: 一个农夫有N头牛,每头牛的高度不同,我们需要找出最高的牛和最低的牛的高度差。 解题思路: 我是用RMQ写的。...

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

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

POJ 3264 Balanced Lineup (RMQ)

Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)