• POJ 3264 Balanced Lineup




    Balanced Lineup
    Time Limit: 5000MSMemory Limit: 65536K
    Total Submissions: 28725Accepted: 13519
    Case Time Limit: 2000MS

    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 ≤ 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

    Source


     


    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1

    const int Max=111111;

    int maxn[Max<<2];
    int minn[Max<<2];

    void pushUP(int rt)
    {
        maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
        minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
    }

    void build(int l,int r,int rt)
    {
        if(l==r)
        {
            int a;
            scanf("%d",&a);
            maxn[rt]=minn[rt]=a;
            return ;
        }
        int m=(r+l)>>1;
        build(lson); build(rson);
        pushUP(rt);
    }

    int querymax(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)
        {
            return maxn[rt];
        }
        int m=(l+r)>>1;
        int ret=0xdeadbeef;
        if(L<=m)
        {
            ret=max(ret,querymax(L,R,lson));
        }
        if(m<R)
        {
            ret=max(ret,querymax(L,R,rson));
        }

        return ret;
    }

    int querymin(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)
        {
            return minn[rt];
        }
        int m=(l+r)>>1;
        int ret=-0xdeadbeef;
        if(L<=m)
        {
            ret=min(ret,querymin(L,R,lson));
        }
        if(R>m)
        {
            ret=min(ret,querymin(L,R,rson));
        }

        return ret;
    }

    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,n,1);

        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            printf("%d ",querymax(a,b,1,n,1)-querymin(a,b,1,n,1));
        }

        return 0;
    }

  • 相关阅读:
    新手学Python必看的几个练手小项目,轻松不枯燥哦!
    实力讲解,一文读懂Python闭包与装饰器!
    Python必不可少的小技巧,一行代码减少一半内存占用!
    千篇一律的Python爬虫,大神精心总结的爬虫套路!
    Python小技巧:获取Linux系统基本信息
    大数据时代|搭建Python数据分析平台
    利用Python实时接收微博小姐姐动态啦~
    Python爬虫:把爬取到的数据插入到execl中
    正则表达式 元字符
    正则表达式——(2) – 语法
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350913.html
Copyright © 2020-2023  润新知