zoukankan      html  css  js  c++  java
  • poj 3264 Balanced Lineup rmq

    Balanced Lineup
    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

    思路:rmq 板子题,区间最大值-区间最小值,无更新;
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    //#define mod 1000000007
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=1e6+10,inf=1e9+10;
    int a[N];
    int dpi[N][30];//存位置
    int dpa[N][30];
    int minn(int x,int y)
    {
        return a[x]<=a[y]?x:y;
    }
    void rmqi(int len)
    {
        for(int i=0; i<len; i++)
        dpi[i][0]=i;
        for(int j=1; (1<<j)<len; j++)
        for(int i=0; i+(1<<j)-1<len; i++)
        dpi[i][j]=minn(dpi[i][j-1],dpi[i+(1<<(j-1))][j-1]);
    }
    int queryi(int l,int r)
    {
        int x=(int)(log((double)(r-l+1))/log(2.0));
        return minn(dpi[l][x],dpi[r-(1<<x)+1][x]);
    }
    int maxx(int x,int y)
    {
        return a[x]>=a[y]?x:y;
    }
    void rmqa(int len)
    {
        for(int i=0; i<len; i++)
        dpa[i][0]=i;
        for(int j=1; (1<<j)<len; j++)
        for(int i=0; i+(1<<j)-1<len; i++)
        dpa[i][j]=maxx(dpa[i][j-1],dpa[i+(1<<(j-1))][j-1]);
    }
    int querya(int l,int r)
    {
        int x=(int)(log((double)(r-l+1))/log(2.0));
        return maxx(dpa[l][x],dpa[r-(1<<x)+1][x]);
    }
    int main()
    {
        int x,y,q,i,t;
        while(~scanf("%d%d",&x,&q))
        {
            for(i=0;i<x;i++)
            scanf("%d",&a[i]);
            rmqi(x);
            rmqa(x);
            while(q--)
            {
                int l,r;
                scanf("%d%d",&l,&r);
                if(l>r)
                swap(l,r);
                printf("%d
    ",a[querya(l-1,r-1)]-a[queryi(l-1,r-1)]);
            }
        }
    }
  • 相关阅读:
    eclipse里面自动添加get和set方法
    初探内联方式的 onload="doSomething()"为何要加"()"?而js代码的 onload="doSomething" 和 addEventListener 为何不加"()"?
    ubuntu下安装git,sublime,nodejs
    ajax学习计划
    ajax学习笔记
    滑动窗口思路精髓总结
    Java给定一个字符串,分割字符串使得每个子字符串都是回文串,求最少分割次数
    顺时针打印
    戳气球最少需要几下一样的题
    ip覆盖算法
  • 原文地址:https://www.cnblogs.com/jhz033/p/5576797.html
Copyright © 2011-2022 走看看