Poj 3264 balanced lineup line segment tree

Source: Internet
Author: User

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, NAnd 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 AAnd B(1 ≤ ABN), Representing the range of cows from ATo BIntrusive.

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 <cstdlib> # include <cstdio> # include <cstring> # include <climits> using namespace STD;/* int max (int A, int B) {return A> B? A: B;} */const int maxn = 50010; struct tnode {int L, R, mMax, mmin;}; tnode node [maxn * 4]; int A [maxn], maxv, MINV; void Init (int K, int L, int R) // The interval corresponding to node K is [L, R) {node [K]. L = L; node [K]. R = r; If (R-l = 1) {// If the interval has only one element, node [K]. mMax = node [K]. mmin = A [l]; return;} int mid = (L + r)/2; Init (2 * k + 1, L, mid ); init (2 * k + 2, mid, R); // construct left and right subtree node [k] respectively. mMax = max (node [2 * k + 1]. mMax, node [2 * k + 2]. mMax); node [K]. mmin = min (node [2 * k + 1]. mmin, node [2 * k + 2]. mmin);} void query (int l, int R, int K) {If (node [K]. L = L & node [K]. R = r) {// the query interval and node interval completely coincide with maxv = max (maxv, node [K]. mMax); MINV = min (MINV, node [K]. mmin); return;} int mid = (node [K]. L + node [K]. r)/2; If (mid <= L) query (L, R, 2 * k + 2 ); // The queried range is in the right subtree else if (mid> = r) query (L, R, 2 * k + 1 ); // The queried range is in the left subtree else {query (L, mid, 2 * k + 1); // The queried range is in the left subtree query (MID, r, 2 * k + 2) ;}} int main () {int N, Q; while (CIN >>n> q) {memset (node, 0, sizeof (node); For (INT I = 0; I <n; I ++) scanf ("% d", & A [I]); Init (0, 0, n); // initialize the line segment tree for (INT I = 0; I <q; I ++) {int X, Y; scanf ("% d ", & X, & Y); maxv = 0; MINV = int_max; query (x-1, Y, 0 ); // here is the left closed right open range cout <maxv-MINV <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.