POJ 3264-Balanced Lineup(简单线段树+区间最值)

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


题目大意:给你一段区间,并给这个区间赋值,有q次查询l,r,没次查询输出[l, r]内最大值与最小值的差。
解题思路:运用线段树即可解决,每个结点分别用一个数来记录该区间的最大和最小值即可,最后O(q*logn)内可以完成所有查询。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeofa());
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, q, x, ma, mi;

struct node
{
    int l;
    int r;
    int mm; //存最大
    int nn; //存最小
}e[50010*4];

void build(int l, int r, int k)
{
    e[k].l = l;
    e[k].r = r;
    if(l == r) {
        in1(x);
        e[k].mm = x;
        e[k].nn = x;
        return;
    }
    int mid = (l+r)/2;
    build(l, mid, 2*k);
    build(mid+1, r, 2*k+1);
    e[k].nn = min(e[2*k].nn, e[2*k+1].nn);
    e[k].mm = max(e[2*k].mm, e[2*k+1].mm);
}

void quary(int l, int r, int k)
{
    if(e[k].l == l && e[k].r == r) {
        mi = min(mi, e[k].nn); //返回最大和最小值
        ma = max(ma, e[k].mm);
        return;
    }
    int mid = (e[k].l+e[k].r)/2;
    if(r <= mid) quary(l, r, 2*k);
    else if(l >= mid+1) quary(l, r, 2*k+1);
    else  {
        quary(l, mid, 2*k);
        quary(mid+1, r, 2*k+1);
    }
}

int main()
{
    int x, y;
    while(~scanf("%d%d", &n, &q)) {
        build(1, n, 1);
        while(q --) {
            ma = 0;
            mi = INF; //记得初始化
            scanf("%d%d", &x, &y);
            quary(x, y, 1);
            out1(ma - mi);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/i_believe_cwj/article/details/80306752