結果

問題 No.789 範囲の合計
ユーザー pockynypockyny
提出日時 2023-01-09 17:23:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,985 bytes
コンパイル時間 705 ms
コンパイル使用メモリ 73,500 KB
実行使用メモリ 34,976 KB
最終ジャッジ日時 2023-08-22 19:25:47
合計ジャッジ時間 3,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;
typedef long long ll;
struct dynamicSegtree{
    ll e(){return 0;}
    ll op(ll a,ll b){return a + b;}
    struct node {
        ll value;
        node* left;
        node* right;
        node(ll value): value(value), left(nullptr), right(nullptr){}
    };
    int n;
    node* root;
    dynamicSegtree(int sz): n(sz), root(nullptr){}
    void set(int pos,ll val){func_set(pos,val,root,0,n);}
    void func_set(int pos,ll val,node* &t,int le,int ri){
        if(!t) t = new node(e());
        if(ri - le==1){
            t->value += val;
            return;
        }
        int mid = (le + ri)/2;
        if(pos<mid) func_set(pos,val,t->left,le,mid);
        else func_set(pos,val,t->right,mid,ri);
        t->value = e();
        if(t->left) t->value = op(t->left->value,t->value);
        if(t->right) t->value = op(t->value,t->right->value);
    }
    ll get(int pos){return func_get(pos,root,0,n);}
    ll func_get(int pos,node* &t,int le,int ri){
        if(!t) return e();
        if(ri - le==1) return t->value;
        int mid = (le + ri)/2;
        if(pos<mid) return func_get(pos,t->left,0,mid);
        return func_get(pos,t->right,mid,ri);
    }
    ll prod(int le,int ri){return func_prod(le,ri,root,0,n);}
    ll func_prod(int query_le,int query_ri,node* &t, int le,int ri){
        if(!t || ri<=query_le || query_ri<=le) return e();
        if(query_le<=le && ri<=query_ri) return t->value;
        int mid = (le + ri)/2;
        return op(func_prod(query_le,query_ri,t->left,le,mid),func_prod(query_le,query_ri,t->right,mid,ri));
    }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int i,q; cin >> q;
    int n = 1000000001;
    dynamicSegtree seg(n);
    for(i=0;i<q;i++){
        int t; cin >> t;
        if(t==0){
            ll x,y; cin >> x >> y;
            seg.set(x,y);
        }else{
            int l,r; cin >> l >> r; l--;
            cout << seg.prod(l,r) << "\n";
        }
    }
}
0