Poj 3264 balanced lineup (rmq)

Source: Internet
Author: User
Balanced lineup
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:24349   Accepted:11348
Case time limit:2000 ms

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, 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 Intrusive.

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
# Include <stdio. h> # Include <Iostream> # Include <Math. h># Include < String . H> Using   Namespace  STD;  Const   Int Maxn = 50050  ;  Int Dpmax [maxn] [ 20  ];  Int Dpmin [maxn] [ 20  ];  Void Makemaxrmq ( Int N, Int  B []) {  For ( Int I = 0 ; I <n; I ++ ) Dpmax [I] [  0 ] = B [I];  For ( Int J = 1 ;( 1 <J) <= N; j ++ ) For ( Int I = 0 ; I + ( 1 <J )- 1 <N; I ++ ) Dpmax [I] [J] = Max (dpmax [I] [J- 1 ], Dpmax [I + ( 1 <(J- 1 )] [J- 1  ]);}  Int Getmax ( Int U,Int  V ){  Int K = ( Int ) (Log (V-U + 1.0 )/Log ( 2.0  ));  Return Max (dpmax [u] [K], dpmax [V -( 1 <K) + 1  ] [K]);}  Void Makeminrmq ( Int N, Int B []) {  For ( Int I = 0 ; I <n; I ++ ) Dpmin [I] [  0 ] = B [I];  For ( Int J = 1 ;( 1 <J) <= N; j ++ )  For ( Int I =0 ; I + ( 1 <J )- 1 <N; I ++ ) Dpmin [I] [J] = Min (dpmin [I] [J- 1 ], Dpmin [I + ( 1 <(J- 1 )] [J- 1  ]);}  Int Getmin ( Int U, Int  V ){  Int K = (Int ) (Log (V-U + 1.0 )/Log ( 2.0  ));  Return Min (dpmin [u] [K], dpmin [V -( 1 <K) + 1  ] [K]);}  Int  A [maxn];  Int  Main (){  Int  N, Q;  Int U, V;  While (Scanf ( "  % D  " , & N, & Q )! = EOF ){  For ( Int I = 0 ; I <n; I ++ ) Scanf (  "  % D  " ,& A [I]); makemaxrmq (n, a); makeminrmq (n, ); While (Q -- ) {Scanf (  "  % D  " , & U ,& V); U -- ; V -- ;  Int T1 = Getmax (u, v );  Int T2 = Getmin (u, v); printf (  "  % D \ n " , T1- T2 );}}  Return   0  ;} 

 

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.