POJ3264 Balanced Lineup

Source: Internet
Author: User

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 44720 Accepted: 20995
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

Source

Usaco January Silver

St algorithm to find the most value in interval

1#include <algorithm>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intmxn=60000;8 intn,q;9 intH[MXN];Ten intfmx[mxn][ -],fmi[mxn][ -];//F[i][j] Represents the target value from the start of I to the i* (1<<j) range One intST (intAintb) {//St algorithm, multiplication to find the maximum interval A     inti,j; -      for(i=1; i<=n;i++) {//itself -fmx[i][0]=fmi[i][0]=H[i]; the     } -     intK= (int) (Log (n1.0)/log (2.0));//k= logarithm of base N of 2 -      for(i=1; i<=k;i++) {//number of first layers -          for(j=1; j<=n;j++) {//The second layer is the range +fmx[j][i]=fmx[j][i-1]; -             if(J+ (1<< (I-1)) <=N) +Fmx[j][i]=max (fmx[j][i],fmx[j+ (1<< (I-1))][i-1]); Afmi[j][i]=fmi[j][i-1]; at             if(J+ (1<< (I-1)) <=N) -Fmi[j][i]=min (fmi[j][i],fmi[j+ (1<< (I-1))][i-1]); -         } -     } -     return 0; - } in intAnsmx (intAintb) {//Find maximum Value -     intK= (int) (Log (b-a+1.0)/log (2.0)); to     returnMax (fmx[a][k],fmx[b-(1&LT;&LT;K) +1][k]); + } - intAnsmi (intAintb) {//Find minimum Value the     intK= (int) (Log (b-a+1.0)/log (2.0)); *     returnMin (fmi[a][k],fmi[b-(1&LT;&LT;K) +1][k]); $ }Panax Notoginseng intMain () { -scanf"%d%d",&n,&Q); the     inti,j; +      for(i=1; i<=n;i++){ Ascanf"%d",&h[i]); the     } +     intb; -ST (1, n); $      for(i=1; i<=q;i++){ $scanf"%d%d",&a,&b); -printf"%d\n", Ansmx (A, B)-ansmi (A, b));//the difference between the maximum and minimum values in the output asking interval -     } the     return 0; -}

POJ3264 Balanced Lineup

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.