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

    ---------------------------------------------------------------------
  • 相关阅读:
    mysql 5.7.28 中GROUP BY报错问题 SELECT list is not in GROUP BY clause and contains no
    mysql 的root 用户无法授权,navicat 远程授权提示1044解决方案
    Java equals(),== 和 hashcode()
    一键批处理图片的脚本(将指定目录中的图片处理成要求的分辨率)
    Anaconda环境下GPT2-Chinese的基本使用记录
    Ubuntu WSL 下编译并使用OpenJDK12
    SSM项目下Druid连接池的配置及数据源监控的使用
    《MySql必知必会》笔记整理
    Java面试题整理
    SpringBoot 访问树莓派上的MySql
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2586308.html
Copyright © 2011-2022 走看看