3304 fruit elder sister stroll Fruit Street Ⅰ

Source: Internet
Author: User

3304 fruit elder sister stroll Fruit Street Ⅰ

time limit: 2 sspace limit: 256000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle 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

Category labels Tags Click here to expandPartition-type DP dynamic programming interval DP segment tree tree structure

Exercises

For an interval, the answer is (take the example from left to right):

max{右区间的最大值-左区间的最小值,左区间的答案,右区间的答案}

Because it is possible to walk from right to left, the line tree records:

Maximum, minimum, answer from left to right, answer from right to left.

#include <bits/stdc++.h>using namespacestd;#defineN 200001#defineLC K<<1#defineRC K<<1|1#defineMid (L+r>>1)structss{intMaxn,minn,ans1,ans2;} A[n<<2];intXl[n],n,m;inlinevoidUpdata (intk) {A[K].MAXN=Max (A[LC].MAXN,A[RC].MAXN); A[k].minn=min (A[lc].minn,a[rc].minn); intt=a[rc].maxn-A[lc].minn; intc=a[lc].maxn-A[rc].minn; A[k].ans1=Max (T,max (a[lc].ans1,a[rc].ans1)); A[k].ans2=Max (C,max (A[RC].ANS2,A[LC].ANS2));}voidBuildintKintLintR) {    if(l==R) {A[K].MAXN=a[k].minn=Xl[l]; A[k].ans1=a[k].ans2=0; return;    } build (Lc,l,mid); Build (Rc,mid+1, R); Updata (k);} SS Query (intKintLintRintXinty) {    if(y<l| | X&GT;R)return(ss) {-1,100000001,-1,-1}; if(X&LT;=L&AMP;&AMP;Y&GT;=R)returnA[k]; SS Tmp1=query (Lc,l,mid,x,y), Tmp2=query (rc,mid+1, r,x,y), TMP; Tmp.ans1=max (Tmp1.ans1,max (tmp2.ans1,tmp2.maxn-Tmp1.minn)); Tmp.ans2=max (Tmp1.ans2,max (tmp2.ans2,tmp1.maxn-Tmp2.minn)); Tmp.minn=min (Tmp1.minn,tmp2.minn); TMP.MAXN=Max (TMP1.MAXN,TMP2.MAXN); returntmp;}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d",&Xl[i]); Build (1,1, N); scanf ("%d",&m);  for(intI=1, l,r;i<=m;i++) {scanf ("%d%d",&l,&R); if(l<=r) printf ("%d\n", Query (1,1, N,l,r). ans1); Elseprintf ("%d\n", Query (1,1, n,r,l). Ans2); }    return 0;} 

3304 fruit elder sister stroll Fruit Street Ⅰ

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.