zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup

    线段树的做法,1438MS;

    ------------------------------------------------------------------

    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
    ---------------------------------------------------------------------
    # include <stdio.h>
    
    # define N 50005
    # define INF 0x7fffffff
    
    int n, m, D;
    int Max[4 * N], Min[4 * N];
    
    int max(int x, int y)
    {
        return x>y ? x:y;
    }
    
    int min(int x, int y)
    {
        return x<y ? x:y;
    }
    
    void update(int i)
    {
        for (i += D; i ^ 1; i >>= 1)
        {
            Max[i >> 1] = max(Max[i], Max[i ^ 1]);
            Min[i >> 1] = min(Min[i], Min[i ^ 1]);
        }
    }
    
    int query(int s, int t)
    {
        int x, y;
    
        x = -INF, y = INF;
        for (s += D-1, t += D+1; s ^ t ^ 1; s >>= 1, t >>= 1)
        {
            if (~s & 0x1) x = max(x, Max[s+1]), y = min(y, Min[s+1]);
            if ( t & 0x1) x = max(x, Max[t-1]), y = min(y, Min[t-1]);
        }
    
        return x - y;
    }
    
    void init(void)
    {
        int i, val;
    
        scanf("%d%d", &n, &m);
        for (D = 1; D < n+2; D <<= 1) ;
        for (i = 0; i <= D*2+2; ++i) 
        {
            Max[i] = -INF;
            Min[i] = INF;
        }
        for (i = 1; i <= n; ++i)
        {
            scanf("%d", &val);
            Min[i+D] = Max[i+D] = val;
            update(i);
        }
    }
    
    void solve(void)
    {
        int s, t, i;
    
        for (i = 1; i <= m; ++i)
        {
            scanf("%d%d", &s, &t);
            printf("%d\n", query(s, t));
        }
    }
    
    int main()
    {
        init();
        solve();
    }

    ---------------------------------------------------------------------
  • 相关阅读:
    Mac从零配置Vim
    Mac效率:配置Alfred web search
    看看你的邻居在干什么
    成功破解邻居的Wifi密码
    MacBook安装Win10
    C陷阱:求数组长度
    Nexus 6P 解锁+TWRP+CM
    搭建树莓派手机远程开门系统
    Ubuntu下配置ShadowS + Chrome
    JS传参出现乱码(转载)
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2586308.html
Copyright © 2011-2022 走看看