結果

問題 No.631 Noelちゃんと電車旅行
ユーザー nebukuro09nebukuro09
提出日時 2018-01-05 22:22:08
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 705 ms
コンパイル使用メモリ 119,540 KB
実行使用メモリ 12,576 KB
最終ジャッジ日時 2023-09-03 17:53:52
合計ジャッジ時間 8,885 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 209 ms
11,288 KB
testcase_01 AC 393 ms
12,224 KB
testcase_02 AC 386 ms
12,108 KB
testcase_03 AC 389 ms
12,072 KB
testcase_04 AC 390 ms
12,576 KB
testcase_05 AC 385 ms
11,608 KB
testcase_06 AC 146 ms
7,140 KB
testcase_07 AC 102 ms
7,892 KB
testcase_08 AC 292 ms
11,576 KB
testcase_09 AC 311 ms
7,532 KB
testcase_10 AC 250 ms
10,972 KB
testcase_11 AC 216 ms
7,836 KB
testcase_12 AC 275 ms
11,360 KB
testcase_13 AC 211 ms
6,412 KB
testcase_14 AC 83 ms
4,380 KB
testcase_15 AC 196 ms
6,732 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.datetime;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto st = new LazySegmentTree!long(N);
    foreach (i; 0..N) st.add(i, i, -3*i);
    foreach (i; 0..N-1) st.add(i, i, A[i]);

    auto Q = readln.chomp.to!int;
    while(Q--) {
        auto s = readln.split.map!(to!int);
        st.add(s[0]-1, s[1]-1, s[2].to!long);
        writeln(st.getVal(0, N-1) + 3 * (N - 1));
    }
}


class LazySegmentTree(T) {
    T[] table;
    T[] lazy_;
    int size;

    this(int n) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new T[](size);
        lazy_ = new T[](size);
        fill(table, 0);
        fill(lazy_, 0);
    }

    void eval(int i, int l, int r) {
        if (lazy_[i] == 0) return;

        table[i] += lazy_[i];
        if (l != r) {
            lazy_[i*2+1] += lazy_[i];
            lazy_[i*2+2] += lazy_[i];
        }

        lazy_[i] = 0;
    }

    void add(int a, int b, T num) {
        add(a, b, num, 0, 0, size/2-1);
    }

    void add(int a, int b, T num, int i, int l, int r) {
        eval(i, l, r);

        if (a > r || b < l) return;
        if (a <= l && r <= b) {
            lazy_[i] += num;
            eval(i, l, r);
        } else {
            add(a, b, num, i*2+1, l, (l+r)/2);
            add(a, b, num, i*2+2, (l+r)/2+1, r);
            table[i] = max(table[i*2+1], table[i*2+2]);
        }
    }

    T getVal(int a, int b) {
        return getVal(a, b, 0, 0, size/2-1);
    }

    T getVal(int a, int b, int i, int l, int r) {
        eval(i, l, r);

        if (a > r || b < l) return 0;
        if (a <= l && r <= b) return table[i];
        return
            max(getVal(a, b, i*2+1, l, (l+r)/2),
                getVal(a, b, i*2+2, (l+r)/2+1, r));
    }
}
0