[Poj 3264] balanced lineup (ST Algorithm for rmq)

Source: Internet
Author: User

Description

For the daily milking, Farmer John'sNCows (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 shocould not differ too much in height.

Farmer John has made a listQ(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, NAnd 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 AAnd B(1 ≤ ABN), Representing the range of cows from ATo BIntrusive.

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 31734251 54 62 2

Sample output

630

Source

Usaco 2007 January silver

The question is very watery. It is not necessary to mention the bare rmq. for the difference between the maximum value and the absolute value of the minimum value of each input and output range, note that the input and output use CIN cout will time out and only scanf printf can be used, the final speed is 1719 Ms.

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define MAXN 50100using namespace std;int max(int a,int b){    if(a>b) return a;    return b;}int min(int a,int b){    if(a<b) return a;    return b;}int minRMQ[MAXN][18],maxRMQ[MAXN][18];int getMinRMQ(int l,int r){    int pos=(int)(log((r-l+1)*1.0)/log(2.0));    return min(minRMQ[l][pos],minRMQ[r-(1<<pos)+1][pos]);}int getMaxRMQ(int l,int r){    int pos=(int)(log((r-l+1)*1.0)/log(2.0));    return max(maxRMQ[l][pos],maxRMQ[r-(1<<pos)+1][pos]);}int main(){    int n,m,q;    scanf("%d%d",&n,&q);    for(int i=1;i<=n;i++)    {        scanf("%d",&minRMQ[i][0]);        maxRMQ[i][0]=minRMQ[i][0];    }    m=(int)(log(n*1.0)/log(2.0));    for(int i=1;i<20;i++)        for(int j=1;j<=n;j++)        {            if(j+(1<<i)-1<=n)            {                maxRMQ[j][i]=max(maxRMQ[j][i-1],maxRMQ[j+(1<<(i-1))][i-1]);                minRMQ[j][i]=min(minRMQ[j][i-1],minRMQ[j+(1<<(i-1))][i-1]);            }        }    for(int i=1;i<=q;i++)    {        int a,b;        scanf("%d%d",&a,&b);        printf("%d\n",getMaxRMQ(a,b)-getMinRMQ(a,b));    }    return 0;}

Zookeeper

[Poj 3264] balanced lineup (ST Algorithm for rmq)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.