Poj 3264-balanced lineup (line segment tree: Single Point update + interval query)

Source: Internet
Author: User
Balanced lineup
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:34522   Accepted:16224
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, 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

Source

Usaco 2007 January Silver said by ysj that I was also preparing to roll the line segment tree. That afternoon, he came to tell me about the line segment tree and typed a template first .. The difference between the maximum and minimum values of a certain interval.
# Include <cstdio> # include <iostream> # include <algorithm> # include <cstring> # include <cctype> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> # include <list> # define ll long longusing namespace STD; const int INF = 1 <27; const int maxn = 200010; ll Minn [maxn], Maxx [maxn]; void Update (LL root, ll l, ll R, ll p, ll v) // Single Point update {If (L = r) Maxx [root] = V; Minn [root] = V; If (L <r) {ll Mid = (L + r)/2; If (P <= mid) Update (root * 2, L, mid, P, V ); else Update (root * 2 + 1, Mid + 1, R, P, V); Maxx [root] = max (Maxx [root * 2], maxx [root * 2 + 1]); Minn [root] = min (Minn [root * 2], Minn [root * 2 + 1]);} ll query_min (LL root, ll l, ll R, ll Ql, ll QR) {ll mid = (L + r)/2, ANS = inf; if (QL <= L & QR> = r) return Minn [root]; If (QL <= mid) ans = min (ANS, query_min (root * 2, l, mid, Ql, QR); If (QR> mid) ans = min (ANS, query_min (root * 2 + 1, Mid + 1, R, Ql, QR )); return ans;} l L query_max (LL root, ll l, ll R, ll Ql, ll QR) {ll mid = (L + r)/2, ANS =-INF; if (QL <= L & QR> = r) return Maxx [root]; If (QL <= mid) ans = max (ANS, query_max (root * 2, l, mid, Ql, QR); If (QR> mid) ans = max (ANS, query_max (root * 2 + 1, Mid + 1, R, Ql, QR )); return ans;} int main () {int N, Q, I, V; while (~ Scanf ("% LLD", & N, & Q) {for (I = 1; I <= N; I ++) {scanf ("% LLD ", & V); Update (1,1, N, I, V) ;}while (Q --) {int Ql, Qr; scanf ("% LLD", & Ql, & QR); printf ("% LLD \ n", query_max (, N, Ql, QR)-query_min (, N, Ql, QR ));}} 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.