Submission #1353882

#TimeUsernameProblemLanguageResultExecution timeMemory
1353882thesentroFoehn Phenomena (JOI17_foehn_phenomena)C++20
100 / 100
406 ms16128 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define ll long long
ll mod = 1e9+7;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll binpow(ll a, ll b)
{
    ll res = 1;
    while (b>0)
    {
        if (b&1)
            res = (res*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return res;
}
ll gcd(ll x, ll y)
{
    if (y==0)
        return x;
    return gcd(y, x%y);
}
const ll mx = 800000;
ll a[mx];
ll st[4*mx];
ll vec[mx];
vector<ll>lazy(mx, 0);
void build (ll l, ll r, ll v)
{
    if (l==r)
    {
        st[v] = vec[l];
        return;
    }
    ll mid = (l+r)/2;
    build(l, mid, 2*v);
    build(mid+1, r, 2*v+1);
    st[v] = st[2*v] + st[2*v+1];
}
void push(ll l, ll r, ll v)
{
    if (lazy[v]==0) return;
    st[v]+=lazy[v]*(r-l+1);
    if (l!=r)
    {
        lazy[2*v] += lazy[v];
        lazy[2*v+1] += lazy[v];
    }
    lazy[v] = 0;
}
ll getsum (ll l, ll r, ll v, ll ql, ll qr)
{
    push(l, r, v);
    if (r<ql or l>qr)
        return 0;
    if (l>=ql and r<=qr)
        return st[v];
    ll mid = (l+r)/2;
    return getsum(l, mid, 2*v, ql, qr) + getsum(mid+1, r, 2*v+1, ql, qr);
}
 
void update(ll l, ll r, ll v, ll ql, ll qr, ll val)
{
    if (r<ql or l>qr)
        return;
    push(l, r, v);  
    if (l>=ql and r<=qr)
    {
        lazy[v] += val;
        push(l, r, v);
        return;
    }
    ll mid = (l+r)/2;
    update(l, mid, 2*v, ql, qr, val);
    update(mid+1, r, 2*v+1, ql, qr, val);
    st[v] = st[2*v]+st[2*v+1];
}
void solve()
{
    ll n,q,s,t;
    cin>>n>>q>>s>>t;
    swap(s, t);
    for (int i=0 ; i<=n ;i++)
        cin>>vec[i];
    build(1, n, 1);
    ll res = 0;
    for (int i=0 ; i<n ;i++)
    {
        if (vec[i+1]>vec[i])
            res -= t*(vec[i+1]-vec[i]);
        else
            res += s*(vec[i]-vec[i+1]);
    }
    //cout<<res<<endl;
    while (q--)
    {
        ll x, y, z;
        cin>>x>>y>>z;
        vec[x] = getsum(1, n, 1, x, x);
        vec[x-1] = getsum(1, n, 1, x-1, x-1);
        vec[y] = getsum(1, n, 1, y, y);
        if (vec[x-1]<vec[x]) res += t*(vec[x]-vec[x-1]);
        else res -= s*(vec[x-1]-vec[x]);
        if (y!=n)
        {
            vec[y+1] = getsum(1, n, 1, y+1, y+1);
            if (vec[y+1]>vec[y]) res += t*(vec[y+1]-vec[y]);
            else res -= s*(vec[y]-vec[y+1]);
        }
        update(1, n, 1, x, y, z);
        vec[x] = getsum(1, n, 1, x, x);
        vec[x-1] = getsum(1, n, 1, x-1, x-1);
        vec[y] = getsum(1, n, 1, y, y);
        if (vec[x-1]<vec[x]) res -= t*(vec[x]-vec[x-1]);
        else res += s*(vec[x-1]-vec[x]);
        if (y!=n)
        {
            vec[y+1] = getsum(1, n, 1, y+1, y+1);
            if (vec[y+1]>vec[y]) res -= t*(vec[y+1]-vec[y]);
            else res += s*(vec[y]-vec[y+1]);
        }
        cout<<res<<endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll tt = 1;
    // cin>>tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}
 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...