POJ3264 Balanced Lineup(线段树入门)



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 ≤ ABN), 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
题目大意:第一行输入一个n和q,接下来输入c[0],c[1]...c[n-1],然后输入q组区间,求每组区间中最大值与最小值的差值。
线段树可以维护一个区间中的一个信息,速度比遍历快许多。数据结构是一颗二叉树,所以满足二叉树的性质。这道题可以求出区间最大值与最小值相减后得答案。具体分析见代码注释(由于我也是初学线段树,所以借鉴了网上一个大神的写法,代码会比较接近)
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>

using namespace std;

struct point
{
    int x;
    int y;
    int maxn;
    int minn;
}tree[200000];//线段树每个点需要维护的信息(下辖区间左域,右域和该区间的最大最小值)

int max0,min0,c[50005];

void build(int l,int r,int root)//递归法建二叉树
{
    int mid;
    tree[root].x=l;
    tree[root].y=r;
    if (tree[root].x==tree[root].y)//左域右域相等,直接等于对应数组的值
    {
        tree[root].maxn=c[l];
        tree[root].minn=c[l];
        return;
    }
    mid=(l+r)/2;
    build(l,mid,root*2);//建立左树
    build(mid+1,r,root*2+1);//建立右树
    tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);//父亲节点等于儿子节点的最大值
    tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
}

void findmax(int l,int r,int root)//查找最大值
{
    int mid;
    if (tree[root].x==l&&tree[root].y==r)//如果查找的左值与右值和该节点的左边界与右边界相同,那么就询问最大值
    {
        if (tree[root].maxn>max0)
        {
             max0=tree[root].maxn;//因为查找区间在分割后不一定处于同一层,所以上来后要判断
        }
        return;
    }
    mid=(tree[root].x+tree[root].y)/2;
    if (mid>=r)//查找区间完全在左儿子
    {
        findmax(l,r,root*2);
    }
    else if (mid<l)//完全在右儿子
    {
        findmax(l,r,root*2+1);
    }
    else//都有
    {
        findmax(l,mid,root*2);
        findmax(mid+1,r,root*2+1);
    }
}

void findmin(int l,int r,int root)
{
    int mid;
    if (tree[root].x==l&&tree[root].y==r)
    {
        if (tree[root].minn<min0)
        {
             min0=tree[root].minn;
        }
        return;
    }
    mid=(tree[root].x+tree[root].y)/2;
    if (mid>=r)
    {
        findmin(l,r,root*2);
    }
    else if (mid<l)
    {
        findmin(l,r,root*2+1);
    }
    else
    {
        findmin(l,mid,root*2);
        findmin(mid+1,r,root*2+1);
    }
}

int main()
{
    int n,q,i,ii,a,b;
    scanf("%d%d",&n,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%d",&c[i]);
    }
    build(1,n,1);
    for (ii=0;ii<=q-1;ii++)
    {
        max0=0;
        min0=999999;
        scanf("%d%d",&a,&b);
        {
            findmax(a,b,1);
            findmin(a,b,1);
        }
        printf("%d\n",max0-min0);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值