poj 3264 Balanced Lineup

                                                                                                                                                               Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 51876   Accepted: 24319
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
题意:给定一串长度为N的数列,现在给定子序列[a,b],要查询连续的子数列[a,b]区间中中的最大值和最小值的差。
思路:典型的线段树问题,对于线段树中每个节点k,维护两个值,即维护该节点对应的区间[l,r)中的最大值和最小值,最后输出其差即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int ST_SIZE = (1 << 17) - 1,N_MAX=50000+2;
int N, Q;
int height[N_MAX];
int dat_large[ST_SIZE], dat_small[ST_SIZE];
void init(int k,int l,int r) {//节点k,对应区间[l,r)
    if (r - l == 1) {
       dat_large[k] = dat_small[k] = height[l];//!!!!!!!!!!
 }
    else {
        int left = 2 * k + 1;
        int right = 2 * k + 2;
        init(left,l,(l+r)/2);
        init(right, (l + r) / 2, r);
        dat_large[k] = max(dat_large[left],dat_large[right]);
        dat_small[k] = min(dat_small[left],dat_small[right]);
    }
}

pair<int,int> query(int k,int l,int r,int a,int b) {//节点k,对应区间[l,r),查找区间[a,b),用于找区间[a,b)的最大最小值
    //pair<int,int>find;//分别存放最大和最小值
    if (b <= l || a >= r) {//无交集
        return make_pair(0,INT_MAX);
    }
    else if (a <= l&& b>= r) {//完全包含区间!!!!!!!!!!!!!!!!!!
        return make_pair(dat_large[k], dat_small[k]);
     }
    else {
        pair<int, int>find1 = query(2*k+1,l,(l+r)/2,a,b);
        pair<int, int>find2 = query(2 * k + 2, (l + r) / 2, r, a, b);
        int large = max(find1.first,find2.first);
        int small = min(find1.second,find2.second);
        return make_pair(large, small);
    }
}

int main() {
    scanf("%d%d",&N,&Q);
    for (int i = 0; i < N;i++) {
        scanf("%d",&height[i]);
    }
    init(0,0,N);
    for (int i = 0; i < Q; i++) {
        int a, b;
        scanf("%d%d",&a,&b);
        a--, b--;
        pair<int, int>find = query(0,0,N,a,b+1);
        printf("%d\n",find.first-find.second);
    }
    return 0;
}

 

posted on 2017-04-10 21:08  ZefengYao  阅读(117)  评论(0编辑  收藏  举报

导航