POJ 3264 Balanced Lineup 【RMQ】OR 【线段树】

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
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAX = 5e4 + 7;
int n, m;
int d[MAX];
int dmax[MAX][20];
int dmin[MAX][20];
void RMQ_init(int d[])
{
    for(int i = 0; i < n; i++)
        dmax[i][0] = dmin[i][0] = d[i];
    for(int j = 1; (1 << j) <= n; j++)
        for(int i = 0; i + (1 << j) - 1 <= n; i++)
        {
            dmax[i][j] = max(dmax[i][j - 1], dmax[i + (1 << (j - 1))][j - 1]);
            dmin[i][j] = min(dmin[i][j - 1], dmin[i + (1 << (j - 1))][j - 1]);
        }
}
int Query(int l, int r)
{
    int k = 0;
    while(1 << (k + 1) <= r - l + 1)
        k++;
    return max(dmax[l][k], dmax[r - (1 << k) + 1][k]) - min(dmin[l][k], dmin[r - (1 << k) + 1][k]);
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n >> m)
    {
        for(int i = 0; i < n; i++)
            cin >> d[i];
        RMQ_init(d);
        while(m--)
        {
            int l, r;
            cin >> l >> r;
            cout << Query(l - 1, r - 1) << endl;
        }
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
const int INF= 0x3f3f3f3f;
const int MAX = 5e4 + 8;
int n, m;
int mx[MAX << 2], mn[MAX << 2];
void PushUp(int rt)
{
    mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
    mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]);
}
void Build(int rt, int l, int r)
{
    if(l == r)
    {
        scanf("%d", &mx[rt]);
        mn[rt] = mx[rt];
        return;
    }
    int mid = (l + r) >> 1;
    Build(lson);
    Build(rson);
    PushUp(rt);
}
int Querymax(int rt, int l, int r, int x, int y)
{
    if(x <= l && r <= y)
        return mx[rt];
    int ans = -INF, mid = (l + r) >> 1;
    if(x <= mid)
        ans = max(ans, Querymax(lson, x, y));
    if(mid < y)
        ans = max(ans, Querymax(rson, x, y));
    return ans;
}
int Querymin(int rt, int l, int r, int x, int y)
{
    if(x <= l && r <= y)
        return mn[rt];
    int ans = INF, mid = (l + r) >> 1;
    if(x <= mid)
        ans = min(ans, Querymin(lson, x, y));
    if(mid < y)
        ans = min(ans, Querymin(rson, x, y));
    return ans;
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        memset(mx, 0, sizeof mx);
        memset(mn, 0, sizeof mn);
        Build(1, 1, n);
        while(m--)
        {
            int l, r;
            scanf("%d%d", &l, &r);
            printf("%d\n", Querymax(1, 1, n, l, r) - Querymin(1, 1, n, l, r));
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/head_hard/article/details/80486401