結果

問題 No.789 範囲の合計
ユーザー たぴちゃんたぴちゃん
提出日時 2019-02-08 22:36:57
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 131 ms / 1,000 ms
コード長 1,539 bytes
コンパイル時間 2,002 ms
使用メモリ 10,280 KB
最終ジャッジ日時 2023-02-01 16:55:37
合計ジャッジ時間 4,376 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
4,904 KB
testcase_01 AC 2 ms
4,900 KB
testcase_02 AC 123 ms
9,684 KB
testcase_03 AC 75 ms
9,628 KB
testcase_04 AC 118 ms
10,280 KB
testcase_05 AC 112 ms
9,672 KB
testcase_06 AC 113 ms
9,640 KB
testcase_07 AC 67 ms
9,588 KB
testcase_08 AC 96 ms
9,660 KB
testcase_09 AC 95 ms
9,596 KB
testcase_10 AC 131 ms
10,064 KB
testcase_11 AC 114 ms
9,640 KB
testcase_12 AC 113 ms
9,636 KB
testcase_13 AC 2 ms
4,904 KB
testcase_14 AC 1 ms
6,952 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