zoukankan      html  css  js  c++  java
  • 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++中#include包含头文件带 .h 和不带 .h 的区别
    #if的使用说明
    非常简单的语音朗读功能
    C#基础笔记(第十一天)
    C#基础笔记(第十天)
    手机管理系统
    编程书籍大集合
    centos 安装多实例数据库
    Python3 网络爬虫(请求库的安装)
  • 原文地址:https://www.cnblogs.com/hlmark/p/4248353.html
Copyright © 2011-2022 走看看