POJ 3264 Balanced Lineup (RMQ)

Source: Internet
Author: User

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 36513 Accepted: 17103
Case Time Limit: 2000MS

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'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has 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≤ H Eight ≤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 G Roup.

Input

Line 1:two space-separated integers, Nand Q.
Lines 2.. N+1:line I+1 contains a single integer which 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 BInclusive.

Output

Lines 1.. Q: Each line contains a single integer so is a response to a reply and indicates the difference in height between the Tal Lest and shortest cow in the range.

Sample Input

6 31734251 54) 62 2

Sample Output

630

Find the interval maximum minimum difference

#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>#include<algorithm>using namespacestd;#defineRoot 1,n,1#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1#defineLR rt<<1#defineRR rt<<1|1typedefLong LongLL;Const intOO = 1e9+7;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6 ;Const intN =50010;Const intMoD =2333333;intdp_m[n][ -], dp_m[n][ -], Mm[n], b[n], N, m;voidINITRMQ (intN) {mm[0] = -1 ;  for(inti =1; I <= N; ++i) {Mm[i]= ((i& (i-1)) ==0)? mm[i-1]+1: mm[i-1]; dp_m[i][0]=dp_m[i][0]=B[i]; }     for(intj =1; J <= Mm[n]; ++j) { for(inti =1; i+ (1&LT;&LT;J)-1<=n; ++i) {dp_m[i][j]= Max (dp_m[i][j-1], dp_m[i+ (1<< (J-1))][j-1]); DP_M[I][J]= Min (dp_m[i][j-1], dp_m[i+ (1<< (J-1))][j-1]); }    }}intRmqintXinty) {    intK = mm[y-x+1]; returnMax (dp_m[x][k],dp_m[y-(1&LT;&LT;K) +1][k])-min (dp_m[x][k],dp_m[y-(1&LT;&LT;K) +1][k]);}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);//freopen ("OUT.txt", "w", stdout);    #endif //LOCAL    int_, X, Y, C;  while(~SCANF ("%d%d",&n,&m)) { for(inti =1; I <= N; ++i) {scanf ("%d",&B[i]);        } INITRMQ (n);  while(m--) {scanf ("%d%d",&x,&y); printf ("%d\n", RMQ (x, y)); }    }}
View Code

POJ 3264 Balanced Lineup (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.