codevs 3304 水果姐逛水果街Ⅰ 解題記錄

(我纔不會告訴你我是看見棟棟AC了不爽才做的)

題目:

題目描述 Description
水果姐第二天心情也很不錯,又來逛水果街。

突然,cgh又出現了。cgh施展了魔法,水果街變成了樹結構(店與店之間只有一條唯一的路徑)。

同樣還是n家水果店,編號爲1~n,每家店能買水果也能賣水果,並且同一家店賣與買的價格一樣。

cgh給出m個問題,每個問題要求水果姐從第x家店出發到第y家店,途中只能選一家店買一個水果,然後選一家店(可以是同一家店,但不能往回走)賣出去。求最多可以賺多少錢。

水果姐向學過oi的你求助。

輸入描述 Input Description
第一行n,表示有n家店

下來n個正整數,表示每家店一個蘋果的價格。

下來n-1行,每行兩個整數x,y,表示第x家店和第y家店有一條邊。

下來一個整數m,表示下來有m個詢問。

下來有m行,每行兩個整數x和y,表示從第x家店出發到第y家店。

輸出描述 Output Description
有m行。

每行對應一個詢問,一個整數,表示面對cgh的每次詢問,水果姐最多可以賺到多少錢。

樣例輸入 Sample Input
10
16 5 1 15 15 1 8 9 9 15
1 2
1 3
2 4
2 5
2 6
6 7
4 8
1 9
1 10
6
9 1
5 1
1 7
3 3
1 1
3 6

樣例輸出 Sample Output
7
11
7
0
0
15

數據範圍及提示 Data Size & Hint
0<=蘋果的價格<=10^8

n<=200000

m<=10000

明顯的線段樹,維護四個值,區間最大,區間最小,區間從左到右最大收益,區間從右到左最大收益。
很容易寫出:

void build(int x,int l,int r)
{
    if(l==r)
    {
        t[x].mi=a[l];
        t[x].ma=a[l];
        t[x].lr=0;
        t[x].rl=0;
        return;
    }
    int mid=(l+r)>>1;
    build(2*x,l,mid);
    build(2*x+1,mid+1,r);
    t[x].ma=max(t[2*x].ma,t[2*x+1].ma);
    t[x].mi=min(t[2*x].mi,t[2*x+1].mi);
    t[x].lr=max(t[2*x+1].ma-t[x*2].mi,max(t[2*x].lr,t[2*x+1].lr));
    t[x].rl=max(t[2*x].ma-t[2*x+1].mi,max(t[2*x].rl,t[2*x+1].rl));
}

因爲題意不需要修改,所以只寫查詢就好;

可以得出在求從左到右的時候 區間交叉的情況爲:
ans=max(ans,max(findlr(2*x,l,mid,xx,mid),max(findlr(2*x+1,mid+1,r,mid+1,yy),findma(2*x+1,mid+1,r,mid+1,yy)-findmi(2*x,l,mid,xx,mid))));
所以需要寫函數findma findmi 查詢區間最大和區間最小。

嗯,就這麼簡單~

就是有點長。
不過。
大部分都是
複製粘貼;


數據範圍較大,直接cin十個測試點要5000ms
加讀入優化之後能到1500ms

