zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup 【线段树/区间最值差】

    Balanced Lineup
    Time Limit: 5000MS Memory Limit: 65536K
    Total Submissions: 62103 Accepted: 29005
    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

    USACO 2007 January Silver
    【代码】:

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 1e3 + 20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int minV=INF;
    int maxV=-INF;
    struct Node
    {
        int L,R;//区间起点和终点
        int minV,maxV;//本区间里的最大最小值
        int Mid(){
           return (L+R)/2;
        }
    };
    Node tree[800010];
    void BuildTree(int root, int L, int R)
    {
        tree[root].L = L;
        tree[root].R = R;
        tree[root].minV = INF;
        tree[root].maxV = -INF;
        if(L != R)
        {
            BuildTree(2*root+1, L, (L+R)/2);
            BuildTree(2*root+2, (L+R)/2 + 1, R);
        }
    }
    void Insert(int root, int i ,int v)//将第i个数,其值为v,插入线段树
    {
        if(tree[root].L == tree[root].R)
        {
            tree[root].minV = tree[root].maxV = v;
            return ;
        }
        tree[root].minV = min(tree[root].minV, v);
        tree[root].maxV = max(tree[root].maxV, v);
        if(i <= tree[root].Mid())
            Insert(2*root+1,i,v);
        else
            Insert(2*root+2,i,v);
    }
    void Query(int root, int s, int e)//查询区间[s,e]中的最小值和最大值,如果更优就记在全局变量里
    //minV和maxV里
    {
        if(tree[root].minV >= minV && tree[root].maxV <= maxV)
            return;
        if(tree[root].L == s && tree[root].R == e)
        {
            minV = min(minV, tree[root].minV);
            maxV = max(maxV, tree[root].maxV);
            return;
        }
        if(e <= tree[root].Mid())
            Query(2*root+1, s, e);
        else if(s > tree[root].Mid())
            Query(2*root+2, s, e);
        else
        {
            Query(2*root+1, s, tree[root].Mid());
            Query(2*root+2, tree[root].Mid()+1, e);
        }
    }
    int main()
    {
        int n,q,h;
        int i,j,k;
        scanf("%d%d",&n,&q);
        BuildTree(0,1,n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&h);
            Insert(0,i,h);
        }
        for(i=0;i<q;i++)
        {
            int s,e;
            scanf("%d%d",&s,&e);
            minV = INF;
            maxV = -INF;
            Query(0,s,e);
            printf("%d
    ",maxV - minV);
        }
    }
    /*
    6 3
    1 7 3 4 2 5
    1 5
    4 6
    2 2
    */
    
    
  • 相关阅读:
    采用GRE隧道连接不同虚拟机上的mininet网络
    ubuntu下安装Pycharm
    Update to openvswitch 2.3.0
    安装Openvswich 2.3.0
    ubuntu安装git
    解决Vmware Vsphere中虚拟机与本地机复制粘贴问题
    windows下安装python的xlrd和xlwd安装包
    ubuntu 下NetworkX的安装和使用
    大整数乘法 python实现
    推特(Twitter)的Snowflake算法——用于生成唯一ID
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9323964.html
Copyright © 2011-2022 走看看