結果

問題 No.631 Noelちゃんと電車旅行
ユーザー merom686merom686
提出日時 2018-01-05 23:03:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,735 bytes
コンパイル時間 691 ms
コンパイル使用メモリ 77,424 KB
実行使用メモリ 6,912 KB
最終ジャッジ日時 2024-04-10 07:46:00
合計ジャッジ時間 3,793 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
5,248 KB
testcase_01 AC 105 ms
6,784 KB
testcase_02 AC 114 ms
6,784 KB
testcase_03 AC 106 ms
6,912 KB
testcase_04 AC 108 ms
6,912 KB
testcase_05 AC 103 ms
6,912 KB
testcase_06 AC 41 ms
5,376 KB
testcase_07 AC 24 ms
5,376 KB
testcase_08 AC 82 ms
6,528 KB
testcase_09 AC 87 ms
5,376 KB
testcase_10 AC 63 ms
6,528 KB
testcase_11 AC 58 ms
5,376 KB
testcase_12 AC 73 ms
6,656 KB
testcase_13 AC 65 ms
5,376 KB
testcase_14 AC 29 ms
5,376 KB
testcase_15 AC 55 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

struct LazySegmentTree {
    struct Node {
        Node operator*(const Node& o) const {
            return { max(m + l, o.m + o.l), 0 };
        }
        int64_t m;
        int64_t l;
    };

    LazySegmentTree(int n) : e({ -((int64_t)1 << 60), 0 }), n(n) {
        for (m = 2; m < n; m *= 2);
        t = vector<Node>(m + n + n % 2, e);
    }
    Node& operator[](int i) {
        return t[m + i];
    }
    const Node& root() const {
        return t[1];
    }
    void build() {
        for (int i = (n - 1 + m) / 2; i > 0; i--) {
            t[i] = t[i * 2] * t[i * 2 + 1];
        }
    }
    void update(int i, const Node& o) {
        i += m;
        for (int k = 30; k > 0; k--) {
            int j = i >> k;
            if (j == 0) continue;
            propagate(j);
        }
        for (t[i] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    Node query(int l, int r, int i = 1, int il = 0, int ir = -1) {
        if (ir < 0) ir = m;
        if (r - l == ir - il) return t[i];
        propagate(i);
        int im = (il + ir) / 2;
        Node o = e;
        if (l < im) o = o * query(l, min(r, im), i * 2 + 0, il, im);
        if (im < r) o = o * query(max(im, l), r, i * 2 + 1, im, ir);
        return o;
    }
    void lazy(int l, int r, int u, int i = 1, int il = 0, int ir = -1) {
        if (ir < 0) ir = m;
        if (r - l <= 0) return;
        if (r - l == ir - il) {
            t[i].l += u; // このあとで先祖に反映させる
            if (i == 1) propagate(i);
            return;
        }
        propagate(i);
        int im = (il + ir) / 2;
        if (l < im) lazy(l, min(r, im), u, i * 2 + 0, il, im);
        if (im < r) lazy(max(im, l), r, u, i * 2 + 1, im, ir);
        t[i] = t[i * 2] * t[i * 2 + 1]; // 子孫ノードが遅延評価の値を持っていても反映されている
    }
    void propagate(int i) {
        if (t[i].l == 0) return;
        if (i >= m) throw;
        t[i * 2 + 0].l += t[i].l;
        t[i * 2 + 1].l += t[i].l;
        t[i].m += t[i].l;
        t[i].l = 0;
    }
    vector<Node> t;
    const Node e;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    LazySegmentTree st(n - 1);
    for (int i = 0; i < n - 1; i++) {
        cin >> st[i].m;
        st[i].m += 3 * (n - i - 1);
    }
    st.build();

    int m;
    cin >> m;

    for (int i = 0; i < m; i++) {
        int l, r, d;
        cin >> l >> r >> d;
        l--;

        st.lazy(l, r, d);
        cout << st.root().m << '\n';
    }
    cout << flush;

    return 0;
}
0