黑科技大法好!!!

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#include<deque>
#define INF 1000000000
#define fi first
#define se second
#define N 100005
#define P 1000000007
#define debug(x) cerr<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
int n,m;
int a[200001]; 
struct zqm
{
    int mi,ma,lr,rl;
}t[1000001];
inline int get_num()
{
int num = 0;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true;
else num = c - '0';
while (isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
}
void build(int x,int l,int r)
{
    if(l==r)
    {
        t[x].mi=a[l];
        t[x].ma=a[l];
        t[x].lr=0;
        t[x].rl=0;
        return;
    }
    int mid=(l+r)>>1;
    build(2*x,l,mid);
    build(2*x+1,mid+1,r);
    t[x].ma=max(t[2*x].ma,t[2*x+1].ma);
    t[x].mi=min(t[2*x].mi,t[2*x+1].mi);
    t[x].lr=max(t[2*x+1].ma-t[x*2].mi,max(t[2*x].lr,t[2*x+1].lr));
    t[x].rl=max(t[2*x].ma-t[2*x+1].mi,max(t[2*x].rl,t[2*x+1].rl));
}
int findma(int x,int l,int r,int xx,int yy)
{
    int ans=0;
    if(l==xx&&yy==r)
    {
        ans=max(ans,t[x].ma);
        return ans;
    }
    int mid=(l+r)>>1;
    if(mid>=yy)
    {
        ans=max(ans,findma(2*x,l,mid,xx,yy));
    }else
    {
        if(mid<xx)
        {
            ans=max(ans,findma(2*x+1,mid+1,r,xx,yy));
        }else
        {
            ans=max(ans,findma(2*x,l,mid,xx,mid));
            ans=max(ans,findma(2*x+1,mid+1,r,mid+1,yy)); 
        }
    }
    return ans;
}
int findmi(int x,int l,int r,int xx,int yy)
{
    int ans=2147483647;
    if(l==xx&&yy==r)
    {
        ans=min(ans,t[x].mi);
        return ans;
    }
    int mid=(l+r)>>1;
    if(mid>=yy)
    {
        ans=min(ans,findmi(2*x,l,mid,xx,yy));
    }else
    {
        if(mid<xx)
        {
            ans=min(ans,findmi(2*x+1,mid+1,r,xx,yy));
        }else
        {
            ans=min(ans,findmi(2*x,l,mid,xx,mid));
            ans=min(ans,findmi(2*x+1,mid+1,r,mid+1,yy)); 
        }
    }
    return ans;
}
int findlr(int x,int l,int r,int xx,int yy)
{
    int ans=0;
    if(l==xx&&yy==r)
    {
        ans=max(ans,t[x].lr);
        return ans;
    }
    int mid=(l+r)>>1;
    if(mid>=yy)
    {
        ans=max(ans,findlr(2*x,l,mid,xx,yy));
    }else
    {
        if(mid<xx)
        {
            ans=max(ans,findlr(2*x+1,mid+1,r,xx,yy));
        }else
        {
            ans=max(ans,max(findlr(2*x,l,mid,xx,mid),max(findlr(2*x+1,mid+1,r,mid+1,yy),findma(2*x+1,mid+1,r,mid+1,yy)-findmi(2*x,l,mid,xx,mid))));
        }
    }
    return ans;
}
int findrl(int x,int l,int r,int xx,int yy)
{
    int ans=0;
    if(l==xx&&yy==r)
    {
        ans=max(ans,t[x].rl);
        return ans;
    }
    int mid=(l+r)>>1;
    if(mid>=yy)
    {
        ans=max(ans,findrl(2*x,l,mid,xx,yy));
    }else
    {
        if(mid<xx)
        {
            ans=max(ans,findrl(2*x+1,mid+1,r,xx,yy));
        }else
        {
            ans=max(ans,max(findrl(2*x,l,mid,xx,mid),max(findrl(2*x+1,mid+1,r,mid+1,yy),findma(2*x,l,mid,xx,mid)-findmi(2*x+1,mid+1,r,mid+1,yy))));
        }
    }
    return ans;
}
int main()
{
    n=get_num();
    for(int i=1;i<=n;i++)
    {
        a[i]=get_num();
    }
    build(1,1,n);
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        int q,w;
        q=get_num();w=get_num();
        if(q==w){
            cout<<0<<"\n";
            continue;
        }
        if(q<w)
        {
            cout<<findlr(1,1,n,q,w)<<"\n";
        }else
        {
            cout<<findrl(1,1,n,w,q)<<"\n";
        }
    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章