이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// press "Read Editorial" on
// https://dmoj.ca/problem/coci20c5p5
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9;  // some random large number
const int N = 8e5+5;
int n, n2, q, m[N];
ll x[N], a[N], b[N], c[N], d[N];
int sign(ll v){
    if(v ==0)
        return 0;
    if(v <0)
        return -1;
    return 1;
}
void pull(int i){
    a[i] = max({b[2*i]+c[2*i+1], b[2*i]+a[2*i+1], a[2*i]+c[2*i+1]});
    b[i] = max({b[2*i]+d[2*i+1], b[2*i]+b[2*i+1], a[2*i]+d[2*i+1]});
    c[i] = max({d[2*i]+c[2*i+1], d[2*i]+a[2*i+1], c[2*i]+c[2*i+1]});
    d[i] = max({d[2*i]+b[2*i+1], c[2*i]+d[2*i+1], d[2*i]+d[2*i+1]});
    if(m[i]+1<n+n2 && sign(x[m[i]]) != sign(x[m[i]+1]))
        return;
    a[i] = max(a[i], a[2*i]+a[2*i+1]);
    b[i] = max(b[i], a[2*i]+b[2*i+1]);
    c[i] = max(c[i], c[2*i]+a[2*i+1]);
    d[i] = max(d[i], c[2*i]+b[2*i+1]);
}
void evan(int i, int l, int r){
    if(i>=n)
        return;
    m[i] = (l+r)/2;
    evan(2*i, l, m[i]);
    evan(2*i+1, m[i]+1, r);
}
void upd(int i, int v){
    x[i] += v;
    a[i] = abs(x[i]);
    for(i /= 2; i > 0; i /= 2)
        pull(i);
}
void upd(int l, int r, int v){
    l += n-1;
    r += n-1;
    if(l>n)
        upd(l-1, -v);
    if(r<n+n2)
        upd(r, v);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n2 >> q;
    if(n2==1){
        while(q--)
            cout << 0 << '\n';
        return 0;
    }
    --n2;
    n = 1;
    while(n<n2)
        n *= 2;
    evan(1, n, 2*n-1);
    for(int i = n; i <= n+n2; ++i)
        cin >> x[i];
    for(int i = n; i < n+n2; ++i) {
        x[i] -= x[i + 1];
        a[i] = abs(x[i]);
        b[i] = c[i] = -inf;
    }
    for(int i = n+n2; i < 2*n; ++i){
        x[i] = 0;
        a[i] = 0;
        b[i] = c[i] = -inf;
    }
    for(int i = n-1; i > 0; --i)
        pull(i);
    while(q--){
        int l, r, v;
        cin >> l >> r >> v;
        upd(l, r, v);
        cout << max({a[1], b[1], c[1], d[1]}) << '\n';
    }
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |