Submission #524803

# Submission time Handle Problem Language Result Execution time Memory
524803 2022-02-10T04:53:42 Z Evang Sjeckanje (COCI21_sjeckanje) C++17
110 / 110
356 ms 20744 KB
// 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
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 3 ms 588 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 3 ms 588 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 356 ms 20628 KB Output is correct
14 Correct 349 ms 20608 KB Output is correct
15 Correct 331 ms 20676 KB Output is correct
16 Correct 330 ms 20676 KB Output is correct
17 Correct 339 ms 20496 KB Output is correct
18 Correct 298 ms 20744 KB Output is correct