結果

問題 No.743 Segments on a Polygon
ユーザー xuzijian629xuzijian629
提出日時 2018-10-10 16:46:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,001 bytes
コンパイル時間 1,783 ms
コンパイル使用メモリ 166,632 KB
実行使用メモリ 5,720 KB
最終ジャッジ日時 2024-04-26 21:21:49
合計ジャッジ時間 3,259 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
5,708 KB
testcase_01 AC 75 ms
5,576 KB
testcase_02 AC 76 ms
5,704 KB
testcase_03 AC 74 ms
5,708 KB
testcase_04 AC 75 ms
5,708 KB
testcase_05 AC 76 ms
5,584 KB
testcase_06 AC 75 ms
5,580 KB
testcase_07 AC 75 ms
5,708 KB
testcase_08 AC 75 ms
5,720 KB
testcase_09 AC 68 ms
5,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
using ii = pair<int, int>;

class BIT {
    int n;
    vi data;

    i64 sum(int i) {
        i64 s = 0;
        while (i > 0) {
            s += data[i];
            i -= i & -i;
        }
        return s;
    }

public:
    BIT(int n) : n(n) {
        data = vi(n + 1);
    }

    void add(int i, i64 x) {
        i++;
        while (i <= n) {
            data[i] += x;
            i += i & -i;
        }
    }

    // [l, r)
    i64 sum(int l, int r) {
        return sum(r) - sum(l);
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<ii> vs;
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        vs.push_back(ii(min(a, b), max(a, b)));
    }
    sort(vs.begin(), vs.end());

    BIT bit(m);
    i64 cnt = 0;
    for (ii& p: vs) {
        cnt += bit.sum(p.first, p.second);
        bit.add(p.second, 1);
    }
    cout << cnt << endl;
}
0