Submission #538880

# Submission time Handle Problem Language Result Execution time Memory
538880 2022-03-17T23:36:37 Z Olympia Sjeckanje (COCI21_sjeckanje) C++17
0 / 110
1 ms 340 KB
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <limits.h>

using namespace std;
const int MOD = 1e9;

class Node {
public:
    int64_t lval;
    int64_t rval;
    int64_t ltot;
    int64_t rtot;
    int64_t mtot;
    int64_t tot;
    void print () {
        cout << lval << " " << rval << " " << ltot << " " << rtot << " " << mtot << " " << tot << '\n';
    }
};

const Node INF = {-1, 0, 0, 0, 0, 0};

Node merge (Node left, int tl1, int tl2, Node right, int tr1, int tr2) {
    if (left.lval == -1) {
        return right;
    }
    if (right.lval == -1) {
        return left;
    }
    Node ans;
    ans.lval = left.lval;
    ans.rval = right.rval;
    ans.tot = max(left.tot + right.tot, left.ltot + right.rtot + abs(left.rval - right.lval));
    ans.mtot = max(left.rtot + right.ltot, left.mtot + right.mtot + abs(left.rval - right.lval));
    ans.ltot = max(left.tot + right.ltot, left.ltot + right.mtot + abs(left.rval - right.lval));
    ans.rtot = max(right.tot + left.rtot, right.rtot + left.mtot + abs(left.rval - right.lval));
    if (tr2 - tl1 + 1 <= 2) {
        ans.ltot = ans.rtot = 0;
    }
    if (tr2 - tl1 + 1 <= 3) {
        ans.mtot = 0;
    }
    return ans;
}

struct segmentTree {
    vector<Node> vec;
    vector<int64_t> addLater;

    void push (int v) {
        addLater[2 * v + 1] += addLater[v];
        vec[2 * v + 1].lval += addLater[v];
        vec[2 * v + 1].rval += addLater[v];
        addLater[2 * v] += addLater[v];
        vec[2 * v].lval += addLater[v];
        vec[2 * v].rval += addLater[v];
        addLater[v] = 0;
    }

    void upd(int dum, int tl, int tr, int l, int r, int64_t val) {
        if (tr < l || tl > r) {
            return;
        }
        if (tl >= l && tr <= r) {
            addLater[dum] += val;
            vec[dum].lval += val;
            vec[dum].rval += val;
            return;
        }
        push(dum);
        int mid = (tl + tr) >> 1;
        upd(2 * dum, tl, mid, l, r, val);
        upd(2 * dum + 1, mid + 1, tr, l, r, val);
        vec[dum] = merge(vec[2 * dum], mid, l, vec[2 * dum + 1], mid + 1, tr);
    }

    void upd(int l, int r, int val) {
        upd(1, 0, (int) vec.size() / 2 - 1, l, r, val);
    }

    Node get(int dum, int tl, int tr, int &l, int &r) {
        if (tl > r || tr < l) {
            return INF;
        }
        if (tl >= l && tr <= r) {
            return vec[dum];
        }
        push(dum);
        int tm = (tl + tr) >> 1;
        return merge(get(dum * 2, tl, tm, l, r), tl, tm, get(dum * 2 + 1, tm + 1, tr, l, r), tm + 1, tr);
    }

    Node get(int l, int r) {
        return get(1, 0, (int) vec.size() / 2 - 1, l, r);
    }

    void resz(int n) {
        int sz = ((1 << (int) ceil(log2(n))));
        vec.resize(sz * 2);
        addLater.resize(sz * 2);
    }

};

int main() {
    //freopen("haybales.in", "r", stdin);
    //freopen("haybales.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, D;
    cin >> N >> D;
    segmentTree st;
    st.resz(N);
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        st.upd(i, i, x);
    }
    while (D--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--, r--;
        st.upd(l, r, x);
        cout << st.get(0, N - 1).tot << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -