zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    How To Change Database Name
    Oracle备份与恢复案例
    Linux 下安装 Oracle9i
    在 Linux x86 上安装 Oracle 数据库 10g_1
    在 Linux x86 上安装 Oracle 数据库 10g_2
    Oracle9i数据库DataGuard实施及维护手册2
    在 Linux x86 上安装 Oracle 数据库 10g_3
    流程企业(钢铁企业)的制造执行系统
    理解和使用Oracle分析工具LogMiner
    Oracle9i数据库Data Guard实施及维护手册 1
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350913.html
Copyright © 2011-2022 走看看