poj 3264 Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 32070 Accepted: 15090
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"stdio.h"
#include"string.h"
#define N 50005
int num[N];
struct node
{
	int l,r,num;
	int high,low;      //分别记录该区间最大和最小高度
}f[N*3];
int Max(int a,int b)
{
	return a>b?a:b;
}
int Min(int a,int b)
{
	return a<b?a:b;
}
void creat(int t,int l,int r)
{
	f[t].l=l;
	f[t].r=r;
	if(l==r)
	{
		f[t].num=f[t].low=f[t].high=num[r];
		return ;
	}
	int temp=t*2,mid=(l+r)/2;
	creat(temp,l,mid);
	creat(temp+1,mid+1,r);
	f[t].high=Max(f[temp].high,f[temp+1].high);
	f[t].low=Min(f[temp].low,f[temp+1].low);
	return ;
}
int fmax(int t,int a,int b)     //查找该区间最大高度
{
	if(f[t].l>=a&&f[t].r<=b)
		return f[t].high;
	int temp=t*2,mid=(f[t].l+f[t].r)/2;
	if(b<=mid)
		return fmax(temp,a,b);
	else if(a>mid)
		return fmax(temp+1,a,b);
	else
		return Max(fmax(temp,a,mid),fmax(temp+1,mid+1,b));
}
int fmin(int t,int a,int b) //查找该区间最小高度
{
	if(f[t].l>=a&&f[t].r<=b)
		return f[t].low;
	int temp=t*2,mid=(f[t].l+f[t].r)/2;
	if(b<=mid)
		return fmin(temp,a,b);
	else if(a>mid)
		return fmin(temp+1,a,b);
	else
		return Min(fmin(temp,a,mid),fmin(temp+1,mid+1,b));
}
int main()
{
	int n,q,i,a,b;
	while(scanf("%d%d",&n,&q)!=-1)
	{
		for(i=1;i<=n;i++)
			scanf("%d",&num[i]);
		creat(1,1,n);
		while(q--)
		{
			scanf("%d%d",&a,&b);
			printf("%d\n",fmax(1,a,b)-fmin(1,a,b));
		}
	}
	return 0;
}

这个速度更快啊!

#include"stdio.h"
#include"math.h"
#include"queue"
#include"string.h"
#include"iostream"
#include"algorithm"
using namespace std;
#define N 50005
const int inf=(int)1e9;
struct node
{
    int l,r;
    int minv,maxv;
    int mid()   //定义函数求均值
    {
        return (l+r)/2;
    }
};
node f[N*3];
int maxv,minv;
void Creat(int t,int l,int r)
{
    f[t].l=l;
    f[t].r=r;
    f[t].minv=inf;
    f[t].maxv=-inf;
    if(l==r)
        return ;
    Creat(t*2,l,f[t].mid());
    Creat(t*2+1,f[t].mid()+1,r);
}
void Insert(int t,int i,int v)
{
    if(f[t].l==f[t].r)
    {
        f[t].minv=f[t].maxv=v;
        return ;
    }
    f[t].maxv=max(f[t].maxv,v);
    f[t].minv=min(f[t].minv,v);
    if(i<=f[t].mid())
        Insert(t*2,i,v);
    else
        Insert(t*2+1,i,v);
}
void Query(int t,int l,int r)
{
    if(f[t].l==l&&f[t].r==r)
    {
        maxv=max(maxv,f[t].maxv);
        minv=min(minv,f[t].minv);
        return ;
    }
    if(r<=f[t].mid())
        Query(t*2,l,r);
    else if(l>f[t].mid())
        Query(t*2+1,l,r);
    else
    {
        Query(t*2,l,f[t].mid());
        Query(t*2+1,f[t].mid()+1,r);
    }
}
int main()
{
    int n,q,i,v,l,r;
    while(scanf("%d%d",&n,&q)!=-1)
    {
        Creat(1,1,n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&v);
            Insert(1,i,v);
        }
        while(q--)
        {
            scanf("%d%d",&l,&r);
            maxv=-inf;
            minv=inf;
            Query(1,l,r);
            printf("%d\n",maxv-minv);
        }
    }
    return 0;
}



RMQ:

#include"stdio.h"
#include"math.h"
#include"string.h"
#define N 50005
const int M=20;
int fm[N][M],fi[N][M];
int max(int a,int b)
{
	return a>b?a:b;
}
int min(int a,int b)
{
	return a<b?a:b;
}
int main()
{
	int i,j,n,q,a,b;
	scanf("%d%d",&n,&q);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a);
		fm[i][0]=fi[i][0]=a;     
	}
	for(j=1;j<=log(1.0*n)/log(2.0);j++)
	{
		for(i=1;i<=n+1-(1<<j);i++)
		{
			fm[i][j]=max(fm[i][j-1],fm[i+(1<<(j-1))][j-1]);
			fi[i][j]=min(fi[i][j-1],fi[i+(1<<(j-1))][j-1]);
		}
	}
	while(q--)
	{
		scanf("%d%d",&a,&b);
		int k=(int)(log(b-a+1.0)/log(2.0));
		int maxx=max(fm[a][k],fm[b-(1<<k)+1][k]);
		int minn=min(fi[a][k],fi[b-(1<<k)+1][k]);
		printf("%d\n",maxx-minn);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值