zoukankan      html  css  js  c++  java
  • poj 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

     
    题目大意:

    描述

    每天挤奶,农民约翰的N牛(1≤N≤50000)总是在相同的顺序排列。一天农夫约翰决定组织一场极限飞盘的奶牛。为简单起见,他将一个连续的范围的奶牛挤奶阵容来玩这个游戏。然而,对于所有的奶牛很有趣他们不应该太多不同的高度。

    农民约翰列了一个清单,问(1 Q≤≤200000)潜在的牛组和他们的高度(1≤高度≤1000000)。对于每一组,他想要你的帮助确定最短之间的高度差和最高的牛。

    输入

    1号线:两个空格分隔的整数,N和Q。
    行2 . .N + 1:i + 1行包含一个整数,牛我的高度
    行N + 2 . .N + Q + 1:A和B两个整数(1≤≤B≤N),代表牛从A到B包容的范围。

    输出

    行1 . .问:每一行包含一个整数,响应回复和显示之间的高度差最高和最短的牛的范围。
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    const int N=200001;
    
    int n,m;
    int a[N],maxrmq[N][51],minrmq[N][51];
    
    inline int read()
    {
        int x=0;int f=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();    }
        while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
        return x*f;
    }
    
    int rmqmax(int l,int r)
    {
        int k(0);
        while(l+(1<<(k+1))<=r+1)k++;
        return max(maxrmq[l][k],maxrmq[r-(1<<k)+1][k]);
    }
    
    int rmqmin(int l,int r)
    {
        int k(0);
        while(l+(1<<(k+1))<=r+1)k++;
        return min(minrmq[l][k],minrmq[r-(1<<k)+1][k]);
    }
    
    int main()
    {
        n=read(),m=read();
        for(int i=1;i<=n;i++)a[i]=read();
        for(int i=1;i<=n;i++)maxrmq[i][0]=minrmq[i][0]=a[i];
        for(int j=1;j<=25;j++)
            for(int i=1;i+(1<<j)<=n+1;i++)
                maxrmq[i][j]=max(maxrmq[i][j-1],maxrmq[i+(1<<(j-1))][j-1]),
                minrmq[i][j]=min(minrmq[i][j-1],minrmq[i+(1<<(j-1))][j-1]);
        for(;m;m--)
        {
            int x=read(),y=read();
            printf("%d
    ",rmqmax(x,y)-rmqmin(x,y));
        }
        return 0;
    }
  • 相关阅读:
    〖教程〗Ladon提权MS16-135参数版(WIN7-2016)
    〖教程〗RDP会话劫持 Ladon无密码登陆管理员桌面会话
    Ladon for PowerShell远程加载教程
    〖教程〗NbtScan 139端口弱口令/Netbios密码爆破
    给你一个免费加入"小密圈"的机会
    活动目录(Active Directory,AD)的主要功能
    如何通过审计安全事件日志检测密码喷洒(Password Spraying)攻击
    mouseenter 和mouseover的区别
    如何获取可视区域宽高,获取元素到在文档中的位置
    闭包
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7147716.html
Copyright © 2011-2022 走看看