题解:P11790 [JOI 2017 Final] 焚风现象 / Foehn Phenomena

· · 题解

建议降黄,三倍经验:1,2。

分析

根据题目,每次计算贡献时都根据两个相邻的海拔计算。所以我们可以把原数组处理成差分数组。这时就可以发现一个奇妙的性质:每次区间加时只有左端点和右端点 +1 的点差分数组值会改变!

于是先预处理成差分数组并算出初始答案,然后对于每个修改,先删去左右端点的贡献,再修改差分数组,加上左右端点的贡献。你就会发现你过了。

由题意可得,计算贡献时: 设 B_i=A_i-A_{i-1},X_i 为该处的贡献,

注意:当修改右端点为 N 时,不要改差分数组和贡献!

代码

#include<bits/stdc++.h>
#define int long long
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,rp,stdin),p1==p2)?EOF:*p1++)

using namespace std;
const int N=2e5+10;
const int rp=1e6+10;

int n,q,s,t,ans,p[N];
char buf[rp],*p1=buf,*p2=buf;

inline int read() 
{
    int x=0,f=1; char c=0;
    while(!isdigit(c)){if(c=='-') f=-1; c=gc();}
    while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=gc();
    return x*f;
}//快读

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    n=read(); q=read(); s=read(); t=read(); int l,r,x;
    for(int i=0;i<=n;++i) p[i]=read();
    for(int i=n;i>=1;--i) p[i]-=p[i-1];//转差分数组 
    for(int i=1;i<=n;++i) 
        if(p[i]>0) ans-=p[i]*s;
        else ans-=p[i]*t;//算最初答案
    while(q--)
    {
        l=read(); r=read(); x=read();

        if(p[l]>0) ans+=p[l]*s;
        else ans+=p[l]*t;//减左端点原贡献 
        p[l]+=x;//改差分数组 
        if(p[l]>0) ans-=p[l]*s;
        else ans-=p[l]*t;//加左端点现贡献

        if(r<n)//防止越界修改而 WA 掉 
        {
            if(p[r+1]>0) ans+=p[r+1]*s;
            else ans+=p[r+1]*t;//减右端点原贡献 
            p[r+1]-=x;//改差分数组 
            if(p[r+1]>0) ans-=p[r+1]*s;
            else ans-=p[r+1]*t;//加右端点现贡献 
        }

        cout<<ans<<"\n";
    }
    return 0;
}