結果

問題 No.789 範囲の合計
ユーザー face4face4
提出日時 2019-02-13 11:29:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 1,000 ms
コード長 1,434 bytes
コンパイル時間 1,113 ms
コンパイル使用メモリ 79,908 KB
実行使用メモリ 7,096 KB
最終ジャッジ日時 2023-10-11 11:08:18
合計ジャッジ時間 3,222 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 134 ms
7,056 KB
testcase_03 AC 77 ms
5,064 KB
testcase_04 AC 128 ms
7,028 KB
testcase_05 AC 119 ms
6,920 KB
testcase_06 AC 121 ms
6,944 KB
testcase_07 AC 66 ms
5,492 KB
testcase_08 AC 97 ms
5,736 KB
testcase_09 AC 94 ms
5,392 KB
testcase_10 AC 148 ms
7,096 KB
testcase_11 AC 124 ms
7,024 KB
testcase_12 AC 123 ms
6,936 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

struct ST{
private:
    int n;
    vector<int> node;

public:
    ST(int _n){
        n = 1;
        while(n < _n)   n *= 2;
        node.resize(2*n-1, 0);
    }

    void add(int k, int x){
        k += n-1;
        node[k] += x;
        while(k > 0){
            k = (k-1)/2;
            node[k] += x;
        }
    }

    ll query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        if(r <= a || b <= l)    return 0;
        if(a <= l && r <= b)    return node[k];
        ll lx = query(a, b, 2*k+1, l, (l+r)/2);
        ll rx = query(a, b, 2*k+2, (l+r)/2, r);
        return lx + rx;
    }
};

int main(){
    int n;
    cin >> n;

    vector<int> v;
    int a[n], b[n], c[n];
    for(int i = 0; i < n; i++){
        cin >> a[i] >> b[i] >> c[i];
        v.push_back(b[i]);
        if(a[i] == 1)   v.push_back(c[i]);
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    int siz = v.size();
    ST st(siz);

    auto pos = [&](int val) -> int{
        return lower_bound(v.begin(), v.end(), val)-v.begin();
    };

    ll ans = 0;
    for(int i = 0; i < n; i++){
        if(a[i] == 0){
            st.add(pos(b[i]), c[i]);
        }else if(a[i] == 1){
            ans += st.query(pos(b[i]), pos(c[i])+1);
        }
    }

    cout << ans << endl;
    
    return 0;
}
0