結果

問題 No.789 範囲の合計
ユーザー srjywrdnprkt
提出日時 2024-02-09 18:09:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 232 ms / 1,000 ms
コード長 1,645 bytes
コンパイル時間 2,794 ms
コンパイル使用メモリ 203,676 KB
最終ジャッジ日時 2025-02-19 03:02:54
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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