Fruit elder sister Stroll Fruit Street Ⅰ (Codevs 3304)

Source: Internet
Author: User

Title Description Description

Fruit sister today in a good mood, came to the fruit street.

Fruit Street has n fruit shop, in a straight line structure, numbered 1~n, each store can buy fruit can also sell fruit, and the same shop to sell and buy the same price.

Learn Oi fruit sister quickly found a way to make money: in a fruit shop to buy a fruit, and then sell to another shop, make a difference.

In the fruit when the elder sister, Cgh suddenly appeared, he in order to embarrass Fruit elder sister, give M a question, each question request fruit elder sister from the X home to the first shop, on the way only choose a shop to buy a fruit, then choose a shop (can be the same shop, but can't go back) sell out, Ask for the maximum amount of money you can make in each question.

Enter a description Input Description

The first line is n, which means n stores

Down n A positive integer that represents the price of an apple per store.

Down an integer m, which represents down there m inquiries.

Down there are M-lines, two integers x and y per line, indicating the departure from the X home to the nth store.

Output description Output Description

There are M lines.

Each line corresponds to a query, an integer, indicating the face of cgh each query, fruit elder sister can earn up to how much money.

Sample input Sample Input

10
2 8 15 1 10 5 19 19 3 5
4
6 6
2 8
2 2
6 3

Sample output Sample Output

0
18
0
14

Data range and Tips Data Size & Hint

0<= Apple Price <=10^8

0<n,m<=200000

/*for each interval, maintain its minimum value, maximum value from left to right, maximum profit from right to left, maximum benefit in one interval =max (left child maximum benefit, right child maximum benefit, right child max-left child minimum)*/#include<iostream>#include<cstdio>#defineLson l,m,now*2#defineRson m+1,r,now*2+1#defineM 800010using namespacestd;intmx[m],mn[m],lmax[m],rmax[m],a[m/4];inttot;voidPUSH_UP (intNow ) {Mx[now]=max (mx[now*2],mx[now*2+1]); Mn[now]=min (mn[now*2],mn[now*2+1]); Lmax[now]=max (Max (lmax[now*2],lmax[now*2+1]), mx[now*2+1]-mn[now*2]); Rmax[now]=max (Max (rmax[now*2],rmax[now*2+1]), mx[now*2]-mn[now*2+1]);}voidBuildintLintRintNow ) {    if(l==R) {Mx[now]=mn[now]=A[l]; return; }    intM= (L+R)/2;    Build (Lson);    Build (Rson); PUSH_UP (now);}intGet_max (intXintYintLintRintNow ) {    if(x<=l&& y>=r)returnMx[now]; intm = (l+r)/2; if(x>m)returnGet_max (X,y,rson); Else if(y<=m)returnGet_max (X,y,lson); Else returnMax (Get_max (m+1, Y,rson), Get_max (X,m,lson));}intGet_min (intXintYintLintRintNow ) {    if(x<=l&& y>=r)returnMn[now]; intm = (l+r)/2; if(x>m)returnget_min (X,y,rson); Else if(y<=m)returnget_min (X,y,lson); Else returnMin (Get_min (m+1, Y,rson), Get_min (X,m,lson));}intQuery_l (intXintYintLintRintNow ) {    intM= (L+R)/2; if(x<=l&& y>=r)returnLmax[now]; if(y<=m)returnquery_l (X,y,lson); Else if(x>m)returnquery_l (X,y,rson); Else     {        inttemp =0; Temp=Max (query_l (X,y,lson), query_l (X,y,rson)); Temp=max (Temp,get_max (X,y,rson)-get_min (X,y,lson)); returntemp; }}intQuery_r (intXintYintLintRintNow ) {    intM= (L+R)/2; if(x<=l&& y>=r)returnRmax[now]; if(y<=m)returnQuery_r (X,y,lson); Else if(x>m)returnQuery_r (X,y,rson); Else     {        inttemp =0; Temp=Max (Query_r (X,m,lson), Query_r (X,y,rson)); Temp=max (Temp,get_max (X,y,lson)-get_min (X,y,rson)); returntemp; }}intMain () {intN,m,l,r; scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Build (1N1); scanf ("%d",&m);  for(intI=1; i<=m;i++) {scanf ("%d%d",&l,&R); if(l<r) printf ("%d\n", query_l (L,r,1N1)); Elseprintf"%d\n", Query_r (R,l,1N1)); }}
View Code

Fruit elder sister Stroll Fruit Street Ⅰ (Codevs 3304)

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.