POJ 题目3264 Balanced Lineup(RMQ)

Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 39046   Accepted: 18291
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

Source

USACO 2007 January Silver

ac代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int minv[50050][20],maxv[50050][20];
int a[50050];
void init(int n)
{
	int i,j,k;
	for(i=1;i<=n;i++)
	{
		maxv[i][0]=minv[i][0]=a[i];
	}
	for(j=1;(1<<j)<=n;j++)
	{
		for(k=1;k+(1<<j)-1<=n;k++)
		{
			minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);
			maxv[k][j]=max(maxv[k][j-1],maxv[k+(1<<(j-1))][j-1]);
		}
	}
}
int q_max(int l,int r)
{
	int k=(int)(log((double)(r-l+1))/(log(2.0)));
	return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
}
int q_min(int l,int r)
{
	int k=(int)(log((double)(r-l+1))/(log(2.0)));
	return min(minv[l][k],minv[r-(1<<k)+1][k]);
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		init(n);
		while(m--)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			printf("%d\n",q_max(l,r)-q_min(l,r));
		}
	}
}

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

时间: 2024-10-07 04:09:05

POJ 题目3264 Balanced Lineup(RMQ)的相关文章

【POJ】3264 Balanced Lineup ——线段树 区间最值

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 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 Joh

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 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 Joh

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

RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就是说,RMQ问题是指求区间最值的问题. Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description For the daily

POJ 3264 Balanced Lineup(RMQ/线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40312   Accepted: 18936 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 Joh

POJ 3264 Balanced Lineup[RMQ入门题]

题目链接:http://poj.org/problem?id=3264 题目大意:n个数,求区间[ L,R ]的最大最小值之差: 题目分析: RQM:dp[ i ][ j ], i开始长度为2^j的长度的区间最值: O(nlog n)的预处理区间值,O(1)的查询: 代码: //author: ACsorry //result: accept #include<iostream> #include<cstdio> #include<cstring> #include&l

POJ 3264 Balanced Lineup(RMQ)

这道题为裸的RMQ.具体RMQ的意思为区间最值查询,他是先预处理出来每一段的最值,然后查询的时候直接O(1)的复杂度得出结果.其实还是个dp.用Rmin[i][j]表示从i开始长度为2^j这个区间的最值.至于为什么是2^j,因为计算机当中是二进制,移位比较方便.查询的时候也是将一个区间分成两部分,其中这两部分有可能重叠. 这个题意:给定区间,让求区间最大值最小值的差值. #include <cstdio> #include <iostream> #include <cstri

POJ 3264 Balanced Lineup(RMQ详解)

RMQ:(区间最值问题) 本质上是动态规划,用d(i, j) 表示 从 i 开始的长度为 2^j 的一段元素的最小值,则可以用递推的方法计算d(i, j) : d(i, j) = min{ d(i, j-1), d(i + 2^(j-1), j-1)} 由于2^j <= n 因此 d数组中元素个数不超过nlogn, 因此总时间复杂度为O(nlogn); #include <iostream> #include <cstring> #include <cstdlib>

poj 3264 Balanced Lineup

题目链接:http://poj.org/problem?id=3264 题目大意:就是给你一串数,问你最大数和最小数的差值....... 思路:最基本的线段树,只需要建树和查询,修改都省啦,但是查询要写两个,一个查询最大值,一个查询最小值......然后就能AC掉.....但是话说poj把它分类到RMQ中.... code: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream

[POJ] 3264 Balanced Lineup [ST算法]

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 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 Joh