POJ 3264 Balanced Lineup (line segment Tree | | RMQ)

Source: Internet
Author: User

a-balanced Lineup crawling in process ... crawling failed time limit:5000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u SubmitStatus Practice POJ 3264

Description

For the daily milking, Farmer John's n cows (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'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has fun they should not differ too much in height.

Farmer John has made a list of Q (1≤ q ≤200,000) Potential groups of cows and their heights (1≤ H Eight ≤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 G Roup.

Input

Line 1:two space-separated integers, Nand Q.
Lines 2.. N+1:line I+1 contains a single integer which 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 BInclusive.

Output

Lines 1.. Q: Each line contains a single integer so is a response to a reply and indicates the difference in height between the Tal Lest and shortest cow in the range.

Sample Input

6 31734251 54) 62 2

Sample Output

630

Main topic:

The question is, give you n number (n<=50000), then Q (q<=2*100000) a query, in each query interval, the maximum and minimum value of the difference is how much?

Problem Solving Ideas:

Directly with the line segment tree can be passed, each node corresponds to an interval, directly logn the time to query the maximum and minimum values, and then through the difference between the two can get the final result.

Code:

1# include<cstdio>2# include<iostream>3 4 using namespacestd;5 6# define MAX500047# define Lid id<<18# define RID id<<1|19 Ten structSegtree One { A     intL,r; -     intmx,mn; -}tree[max*4]; the intA[max]; -  - voidPUSH_UP (intID) - { +tree[id].mx =Max (tree[lid].mx,tree[rid].mx); -TREE[ID].MN =min (tree[lid].mn,tree[rid].mn); + } A  at voidBuildintIdintLintR) - { -TREE[ID].L = l; TREE[ID].R =R; -     if(l==R) -     { -tree[id].mx = Tree[id].mn =A[l]; in         return; -     } to     intMid = (TREE[ID].L+TREE[ID].R)/2; + build (Lid,l,mid); -Build (rid,mid+1, R); the push_up (ID); * } $ Panax Notoginseng intQuery1 (intIdintLintR) - { the     if(tree[id].l==l&&tree[id].r==R) +     { A         returntree[id].mx; the     } +     intMid = (TREE[ID].L+TREE[ID].R)/2; -     if(R <=mid) $         returnQuery1 (lid,l,r); $     Else if(L >mid) -         returnQuery1 (rid,l,r); -     Else the     { -         returnMax (Query1 (Lid,l,mid), Query1 (rid,mid+1, R));Wuyi     } the } -  Wu intQuery2 (intIdintLintR) - { About     if(tree[id].l==l&&tree[id].r==R) $     { -         returntree[id].mn; -     } -     intMid = (TREE[ID].L+TREE[ID].R)/2; A     if(R <=mid) +         returnQuery2 (lid,l,r); the     Else if(L >mid) -         returnQuery2 (rid,l,r); $     Else the     { the         returnMin (Query2 (lid,l,mid), Query2 (rid,mid+1, R)); the     } the } -  in  the intMainvoid) the { About     intN scanf"%d",&n); the     intQ scanf"%d",&q); the      for(inti =1; I <= n;i++ ) the     { +scanf"%d",&a[i]); -     } theBuild1,1, n);Bayi      while(q-- ) the     { the         intT1,t2; scanf"%d%d",&t1,&T2); -         intAns = query1 (1, T1,t2)-query2 (1, t1,t2); -printf"%d\n", ans); the     } the  the  the     return 0; -}

POJ 3264 Balanced Lineup (line segment Tree | | RMQ)

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.