結果

問題 No.789 範囲の合計
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-02-09 18:09:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 150 ms / 1,000 ms
コード長 1,645 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 213,396 KB
実行使用メモリ 18,704 KB
最終ジャッジ日時 2024-02-09 18:10:05
合計ジャッジ時間 4,873 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 130 ms
17,168 KB
testcase_03 AC 36 ms
7,824 KB
testcase_04 AC 138 ms
16,656 KB
testcase_05 AC 84 ms
17,424 KB
testcase_06 AC 104 ms
17,168 KB
testcase_07 AC 35 ms
7,824 KB
testcase_08 AC 64 ms
11,408 KB
testcase_09 AC 60 ms
11,024 KB
testcase_10 AC 150 ms
18,704 KB
testcase_11 AC 107 ms
16,784 KB
testcase_12 AC 104 ms
16,784 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename T>
map<T, T> compress(vector<T> &A){

    map<T, T> comp;
    int N = A.size(), i=0;
    for (int i=0; i<N; i++) comp[A[i]];

    for (auto &e : comp){
        e.second = i;
        i++;
    }

    return comp;
}

template<class T> struct BIT {
    private:
        vector<T> bit; int N;
        T _sum(int r){
            r++; T res = 0;
            while(r > 0) res += bit[r-1], r -= r & -r;
            return res;
        }

    public:
        BIT (int n){ N = n; bit.resize(N);}
        BIT (vector<T> &a) : BIT(a.size()) { for (int i=0; i<N; i++) add(i, a[i]);}
        void add(int loc, T val){
            loc++;
            while(loc <= N) bit[loc-1] += val, loc += loc & -loc;
        }
        void change(int loc, T val){add(loc, -get(loc)+val);}
        T sum(int l, int r) {return _sum(r) - _sum(l-1);}
        T get(int l) {return sum(l, l);}
        void clear() {for (int i=0; i<N; i++) bit[i] = 0;}
};

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    ll n, ans=0;
    cin >> n;
    vector<ll> t(n), x(n), y(n), z;
    for (int i=0; i<n; i++){
        cin >> t[i] >> x[i] >> y[i];
        z.push_back(x[i]);
        if (t[i] == 1) z.push_back(y[i]);
    }

    auto comp = compress(z);
    for (int i=0; i<n; i++){
        x[i] = comp[x[i]];
        if (t[i] == 1) y[i] = comp[y[i]];
    }

    BIT<ll> bit(comp.size()+1);
    for (int i=0; i<n; i++){
        if (t[i] == 0) bit.add(x[i], y[i]);
        else{
            ans += bit.sum(x[i], y[i]);
        }
    }

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