結果

問題 No.631 Noelちゃんと電車旅行
ユーザー finefine
提出日時 2019-11-09 17:33:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 5,576 bytes
コンパイル時間 1,908 ms
コンパイル使用メモリ 172,280 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-04-10 07:59:40
合計ジャッジ時間 5,616 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
6,816 KB
testcase_01 AC 167 ms
7,296 KB
testcase_02 AC 172 ms
7,296 KB
testcase_03 AC 169 ms
7,296 KB
testcase_04 AC 159 ms
7,168 KB
testcase_05 AC 162 ms
7,296 KB
testcase_06 AC 63 ms
6,940 KB
testcase_07 AC 41 ms
6,940 KB
testcase_08 AC 119 ms
7,296 KB
testcase_09 AC 129 ms
6,940 KB
testcase_10 AC 101 ms
7,296 KB
testcase_11 AC 88 ms
6,944 KB
testcase_12 AC 110 ms
7,168 KB
testcase_13 AC 92 ms
6,944 KB
testcase_14 AC 37 ms
6,940 KB
testcase_15 AC 77 ms
6,940 KB
testcase_16 AC 2 ms
6,944 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 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

template <class MonoidOp>
struct LazySegmentTree {
    using Tm = typename MonoidOp::Tm;
    using To = typename MonoidOp::To;

    int n;
    vector<Tm> data;
    vector<To> lazy;

    LazySegmentTree(int size, Tm initial_data_value = MonoidOp::unit()) {
        n = 1;
        while (n < size) n <<= 1;

        data.assign(2 * n - 1, initial_data_value);
        lazy.assign(2 * n - 1, MonoidOp::op_unit());

        if (initial_data_value != MonoidOp::unit()) {
            for (int i = n - 2; i >= 0; i--) data[i] = MonoidOp::merge(data[i * 2 + 1], data[i * 2 + 2]);
        }
    }

    LazySegmentTree(const vector<Tm>& v) {
        int size = v.size();
        n = 1;
        while (n < size) n <<= 1;
        data.assign(2 * n - 1, MonoidOp::unit());
        lazy.assign(2 * n - 1, MonoidOp::op_unit());

        for (int i = 0; i < size; i++) data[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) data[i] = MonoidOp::merge(data[i * 2 + 1], data[i * 2 + 2]);
    }

    Tm getLeaf(int k) {
        return query(k, k + 1);
    }

    void push(int k, int l, int r) {
        if (lazy[k] == MonoidOp::op_unit()) return;
        MonoidOp::apply(data[k], lazy[k], r - l);
        if (r - l > 1) {
            MonoidOp::update(lazy[2 * k + 1], lazy[k]);
            MonoidOp::update(lazy[2 * k + 2], lazy[k]);
        }
        lazy[k] = MonoidOp::op_unit();
    }

    //区間[a, b)に対する更新
    //k:節点番号, [l, r):節点に対応する区間
    void update(int a, int b, To x, int k, int l, int r) {
        push(k, l, r);
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return;
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) {
            MonoidOp::update(lazy[k], x);
            push(k, l, r);
        } else {
            update(a, b, x, k * 2 + 1, l, (l + r) / 2);
            update(a, b, x, k * 2 + 2, (l + r) / 2, r);
            data[k] = MonoidOp::merge(data[2 * k + 1], data[2 * k + 2]);
        }
    }

    void update(int a, int b, To x) {
        update(a, b, x, 0, 0, n);
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    Tm query(int a, int b, int k, int l, int r) {
        push(k, l, r);
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return MonoidOp::unit();
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            Tm vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            Tm vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return MonoidOp::merge(vl, vr);
        }
    }

    //外から呼ぶ用
    Tm query(int a, int b) {
        return query(a, b, 0, 0, n);
    }
};

// 以下、MonoidOpの例
template<class U = ll, class V = U>
struct RangeSumAdd {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return x + y; }
    static void update(To& target, To x) { target += x; }
    static void apply(Tm& target, To x, int seg_len) { target += x * seg_len; }
    static constexpr Tm unit() { return Tm(0); }
    static constexpr To op_unit() { return To(0); }
};

template<class U = ll, class V = U>
struct RangeMinUpdate {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return min(x, y); }
    static void update(To& target, To x) { target = x; }
    static void apply(Tm& target, To x, int seg_len) { target = x; }
    static constexpr Tm unit() { return numeric_limits<Tm>::max(); }
    static constexpr To op_unit() { return numeric_limits<To>::max(); }
};

// 作用素の初期値は更新クエリで与えられる値の定義域の範囲外の値にする
template<class U = ll, class V = U>
struct RangeSumUpdate {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return x + y; }
    static void update(To& target, To x) { target = x; }
    static void apply(Tm& target, To x, int seg_len) { target = x * seg_len; }
    static constexpr Tm unit() { return Tm(0); }
    static constexpr To op_unit() { return numeric_limits<To>::min(); }
};

// 初期値 != 単位元 の場合に注意
template<class U = ll, class V = U>
struct RangeMinAdd {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return min(x, y); }
    static void update(To& target, To x) { target += x; }
    static void apply(Tm& target, To x, int seg_len) { target += x; }
    static constexpr Tm unit() { return numeric_limits<Tm>::max(); }
    static constexpr To op_unit() { return To(0); }
};

template<class U = ll, class V = U>
struct RangeMaxAdd {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return max(x, y); }
    static void update(To& target, To x) { target += x; }
    static void apply(Tm& target, To x, int seg_len) { target += x; }
    static constexpr Tm unit() { return numeric_limits<Tm>::min(); }
    static constexpr To op_unit() { return To(0); }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    n--;
    LazySegmentTree< RangeMaxAdd<> > lst(n, 0);
    for (int i = 0; i < n; i++) {
        ll t;
        cin >> t;
        lst.update(i, i + 1, t + 3 * (n - i));
    }
    
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        int l, r;
        ll d;
        cin >> l >> r >> d;
        l--;
        lst.update(l, r, d);
        cout << lst.query(0, n) << "\n";
    }
    return 0;
}
0