結果

問題 No.743 Segments on a Polygon
ユーザー たこしたこし
提出日時 2018-10-05 22:00:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,619 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 205,652 KB
実行使用メモリ 10,808 KB
最終ジャッジ日時 2023-08-09 02:35:38
合計ジャッジ時間 4,045 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
9,112 KB
testcase_01 AC 107 ms
10,808 KB
testcase_02 AC 108 ms
9,828 KB
testcase_03 AC 109 ms
8,908 KB
testcase_04 AC 106 ms
9,604 KB
testcase_05 AC 108 ms
10,032 KB
testcase_06 AC 109 ms
9,424 KB
testcase_07 AC 107 ms
9,228 KB
testcase_08 AC 108 ms
9,160 KB
testcase_09 AC 92 ms
8,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <cstdio>
#include <string>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

const int MAX_N = 100005;
const int MAX_M = 200005;

int N, M;

class Range {
public:
    int a, b;
    bool operator < (const Range& r) const {
        return a < r.a;
    }
}rangeList[MAX_N];

class Node {
public:
    Node(LL v = unitValue) : value(v) {

    }

    Node operator + (const Node& monoid) const {
        return Node(this->getValue() + monoid.getValue());
    }

    static Node unit() {
        return Node(unitValue);
    }

    void setValue(LL l) {
        this->value = l;
    }

    LL getValue() const {
        return this->value;
    }

private:
    LL value;
    static const LL unitValue = 0;
};

template <class T>
class SegmentTree {
public:
    SegmentTree(int size) {
        init(size);
    }

    void init(int size) {
        vec.clear();
        k = 1;
        while(k < size) {
            k *= 2;
        }
        for(int i = 0; i < 2*k; i++) {
            vec.push_back(T::unit());
        }
    }

    void update(int i, T x) { //iをxに変更する
        i += k-1;
        vec[i] = x;
        while(i > 0) {
            i = (i-1)/2;
            vec[i] = vec[i*2+1] + vec[i*2+2];
        }
    }

    T find(int l, int r) {
        return _find(l, r, 0, 0, k);
    }

private:
    vector<T> vec;
    int k;

    T _find(int l, int r, int k, int x, int y) {
        if (r <= x || y <= l) { //重なってない場合
            return T::unit();
        } else if (l <= x && y <= r) { //完全にかぶってる場合
            return vec[k];
        } else { //中途半端にかぶってる場合
            int mid = (x+y)/2;
            T lNode = _find(l, r, 2*k+1, x, mid);
            T rNode = _find(l, r, 2*k+2, mid, y);
            return lNode + rNode;
        }
    }
};

signed main()
{
    cin >> N >> M;
    for(int i = 0; i < N; i++) {
        cin >> rangeList[i].a >> rangeList[i].b;
        if(rangeList[i].a > rangeList[i].b) {
            swap(rangeList[i].a, rangeList[i].b);
        }
    }

    sort(rangeList, rangeList + N);

    SegmentTree<Node> segmentTree(MAX_M);
    int ans = 0;
    for(int i = 0; i < N; i++) {
        ans += segmentTree.find(rangeList[i].a, rangeList[i].b+1).getValue();
        segmentTree.update(rangeList[i].b, 1);
    }

    cout << ans << endl;

    return 0;
}
0