結果

問題 No.631 Noelちゃんと電車旅行
ユーザー kimiyukikimiyuki
提出日時 2018-01-05 23:06:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 4,359 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 204,220 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 07:45:31
合計ジャッジ時間 6,543 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
6,816 KB
testcase_01 AC 180 ms
6,944 KB
testcase_02 AC 182 ms
6,944 KB
testcase_03 AC 185 ms
6,944 KB
testcase_04 AC 185 ms
6,944 KB
testcase_05 AC 182 ms
6,940 KB
testcase_06 AC 71 ms
6,944 KB
testcase_07 AC 48 ms
6,940 KB
testcase_08 AC 140 ms
6,940 KB
testcase_09 AC 145 ms
6,944 KB
testcase_10 AC 115 ms
6,940 KB
testcase_11 AC 101 ms
6,944 KB
testcase_12 AC 124 ms
6,944 KB
testcase_13 AC 103 ms
6,940 KB
testcase_14 AC 40 ms
6,940 KB
testcase_15 AC 91 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))
#define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; -- (i))
using ll = long long;
using namespace std;

template <class Monoid, class OperatorMonoid>
struct lazy_propagation_segment_tree { // on monoids
    static_assert (is_same<typename Monoid::underlying_type, typename OperatorMonoid::target_type>::value, "");
    typedef typename Monoid::underlying_type underlying_type;
    typedef typename OperatorMonoid::underlying_type operator_type;
    const Monoid mon;
    const OperatorMonoid op;
    int n;
    vector<underlying_type> a;
    vector<operator_type> f;
    lazy_propagation_segment_tree() = default;
    lazy_propagation_segment_tree(int a_n, underlying_type initial_value = Monoid().unit(), Monoid const & a_mon = Monoid(), OperatorMonoid const & a_op = OperatorMonoid())
            : mon(a_mon), op(a_op) {
        n = 1; while (n <= a_n) n *= 2;
        a.resize(2 * n - 1, mon.unit());
        fill(a.begin() + (n - 1), a.begin() + ((n - 1) + a_n), initial_value); // set initial values
        REP_R (i, n - 1) a[i] = mon.append(a[2 * i + 1], a[2 * i + 2]); // propagate initial values
        f.resize(max(0, (2 * n - 1) - n), op.identity());
    }
    void point_set(int i, underlying_type z) {
        assert (0 <= i and i < n);
        point_set(0, 0, n, i, z);
    }
    void point_set(int i, int il, int ir, int j, underlying_type z) {
        if (i == n + j - 1) { // 0-based
            a[i] = z;
        } else if (ir <= j or j+1 <= il) {
            // nop
        } else {
            range_apply(2 * i + 1, il, (il + ir) / 2, 0, n, f[i]);
            range_apply(2 * i + 2, (il + ir) / 2, ir, 0, n, f[i]);
            f[i] = op.identity();
            point_set(2 * i + 1, il, (il + ir) / 2, j, z);
            point_set(2 * i + 2, (il + ir) / 2, ir, j, z);
            a[i] = mon.append(a[2 * i + 1], a[2 * i + 2]);
        }
    }
    void range_apply(int l, int r, operator_type z) {
        assert (0 <= l and l <= r and r <= n);
        range_apply(0, 0, n, l, r, z);
    }
    void range_apply(int i, int il, int ir, int l, int r, operator_type z) {
        if (l <= il and ir <= r) { // 0-based
            a[i] = op.apply(z, a[i]);
            if (i < f.size()) f[i] = op.compose(z, f[i]);
        } else if (ir <= l or r <= il) {
            // nop
        } else {
            range_apply(2 * i + 1, il, (il + ir) / 2, 0, n, f[i]);
            range_apply(2 * i + 2, (il + ir) / 2, ir, 0, n, f[i]);
            f[i] = op.identity();
            range_apply(2 * i + 1, il, (il + ir) / 2, l, r, z);
            range_apply(2 * i + 2, (il + ir) / 2, ir, l, r, z);
            a[i] = mon.append(a[2 * i + 1], a[2 * i + 2]);
        }
    }
    underlying_type range_concat(int l, int r) {
        assert (0 <= l and l <= r and r <= n);
        return range_concat(0, 0, n, l, r);
    }
    underlying_type range_concat(int i, int il, int ir, int l, int r) {
        if (l <= il and ir <= r) { // 0-based
            return a[i];
        } else if (ir <= l or r <= il) {
            return mon.unit();
        } else {
            return op.apply(f[i], mon.append(
                    range_concat(2 * i + 1, il, (il + ir) / 2, l, r),
                    range_concat(2 * i + 2, (il + ir) / 2, ir, l, r)));
        }
    }
};
struct max_monoid {
    typedef ll underlying_type;
    ll unit() const { return INT_MIN; }
    ll append(ll a, ll b) const { return max(a, b); }
};
struct plus_operator_monoid {
    typedef ll underlying_type;
    typedef ll target_type;
    ll identity() const { return 0; }
    ll apply(underlying_type a, target_type b) const { return a + b; }
    ll compose(underlying_type a, underlying_type b) const { return a + b; }
};

int main() {
    // input
    int n; scanf("%d", &n);
    vector<int> t(n - 1); REP (i, n - 1) scanf("%d", &t[i]);
    // prepare
    lazy_propagation_segment_tree<max_monoid, plus_operator_monoid> segtree(n - 1);
    REP (i, n - 1) {
        segtree.point_set(i, t[i] + (n - 1 - i) * 3);
    }
    // serve
    int m; scanf("%d", &m);
    while (m --) {
        int l, r, d; scanf("%d%d%d", &l, &r, &d);
        -- l;
        segtree.range_apply(l, r, d);
        ll result = segtree.range_concat(0, n - 1);
        // output
        printf("%lld\n", result);
    }
    return 0;
}
0