Submission #873496

#TimeUsernameProblemLanguageResultExecution timeMemory
873496NintsiChkhaidzeFoehn Phenomena (JOI17_foehn_phenomena)C++17
100 / 100
561 ms22100 KiB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define ll long long
#define left (h<<1),l,((l + r)>>1)
#define right ((h<<1)|1),((l + r)>>1) + 1,r
#define pii pair<int,int>
#define int ll
using namespace std;

const int N = 2e5 + 5;
int a[N],A[4*N],b[4*N],S,T;

int getans(int x,int y){
    if (x < y) return -S*(y-x);
    return (x - y)*T;
}

void upd(int h,int l,int r,int L,int R,int val){
    if (r < L || R < l) return;
    if (L <= l && r <= R){
        b[h] += val;
        return;
    }
    upd(left,L,R,val);
    upd(right,L,R,val);
}
int get(int h,int l,int r,int idx){
    if (l == r) return b[h];
    if (idx>(l+r)/2) return b[h] + get(right,idx);
    return b[h] + get(left,idx);
}

void UPD(int h,int l,int r,int L,int R,int val){
    if (r < L || R < l) return;
    if (L <= l && r <= R){
        A[h] += val;
        return;
    }
    UPD(left,L,R,val);
    UPD(right,L,R,val);
}

int GET(int h,int l,int r,int idx){
    if (l == r) return A[h];
    if (idx>(l+r)/2) return A[h] + GET(right,idx);
    return A[h] + GET(left,idx);
}

signed main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    
    int n,q;
    cin>>n>>q>>S>>T;

    ++n;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
        int val = getans(a[i - 1],a[i]);
        upd(1,1,n,i,n,val);
        UPD(1,1,n,i,i,a[i]);
    }

    while (q--){
        int l,r,x;
        cin>>l>>r>>x;
        l++,r++;
        UPD(1,1,n,l,r,x); 

        int newx = GET(1,1,n,l - 1);
        int newy = GET(1,1,n,l);

        int oldx = newx;
        int oldy = newy - x;

        int oldval = getans(oldx,oldy);
        int newval = getans(newx,newy);
        upd(1,1,n,l,n,newval - oldval);

        if (r + 1 <= n){
            newx = GET(1,1,n,r);
            newy = GET(1,1,n,r+1);
            oldx = newx - x;
            oldy = newy;

            oldval = getans(oldx,oldy);
            newval = getans(newx,newy);
            upd(1,1,n,r+1,n,newval - oldval);
        }
        
        cout<<get(1,1,n,n)<<endl;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...