P2880 [usaco 07jan] Balanced Lineup and p2880usaco 07jan

Source: Internet
Author: User

P2880 [usaco 07jan] Balanced Lineup and p2880usaco 07jan
Background

Description:

Every day, John's N (1 <=n <= 50,000) cows always queue in the same sequence. one day John decided to let some cows play a frisbee game. he is going to find a group of continuous cattle in the pair column for the competition. however, to avoid horizontal disparity, the height of a cow should not be too different. john prepared Q (1 <= Q <= 180,000) a possible selection of cattle and the height of all cattle (1 <= height <= 1,000,000 ). he wants to know the height difference between the highest and lowest cattle in each group.

Input:

Row 3: N, Q

Rows from 2nd to N + 1: height of each ox

Line N + 2 to N + Q + 1: two integers A and B, representing all the cows from A to B. (1 <= A <= B <= N)

Output:

Output one number per line, which is the difference between the maximum number and the minimum number.

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 shocould 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.

A farmer has a nheaded ox, and the height of each ox is different. We need to find the highest and lowest bits.

Input/Output Format

Input Format:

 

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 Format:

 

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.

 

Input and Output sample input sample #1: Copy
6 31734251 54 62 2
Output example #1: Copy
630

Bare earth ST table

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define LL long long  7 #define lb(x)    ((x)&(-x)) 8 using namespace std; 9 const int MAXN=500001;10 inline int read()11 {12     char c=getchar();int x=0,f=1;13     while(c<'0'||c>'9')    {if(c=='-')    f=-1;c=getchar();}14     while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*f;15 }16 int dpmin[MAXN][21];17 int dpmax[MAXN][21];18 int n,m;19 int getmax(int l,int r)20 {21     int k=log2(r-l+1);22     return max(dpmax[l][k],dpmax[r-(1<<k)+1][k]);23 }24 int getmin(int l,int r)25 {26     int k=log2(r-l+1);27     return min(dpmin[l][k],dpmin[r-(1<<k)+1][k]);28 }29 int main()30 {31     n=read();m=read();32     for(int i=1;i<=n;i++)33         dpmax[i][0]=read(),dpmin[i][0]=dpmax[i][0];34     for(int i=1;i<=19;i++)35         for(int j=1;j<=n&&j+(1<<i)-1<=n;j++)36             dpmax[j][i]=max(dpmax[j][i-1],dpmax[j+(1<<i-1)][i-1]),37             dpmin[j][i]=min(dpmin[j][i-1],dpmin[j+(1<<i-1)][i-1]);38     for(int i=1;i<=m;i++)39     {40         int x=read(),y=read();41         printf("%d\n",getmax(x,y)-getmin(x,y));42     }43     return 0;44 }

 

 

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.