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

    ---------------------------------------------------------------------
  • 相关阅读:
    HackingLab-再加密一次你就得到key啦~
    操作系统理论细节 2
    操作系统理论细节 1
    各种调度算法均衡利弊
    Github实现代码高亮
    Oracle 生成一张测试表并插入随机的个人基本信息数据
    一,Spring Boot 入门
    关于IDEA2020年1月新版MAVEN无法自动导入依赖
    操作系统存储管理--虚拟内存地址和物理内存地址
    apache2.4 tomcat 出现502 Proxy Error错误
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2586308.html
Copyright © 2011-2022 走看看