Poj3264 balanced lineup

Source: Internet
Author: User

This question is very basic, almost a StAlgorithm.

This question can be used to practice the st Algorithm for the first time.

 

Balanced lineup
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:23289   Accepted:10827
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

 

 

# Include <iostream> # include <cstring> # include <cmath> using namespace STD; int dp1 [50050] [20]; // MININT dp2 [50050] [20]; // maxint cow [50050]; int n, m; int max (int A, int B) {return A> B? A: B;} int min (int A, int B) {return a <B? A: B;} void Init () {for (INT I = 1; I <= N; I ++) {scanf ("% d ", & cow [I]); dp1 [I] [0] = dp2 [I] [0] = cow [I];} int K = floor (log (double) (n + 1)/log (2.0); int m; For (Int J = 1; j <= K; j ++) for (INT I = 0; I + (1 <j)-1 <= N; I ++) {M = I + (1 <(J-1 )); dp1 [I] [J] = min (dp1 [I] [J-1], dp1 [m] [J-1]); dp2 [I] [J] = max (dp2 [I] [J-1], dp2 [m] [J-1]);} int rmq (int I, Int J) {int M = floor (log (double) (J-I + 1)/log (2.0 ))); int x = max (dp2 [I] [m], dp2 [J-(1 <m) + 1] [m]); int y = min (dp1 [I] [m], dp1 [J-(1 <m) + 1] [m]); Return x-y ;} int main () {While (CIN> N> m) {Init (); int X, Y; For (INT I = 1; I <= m; I ++) {scanf ("% d", & X, & Y); cout <rmq (x, y) <Endl ;}} 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.