Submission #1355134

#TimeUsernameProblemLanguageResultExecution timeMemory
1355134Huseyn123Sjeckanje (COCI21_sjeckanje)C++20
0 / 110
0 ms344 KiB
#include <bits/stdc++.h>
#define int long long
const long long INF=1e14;
using namespace std;
struct node{
    int i_d,i_i,d_d,d_i,f,l;
};
struct segtree{
    int sz;
    vector<node> st;
    vector<int> lazy;
    void init(int n){
        sz=1;
        while(sz<n){
            sz*=2;
        }
        st.resize(2*sz);
        lazy.resize(2*sz,0);
    }
    node mrg(node x,node y){
        if(x.f==-INF) return y;
        if(y.f==-INF) return x;
        node res;
        res.i_d=max(x.i_i,x.d_i)+max(y.d_d,y.d_i);
        res.i_i=max(x.i_i,x.d_i)+max(y.i_d,y.i_i);
        res.d_d=max(x.i_d,x.d_d)+max(y.d_d,y.d_i);
        res.d_i=max(x.i_d,x.d_d)+max(y.i_d,y.i_i);
        res.f=x.f;
        res.l=y.l;
        if(y.f!=-INF){
            if(x.l<y.f){
                res.i_d=max(res.i_d,x.i_i+y.i_d+y.f-x.l);
                res.i_i=max(res.i_i,x.i_i+y.i_i+y.f-x.l);
                res.d_d=max(res.d_d,x.d_i+y.i_d+y.f-x.l);
                res.d_i=max(res.d_i,x.d_i+y.i_i+y.f-x.l);
            }
            else{
                res.i_d=max(res.i_d,x.i_d+y.d_d-y.f+x.l);
                res.i_i=max(res.i_i,x.i_d+y.d_i-y.f+x.l);
                res.d_d=max(res.d_d,x.d_d+y.d_d-y.f+x.l);
                res.d_i=max(res.d_i,x.d_d+y.d_i-y.f+x.l);
            }
        }
        return res;
    }
    void propogate(int x,int lx,int rx){
        if(rx-lx==1){
            return;
        }
        st[2*x+1].f+=lazy[x];
        st[2*x+1].l+=lazy[x];
        lazy[2*x+1]+=lazy[x];
        st[2*x+2].f+=lazy[x];
        st[2*x+2].l+=lazy[x];
        lazy[2*x+2]+=lazy[x];
        lazy[x]=0;
    }
    void build(int x,int lx,int rx,vector<int> &a){
        st[x]={-INF,-INF,-INF,-INF,-INF,-INF};
        if(rx-lx==1){
            if(lx<a.size()){
                st[x]={-INF,0,0,-INF,a[lx],a[lx]};
            }
            return;
        }
        int m=(lx+rx)/2;
        build(2*x+1,lx,m,a);
        build(2*x+2,m,rx,a);
        st[x]=mrg(st[2*x+1],st[2*x+2]);
    }
    void build(vector<int> &a){
        build(0,0,sz,a);
    }
    void upd(int x,int lx,int rx,int l,int r,int v){
        if(lx>=l && rx<=r){
            st[x].f+=v;
            st[x].l+=v;
            lazy[x]+=v;
            return;
        }
        if(rx<=l || lx>=r){
            return;
        }
        propogate(x,lx,rx);
        int m=(lx+rx)/2;
        upd(2*x+1,lx,m,l,r,v);
        upd(2*x+2,m,rx,l,r,v);
        st[x]=mrg(st[2*x+1],st[2*x+2]);
    }
    void upd(int l,int r,int v){
        upd(0,0,sz,l,r,v);
    }
};
int n,q;
void solve(){
    cin >> n >> q;
    vector<int> a;
    for(int i=0;i<n;i++){
        int x;
        cin >> x;
        a.push_back(x);
    }
    segtree seg;
    seg.init(n);
    seg.build(a);
    for(int i=0;i<q;i++){
        int x,y,z;
        cin >> x >> y >> z;
        x--;
        seg.upd(x,y,z);
        cout << max({seg.st[0].i_i,seg.st[0].i_d,seg.st[0].d_i,seg.st[0].d_d}) << "\n";
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...