結果

問題 No.789 範囲の合計
ユーザー Manuel1024Manuel1024
提出日時 2021-03-24 01:32:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 245 ms / 1,000 ms
コード長 1,340 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 83,168 KB
実行使用メモリ 13,316 KB
最終ジャッジ日時 2023-08-17 12:44:49
合計ジャッジ時間 3,892 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 244 ms
12,200 KB
testcase_03 AC 71 ms
4,380 KB
testcase_04 AC 221 ms
11,724 KB
testcase_05 AC 168 ms
12,244 KB
testcase_06 AC 178 ms
12,204 KB
testcase_07 AC 62 ms
4,380 KB
testcase_08 AC 118 ms
7,704 KB
testcase_09 AC 111 ms
7,452 KB
testcase_10 AC 245 ms
13,316 KB
testcase_11 AC 190 ms
12,016 KB
testcase_12 AC 172 ms
11,992 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;
using ll = long long int;

class BIT{
private:
    vector<ll> a;
    int n;
public:
    BIT(int len){
        a = vector<ll>(len+1, 0);
        n = len;
    }

    void add(int ind, ll x){
        ind++;
        while(ind <= n){
            a[ind] += x;
            ind += ind & (-ind);
        }
    }

    ll sum_sub(int end){
        end++;
        ll ans = 0;
        while(end > 0){
            ans += a[end];
            end -= end & (-end);
        }
        return ans;
    }

    ll sum(int start, int end){
        return sum_sub(end)-sum_sub(start-1);
    }
};

int main(){
    int n;
    cin >> n;
    vector<int> q(n), x(n), y(n);
    for(int i = 0; i < n; i++) cin >> q[i] >> x[i] >> y[i];
    map<int, int> ind;
    for(int i = 0; i < n; i++){
        ind[x[i]];
        if(q[i] == 1) ind[y[i]];
    }

    {
        int i = 0;
        for(auto &p: ind){
            p.second = i;
            i++;
        }
    }

    BIT data(ind.size()+1);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        if(q[i] == 0){
            data.add(ind[x[i]], y[i]);
        }else{
            ans += data.sum(ind[x[i]], ind[y[i]]);
            //cerr << ans << " " << data.sum(ind[x[i]], ind[y[i]]) << endl;
        }
    }
    cout << ans << endl;
    return 0;
}
0