結果

問題 No.1000 Point Add and Array Add
ユーザー nanaenanae
提出日時 2020-02-28 21:46:01
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 294 ms / 2,000 ms
コード長 2,437 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 156,016 KB
実行使用メモリ 31,556 KB
最終ジャッジ日時 2023-09-04 06:12:54
合計ジャッジ時間 5,882 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 211 ms
17,804 KB
testcase_17 AC 173 ms
10,776 KB
testcase_18 AC 294 ms
31,520 KB
testcase_19 AC 293 ms
31,556 KB
testcase_20 AC 228 ms
16,976 KB
testcase_21 AC 280 ms
17,948 KB
testcase_22 AC 282 ms
17,768 KB
testcase_23 AC 282 ms
17,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}



enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;

alias Query = Tuple!(char, "c", int, "x", int, "y");
void main() {
    int N, Q;
    scan(N, Q);
    auto a = readln.split.to!(long[]);
    auto b = new long[](N);
    auto ft = FenwickTree!(long)(N + 1);
    auto qs = new Query[](Q);
    foreach (i ; 0 .. Q) {
        char c; int x, y;
        scan(c, x, y);
        qs[i] = Query(c, x, y);
    }

    foreach_reverse (i ; 0 .. Q) {
        char c = qs[i].c;
        int x = qs[i].x, y = qs[i].y;
        if (c == 'A') {
            b[x - 1] += 1L * y * ft.sum(x);
        }
        else {
            x--;
            ft.add(x, 1);
            ft.add(y, -1);
        }
    }
    foreach (i ; 0 .. N) {
        b[i] += a[i] * ft.sum(i + 1);
    }
    writefln("%(%s %)", b);
}

struct FenwickTree(T) {
    private {
        int _size;
        T[] _data;
    }

    this(int N) {
        _size = N;
        _data = new T[](_size + 1);
    }

    void add(int i, T x) {
        i++;
        while (i <= _size) {
            _data[i] += x;
            i += i & (-i);
        }
    }

    T sum(int r) {
        T res = 0;
        while (r > 0) {
            res += _data[r];
            r -= r & (-r);
        }
        return res;
    }
}

unittest {
    auto ft = FenwickTree!(int)(10);
    ft.add(0, 3);
    ft.add(2, 4);
    ft.add(5, 1);
    assert(ft.sum(0) == 0);
    assert(ft.sum(1) == 3);
    assert(ft.sum(2) == 3);
    assert(ft.sum(3) == 7);
    assert(ft.sum(5) == 7);
    assert(ft.sum(6) == 8);
}
0