이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "collapse.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> simulateCollapse(int n, vector<int> t, vector<int> x, vector<int> y, vector<int> w, vector<int> p) {
int m = (int) t.size();
int q = (int) w.size();
for (int i = 0; i < m; i++) {
if (x[i] > y[i]) {
swap(x[i], y[i]);
}
}
vector<int> to(m);
map<pair<int, int>, int> mp;
for (int i = 0; i < m; i++) {
if (t[i] == 0) {
to[i] = m - 1;
mp[make_pair(x[i], y[i])] = i;
} else {
to[mp[make_pair(x[i], y[i])]] = i - 1;
}
}
vector<vector<int>> qs(m);
for (int i = 0; i < q; i++) {
qs[w[i]].push_back(i);
}
vector<int> par(n);
vector<int> sz(n, 1);
vector<tuple<int, int, int>> stk;
int comps = 0;
iota(par.begin(), par.end(), 0);
function<int(int)> root = [&](int x) {
if (par[x] == x) {
return x;
}
return root(par[x]);
};
auto Unite = [&](int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
stk.emplace_back(-1, -1, -1);
return;
}
if (sz[x] < sz[y]) {
swap(x, y);
}
stk.emplace_back(x, y, sz[y]);
par[y] = x;
sz[x] += sz[y];
comps += 1;
};
auto Rollback = [&](int T) {
while ((int) stk.size() > T) {
auto it = stk.back();
stk.pop_back();
int x = get<0>(it);
int y = get<1>(it);
int s = get<2>(it);
if (x == -1) {
continue;
}
par[y] = y;
sz[y] = s;
sz[x] -= s;
comps -= 1;
}
};
const int BLOCK = 6;
vector<int> ans(q);
for (int l = 0; l < m; l += BLOCK) {
int r = min(m - 1, l + BLOCK - 1);
vector<int> full;
vector<int> half;
for (int i = 0; i <= r; i++) {
if (t[i] == 1 || to[i] < l) {
continue;
}
if (i <= l && to[i] >= r) {
full.push_back(i);
} else {
half.push_back(i);
}
}
vector<int> qq;
for (int i = l; i <= r; i++) {
for (int j : qs[i]) {
qq.push_back(j);
}
}
{
sort(qq.begin(), qq.end(), [&](int i, int j) {
return p[i] < p[j];
});
sort(full.begin(), full.end(), [&](int i, int j) {
return y[i] < y[j];
});
int ptr = 0;
for (int i : qq) {
while (ptr < (int) full.size() && y[full[ptr]] <= p[i]) {
Unite(x[full[ptr]], y[full[ptr]]);
ptr += 1;
}
int T = (int) stk.size();
for (int j : half) {
if (j <= w[i] && to[j] >= w[i] && y[j] <= p[i]) {
Unite(x[j], y[j]);
}
}
ans[i] += (p[i] + 1) - comps;
Rollback(T);
}
Rollback(0);
}
{
sort(qq.begin(), qq.end(), [&](int i, int j) {
return p[i] > p[j];
});
sort(full.begin(), full.end(), [&](int i, int j) {
return x[i] > x[j];
});
int ptr = 0;
for (int i : qq) {
while (ptr < (int) full.size() && x[full[ptr]] > p[i]) {
Unite(x[full[ptr]], y[full[ptr]]);
ptr += 1;
}
int T = (int) stk.size();
for (int j : half) {
if (j <= w[i] && to[j] >= w[i] && x[j] > p[i]) {
Unite(x[j], y[j]);
}
}
ans[i] += (n - p[i] - 1) - comps;
Rollback(T);
}
Rollback(0);
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |