結果

問題 No.789 範囲の合計
ユーザー pianonekopianoneko
提出日時 2019-03-19 12:20:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 1,000 ms
コード長 2,564 bytes
コンパイル時間 1,982 ms
コンパイル使用メモリ 178,376 KB
実行使用メモリ 9,424 KB
最終ジャッジ日時 2023-10-11 21:03:31
合計ジャッジ時間 5,921 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 163 ms
9,200 KB
testcase_03 AC 86 ms
5,384 KB
testcase_04 AC 157 ms
8,960 KB
testcase_05 AC 139 ms
9,424 KB
testcase_06 AC 145 ms
9,132 KB
testcase_07 AC 71 ms
5,248 KB
testcase_08 AC 119 ms
6,768 KB
testcase_09 AC 112 ms
6,116 KB
testcase_10 AC 175 ms
9,200 KB
testcase_11 AC 146 ms
8,856 KB
testcase_12 AC 147 ms
9,012 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

template <typename Monoid>
struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;

    int size = 1;
    vector<Monoid> node;

    const F f;
    const Monoid e;

    SegmentTree(int n, const F f, const Monoid &e) : f(f), e(e) {
        while (size < n) size <<= 1;
        node.assign(2 * size, e);

        for (int i = size - 2; i >= 0; i--) {
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    SegmentTree(vector<Monoid> vec, const F f, const Monoid &e) : f(f), e(e) {
        int n = (int)vec.size();
        while (size < n) size <<= 1;
        node.assign(2 * size, e);

        for (int i = 0; i < n; i++) {
            node[i + size - 1] = vec[i];
        }

        for (int i = n - 2; i >= 0; i--) {
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    void set(int i, const Monoid &x) { node[i + size - 1] = x; }

    void update(int i, const Monoid &x) {
        i += (size - 1);

        node[i] = x;

        while (i > 0) {
            i = (i - 1) / 2;
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    Monoid query(int a, int b, int i = 0, int l = 0, int r = -1) {
        if (r < 0) r = size;

        if (r <= a || b <= l) return e;

        if (a <= l && r <= b) return node[i];

        Monoid xl = query(a, b, 2 * i + 1, l, (l + r) / 2);
        Monoid xr = query(a, b, 2 * i + 2, (r + l) / 2, r);

        return f(xl, xr);
    }
};

int main() {
    int N;
    cin >> N;
    vector<int> query(N), x(N), y(N), num;
    REP(i, N) {
        cin >> query[i] >> x[i] >> y[i];
        if (query[i] == 0) {
            num.push_back(x[i]);
        } else {
            num.push_back(x[i]);
            num.push_back(y[i]);
        }
    }
    sort(ALL(num));
    num.erase(unique(ALL(num)), num.end());
    long long ans = 0;
    SegmentTree<long long> seg(
        num.size(), [](long long a, long long b) { return a + b; }, 0);
    REP(i, N) {
        if (query[i] == 0) {
            int index = lower_bound(ALL(num), x[i]) - num.begin();
            seg.update(index, y[i] + seg.query(index, index + 1));
        } else {
            int l = lower_bound(ALL(num), x[i]) - num.begin();
            int r = lower_bound(ALL(num), y[i]) - num.begin();
            ans += seg.query(l, r + 1);
        }
    }
    cout << ans << endl;
}
0