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

    Balanced Lineup  POJ - 3264 

    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.. NQ+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to Binclusive.

    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 <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <list>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <iomanip>
    #define ull unsigned long long
    #define ll long long
    #define pb push_back
    #define mem(sum,x) memset(sum,x,sizeof(sum))
    #define rep(i,start,end) for(int i=start;i<=end;i++)
    #define per(i,end,start) for(int i=end;i>=start;i--)
    #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    using namespace std;
    const int mod = 998244353;
    const int mxn = 1e5 +7;
    int _,n,m,t,u,v,ans,cnt,ok,lim;
    ll w[mxn] , cost[mxn] , dp[mxn] , far[mxn] , siz[mxn];
    double num[mxn];
    #define lc now<<1
    #define rc now<<1|1
    string str ;
    struct node {int l,r,val,lazy,mx,mn;}p[mxn<<4];
    void pushup(int now){
        /// p[now].val = p[lc].val+p[rc].val;
        p[now].mx = max(p[lc].mx , p[rc].mx);
        p[now].mn = min(p[lc].mn , p[rc].mn);
    }
    void pushdown(int now)
    {
        if(p[now].lazy)
        {
            p[lc].lazy = p[rc].lazy = p[now].lazy;
            p[lc].val = p[now].lazy*(p[lc].r-p[lc].l+1);
            p[rc].val = p[now].lazy*(p[rc].r-p[rc].l+1);
            p[now].lazy = 0 ;
        }
    }
    void build(int l,int r,int now)
    {
        p[now].l = l,p[now].r = r ;/// p[now].lazy = 0 ;
        if(l==r){
            /// cin>>p[now].val;
            scanf("%d",&p[now].val);
            p[now].mx = p[now].mn = p[now].val ;
            return ;
        }
        int mid = (l+r)>>1;
        build(l,mid,lc);build(mid+1,r,rc);pushup(now);
    }
    void ask(int l,int r,int now)
    {
        if(l<=p[now].l && r>=p[now].r){
            u = max(u,p[now].mx);
            v = min(v,p[now].mn);
            return ;
        }
        int mid = (p[now].l+p[now].r)>>1;
        if(l<=mid) ask(l,r,lc);
        if(r>mid) ask(l,r,rc);
    }
    int main()
    {
        tle;
        ///while(cin>>n>>m)
        while(~scanf("%d %d",&n,&m))
        {
            build(1,n,1);
            int l,r;
            while(m--)
            {
                /// cin>>l>>r;
                scanf("%d %d",&l,&r);
                u = -1 , v = mod ;ask(l,r,1);
                printf("%d
    ",u-v);
                /// cout<<u-v<<endl;
            }
        }
    }

    解二:RMQ
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <string>
     5 #include <vector>
     6 #include <map>
     7 #include <set>
     8 #include <list>
     9 #include <deque>
    10 #include <queue>
    11 #include <stack>
    12 #include <cstdlib>
    13 #include <cstdio>
    14 #include <cmath>
    15 #include <iomanip>
    16 #define ull unsigned long long
    17 #define ll long long
    18 #define pb push_back
    19 #define mem(sum,x) memset(sum,x,sizeof(sum))
    20 #define rep(i,start,end) for(int i=start;i<=end;i++)
    21 #define per(i,end,start) for(int i=end;i>=start;i--)
    22 #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    23 using namespace std;
    24 const int mod = 998244353;
    25 const int mxn = 1e5 +7;
    26 int _,n,m,t,u,v,ans,cnt,ok,lim;
    27 ll w[mxn] , cost[mxn] ,  far[mxn] , siz[mxn];
    28 double num[mxn];
    29 #define lc now<<1
    30 #define rc now<<1|1
    31 string str ;
    32 struct node {int l,r,val,lazy,mx,mn;}p[mxn<<4];
    33 void pushup(int now){
    34     /// p[now].val = p[lc].val+p[rc].val;
    35     p[now].mx = max(p[lc].mx , p[rc].mx);
    36     p[now].mn = min(p[lc].mn , p[rc].mn);
    37 }
    38 void pushdown(int now)
    39 {
    40     if(p[now].lazy)
    41     {
    42         p[lc].lazy = p[rc].lazy = p[now].lazy;
    43         p[lc].val = p[now].lazy*(p[lc].r-p[lc].l+1);
    44         p[rc].val = p[now].lazy*(p[rc].r-p[rc].l+1);
    45         p[now].lazy = 0 ;
    46     }
    47 }
    48 void build(int l,int r,int now)
    49 {
    50     p[now].l = l,p[now].r = r ;/// p[now].lazy = 0 ;
    51     if(l==r){
    52         /// cin>>p[now].val;
    53         scanf("%d",&p[now].val);
    54         return ;
    55     }
    56     int mid = (l+r)>>1;
    57     build(l,mid,lc);build(mid+1,r,rc);pushup(now);
    58 }
    59 int dp[mxn][20][2];
    60 void rmq()
    61 {
    62     for(int j=1;j<=log((double)n)/log(2.0)+1;j++)
    63     {
    64         for(int i=1;i+(1<<j)-1<=n;i++)
    65         {
    66             dp[i][j][0] = max(dp[i][j-1][0] , dp[i+(1<<(j-1))][j-1][0] );
    67             dp[i][j][1] = min(dp[i][j-1][1] , dp[i+(1<<(j-1))][j-1][1] );
    68         }
    69     }
    70 }
    71 int main()
    72 {
    73     tle;
    74     while(~scanf("%d %d",&n,&m))
    75     {
    76         for(int i=1,k;i<=n;i++)
    77             scanf("%d",&k) ,dp[i][0][0] = dp[i][0][1] = k ;
    78         rmq();
    79         int l,r;
    80         while(m--)
    81         {
    82             /// cin>>l>>r;
    83             scanf("%d %d",&l,&r);
    84             int k = log((double)(r-l+1))/log(2.0);
    85             printf("%d
    ", max(dp[l][k][0],dp[r-(1<<k)+1][k][0]) - min(dp[l][k][1],dp[r-(1<<k)+1][k][1]) );
    86             /// cout<<u-v<<endl;
    87         }
    88     }
    89 }
    所遇皆星河
  • 相关阅读:
    jQurey中getJSON方法错误回调方法
    easyui-datagrid 假分页
    布局页
    SQL游标(cursor)详细说明及内部循环使用示例
    Jquery 实现事件冒泡
    Jquery 实现选项卡
    Jquery 实现折叠菜单
    Jquery 实现表格的隔行换色
    Html5 考点内容
    Html5 之Canvas [画布]
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/12828834.html
Copyright © 2011-2022 走看看