답안 #559829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
559829 2022-05-10T17:30:35 Z thecodingwizard Sjeckanje (COCI21_sjeckanje) C++11
0 / 110
1 ms 340 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const ll inf = 100000000000;

int n, q; 
vector<ll> A;

const int maxst = 800000;

// 0 = take last element, 1 = don't take last element
ll lhs[maxst], rhs[maxst], val[maxst][2][2];

void merge(int p, int i, int j) {
    lhs[p] = lhs[p*2];
    rhs[p] = rhs[p*2+1];

    // two separate things
    for (int a = 0; a < 2; a++) {
        for (int b = 0; b < 2; b++) {
            val[p][a][b] = val[p*2][a][0] + val[p*2+1][0][b];
        }
    }

    // one continuous increasing in the middle
    if (lhs[p*2+1] > 0) {
        for (int a = 0; a < 2; a++) {
            for (int b = 0; b < 2; b++) {
                val[p][a][b] = max(val[p][a][b], val[p*2][a][1] + val[p*2+1][1][b] + lhs[p*2+1]);
            }
        }
    }

    // one decreasing increasing in the middle
    if (lhs[p*2+1] < 0) {
        for (int a = 0; a < 2; a++) {
            for (int b = 0; b < 2; b++) {
                val[p][a][b] = max(val[p][a][b], val[p*2][a][1] + val[p*2+1][1][b] - lhs[p*2+1]);
            }
        }
    }
}

void build(int p, int i, int j) {
    if (i == j) {
        lhs[p] = A[i];
        rhs[p] = A[i];
        for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) val[p][i][j] = -inf;
        val[p][0][1] = val[p][1][0] = 0;
    } else {
        build(p*2, i, (i+j)/2);
        build(p*2+1, (i+j)/2+1, j);
        merge(p, i, j);
    }
}

void pans() {
    ll ans = 0;
    for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) ans = max(ans, val[1][i][j]);
    cout << ans << endl;
}

void upd(int p, int i, int j, int k, int v) {
    if (i > k || j < k) return;
    if (i == j) {
        lhs[p] += v; rhs[p] += v;
    } else {
        upd(p*2, i, (i+j)/2, k, v);
        upd(p*2+1, (i+j)/2+1, j, k, v);
        merge(p, i, j);
    }
}

int main() {
    cin >> n >> q;
    A.resize(n);

    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }
    for (int i = n-1; ~i; i--) {
        A[i] = A[i] - (i==0?0:A[i-1]);
    }

    build(1, 0, n-1);

    for (int i = 0; i < q; i++) {
        int l, r, x; cin >> l >> r >> x;
        upd(1, 0, n-1, l-1, x);
        upd(1, 0, n-1, r, -x);
        pans();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -