zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup (RMQ | 线段树 | ST )

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

    1. **  RMQ(Range Minimum/Maximum Query)问题: 
    2. **  RMQ问题是求给定区间中的最值问题。当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的算法效率不够。 
    3. **  可以用线段树将算法优化到O(logn)(在线段树中保存线段的最值)。不过,Sparse_Table算法才是最好的: 
    4. **  它可以在O(nlogn)的预处理以后实现O(1)的查询效率。下面把Sparse Table算法分成预处理和查询两部分来说明(以求最小值为例)。  
    5. **  预处理: 
    6. **  预处理使用DP的思想,f(i, j)表示[i, i+2^j - 1]区间中的最小值,我们可以开辟一个数组专门来保存f(i, j)的值。 
    7. **  例如,f(0, 0)表示[0,0]之间的最小值,就是num[0], f(0, 2)表示[0, 3]之间的最小值, f(2, 4)表示[2, 17]之间的最小值 
    8. **  注意, 因为f(i, j)可以由f(i, j - 1)和f(i+2^(j-1), j-1)导出, 而递推的初值(所有的f(i, 0) = i)都是已知的 
    9. **  所以我们可以采用自底向上的算法递推地给出所有符合条件的f(i, j)的值。 
    10. **  查询: 
    11. **  假设要查询从m到n这一段的最小值, 那么我们先求出一个最大的k, 使得k满足2^k <(n - m + 1). 
    12. **  于是我们就可以把[m, n]分成两个(部分重叠的)长度为2^k的区间: [m, m+2^k-1], [n-2^k+1, n]; 
    13. **  而我们之前已经求出了f(m, k)为[m, m+2^k-1]的最小值, f(n-2^k+1, k)为[n-2^k+1, n]的最小值 
    14. **  我们只要返回其中更小的那个, 就是我们想要的答案, 这个算法的时间复杂度是O(1)的. 
    15. **  例如, rmq(0, 11) = min(f(0, 3), f(4, 3)) 
    16. **  由此我们要注意的是预处理f(i,j)中的j值只需要计算log(n+1)/log(2)即可,而i值我们也只需要计算到n-2^k+1即可。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    const int N=50010;
    const int INF=0x3f3f3f3f;
    
    int num[N],dpmin[N][20],dpmax[N][20];
    int n,q;
    
    void create_Dpmin(){
        int i,j;
        for(i=1;i<=n;i++)
            dpmin[i][0]=num[i];
        int sum=log((double)(n+1))/log(2.0);
        for(j=1;j<=sum;j++)
            for(i=1;i+(1<<j)-1<=n;i++)
                dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
    }
    
    void create_Dpmax(){
        int i,j;
        for(i=1;i<=n;i++)
            dpmax[i][0]=num[i];
        int sum=log((double)(n+1))/log(2.0);
        for(j=1;j<=sum;j++)
            for(i=1;i+(1<<j)-1<=n;i++)
                dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
    }
    
    int getmax(int l,int r){
        int k=(int)(log((double)(r-l+1))/log(2.0));
        return max(dpmax[l][k],dpmax[r-(1<<k)+1][k]);
    }
    
    int getmin(int l,int r){
        int k=(int)(log((double)(r-l+1))/log(2.0));
        return min(dpmin[l][k],dpmin[r-(1<<k)+1][k]);
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&q)){
            for(int i=1;i<=n;i++)
                scanf("%d",&num[i]);
            create_Dpmin();
            create_Dpmax();
            int l,r;
            while(q--){
                scanf("%d%d",&l,&r);
                printf("%d\n",getmax(l,r)-getmin(l,r));
            }
        }
        return 0;
    }

    线段树:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=50010;
    const int INF=0x3f3f3f3f;
    
    #define L(rt) (rt<<1)
    #define R(rt) (rt<<1|1)
    
    struct node{
        int l,r;
        int MIN,MAX,num;
    }tree[N<<2];
    
    int n,q;
    
    void PushUp(int rt){
        tree[rt].MIN=min(tree[L(rt)].MIN,tree[R(rt)].MIN);
        tree[rt].MAX=max(tree[L(rt)].MAX,tree[R(rt)].MAX);
    }
    
    void build(int L,int R,int rt){
        tree[rt].l=L;
        tree[rt].r=R;
        if(tree[rt].l==tree[rt].r){
            scanf("%d",&tree[rt].num);
            tree[rt].MIN=tree[rt].MAX=tree[rt].num;
            return ;
        }
        int mid=(L+R)>>1;
        build(L,mid,L(rt));
        build(mid+1,R,R(rt));
        PushUp(rt);
    }
    
    int Min,Max;
    
    void query(int L,int R,int rt){
        if(tree[rt].l>=L && tree[rt].r<=R){
            Min=min(Min,tree[rt].MIN);
            Max=max(Max,tree[rt].MAX);
            return ;
        }
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(R<=mid)
            query(L,R,L(rt));
        else if(L>=mid+1)
            query(L,R,R(rt));
        else{
            query(L,mid,L(rt));
            query(mid+1,R,R(rt));
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&q)){
            build(1,n,1);
            int l,r;
            while(q--){
                scanf("%d%d",&l,&r);
                Min=INF,    Max=-INF;
                query(l,r,1);
                printf("%d\n",Max-Min);
            }
        }
        return 0;
    }
  • 相关阅读:
    Apache commons-net用法的一个示例
    Apache commons(Java常用工具包)简介
    MyBatis动态sql之${}和#{}区别
    Spring事务管理
    Spring:源码解读Spring IOC原理
    Spring常用注解总结
    maven常用命令
    Spring事务回滚和异常类
    CSS3--难以想象的滤镜效果
    Composer安装
  • 原文地址:https://www.cnblogs.com/jackge/p/3138551.html
Copyright © 2011-2022 走看看