QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#641812 | #7993. 哈密顿 | Flandre | WA | 60ms | 7328kb | C++17 | 2.4kb | 2024-10-15 00:07:42 | 2024-10-15 00:07:44 |
Judging History
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] += k;
}
}
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);
std::cout << ans << '\n';
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3592kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3484kb
input:
2 732734236 669531729 368612323 916696032
output:
484881202
result:
ok single line: '484881202'
Test #3:
score: 0
Accepted
time: 60ms
memory: 7328kb
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:
48459768018811
result:
ok single line: '48459768018811'
Test #4:
score: -100
Wrong Answer
time: 59ms
memory: 7308kb
input:
96785 270111821 467151412 338110212 587493839 880232679 190826309 757074784 388758646 266705411 248083736 932249293 549449409 813345622 128324633 997551674 739367275 118482916 584132988 530659142 718566145 843527299 299368287 351399254 123105310 94363217 996225281 372809651 824469842 227112349 94127...
output:
48406205343320
result:
wrong answer 1st lines differ - expected: '48406205356958', found: '48406205343320'