码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

时间:2015-02-25 01:53:16      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:poj   线段树   


Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 36820   Accepted: 17244
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 ≤ 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

Source

USACO 2007 January Silver

题目链接:http://poj.org/problem?id=3264

题目大意:给一组有序数列,求区间内最大值减最小值的值

题目分析:裸线段树单点更新,交的时候C++速度比G++快一倍

#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 50005;
int a[MAX], ma, mi;
struct node
{
    int l, r;
    int mi, ma;
    int mid()
    {
        return (l + r) / 2;
    }
}t[3 * MAX];

void push_up(int rt)
{
    t[rt].ma = max(t[rt << 1].ma, t[rt << 1 | 1].ma);
    t[rt].mi = min(t[rt << 1].mi, t[rt << 1 | 1].mi);
}

void Build(int l, int r, int rt)
{
    t[rt].l = l;
    t[rt].r = r;
    if(l == r)
    {
        t[rt].ma = t[rt].mi = a[l];
        return;
    }
    else
    {
        int mid = t[rt].mid();
        Build(lson);
        Build(rson);
        push_up(rt);
    }
}

void Query(int l, int r, int rt)
{
    if(t[rt].l == l && t[rt].r == r)
    {
        ma = max(t[rt].ma, ma);
        mi = min(t[rt].mi, mi);
        return;
    }
    else
    {
        int mid = t[rt].mid();
        if(r <= mid)
            Query(l, r, rt << 1);
        else if(l > mid)
            Query(l, r, rt << 1 | 1);
        else
        {
            Query(lson);
            Query(rson);
        }
    }
}

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    Build(1, n, 1);
    for(int i = 1; i <= m; i++)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        ma = -INF;
        mi = INF;
        Query(l, r, 1);
        printf("%d\n", ma - mi);
    }
}




POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

标签:poj   线段树   

原文地址:http://blog.csdn.net/tc_to_top/article/details/43931185

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!