結果

問題 No.789 範囲の合計
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-09-08 18:35:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 1,000 ms
コード長 2,934 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 171,956 KB
実行使用メモリ 6,608 KB
最終ジャッジ日時 2023-08-27 02:20:14
合計ジャッジ時間 3,298 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 73 ms
5,972 KB
testcase_03 AC 48 ms
4,380 KB
testcase_04 AC 76 ms
6,296 KB
testcase_05 AC 58 ms
5,772 KB
testcase_06 AC 61 ms
6,012 KB
testcase_07 AC 38 ms
4,380 KB
testcase_08 AC 57 ms
6,608 KB
testcase_09 AC 55 ms
6,324 KB
testcase_10 AC 68 ms
5,080 KB
testcase_11 AC 45 ms
6,180 KB
testcase_12 AC 47 ms
6,228 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

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);
        set(root, 0, n, p, x);
    }

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

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

    S all_prod() const { return root ? root->product : e(); }

private:
    struct node;
    using node_ptr = 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) {}

        void update() {
            product = op(op(left ? left->product : e(), value),
                         right ? right->product : e());
        }
    };

    const size_t n;
    node_ptr root;

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

    S get(const 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(const 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 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 = ll;
S op(S a, S b) { return a + b; }
S e() { return 0; }

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

    int q;
    scanf("%d",&q);
    long long ans = 0;
    while (q--) {
        int type;
        scanf("%d",&type);
        if (type == 0) {
            int x;
            int y;
            scanf("%d%d",&x,&y);
            seg.set(x, seg.get(x) + y);
        } else {
            int l, r;
            scanf("%d%d",&l,&r);
            ans += seg.prod(l, r + 1);
        }
    }
    printf("%lld\n",ans);
}
0