codevs3304 Fruit Street Shopping

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

/*consider the line segment tree, each answer may be obtained within the interval, can also be obtained between the interval, the first case directly in the revision of the time to record, the second can be used in DFS sequence*/#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#definell Long Long#defineFo (i,l,r) for (int i = l;i <= r;i++)#defineFD (I,L,R) for (int i = r;i >= l;i--)#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intN =200000; ll read () {ll x=0, f=1; CharCh=GetChar ();  while(! (ch>='0'&&ch<='9')){if(ch=='-') f=-1; ch=GetChar ();};  while(ch>='0'&&ch<='9') {x=x*Ten+ (ch-'0'); ch=GetChar ();}; returnx*F;}intn,m,u,v;BOOLf;ll minv[n<<4],maxv[n<<4],d1[n<<4],d2[n<<4],reca[n<<4],recb[n<<4],val[n],cnt;ll ans;voidMaintain (intRT) {Minv[rt]= Min (minv[rt<<1],minv[rt<<1|1]); MAXV[RT]= Max (maxv[rt<<1],maxv[rt<<1|1]); D1[RT]= Max (d1[rt<<1],d1[rt<<1|1]); D1[RT]= Max (d1[rt],maxv[rt<<1|1]-minv[rt<<1]); D2[RT]= Max (d2[rt<<1],d2[rt<<1|1]); D2[RT]= Max (d2[rt],maxv[rt<<1]-minv[rt<<1|1]);}voidBuildintLintRintRT) {    if(L = =R) {Minv[rt]= Maxv[rt] =Val[l]; D1[RT]= D2[rt] =0; }Else{        intm = (L + r) >>1;        Build (Lson);        Build (Rson);    Maintain (RT); } }voidQueryintLintRintRT) {    if(U <= l && v >=R) {        if(!f) ans =Max (Ans,d1[rt]); ElseAns =Max (Ans,d2[rt]); reca[++CNT] =Minv[rt]; RECB[CNT]=Maxv[rt]; }Else{        intm = (L + r) >>1; if(U <=m) query (Lson); if(V >m) query (Rson); }}voidsolve () {CNT= ans =0; F=false; if(U >v)        {swap (U,V); F=true; } query (1N1); LL TMP=987654321012345LL; if(!f) {FO (i,2, CNT) {tmp= Min (tmp,reca[i-1]); Ans= Max (ans,recb[i]-tmp); }    }Else{FD (i,1, cnt-1) {tmp= Min (tmp,reca[i+1]); Ans= Max (ans,recb[i]-tmp); }} printf ("%lld\n", ans);}intMain () {n=read (); Fo (i,1, n) val[i] =read (); Build (1N1); M=read (); Fo (i,1, M) {u= Read (); v=read (); solve (); }    return 0;} 

codevs3304 Fruit Street Shopping

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.