結果

問題 No.789 範囲の合計
ユーザー FuyuruFuyuru
提出日時 2024-02-16 22:30:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 406 ms / 1,000 ms
コード長 1,379 bytes
コンパイル時間 1,991 ms
コンパイル使用メモリ 208,172 KB
実行使用メモリ 24,956 KB
最終ジャッジ日時 2024-02-16 22:30:23
合計ジャッジ時間 6,144 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 303 ms
22,488 KB
testcase_03 AC 75 ms
6,676 KB
testcase_04 AC 406 ms
23,164 KB
testcase_05 AC 327 ms
22,488 KB
testcase_06 AC 303 ms
22,488 KB
testcase_07 AC 112 ms
6,676 KB
testcase_08 AC 318 ms
24,572 KB
testcase_09 AC 310 ms
23,164 KB
testcase_10 AC 244 ms
14,516 KB
testcase_11 AC 337 ms
24,956 KB
testcase_12 AC 344 ms
24,956 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")


template <typename T>
struct BIT{
    int N;
    unordered_map<int, T> data;
    
    BIT(int size){
        N = size;
    }

    void add(int i,T x){
        i++;
        while (i <= N){
            if (data.find(i) != data.end()){
                data[i] += x;
            }
            else{
                data[i] = x;
            }
            i += i&-i;
        }
    }
    
    T fold(int l,int r){
        T res {};
        while (r > 0){
            if (data.find(r) != data.end()){
                res += data[r];
            }
            r -= r&-r;
        }
        while (l > 0){
            if (data.find(l) != data.end()){
                res -= data[l];
            }
            l -= l&-l;
        }
        return res;
    }
};

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int N;
    cin >> N;
    BIT<ll> bit = BIT<ll>(1000000001L);
    ll ans = 0;
    for (int i = 0;i < N;i++){
        int op;
        cin >> op;
        if (op == 0){
            int x,y;
            cin >> x >> y;
            bit.add(x,y);
        }
        else{
            int l,r;
            cin >> l >> r;
            ans += bit.fold(l,r+1);
        }
    }
    cout << ans << endl;
}
0