zoukankan      html  css  js  c++  java
  • poj 3264 Balanced Lineup RMQ问题

    Balanced Lineup

    Time Limit: 1 Sec  Memory Limit: 256 MB

    题目连接

    http://poj.org/problem?id=3264

    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

    HINT

    题意

    给你n个数,然后查询区间的最大值减去最小值是多少

    题解:

    线段树RMQ,也可以ST来做,都是nlogn的复杂度

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    //**************************************************************************************
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    struct node
    {
        int l,r,ma,mi;
    }a[maxn];
    int d[maxn];
    void build(int x,int l,int r)
    {
        a[x].l=l,a[x].r=r;
        a[x].ma=-inf,a[x].mi=inf;
        if(l==r)
        {
            a[x].ma=d[l];
            a[x].mi=d[l];
            return;
        }
        else
        {
            int mid=(l+r)>>1;
            build(x<<1,l,mid);
            build(x<<1|1,mid+1,r);
            a[x].ma=max(a[x<<1].ma,a[x<<1|1].ma);
            a[x].mi=min(a[x<<1].mi,a[x<<1|1].mi);
        }
    }
    int query1(int x,int st,int ed)
    {
        int l=a[x].l,r=a[x].r;
        if(st<=l&&r<=ed)
            return a[x].mi;
        else
        {
            int mid=(l+r)>>1;
            int mi1=inf,mi2=inf;
            if(st<=mid)
                mi1=query1(x<<1,st,ed);
            if(ed>mid)
                mi2=query1(x<<1|1,st,ed);
            return min(mi1,mi2);
        }
    }
    
    int query2(int x,int st,int ed)
    {
        int l=a[x].l,r=a[x].r;
        if(st<=l&&r<=ed)
            return a[x].ma;
        else
        {
            int mid=(l+r)>>1;
            int mi1=-inf,mi2=-inf;
            if(st<=mid)
                mi1=query2(x<<1,st,ed);
            if(ed>mid)
                mi2=query2(x<<1|1,st,ed);
            return max(mi1,mi2);
        }
    }
    
    int main()
    {
        int n=read(),q=read();
        int dd,ee;
        for(int i=1;i<=n;i++)
            d[i]=read();
        build(1,1,n);
        for(int i=0;i<q;i++)
        {
            dd=read(),ee=read();
            printf("%d
    ",query2(1,dd,ee)-query1(1,dd,ee));
        }
    }
  • 相关阅读:
    DrawerLayout侧滑菜单
    pagerslidingtabstrip 横向滑动
    Android对话框
    关于不同页面的传参分析
    ajax异步传输数据,return返回值为空
    mCustomScrollbar 滚动条的使用
    angular实现表格的全选、单选、部分删除以及全部删除
    iOS风格的弹出框(alert,prompt,confirm)
    下拉刷新,上拉加载功能--dropload.js的使用
    页面水印效果的实现--新篇
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4447979.html
Copyright © 2011-2022 走看看