結果

問題 No.789 範囲の合計
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-09-08 18:27:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 1,000 ms
コード長 3,309 bytes
コンパイル時間 1,947 ms
コンパイル使用メモリ 178,256 KB
実行使用メモリ 8,968 KB
最終ジャッジ日時 2023-08-27 02:10:08
合計ジャッジ時間 3,810 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 83 ms
7,840 KB
testcase_03 AC 54 ms
4,380 KB
testcase_04 AC 95 ms
8,432 KB
testcase_05 AC 77 ms
7,620 KB
testcase_06 AC 80 ms
7,904 KB
testcase_07 AC 42 ms
4,380 KB
testcase_08 AC 67 ms
8,968 KB
testcase_09 AC 59 ms
8,512 KB
testcase_10 AC 81 ms
6,080 KB
testcase_11 AC 58 ms
8,256 KB
testcase_12 AC 60 ms
8,276 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.09.08 18:23:55
**/

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

template< typename Monoid >
struct DynamicSegmentTree {
private:
    struct Node;
    using Func = function<Monoid(Monoid,Monoid)>;
    using node_ptr = unique_ptr<Node>;
    Func F;
    Monoid e;
    size_t n;
    struct Node {
        size_t index;
        Monoid value,prod;
        node_ptr left, right;
        Node(size_t index, Monoid value) : index(index),
                                           value(value),
                                           prod(value),
                                           left(nullptr),
                                           right(nullptr) {}
    };
    
    void node_update(node_ptr &np) const {
        np->prod = F(F(np->left?np->left->prod:e,np->value),
                       np->right?np->right->prod:e);
    }
    void _update(node_ptr& t, size_t a, size_t b, size_t p, Monoid &val) const {
        if (!t) {
            t = make_unique<Node>(p, val);
            return;
        }
        if (t->index == p) {
            t->value = val;
            node_update(t);
            return;
        }
        size_t c = (a+b)/2;
        if (p < c) {
            if (t->index < p) swap(t->index,p), swap(t->value,val);
            _update(t->left,a,c,p,val);
        }
        else {
            if (p < t->index) swap(p,t->index), swap(val,t->value);
            _update(t->right,c,b,p,val);
        }
        node_update(t);
    }
    Monoid _get(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->prod;
        size_t c = (a + b) / 2;
        Monoid result = _get(t->left, a, c, l, r);
        if (l <= t->index && t->index < r) result = F(result, t->value);
        return F(result, _get(t->right, c, b, l, r));
    }
    void _print(node_ptr& t) {
        if (!t) return;
        cout << "[" << t->index << "] : " << t->value << endl;
        _print(t->left);
        _print(t->right);
    }
    node_ptr root;
public:
    DynamicSegmentTree(size_t n, const Func f, const Monoid &e) : n(n),
                                                                  F(f),
                                                                  e(e),
                                                                  root(nullptr) {}
    void update_query(size_t x, Monoid val) {
        assert(x < n);
        _update(root, 0, n, x, val);
    }
    Monoid get_query(size_t l, size_t r) const {
        assert(l <= r && r <= n);
        return _get(root, 0, n, l, r);
    }
    
    
    Monoid operator[](int x) const {
        return get_query(x,x+1);
    }
    
    void print() {
        _print(root);
    }
    size_t size() {
        return n;
    }
};
int main() {
    int n;scanf("%d",&n);
    DynamicSegmentTree<ll> seg(1000000001,[](ll a,ll b){return a+b;},0);
    unordered_map<int,int> mp;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int t,x,y;scanf("%d%d%d",&t,&x,&y);
        if (t == 0) {
            mp[x] += y;
            seg.update_query(x,mp[x]);
        }
        else {
            ans += seg.get_query(x,y+1);
        }
    }
    printf("%lld\n",ans);
    return 0;
}
0