(转)POJ 3264 - Balanced Lineup (线段树)

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..  NQ+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

转载地址:poj3264 - Balanced Lineup

题目大意:给出一串的数字,然后给出一个区间a b,输出从a到b的最大的数和最小的数的差

解题方法:线段树

用线段树求出从a到b的最小值与最大值,直接相减输出就可以了

#include <iostream>
#include<stdio.h>
using namespace std;
#define MAXV 50100
#define MAXT 1<<18
#define max(a,b) a>b?a:b
#define min(a,b) a>b?b:a

int a[MAXV],TL[MAXT],TR[MAXT],TMAX[MAXT],TMIN[MAXT];
//TL与TR代表结点v区间的左边与右边的数,这样方便查询
//TMAX,TMIN,代表结点v的最大值与最小值

void createTree(int v,int f,int t){
    TL[v]=f;
    TR[v]=t;
    if(f==t){
        TMAX[v]=a[f];
        TMIN[v]=a[f];
        return;
    }

    int mid=(f+t)>>1;//意为(f+t)/2
    createTree(v<<1,f,mid); //V*2*2*2*2*2......
    createTree((v<<1)|1,mid+1,t);

    TMAX[v]=max(TMAX[v<<1],TMAX[(v<<1)|1]);//TMAX,TMIN,代表结点v的最大值与最小值
    TMIN[v]=min(TMIN[v<<1],TMIN[(v<<1)|1]);
}

int queryTree(int v,int L,int R,int flag)//查询树
{
    if(L==TL[v] && R==TR[v])//TL与TR代表结点v区间的左边与右边的数,这样方便查询
    {
        return (flag?TMIN[v]:TMAX[v]);
    }

    int mid=(TL[v]+TR[v])>>1; //意为(TL[v]+TR[v])/2
    if(R<=mid)
        return queryTree(v<<1,L,R,flag);
    else if(L>mid)
        return queryTree((v<<1)|1,L,R,flag);
    else{
        if(flag) return min(queryTree(v<<1,L,mid,flag),queryTree((v<<1)|1,mid+1,R,flag));
        else return max(queryTree(v<<1,L,mid,flag),queryTree((v<<1)|1,mid+1,R,flag));
    }
}

int main(){
    int N,Q,L,R,i;
    while(scanf("%d%d",&N,&Q)!=EOF){
        for(i=1;i<=N;i++)
            scanf("%d",&a[i]);

        createTree(1,1,N);
        while(Q--){
            scanf("%d%d",&L,&R);
            printf("%d\n",queryTree(1,L,R,0)-queryTree(1,L,R,1));
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值