結果

問題 No.789 範囲の合計
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-03-12 01:00:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 1,000 ms
コード長 2,932 bytes
コンパイル時間 2,961 ms
コンパイル使用メモリ 214,184 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-04-21 13:03:54
合計ジャッジ時間 5,287 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 81 ms
6,016 KB
testcase_03 AC 51 ms
5,376 KB
testcase_04 AC 84 ms
6,144 KB
testcase_05 AC 66 ms
5,632 KB
testcase_06 AC 68 ms
6,016 KB
testcase_07 AC 39 ms
5,376 KB
testcase_08 AC 66 ms
6,528 KB
testcase_09 AC 61 ms
6,144 KB
testcase_10 AC 73 ms
5,376 KB
testcase_11 AC 48 ms
6,144 KB
testcase_12 AC 49 ms
6,016 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

__attribute__((constructor))
void fast_io() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

template <class S, S (*op)(S, S), S (*e)()> class dynamic_segtree {
public:
    dynamic_segtree(size_t n): n(n), root(nullptr) {}

    void set(size_t p, S x) {
        assert(p < n);
        root = move(set(root, 0, n, p, x));
    }

    S get(size_t p) {
        assert(p < n);
        return get(root, 0, n, p);
    }

    S prod(size_t l, size_t r) {
        assert(l <= r && r <= n);
        return prod(root, 0, n, l, r);
    }

private:
    struct node {
        size_t index;
        S value, product;
        std::unique_ptr<node> left, right;
        node(size_t index, S value)
            : index(index), value(value), product(value), left(nullptr), right(nullptr) {}
    };

    using node_ptr = std::unique_ptr<node>;

    const size_t n;
    node_ptr root;

    S value(node_ptr &t) const { return t ? t->product : e(); }

    node_ptr &set(node_ptr &t, size_t a, size_t b, size_t p, S x) const {
        if (!t) return t = std::make_unique<node>(p, x);
        if (t->index == p) {
            t->value = x;
            t->product = op(value(t->left), op(t->value, value(t->right)));
            return t;
        }
        size_t c = (a + b) >> 1;
        if (p < c) {
            if (t->index < p) std::swap(t->index, p), std::swap(t->value, x);
            t->left = move(set(t->left, a, c, p, x));
        } else {
            if (p < t->index) std::swap(p, t->index), std::swap(x, t->value);
            t->right = move(set(t->right, c, b, p, x));
        }
        t->product = op(value(t->left), op(t->value, value(t->right)));
        return t;
    }

    S get(node_ptr &t, size_t a, size_t b, size_t p) const {
        if (!t) return e();
        if (t->index == p) return t->value;
        size_t c = (a + b) >> 1;
        if (p < c) return get(t->left, a, c, p);
        else return get(t->right, c, b, p);
    }

    S prod(node_ptr &t, size_t a, size_t b, size_t l, size_t r) const {
        if (!t || b <= l || r <= a) return e();
        if (l <= a && b <= r) return t->product;
        size_t c = (a + b) >> 1;
        S res = prod(t->left, a, c, l, r);
        if (l <= t->index && t->index < r) res = op(res, t->value);
        return op(res, prod(t->right, c, b, l, r));
    }
};

using S = int;
S op(S a, S b) { return a + b; }
S e() { return 0; }

int main() {
    const size_t n = 1000000001;
    dynamic_segtree<S, op, e> seg(n);

    size_t q;
    std::cin >> q;
    long long ans = 0;
    while (q--) {
        int type;
        std::cin >> type;
        if (type == 0) {
            size_t x;
            int y;
            std::cin >> x >> y;
            seg.set(x, seg.get(x) + y);
        } else {
            size_t l, r;
            std::cin >> l >> r;
            ans += seg.prod(l, r + 1);
        }
    }
    std::cout << ans << '\n';
}
0