結果
| 問題 | No.789 範囲の合計 | 
| コンテスト | |
| ユーザー |  lorent_kyopro | 
| 提出日時 | 2021-03-12 13:03:12 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 70 ms / 1,000 ms | 
| コード長 | 3,114 bytes | 
| コンパイル時間 | 1,130 ms | 
| コンパイル使用メモリ | 94,968 KB | 
| 最終ジャッジ日時 | 2025-01-19 13:47:39 | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 15 | 
ソースコード
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <iostream>
#include <memory>
#include <utility>
__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 = std::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;
    using node_ptr = std::unique_ptr<node>;
    struct node {
        size_t index;
        S value, product;
        node_ptr left, right;
        node(size_t index, S value)
            : index(index), value(value), product(value), left(nullptr), right(nullptr) {}
    };
    const size_t n;
    node_ptr root;
    S value(const node_ptr& t) const { return t == nullptr ? e() : t->product; }
    node_ptr& set(node_ptr& t, size_t a, size_t b, size_t p, S x) const {
        if (t == nullptr) 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 = std::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 = std::move(set(t->right, c, b, p, x));
        }
        t->product = op(value(t->left), op(t->value, value(t->right)));
        return t;
    }
    S get(const node_ptr& t, size_t a, size_t b, size_t p) const {
        if (t == nullptr) 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(const node_ptr& t, size_t a, size_t b, size_t l, size_t r) const {
        if (t == nullptr || b <= l || r <= a) return e();
        if (l <= a && b <= r) return t->product;
        size_t c = (a + b) >> 1;
        S result = prod(t->left, a, c, l, r);
        if (l <= t->index && t->index < r) result = op(result, t->value);
        return op(result, 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';
}
            
            
            
        