結果

問題 No.1000 Point Add and Array Add
ユーザー finefine
提出日時 2020-02-29 01:37:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,745 bytes
コンパイル時間 1,743 ms
コンパイル使用メモリ 167,200 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-04-21 21:17:25
合計ジャッジ時間 4,563 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 75 ms
9,344 KB
testcase_17 AC 60 ms
8,192 KB
testcase_18 AC 102 ms
11,264 KB
testcase_19 AC 102 ms
11,264 KB
testcase_20 AC 91 ms
11,264 KB
testcase_21 AC 105 ms
11,136 KB
testcase_22 AC 97 ms
11,264 KB
testcase_23 AC 104 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

//1-indexであることに注意
template <typename T>
struct BIT {
    int n;
    vector<T> data;
    int n_msb;

    BIT(int n) : n(n), data(n + 1, 0) {
        n_msb = 1;
        while (n_msb <= n) n_msb <<= 1;
        n_msb >>= 1;
    }

    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += data[i];
            i -= i & -i;
        }
        return s;
    }

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

    // sum(i) >= val となる最小のiを返す
    // ただし、sum(i)は広義単調増加すると仮定
    int lower_bound(T val) {
        if (val <= 0) return 0;

        int res = 0;
        for (int k = n_msb; k > 0; k >>= 1) {
            if (res + k <= n && data[res + k] < val) {
                val -= data[res + k];
                res += k;
            }
        }
        return res + 1;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<char> c(q);
    vector<ll> x(q), y(q);
    for (int i = 0; i < q; ++i) {
        cin >> c[i] >> x[i] >> y[i];
    }

    BIT<ll> bt(n + 1);
    vector<ll> b(n, 0);
    for (int i = q - 1; i >= 0; --i) {
        if (c[i] == 'A') {
            b[x[i] - 1] += y[i] * bt.sum(x[i]);
        } else {
            bt.add(x[i], 1);
            bt.add(y[i] + 1, -1);
        }
    }

    for (int i = 0; i < n; ++i) {
        b[i] += a[i] * bt.sum(i + 1);
    }

    for (int i = 0; i < n; ++i) {
        cout << b[i] << " \n"[i + 1 == n];
    }
    return 0;
}
0