Submission #57514


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "bits/stdc++.h"
using namespace std;
struct UnionFind { // {{{
vector<int> u;
explicit UnionFind(int n): u(n+5, -1) {}
int size(int x) { return -u[root(x)]; }
int root(int x) { return (u[x] < 0)? x : (u[x] = root(u[x])); }
bool same(int x, int y) { return (root(x) == root(y)); }
bool unite(int x, int y){ return (x=root(x)) == (y=root(y))? false : (u[x]+=u[y], u[y]=x, true); }
}; // }}}
inline bool inRange(int value, int closedMin, int closedMax) {
return closedMin <= value && value <= closedMax;
}
int main()
{
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int N, M, K;
cin >> N >> M >> K;
// assert(inRange(N, 1, 10'000));
// assert(inRange(M, 1, 10'000));
// assert(inRange(K, 1, N));
UnionFind uf(N);
vector<int> a(N);
unordered_map<int,int> id;
vector<priority_queue<int>> kthFinder(N);
for (int i = 0; i < N; ++i) {
cin >> a[i];
// assert(inRange(a[i], 1, 1'000'000'000));
// assert(id.count(a[i]) == 0);
id[a[i]] = i;
kthFinder[i].push(a[i]);
}
const auto merge = [&](int x, int y)
{
x = uf.root(x);
y = uf.root(y);
int parentID, childID;
if (kthFinder[x].size() > kthFinder[y].size()) {
parentID = x;
childID = y;
} else {
parentID = y;
childID = x;
}
uf.unite(parentID, childID);
auto &parent = kthFinder[parentID];
auto &child = kthFinder[childID];
while (not child.empty()) {
const int v = child.top(); child.pop();
if (parent.size() < K) {
parent.push(v);
} else if (v < parent.top()) {
parent.pop();
parent.push(v);
}
}
return;
};
const auto findKth = [&](int x) -> int
{
x = uf.root(x);
if (kthFinder[x].size() < K)
return -1;
else
return kthFinder[x].top();
};
int findKthQueryCount = 0;
for (int _ = 0; _ < M; ++_) {
int com, x, y;
cin >> com;
if (com == 1) {
cin >> x >> y;
// assert(id.count(x) == 1);
// assert(id.count(y) == 1);
const int id_x = id[x];
const int id_y = id[y];
if (not uf.same(id_x, id_y)) {
merge(id_x, id_y);
}
} else {
cin >> x;
// assert(id.count(x) == 1);
cout << findKth(id[x]) << "\n";
++findKthQueryCount;
}
}
// assert(findKthQueryCount >= 1);
return 0;
}

ステータス

項目 データ
問題 1232 - Corner Kth ⇔ Corner Case
ユーザー名 Arumakan_ei1727
投稿日時 2019-12-17 23:51:36
言語 C++14
状態 Accepted
得点 100
ソースコード長 2800 Byte
最大実行時間 94 ms
最大メモリ使用量 13152 KB

セット

セット 得点 Cases
1 ALL 100 / 100 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
corner_in1.txt AC 58 ms 11644 KB
1
corner_in2.txt AC 56 ms 11432 KB
1
corner_in3.txt AC 59 ms 11456 KB
1
corner_in4.txt AC 59 ms 11496 KB
1
corner_in5.txt AC 55 ms 11544 KB
1
large_in1.txt AC 92 ms 12224 KB
1
large_in2.txt AC 81 ms 12244 KB
1
large_in3.txt AC 87 ms 12520 KB
1
large_in4.txt AC 89 ms 12812 KB
1
large_in5.txt AC 79 ms 12908 KB
1
large_in6.txt AC 94 ms 13152 KB
1
sample_in1.txt AC 16 ms 1824 KB
1
sample_in2.txt AC 22 ms 1640 KB
1
small_in1.txt AC 23 ms 1856 KB
1
small_in2.txt AC 21 ms 2068 KB
1
small_in3.txt AC 19 ms 1984 KB
1
small_in4.txt AC 20 ms 1776 KB
1
small_in5.txt AC 17 ms 1824 KB
1
small_in6.txt AC 24 ms 1872 KB
1
small_in7.txt AC 18 ms 1920 KB
1
small_in8.txt AC 24 ms 1836 KB
1
small_in9.txt AC 20 ms 1752 KB
1
small_in10.txt AC 23 ms 1896 KB
1