"Segment Tree" "Category discussion" fruit Sister stroll Fruit Street Ⅰ

Source: Internet
Author: User
Tags time limit
3304 fruit elder sister stroll Fruit Street Ⅰ
Time limit: 2 s
space limit: 256000 KB
title level: Diamonds Diamond

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.

outputs 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 to sample

2 8 1 (5) 3 5 4
6 6
2 8
2 2
6 3

sample output Sample outputs

0
0
14

Data Size & Hint

0<= Apple price <=10^8
0<n,m<=200000

The following:

The obvious line-segment tree. Maintain the interval maximum, minimum, the maximum difference from the front to the back, and the maximum difference from the backward forward. Just in the query when you need to discuss the classification, a bit disgusting.

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath  > #include <algorithm> #define root 1,1,n #define LCHILD rt<<1,l,m #define RCHILD rt<<1|1,m+1,r using
namespace Std; struct tree{int maxn,minn,chaq,chah;}
TREE[1000010];
int n,q;
    int in () {int x=0; char Ch=getchar ();
    while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar ();
    while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
return x;
    } void push_up (int rt) {Tree[rt].maxn=max (TREE[RT&LT;&LT;1].MAXN,TREE[RT&LT;&LT;1|1].MAXN);
    Tree[rt].minn=min (Tree[rt<<1].minn,tree[rt<<1|1].minn);
    int q=tree[rt<<1].maxn-tree[rt<<1|1].minn,h=tree[rt<<1|1].maxn-tree[rt<<1].minn;
    Tree[rt].chaq=max (Q,max (Tree[rt<<1].chaq,tree[rt<<1|1].chaq));
Tree[rt].chah=max (H,max (Tree[rt<<1].chah,tree[rt<<1|1].chah)); } void Build (int rt,int l,int R) {if (l==r) {TREE[RT].MAXN=tree[rt].minn=in ();
        tree[rt].chaq=tree[rt].chah=0;
    Return
    } int m= (L+R) >>1; Build (Lchild);
    Build (Rchild);
PUSH_UP (RT);
    } tree Query (int rt,int l,int r,int ll,int rr) {if (ll<=l && r<=rr) return (Tree) TREE[RT]; int m= (L+R) >>1;
    Tree S1,s2,s3;
    S1=s2=s3= (Tree) {0,0x7fffffff,0,0};
    if (ll<=m) s1= (Tree) query (LCHILD,LL,RR);
    if (rr>m) s2= (Tree) query (RCHILD,LL,RR); S3.maxn=max (S1.MAXN,S2.MAXN);
    S3.minn=min (S1.minn,s2.minn);
    S3.chaq=max (Max (S1.chaq,s2.chaq), S1.maxn-s2.minn);
    S3.chah=max (Max (S1.chah,s2.chah), S2.maxn-s1.minn);
return S3;
    } int main () {n=in ();
    Build (root);
    Q=in ();
        while (q--) {int x=in (), Y=in (), ans;
        if (x==y) ans=0;
        else if (x<y) ans=query (root,x,y). Chah;
        else Ans=query (root,y,x). Chaq;
    printf ("%d\n", ans);
} return 0; }

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.