Poj3264-balanced lineup

Source: Internet
Author: User

Want to see more problem-solving reports:
Http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source:Http://blog.csdn.net/wangjian8006

Give a string of numbers, then give a range of a B, and output the maximum number from A to B and the Minimum Number Difference

Solution: Line Segment tree

use the line segment tree to obtain the minimum and maximum values from A to B, directly subtract the output

# Include <iostream> using namespace STD; # define maxv 50100 # define maxt 1 <18 # define max (A, B) A> B? A: B # define min (A, B) A> B? B: aint a [maxv], Tl [maxt], TR [maxt], Tmax [maxt], tmin [maxt]; // TL and TR represent the numbers on the left and right of the node v range, which facilitates the query // Tmax and tmin, representing the maximum and minimum values of node v void createtree (int v, int F, int t) {TL [v] = f; tr [v] = T; If (F = T) {Tmax [v] = A [f]; tmin [v] = A [f]; return;} int mid = (F + T)> 1; createtree (v <1, F, mid ); createtree (v <1) | 1, Mid + 1, t); Tmax [v] = max (Tmax [v <1], tmax [(v <1) | 1]); tmin [v] = min (tmin [v <1], tmin [(v <1) | 1]);} int querytree (int v, int L, int R, int flag) {If (L = tl [V] & R = tr [v]) {return (flag? Tmin [v]: Tmax [v]);} int mid = (TL [v] + Tr [v])> 1; if (r <= mid) return querytree (v <1, L, R, flag); else if (L> mid) return querytree (v <1) | 1, L, R, flag); else {If (FLAG) return min (querytree (v <1, L, mid, flag), querytree (v <1) | 1, mid + 1, R, flag); else return max (querytree (v <1, L, mid, flag), querytree (v <1) | 1, mid + 1, R, flag) ;}} int main () {int N, Q, L, R, I; while (scanf ("% d ", & N, & Q )! = EOF) {for (I = 1; I <= N; I ++) scanf ("% d", & A [I]); createtree (1, 1, n ); while (Q --) {scanf ("% d", & L, & R); printf ("% d \ n", querytree (1, L, R, 0)-querytree (1, L, R, 1) ;}} 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.