結果

問題 No.789 範囲の合計
ユーザー merom686merom686
提出日時 2019-02-08 22:43:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 1,000 ms
コード長 1,422 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 83,588 KB
実行使用メモリ 6,252 KB
最終ジャッジ日時 2023-09-14 03:52:15
合計ジャッジ時間 2,345 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 48 ms
6,140 KB
testcase_03 AC 45 ms
5,376 KB
testcase_04 AC 45 ms
5,892 KB
testcase_05 AC 42 ms
5,884 KB
testcase_06 AC 46 ms
5,940 KB
testcase_07 AC 40 ms
5,088 KB
testcase_08 AC 41 ms
5,412 KB
testcase_09 AC 39 ms
5,440 KB
testcase_10 AC 51 ms
6,252 KB
testcase_11 AC 45 ms
5,888 KB
testcase_12 AC 46 ms
5,944 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct P {
    int t, x, y;
};

struct BIT {
    BIT(int n) : b(n + 1), n(n) {}
    void add(int i, int v) {
        for (int k = i + 1; k <= n; k += k & -k) b[k] += v;
    }
    int sum(int k) {
        int s = 0;
        for (; k > 0; k -= k & -k) s += b[k];
        return s;
    }
    vector<int> b;
    int n;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<P> p(n);
    int m = n;
    for (int i = 0; i < n; i++) {
        cin >> p[i].t >> p[i].x >> p[i].y;
        if (p[i].t == 1) p[i].y++, m++;
    }

    vector<pair<int, int>> t(m);
    int j = 0;
    for (int i = 0; i < n; i++) {
        t[j++] = { p[i].x, i * 2 };
        if (p[i].t == 1) t[j++] = { p[i].y, i * 2 + 1 };
    }
    sort(t.begin(), t.end());
    int k = 0;
    for (int i = 0; i < m; i++) {
        if (i > 0 && t[i].first != t[i - 1].first) k++;
        P& q = p[t[i].second / 2];
        if (t[i].second % 2 == 0) q.x = k; else q.y = k;
    }

    k++;
    BIT bt(k);
    ll ans = 0;

    for (int i = 0; i < n; i++) {
        if (p[i].t == 0) {
            bt.add(p[i].x, p[i].y);
        } else {
            ans += bt.sum(p[i].y) - bt.sum(p[i].x);
        }
    }

    cout << ans << endl;

    return 0;
}
0