結果

問題 No.789 範囲の合計
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-04 17:26:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 1,000 ms
コード長 1,960 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 173,876 KB
実行使用メモリ 5,332 KB
最終ジャッジ日時 2023-09-05 06:48:27
合計ジャッジ時間 4,322 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 58 ms
4,868 KB
testcase_03 AC 37 ms
4,576 KB
testcase_04 AC 55 ms
5,332 KB
testcase_05 AC 43 ms
4,868 KB
testcase_06 AC 47 ms
4,928 KB
testcase_07 AC 30 ms
4,908 KB
testcase_08 AC 44 ms
5,324 KB
testcase_09 AC 41 ms
5,212 KB
testcase_10 AC 59 ms
4,944 KB
testcase_11 AC 39 ms
5,208 KB
testcase_12 AC 45 ms
5,192 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// SegmentTree on range sum, single value modification.
template<typename T>
class SegTreeSum{
    size_t n;
    vector<T> data;
public:
    SegTreeSum(size_t _n): n(_n), data(2 * _n, 0) {}
    SegTreeSum(const vector<T> &src): n(src.size()), data(2 * n, 0) {init(src);}
    void init(const vector<T> &src){
        for (size_t i = 0; i != n; ++i) data[n + i] = src[i];
        for (size_t i = n - 1; i != 0; --i) data[i] = data[i*2] + data[i*2+1];
    }
    void modify(size_t i, T v){   // set value at index i to v.
        for (data[i += n] = v; i > 1; i >>= 1) data[i>>1] = data[i] + data[i^1];
    }
    void add(size_t i, T v){
        modify(i, v + data[i + n]);
    }
    T query(size_t L, size_t R){  // sum on interval [L, R)
        T ret = 0;
        for (L += n, R += n; L < R; L >>= 1, R >>= 1){
            if (L & 1) ret += data[L++];
            if (R & 1) ret += data[--R];
        }
        return ret;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> q(n, 0);
    vector<int> a(n, 0);
    vector<int> b(n, 0);
    for (int i = 0; i < n; ++i) cin >> q[i] >> a[i] >> b[i];
    vector<int> xs = {-1};
    for (int i = 0; i < n; ++i) if (q[i] == 0) xs.push_back(a[i]);
    xs.push_back(1e9+1);
    sort(xs.begin(), xs.end());
    auto xsend = unique(xs.begin(), xs.end());
    int N = xsend - xs.begin();
    if (N == 2){
        cout << 0 << endl;
        return 0;
    }
    SegTreeSum<int> seg(N);
    long long ans = 0;
    for (int i = 0; i < n; ++i){
        if (q[i] == 0){
            int x = lower_bound(xs.begin(), xsend, a[i]) - xs.begin();
            seg.add(x, b[i]);
        }else{
            int L = lower_bound(xs.begin(), xsend, a[i]) - xs.begin();
            int R = upper_bound(xs.begin(), xsend, b[i]) - xs.begin();
            ans += seg.query(L, R);
        }
    }
    cout << ans << endl;
    return 0;
}

0