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();
    }

    ---------------------------------------------------------------------
  • 相关阅读:
    [BZOJ1294][SCOI2009]围豆豆Bean 射线法+状压dp+spfa
    [BZOJ1060][ZJOI2007]时态同步 树形dp
    [BZOJ1082][SCOI2005]栅栏 二分+搜索减枝
    [BZOJ1055][HAOI2008]玩具取名 区间dp
    [BZOJ1070][SCOI2007]修车 费用流
    [LeetCode 560] Subarray Sum Equals K
    Line of wines
    [LeetCode 1197] Minimum Knight Moves
    [Daily Coding Problem 293] Minimum Cost to Construct Pyramid with Stones
    [Daily Coding Problem 294] Shortest round route with rising then falling elevations
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2586308.html
Copyright © 2011-2022 走看看