Submission #1292004

#TimeUsernameProblemLanguageResultExecution timeMemory
1292004herominhsteveSjeckanje (COCI21_sjeckanje)C++20
110 / 110
393 ms37816 KiB
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "COCI21_sjeckanje"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9 + 7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}

void setup(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    if (fopen(FNAME".inp","r")){
        freopen(FNAME".inp","r",stdin);
        freopen(FNAME".out","w",stdout);
    }
}

const long long INF = (long long) 1e18 + 15092007;

struct Matrix{
    int n=2,m=2;
    long long matrix[2][2];
    
    Matrix(){
        matrix[0][0] = matrix[1][1] = 0;
        matrix[1][0] = matrix[0][1] = -INF;
    }

    inline void toUnit(){
        for (int i=0;i<n;i++) matrix[i][i] = 0;
    }

    inline void fullZero(){
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                matrix[i][j] = 0;
            }
        }
    }

    inline void fullINF(){
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                matrix[i][j] = -INF;
            }
        }
    }

    Matrix operator * (const Matrix &other) const{
        Matrix res;
        res.fullINF();
        for (int i=0;i<n;i++){
            for (int j=0;j<other.m;j++){
                for (int k=0;k<m;k++){
                    long long total = matrix[i][k] + other.matrix[k][j];
                    maximize(res.matrix[i][j],total);
                }
            }
        }
        return res;
    }   

    long long getMax() const{
        long long res = 0;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                maximize(res,matrix[i][j]);
            }
        }
        return res;
    }
};

int n,q;
vector<long long> a;

void init(){
    cin>>n>>q;
    a.assign(n+1,0);
    for (int i=1;i<=n;i++)
        cin>>a[i];
}

struct SegmentTree{
    int n;
    vector<Matrix> st;

    SegmentTree(int N): n(N){
        st.assign(4 * n + 4,Matrix());
    }

    void build(int node,int l,int r,const vector<long long> &a){
        if (l==r){
            if (l == n) st[node] = Matrix();
            else{
                st[node].fullZero();
                if (a[l] < 0) st[node].matrix[0][0] = -a[l];
                else st[node].matrix[1][1] = a[l];
            }
            return;
        }
        int mid = (l+r)>>1;
        build(2*node,l,mid,a);
        build(2*node+1,mid+1,r,a);
        st[node] = st[2*node] * st[2*node + 1];
    }

    void build(const vector<long long> &a){
        build(1,1,n,a);
    }

    void update(int node,int l,int r,int pos,long long val){
        if (l>r) return;
        if (l==r){
            st[node].fullZero();
            if (val < 0) st[node].matrix[0][0] = -val;
            else st[node].matrix[1][1] = val;
            return;
        }
        int mid = (l+r)>>1;
        if (pos <= mid) update(2*node,l,mid,pos,val);
        else update(2*node+1,mid+1,r,pos,val);
        st[node] = st[2*node] * st[2*node + 1];
    }

    void update(int pos,long long val){
        update(1,1,n,pos,val);
    }

};

void sol(){
    if (n == 1) {
        while (q--) {
            int l, r; 
            long long x;
            cin >> l >> r >> x;
            cout << 0 << el;
        }
        return;
    }

    vector<long long> diff(n+1,0);
    for (int i=1;i<n;i++)
        diff[i] = a[i+1] - a[i];
    SegmentTree st(n);
    st.build(diff);
    
    while (q--){
        int l,r;
        long long x;
        cin>>l>>r>>x;
        
        if (l > 1) {
            int pos = l-1;
            diff[pos] += x;
            st.update(pos, diff[pos]);
        }
        if (r < n) {  
            int pos = r;
            diff[pos] -= x;
            st.update(pos, diff[pos]);
        }
        
        cout << st.st[1].getMax() << el;
    }
}

int main(){
    setup();
    init();
    sol();
}

Compilation message (stderr)

Main.cpp: In function 'void setup()':
Main.cpp:16:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |         freopen(FNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen(FNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...