結果

問題 No.789 範囲の合計
ユーザー face4face4
提出日時 2019-02-08 22:44:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 1,000 ms
コード長 1,483 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 88,984 KB
実行使用メモリ 10,996 KB
最終ジャッジ日時 2023-09-14 03:52:27
合計ジャッジ時間 3,314 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,504 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 159 ms
9,644 KB
testcase_03 AC 65 ms
4,568 KB
testcase_04 AC 163 ms
10,140 KB
testcase_05 AC 139 ms
9,384 KB
testcase_06 AC 147 ms
9,644 KB
testcase_07 AC 58 ms
4,584 KB
testcase_08 AC 153 ms
10,996 KB
testcase_09 AC 141 ms
10,464 KB
testcase_10 AC 142 ms
7,716 KB
testcase_11 AC 132 ms
10,288 KB
testcase_12 AC 126 ms
10,264 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct BIT{
    // 1-origin
    vector<ll> v;
    int n;

    BIT(int _n):
        v(vector<ll>(_n+1, 0)), n(_n) {}

    ll sum(int i){
        ll s = 0;
        while(i > 0){
            s += v[i];
            i -= lsb(i);
        }
        return s;
    }

    ll sum(int l, int r){
        return sum(r) - sum(l-1);
    }

    void add(int i, int x){
        while(i <= n){
            v[i] += x;
            i += lsb(i);
        }
    }

private:
    // least significant bit
    int lsb(int i){
        return i & -i;
    }
};

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

    set<int> s;
    int a[n], b[n], c[n];
    for(int i = 0; i < n; i++){
        cin >> a[i] >> b[i] >> c[i];
        if(a[i] == 0)   s.insert(b[i]);
    }

    s.insert(0);
    s.insert(1000000000);
    map<int,int> zip;
    int cur = 1;
    for(auto it = s.begin(); it != s.end(); it++){
        zip[*it] = cur++;
    }

    BIT bit(zip.size());

    ll ans = 0;
    for(int i = 0; i < n; i++){
        if(a[i] == 0){
            bit.add(zip[b[i]], c[i]);
        }else if(a[i] == 1){
            auto l = zip.lower_bound(b[i]);
            auto r = zip.lower_bound(c[i]);
            int le = (*l).second, ri = (*r).second;
            if((*r).first != c[i])  ri--;
            if(le <= ri)    ans += bit.sum(le, ri);
        }
    }

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