• POJ 3264 Balanced Lineup(RMQ)


    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 36513   Accepted: 17103
    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 ≤ ABN), 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

    求区间最大最小值差

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define root 1,n,1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    #define lr rt<<1
    #define rr rt<<1|1
    typedef long long LL;
    const int oo = 1e9+7;
    const double PI = acos(-1.0);
    const double eps = 1e-6 ;
    const int N =  50010;
    const int mod = 2333333;
    int dp_m[N][20] , dp_M[N][20] , mm[N] , b[N] , n , m ;
    void initRMQ( int n ) {
        mm[0] = -1 ;
        for( int i = 1 ; i <= n ; ++i ){
            mm[i] = ( (i&(i-1)) ==0 )?mm[i-1]+1:mm[i-1];
            dp_m[i][0]=dp_M[i][0]=b[i];
        }
        for(int j = 1 ; j <= mm[n] ; ++j ) {
            for( int i = 1 ; i+(1<<j)-1<=n ; ++i ) {
                dp_M[i][j] = max( dp_M[i][j-1] ,dp_M[i+(1<<(j-1))][j-1]);
                dp_m[i][j] = min( dp_m[i][j-1] ,dp_m[i+(1<<(j-1))][j-1]);
            }
        }
    }
    int rmq( int x , int y ){
        int k = mm[y-x+1];
        return max(dp_M[x][k],dp_M[y-(1<<k)+1][k]) - min(dp_m[x][k],dp_m[y-(1<<k)+1][k]) ;
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
    //        freopen("out.txt","w",stdout);
        #endif // LOCAL
        int _ ,x ,y , c ;
        while( ~scanf("%d%d",&n,&m) ) {
            for( int i =1 ; i <= n ; ++i ){
                scanf("%d",&b[i]);
            }
            initRMQ(n);
            while(m--) {
                scanf("%d%d",&x,&y);
                printf("%d
    ",rmq(x,y));
            }
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    C#中的“装箱”与“拆箱”[转贴]
    C#中的委托和事件[转帖]
    ASP.NET的四种错误机制[转帖]
    读技术书的技巧
    《AdvancED ActionScript 3.0 Animation》读书笔记(2) —— 2.5d
    读《JavaScript大师Nicholas C. Zakas谈TypeScript》有感,也谈编译js问题
    《游戏人工智能编程》读书笔记 —— 向量的归一和点乘
    浏览器图形渲染性能测试
    对对碰游戏demo
    A星寻路demo
  • 原文地址:https://www.cnblogs.com/hlmark/p/4248353.html
Copyright © 2020-2023  润新知