ABC 372
Table of Contents
D - Buildings
解説 AC.
void solve() {
ll n;
cin >> n;
vll h(n);
rep(i, n) cin >> h[i];
vll ans(n);
vll st;
for (int i = n - 1; i >= 0; i--) {
ans[i] = st.size();
while (st.size() != 0 && st.back() < h[i]) {
st.pop_back();
}
st.push_back(h[i]);
}
print(ans);
}
E - K-th Largest Connected Components
https://atcoder.jp/contests/abc372/tasks/abc372_e
解説 AC
void solve() {
ll N, Q;
cin >> N >> Q;
dsu uf(N);
vvint nodes(N);
rep(i, N) {
nodes[i].push_back(i);
}
rep(i, Q) {
int q, a, b;
cin >> q >> a >> b;
if (q == 1) {
int u = a - 1, v = b - 1;
if (uf.same(u, v))
continue;
int ou = uf.leader(u);
int ov = uf.leader(v);
int newl = uf.merge(u, v);
vint tmp = nodes[ou];
for (auto x : nodes[ov])
tmp.push_back(x);
sort(rall(tmp));
vint newv;
for (int i = 0; i < 10 && i < tmp.size(); i++) {
newv.push_back(tmp[i]);
}
nodes[newl] = newv;
} else {
int v = a - 1, k = b;
int l = uf.leader(v);
if (nodes[l].size() >= k) {
cout << nodes[l][k - 1] + 1 << endl;
} else {
cout << -1 << endl;
}
}
}
}