結果

問題 No.789 範囲の合計
ユーザー たぴちゃんたぴちゃん
提出日時 2019-02-08 22:36:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 1,000 ms
コード長 1,539 bytes
コンパイル時間 2,628 ms
コンパイル使用メモリ 210,044 KB
実行使用メモリ 9,900 KB
最終ジャッジ日時 2023-09-14 03:51:23
合計ジャッジ時間 4,607 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 124 ms
9,580 KB
testcase_03 AC 78 ms
9,792 KB
testcase_04 AC 122 ms
9,796 KB
testcase_05 AC 115 ms
9,780 KB
testcase_06 AC 116 ms
9,572 KB
testcase_07 AC 71 ms
9,836 KB
testcase_08 AC 99 ms
9,584 KB
testcase_09 AC 97 ms
9,472 KB
testcase_10 AC 135 ms
9,840 KB
testcase_11 AC 117 ms
9,612 KB
testcase_12 AC 118 ms
9,900 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;

struct BIT{
    int N;
    vector<int> dat;
    BIT(int n) {
        N = n;
        dat.resize(N + 1);
    }
    void add(int k, int x){
        k++;
        while(k <= N){
            dat[k] += x;
            k += k&-k;
        }
    }
    int sum(int k){
        int s = 0;
        while(k > 0){
            s += dat[k];
            k -= k&-k;
        }
        return s;
    }
    int query(int a, int b){
        return sum(b) - sum(a);
    }
};

signed main(){
    int n;
    cin >> n;
    vector<PP> q;
    vector<int> d;
    for(int i = 0; i < n; i++){
        int c, a, b;
        cin >> c >> a >> b;
        d.push_back(a);
        if(c == 1) d.push_back(b);
        q.push_back({c, {a, b}});
    }
    vector<int> e;
    for(int i = 0; i < d.size(); i++) e.push_back(d[i]);
    sort(e.begin(), e.end());
    e.erase(unique(e.begin(), e.end()), e.end());
    for(int i = 0; i < n; i++){
        q[i].second.first = lower_bound(e.begin(), e.end(), q[i].second.first) - e.begin();
        if(q[i].first == 1){
            q[i].second.second = lower_bound(e.begin(), e.end(), q[i].second.second) - e.begin();
        }
    }
    BIT bt(e.size());
    int ans = 0;
    for(int i = 0; i < n; i++){
        if(q[i].first == 0){
            bt.add(q[i].second.first, q[i].second.second);
        }else{
            ans += bt.query(q[i].second.first, q[i].second.second + 1);
        }
    }
    cout << ans << endl;
}
0