結果

問題 No.789 範囲の合計
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-04 17:09:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,976 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 173,200 KB
実行使用メモリ 5,368 KB
最終ジャッジ日時 2023-09-05 06:25:15
合計ジャッジ時間 4,584 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
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;
    for (int i = 0; i < n; ++i) if (q[i] == 0) xs.push_back(a[i]);
    xs.push_back(-1);
    xs.push_back(1e9+1);
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    int N = xs.size();
    if (N == 2){
        cout << 0 << endl;
        return 0;
    }
    SegTreeSum<int> seg(N);
    int ans = 0;
    for (int i = 0; i < n; ++i){
        if (q[i] == 0){
            int x = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
            seg.add(x, b[i]);
        }else{
            int L = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
            int R = upper_bound(xs.begin(), xs.end(), b[i]) - xs.begin();
            ans += seg.query(L, R);
        }
    }
    cout << ans << endl;
    return 0;
}

0