zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup

    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 55414   Accepted: 25969
    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

     
    题意:给定x,y区间 
    求区间内极差
    #include <ctype.h>
    #include <cstdio>
    
    void read(int &x)
    {
        x=0;bool f=0;
        register char ch=getchar();
        for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
        for(; isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
        x=f?(~x)+1:x;
    }
    int n,m;
    int max(int a,int b) {return a>b?a:b;}
    int min(int a,int b) {return a>b?b:a;}
    struct Typetree
    {
        int l,r,dis,Max,Min;
        Typetree *ch[2];
        void pushup()
        {
            Max=max(ch[0]->Max,ch[1]->Max);
            Min=min(ch[0]->Min,ch[1]->Min);
        }
        void build(int l,int r,int *a)
        {
            if(l==r)
            {
                dis=Max=Min=a[l];
                return;
            }
            int mid=(l+r)>>1;
            (ch[0]=new Typetree)->build(l,mid,a);
            (ch[1]=new Typetree)->build(mid+1,r,a);
            pushup();
        }
        int Query_Max(int l,int r,int L,int R)
        {
            if(L<=l&&r<=R) return Max;
            int mid=(l+r)>>1,ret=-2;
            if(L<=mid) ret=max(ch[0]->Query_Max(l,mid,L,R),ret);
            if(R>mid) ret=max(ch[1]->Query_Max(mid+1,r,L,R),ret);
            return ret;
        }
        int Query_Min(int l,int r,int L,int R)
        {
            if(L<=l&&r<=R) return Min;
            int mid=(l+r)>>1,ret=0x7fffffff;
            if(L<=mid) ret=min(ch[0]->Query_Min(l,mid,L,R),ret);
            if(R>mid) ret=min(ch[1]->Query_Min(mid+1,r,L,R),ret);
            return ret;
        }
    }*root=new Typetree;
    int N,Q,data[50005];
    int main()
    {
        read(N);read(Q);
        for(int i=1;i<=N;i++) read(data[i]);
        root->build(1,N,data);
        for(int x,y;Q--;)
        {
            read(x);read(y);
            printf("%d
    ",root->Query_Max(1,N,x,y)-root->Query_Min(1,N,x,y));
        }
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    Windows 解压缩XX.zip.001 XX.z01分卷文件的方法
    Android Library 发布开源库 JCenter & JitPack 攻略
    常见场景下Fragment和Activity的生命周期对比
    Android Studio 插件 ADBWifi 无线调试真机
    Flutter upgrade更新版本引发的无法启动调试APP的错误 target:kernel_snapshot failed”
    Glide异常:Failed to find GeneratedAppGlideModule 解决实践
    Android 讯飞语音听写SDK快速接入(附空指针解决和修改对话框文字方法)
    Android 自定义View—清爽小巧灵活的多节点进度条
    Android EXCEL 解析 xls 和 xlsx,方法其实很简单
    解决repo从codeaurora.org同步Android代码失败问题
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7325425.html
Copyright © 2011-2022 走看看