結果

問題 No.789 範囲の合計
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-03-12 13:14:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 1,000 ms
コード長 1,181 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 96,672 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-04-22 00:09:40
合計ジャッジ時間 2,129 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 57 ms
5,376 KB
testcase_03 AC 36 ms
5,376 KB
testcase_04 AC 56 ms
5,376 KB
testcase_05 AC 42 ms
5,504 KB
testcase_06 AC 45 ms
5,376 KB
testcase_07 AC 28 ms
5,376 KB
testcase_08 AC 43 ms
6,016 KB
testcase_09 AC 40 ms
5,376 KB
testcase_10 AC 57 ms
5,376 KB
testcase_11 AC 40 ms
5,632 KB
testcase_12 AC 40 ms
5,504 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

#include <atcoder/segtree>

__attribute__((constructor))
void fast_io() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

using S = int;
S op(S a, S b) { return a + b; }
S e() { return 0; }

int main() {
    size_t q;
    std::cin >> q;

    std::vector<int> type(q), a(q), b(q), xs;
    for (size_t i = 0; i < q; ++i) {
        std::cin >> type[i] >> a[i] >> b[i];
        if (type[i] == 0) xs.push_back(a[i]);
    }

    std::sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    atcoder::segtree<S, op, e> seg(xs.size());

    long long ans = 0;
    for (size_t i = 0; i < q; ++i) {
        if (type[i] == 0) {
            size_t j = std::distance(xs.begin(), std::lower_bound(xs.begin(), xs.end(), a[i]));
            seg.set(j, seg.get(j) + b[i]);
        } else {
            size_t l = std::distance(xs.begin(), std::lower_bound(xs.begin(), xs.end(), a[i]));
            size_t r = std::distance(xs.begin(), std::upper_bound(xs.begin(), xs.end(), b[i]));
            ans += seg.prod(l, r);
        }
    }
    std::cout << ans << '\n';
}
0