結果

問題 No.631 Noelちゃんと電車旅行
ユーザー pianonekopianoneko
提出日時 2018-12-26 20:38:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 369 ms / 2,000 ms
コード長 2,405 bytes
コンパイル時間 1,778 ms
コンパイル使用メモリ 171,700 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-10 07:55:44
合計ジャッジ時間 7,739 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 274 ms
6,816 KB
testcase_01 AC 363 ms
8,832 KB
testcase_02 AC 369 ms
8,960 KB
testcase_03 AC 369 ms
8,832 KB
testcase_04 AC 368 ms
8,832 KB
testcase_05 AC 363 ms
8,832 KB
testcase_06 AC 145 ms
6,944 KB
testcase_07 AC 75 ms
6,944 KB
testcase_08 AC 283 ms
8,576 KB
testcase_09 AC 327 ms
6,940 KB
testcase_10 AC 223 ms
8,448 KB
testcase_11 AC 198 ms
6,944 KB
testcase_12 AC 246 ms
8,576 KB
testcase_13 AC 241 ms
6,940 KB
testcase_14 AC 112 ms
6,944 KB
testcase_15 AC 213 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,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using i64 = long long int;

const int iINF = INT_MAX;

template <typename Monoid>
struct StarrySky {
    using F = function<Monoid(Monoid, Monoid)>;

    int size = 1;
    vector<Monoid> node, lazy;

    const F f;
    const Monoid M1, M2;

    StarrySky(vector<Monoid> vec, const F f, const Monoid &M1, const Monoid &M2)
        : f(f), M1(M1), M2(M2) {
            init((int)vec.size());

            for(int i = 0; i < (int)vec.size(); i++) {
                node[i + size - 1] = vec[i];
            }

            for(int i = size - 2; i >= 0; i--) {
                node[i] = f(node[i * 2 + 1], node[i * 2 + 2]);
            }
        }

    void init(int n) {
        while(size < n) size <<= 1;
        node.resize(size * 2, M1);
        lazy.resize(size * 2, M2);
    }

    void lazyEvaluate(int i, int l, int r) {
        if(lazy[i] == M2) return;
        node[i] += lazy[i];
        if(r - l > 1) {
            lazy[i * 2 + 1] += lazy[i];
            lazy[i * 2 + 2] += lazy[i];
        }
        lazy[i] = M2;
    }

    void update(int a, int b, Monoid &x, int i = 0, int l = 0, int r = -1) {
        if(r < 0) r = size;
        lazyEvaluate(i, l, r);
        if(r <= a || b <= l) return;
        if(a <= l && r <= b) {
            lazy[i] += x;
            lazyEvaluate(i, l, r);
        } else {
            update(a, b, x, i * 2 + 1, l, (l + r) / 2);
            update(a, b, x, i * 2 + 2, (l + r) / 2, r);
            node[i] = f(node[i * 2 + 1], node[i * 2 + 2]);
        }
    }

    Monoid query(int a, int b, int i = 0, int l = 0, int r = -1) {
        if(r < 0) r = size;
        lazyEvaluate(i, l, r);
        if(r <= a || b <= l) return M1;
        if(a <= l && r <= b) return node[i];
        Monoid xl = query(a, b, i * 2 + 1, l, (l + r) / 2);
        Monoid xr = query(a, b, i * 2 + 2, (l + r) / 2, r);
        return f(xl, xr);
    }
};

int main() {
    int n, m;
    cin >> n;
    n -= 1;
    vector<i64> vec(n);

    for(int i = 0; i < n; i++) {
        cin >> vec[i];
        vec[i] += 3 * (n - i);
    }

    StarrySky<i64> seg(vec, [] (i64 a, i64 b) { return max(a, b); }, 0, 0);

    cin >> m;

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

        seg.update(l, r + 1, d);

        cout << seg.query(0, n + 1) << endl;
    }

    return 0;
}
0