CODE[VS] 3304 水果姐逛水果街I(線段樹求區間最大最小值)

題目描述 Description
水果姐今天心情不錯,來到了水果街。
水果街有n家水果店,呈直線結構,編號爲1~n,每家店能買水果也能賣水果,並且同一家店賣與買的價格一樣。
學過oi的水果姐迅速發現了一個賺錢的方法:在某家水果店買一個水果,再到另外一家店賣出去,賺差價。
就在水果姐竊喜的時候,cgh突然出現,他爲了爲難水果姐,給出m個問題,每個問題要求水果姐從第x家店出發到第y家店,途中只能選一家店買一個水果,然後選一家店(可以是同一家店,但不能往回走)賣出去,求每個問題中最多可以賺多少錢。
輸入描述 Input Description
第一行n,表示有n家店
下來n個正整數,表示每家店一個蘋果的價格。
下來一個整數m,表示下來有m個詢問。
下來有m行,每行兩個整數x和y,表示從第x家店出發到第y家店。
輸出描述 Output Description
有m行。
每行對應一個詢問,一個整數,表示面對cgh的每次詢問,水果姐最多可以賺到多少錢。
樣例輸入 Sample Input
10
2 8 15 1 10 5 19 19 3 5
4
6 6
2 8
2 2
6 3
樣例輸出 Sample Output
0
18
0
14
數據範圍及提示 Data Size & Hint
0<=蘋果的價格<=10^8
0 < n,m<=200000

思路:
最多賺多少錢肯定是求最大最小值呀╮(╯▽╰)╭
區間詢問、nlogn、線段樹沒跑了
水果姐是不能回頭的 我們一定最優的答案是在一個左區間找到了一個值 在對應的右區間找到了一個值使它們兩個的差值是最大的 這就是最優解 不斷去求區間最值 取max即可
最後,水果姐的行動方向是不定的,我們需要考慮查詢的是否是否應反向

代碼:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 200050;
typedef long long ll;
int n,m,num[maxn];
struct dc{
    int l,r;
    ll sum,add,min,max;
    ll ans1,ans2;
}tree[maxn<<2];
ll min(ll a,ll b)
{
    if(a > b) return b;
    return a;
}
ll max(ll a,ll b)
{
    if(a > b) return a;
    return b;
}
void up(int p)
{
    tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
    tree[p].max = max(tree[p<<1].max,tree[p<<1|1].max);
    tree[p].min = min(tree[p<<1].min,tree[p<<1|1].min);
    tree[p].ans1 = max(tree[p<<1|1].max - tree[p<<1].min,max(tree[p<<1].ans1,tree[p<<1|1].ans1));
    tree[p].ans2 = max(tree[p<<1].max - tree[p<<1|1].min,max(tree[p<<1].ans2,tree[p<<1|1].ans2));
}
void push(int p)
{
    tree[p<<1].sum += tree[p].add * (tree[p<<1].r - tree[p<<1].l + 1);
    tree[p<<1].add += tree[p].add;
    tree[p<<1|1].sum += tree[p].add * (tree[p<<1|1].r - tree[p<<1|1].l + 1);
    tree[p<<1|1].add += tree[p].add;
    tree[p].add = 0;
} 
void build(int p,int l,int r)
{
    tree[p].l = l;
    tree[p].r = r;
    if(l == r)
    {
        tree[p].min = tree[p].max = tree[p].sum = num[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    up(p);
}
void change(int p,int l,int r,int v)
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        tree[p].sum += v * (tree[p].r - tree[p].l +1);
        tree[p].max += v;
        tree[p].min += v;
        tree[p].add += v;
        return;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if(l <= mid ) change(p<<1,l,r,v);
    if(mid < r) change(p<<1|1,l,r,v);
    up(p);
}
ll ask_min(int p,int l,int r)
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        return tree[p].min;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 21474836472333333;
    if(l <= mid) ans = min(ans,ask_min(p<<1,l,r));
    if(mid < r) ans = min(ans,ask_min(p<<1|1,l,r));
    return ans;

}
ll ask_max(int p,int l,int r)
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        return tree[p].max;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 0;
    if(l <= mid) ans = max(ans,ask_max(p<<1,l,r));
    if(mid < r) ans = max(ans,ask_max(p<<1|1,l,r));
    return ans;
}
ll ask1(int p,int l,int r)
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        return tree[p].ans1;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 0;
    if(l <= mid) ans = max(ans,ask1(p<<1,l,r));
    if(mid < r) ans = max(ans,ask1(p<<1|1,l,r));
    if(l <= mid && mid < r)
    ans = max(ans,ask_max(p<<1|1,mid + 1,r) - ask_min(p<<1,l,mid));
    return ans;
}
ll ask2(int p,int l,int r)
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        return tree[p].ans2;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 0;
    if(l <= mid) ans = max(ans,ask2(p<<1,l,r));
    if(mid < r) ans = max(ans,ask2(p<<1|1,l,r)); 
    if(l <= mid && mid < r) 
    ans = max(ans,ask_max(p<<1,l,mid) - ask_min(p<<1|1,mid + 1,r)); 
    return ans;
}
int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++)
    scanf("%d",&num[i]);
    build(1,1,n);
    scanf("%d",&m);
    while(m --)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        ll ans;
        if(x < y) ans = ask1(1,x,y);
        else ans = ask2(1,y,x);
        printf("%lld\n",ans);
    }
    return 0;
}
發佈了72 篇原創文章 · 獲贊 39 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章