P1699: [Usaco2007 jan]balanced lineup queue

Source: Internet
Author: User

is obviously a rmq problem, multiply the method, maintain the area of the maximum/small value on the line.

1 varN,i,j,q,f,t,times:longint;2hmin,hmax:array[0.. -,0..50000] of Longint;3 function min (a,b:longint): Longint;4 begin5   ifA>B then exit (b)6     Elseexit (a);7 end;8 function Max (a,b:longint): Longint;9 beginTen   ifA>B then exit (a) One     Elseexit (b); A end; - begin - readln (n,q); the    fori:=1to n Do - begin -READLN (hmax[0, I]); -hmin[0, i]:=hmax[0, I]; + end; -    fori:=1to Trunc (ln (n)/ln (2))+1  Do +      forj:=1To N (1SHL i) +1  Do A begin atHmin[i,j]:=min (hmin[i-1, j],hmin[i-1, j+ (1SHL (i1))]); -Hmax[i,j]:=max (hmax[i-1, j],hmax[i-1, j+ (1SHL (i1))]); - end; -    fori:=1to Q Do - begin - readln (f,t); inTimes:=trunc (LN (t-f+1)/LN (2)); -Writeln (Max (hmax[times,f],hmax[times,t-(1SHL times) +1])- toMin (hmin[times,f],hmin[times,t-(1SHL times) +1])); + end; -End.

P1699: [Usaco2007 jan]balanced lineup queue

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.