QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#641797#7993. 哈密顿FlandreRE 0ms3540kbC++172.5kb2024-10-14 23:54:302024-10-14 23:54:30

Judging History

你现在查看的是最新测评结果

  • [2024-10-14 23:54:30]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3540kb
  • [2024-10-14 23:54:30]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = int64_t;
constexpr int MAXN = 2e5 + 19;

struct BIT {
    int n;
    int tr[MAXN];
    void update(int x, int k) {
        for (; x <= n; x += x & -x) {
            tr[x] += 1;
        }
    }
    int query(int rk) {
        int now = 0, tot = 0;
        for (int i = 20; i >= 0; --i) {
            if (now + (1 << i) <= n && tot + tr[now + (1 << i)] < rk) {
                now += 1 << i;
                tot += tr[now];
            }
        }
        return now + 1;
    }
} mt[2];

struct DSU {
    std::vector<int> fa;
    explicit DSU(int n) : fa(n) {
        std::iota(fa.begin(), fa.end(), 0);
    }
    int find(int x) {
        return fa[x] == x ? x : fa[x] = find(fa[x]);
    }
    void merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) {
            fa[x] = y;
        }
    }
};

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

    int n;
    std::cin >> n;

    std::vector<std::pair<int, int>> p(n);
    std::vector<int> a;
    for (int i = 0; i < n; ++i) {
        auto &[x, y] = p[i];
        std::cin >> x >> y;
        a.push_back(x);
        a.push_back(y);
    }

    std::sort(a.begin(), a.end());
    std::vector<uint8_t> type(n * 2 + 1);
    DSU dsu(n * 2 + 1);
    for (auto [x, y] : p) {
        x = std::lower_bound(a.begin(), a.end(), x) - a.begin() + 1;
        y = std::lower_bound(a.begin(), a.end(), y) - a.begin() + 1;
        type[x] = 1;
        type[y] = 0;
        dsu.merge(x, y);
    }

    std::vector<uint8_t> used(n * 2 + 1);
    mt[0].n = n * 2;
    mt[1].n = n * 2;
    for (int i = 1; i <= n * 2; ++i) {
        mt[type[i]].update(i, 1);
    }

    i64 ans = 0;
    int paired = 0;
    for (int i = n * 2; i >= 1; --i) {
        if (used[i]) {
            continue;
        }
        ++paired;
        int x = mt[type[i] ^ 1].query(1);
        if (paired != n && dsu.find(x) == dsu.find(i)) {
            x = mt[type[i] ^ 1].query(2);
            assert(!used[x]);
        }
        used[i] = true;
        mt[type[i]].update(i, -1);
        used[x] = true;
        mt[type[x]].update(x, -1);
        dsu.merge(i, x);

        assert(type[i] != type[x]);

        ans += a[i - 1] - a[x - 1];
    }
    assert(paired == n);

    for (int i = 1; i <= n * 2; ++i) {
        if (dsu.find(i) != dsu.find(1)) {
            std::cout << i << '\n';
        }
    }

    std::cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3532kb

input:

3
1 10
8 2
4 5

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

2
732734236 669531729
368612323 916696032

output:

484881202

result:

ok single line: '484881202'

Test #3:

score: -100
Runtime Error

input:

96739
960257511 790393830
766983782 374475134
476482176 529858300
310917487 76906076
81191659 653180897
119832756 362638666
228783015 626981197
74837554 781854003
663345706 153150021
576244413 708232220
842559077 417230462
249522463 815253339
391663517 742521049
60507117 258429449
112143256 81408991...

output:


result: