結果

問題 No.631 Noelちゃんと電車旅行
ユーザー ふーらくたるふーらくたる
提出日時 2018-01-05 23:20:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 294 ms / 2,000 ms
コード長 2,954 bytes
コンパイル時間 590 ms
コンパイル使用メモリ 64,736 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-04-10 07:46:07
合計ジャッジ時間 5,779 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 222 ms
5,248 KB
testcase_01 AC 288 ms
8,064 KB
testcase_02 AC 290 ms
7,936 KB
testcase_03 AC 288 ms
7,936 KB
testcase_04 AC 289 ms
7,936 KB
testcase_05 AC 294 ms
7,936 KB
testcase_06 AC 116 ms
5,504 KB
testcase_07 AC 61 ms
5,760 KB
testcase_08 AC 212 ms
7,936 KB
testcase_09 AC 248 ms
5,504 KB
testcase_10 AC 173 ms
7,936 KB
testcase_11 AC 152 ms
5,632 KB
testcase_12 AC 198 ms
7,808 KB
testcase_13 AC 182 ms
5,376 KB
testcase_14 AC 86 ms
5,376 KB
testcase_15 AC 155 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 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 <limits>
using namespace std;

using int64 = long long;

template <typename T>
class segment_tree {
public:
    segment_tree(int n_) : n(__lowest_power_of_2(n_)), dat(n * 2, 0), lazy(n * 2, 0) {}

    template <typename Iterator>
    segment_tree(Iterator left, Iterator right)
        : n(__lowest_power_of_2(std::distance(left, right))), dat(n * 2, 0), lazy(n * 2, 0) {
        std::copy(left + n, right + n, dat.begin());
        for (int i = n - 1; i >= 1; --i) {
            dat[i] = dat[i * 2] + dat[i * 2 + 1];
        }
    }

private:
    int n;
    std::vector<T> dat, lazy;
    static const T INF = std::numeric_limits<T>::max() / 2;

public:
    void add(int ql, int qr, const T &x) { __add(1, 0, n, ql, qr, x); }

    T max(int ql, int qr) { return __max(1, 0, n, ql, qr); }

private:
    void __flush_lazy(int v) {
        dat[v] += lazy[v];
        if (v < n) {
            lazy[v * 2] += lazy[v];
            lazy[v * 2 + 1] += lazy[v];
        }
        lazy[v] = 0;
    }

    void __update_dat(int v) { dat[v] = std::max(dat[v * 2], dat[v * 2 + 1]); }

    void __add(int n, int l, int r, const int ql, const int qr, const T &x) {
        __flush_lazy(n);
        if (r <= ql || qr <= l) {
            return;
        } else if (ql <= l && r <= qr) {
            lazy[n] += x;
            __flush_lazy(n);
        } else {
            int m = (l + r) / 2;
            __add(n * 2, l, m, ql, qr, x);
            __add(n * 2 + 1, m, r, ql, qr, x);
            __update_dat(n);
        }
    }

    T __max(int n, int l, int r, const int ql, const int qr) {
        __flush_lazy(n);
        if (r <= ql || qr <= l) {
            return -INF;
        } else if (ql <= l && r <= qr) {
            return dat[n];
        } else {
            int m = (l + r) / 2;
            T res_left = __max(n * 2, l, m, ql, qr);
            T res_right = __max(n * 2 + 1, m, r, ql, qr);
            __update_dat(n);
            return std::max(res_left, res_right);
        }
    }

    int __lowest_power_of_2(int x) const {
        int res = 1;
        while (res < x) res <<= 1;
        return res;
    }
};

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

    int N;
    cin >> N;

    vector<int64> T(N - 1);
    for (int i = 0; i < N - 1; i++) {
        cin >> T[i];
        T[i] -= 3 * i;
    }
    segment_tree<int64> segtree(T.size());
    for (int i = 0; i < N - 1; i++) {
        segtree.add(i, i + 1, T[i]);
    }
    
    /*
    cerr << "T: " << endl;
    for (int i = 0; i < N - 1; i++) {
        cerr << segtree.max(i, i + 1) << endl;
    }
    */

    int M;
    cin >> M;
    for (int i = 0; i < M; i++) {
        int L, R, D;
        cin >> L >> R >> D;
        L--; R--;

        segtree.add(L, R + 1, D);
        int64 d = segtree.max(0, N - 1);

        /*
        */

        // cerr << "d: " << d << endl;

        cout << 3 * (N - 1) + d << endl;
    }
    return 0;
}
0