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.
Inputline 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≤
A≤
B≤
N), representing the range of cows from
ATo
BInclusive. Outputlines 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
Sourceusaco January Silver
Given an interval, the difference between the maximum value and the minimum value is calculated.
St Table
#include <iostream>#include<cstdio>#include<cstring>#defineN 50010using namespacestd;intff[1<< -];inta[n],rmq[ -][n],rmx[ -][n];intn,m;voidRmq_init () { for(intI=1; i<=n;i++) {rmq[0][i] =A[i]; rmx[0][i] =A[i]; } for(intj=1;j< -; j + +) for(intI=1; i+ (1<<J)-1<=n;i++) {Rmq[j][i]= Min (rmq[j-1][i],rmq[j-1][i+ (1<<j>>1)]); Rmx[j][i]= Max (rmx[j-1][i],rmx[j-1][i+ (1<<j>>1)]); } memset (FF,-1,sizeof(FF)); for(intI=0;i< -; i++) ff[1<<i] =i; for(intI=0; i<n;i++) if(Ff[i] = =-1) Ff[i]= ff[i-1];} Pair<int,int> Rmq_find (intXinty) { intt=ff[y-x+1]; returnMake_pair (min (rmq[t][x],rmq[t][y-(1<<T) +1]), Max (Rmx[t][x],rmx[t][y-(1<<T) +1]));}intMain () {intL,r; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Rmq_init (); for(intI=1; i<=m;i++) {scanf ("%d%d",&l,&R); printf ("%d\n", Rmq_find (l,r). second-Rmq_find (l,r). first); }}
"Poj3264" Balanced lineup