結果

問題 No.743 Segments on a Polygon
ユーザー t98slidert98slider
提出日時 2023-04-26 22:30:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,226 bytes
コンパイル時間 4,045 ms
コンパイル使用メモリ 168,912 KB
実行使用メモリ 5,504 KB
最終ジャッジ日時 2023-08-10 05:50:53
合計ジャッジ時間 4,955 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
5,464 KB
testcase_01 AC 29 ms
5,428 KB
testcase_02 AC 29 ms
5,348 KB
testcase_03 AC 29 ms
5,348 KB
testcase_04 AC 29 ms
5,460 KB
testcase_05 AC 29 ms
5,416 KB
testcase_06 AC 28 ms
5,412 KB
testcase_07 AC 29 ms
5,344 KB
testcase_08 AC 29 ms
5,504 KB
testcase_09 AC 26 ms
5,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T> struct fenwick_tree {
    using U = T;

    public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

    private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

int main(){
	ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, l, r;
    cin >> n >> m;
    vector<int> tb(m, -1);
    for(int i = 0; i < n; i++){
        cin >> l >> r;
        if(l > r) swap(l, r);
        tb[l] = r;
        tb[r] = l;
    }
    ll ans = 0;
    fenwick_tree<ll> fw(m);
    for(int i = 0; i < m; i++){
        if(tb[i] == -1) continue;
        if(tb[i] < i){
            ans += fw.sum(tb[i], i);
            fw.add(tb[i], -1);
            fw.add(i, 1);
        }
    }
    cout << ans << '\n';
}
0