#include <bits/stdc++.h>
#include "collapse.h"
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
const int S = 320;
vector<int> simulateCollapse(int n, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P) {
int Q = int(W.size()), C = int(X.size());
vector<int> ord(Q), res(Q);
vector<pair<int, int>> edges;
for (int i = 0; i < C; ++i) {
if (X[i] > Y[i]) {
swap(X[i], Y[i]);
}
edges.emplace_back(X[i], Y[i]);
}
sort(edges.begin(), edges.end());
edges.erase(unique(edges.begin(), edges.end()), edges.end());
int m = int(edges.size());
vector<int> idx(C);
for (int i = 0; i < C; ++i) {
idx[i] = lower_bound(edges.begin(), edges.end(), make_pair(X[i], Y[i])) - edges.begin();
}
vector<vector<int>> qry(C);
for (int i = 0; i < Q; ++i) {
qry[W[i]].emplace_back(i);
}
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) {
return W[i] < W[j];
});
vector<int> lab(n, -1);
vector<pair<int, int>> his;
int cc = n;
auto get = [&](auto get, int u) -> int {
return lab[u] < 0 ? u : get(get, lab[u]);
};
auto unite = [&](int u, int v, bool rb) {
u = get(get, u), v = get(get, v);
if (u ^ v) {
if (lab[u] > lab[v]) {
swap(u, v);
}
if (rb) {
his.emplace_back(v, lab[v]);
}
lab[u] += lab[v];
lab[v] = u;
cc--;
}
};
auto rollback = [&]() {
while (his.size()) {
auto [v, sz] = his.back();
his.pop_back();
int u = lab[v];
lab[u] -= sz;
lab[v] = sz;
cc++;
}
};
auto reset = [&]() {
cc = n;
fill(lab.begin(), lab.end(), -1);
his.clear();
};
vector<int> inc(m);
iota(inc.begin(), inc.end(), 0);
auto dec = inc;
sort(inc.begin(), inc.end(), [&](int i, int j) {
return edges[i].second < edges[j].second;
});
sort(dec.begin(), dec.end(), [&](int i, int j) {
return edges[i].first > edges[j].first;
});
vector<bool> cant(m, false), state(m, false);
vector<vector<int>> gL(Q), gR(Q);
int time = 0;
for (int s = 0; s < C; s += S) {
int l = s, r = min(C, s + S) - 1;
vector<int> cands;
for (int i = l; i <= r; ++i) {
cands.emplace_back(idx[i]);
}
sort(cands.begin(), cands.end());
cands.erase(unique(cands.begin(), cands.end()), cands.end());
for (int id : cands) {
cant[id] = true;
}
vector<int> v;
for (int i = l; i <= r; ++i) {
state[idx[i]] = 1 - state[idx[i]];
for (int id : qry[i]) {
v.emplace_back(id);
for (int u : cands) {
if (state[u]) {
if (edges[u].second <= P[id]) {
gL[id].emplace_back(u);
} else if (edges[u].first > P[id]) {
gR[id].emplace_back(u);
}
}
}
}
}
sort(v.begin(), v.end(), [&](int i, int j) {
return P[i] < P[j];
});
reset();
int j = 0;
for (int id : v) {
while (j < m && edges[inc[j]].second <= P[id]) {
int i = inc[j];
if (!cant[i] && state[i]) {
unite(edges[i].first, edges[i].second, false);
}
j++;
}
for (int k : gL[id]) {
unite(edges[k].first, edges[k].second, true);
}
res[id] += cc - n + P[id] + 1;
rollback();
}
reverse(v.begin(), v.end());
reset();
j = 0;
for (int id : v) {
while (j < m && edges[dec[j]].first > P[id]) {
int i = dec[j];
if (!cant[i] && state[i]) {
unite(edges[i].first, edges[i].second, false);
}
j++;
}
for (int k : gR[id]) {
unite(edges[k].first, edges[k].second, true);
}
res[id] += cc - P[id] - 1;
rollback();
}
for (int id : cands) {
cant[id] = false;
}
}
return res;
}
#ifdef ngu
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
int n, C, Q;
cin >> n >> C >> Q;
vector<int> T(C), X(C), Y(C);
for (int i = 0; i < C; ++i) {
cin >> T[i] >> X[i] >> Y[i];
}
vector<int> W(Q), P(Q);
for (int i = 0; i < Q; ++i) {
cin >> W[i] >> P[i];
}
auto res = simulateCollapse(n, T, X, Y, W, P);
for (int i = 0; i < Q; ++i) {
cout << res[i] << " ";
}
}
#endif ngu
Compilation message
collapse.cpp:180:8: warning: extra tokens at end of #endif directive [-Wendif-labels]
180 | #endif ngu
| ^~~
collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:86:9: warning: unused variable 'time' [-Wunused-variable]
86 | int time = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
4 ms |
1116 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
7 ms |
1372 KB |
Output is correct |
6 |
Correct |
18 ms |
4296 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
1084 KB |
Output is correct |
9 |
Correct |
8 ms |
1628 KB |
Output is correct |
10 |
Correct |
18 ms |
3420 KB |
Output is correct |
11 |
Correct |
24 ms |
4444 KB |
Output is correct |
12 |
Correct |
21 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
8916 KB |
Output is correct |
2 |
Correct |
31 ms |
10452 KB |
Output is correct |
3 |
Correct |
137 ms |
26216 KB |
Output is correct |
4 |
Correct |
79 ms |
19316 KB |
Output is correct |
5 |
Correct |
259 ms |
43600 KB |
Output is correct |
6 |
Correct |
206 ms |
56660 KB |
Output is correct |
7 |
Correct |
445 ms |
68700 KB |
Output is correct |
8 |
Correct |
257 ms |
47176 KB |
Output is correct |
9 |
Correct |
33 ms |
12500 KB |
Output is correct |
10 |
Correct |
47 ms |
15568 KB |
Output is correct |
11 |
Correct |
238 ms |
50276 KB |
Output is correct |
12 |
Correct |
268 ms |
50844 KB |
Output is correct |
13 |
Correct |
427 ms |
66816 KB |
Output is correct |
14 |
Correct |
467 ms |
68780 KB |
Output is correct |
15 |
Correct |
424 ms |
65344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
8908 KB |
Output is correct |
2 |
Correct |
46 ms |
11760 KB |
Output is correct |
3 |
Correct |
58 ms |
14540 KB |
Output is correct |
4 |
Correct |
94 ms |
19660 KB |
Output is correct |
5 |
Correct |
253 ms |
61224 KB |
Output is correct |
6 |
Correct |
287 ms |
70320 KB |
Output is correct |
7 |
Correct |
614 ms |
78444 KB |
Output is correct |
8 |
Correct |
1016 ms |
80932 KB |
Output is correct |
9 |
Correct |
40 ms |
11980 KB |
Output is correct |
10 |
Correct |
287 ms |
68268 KB |
Output is correct |
11 |
Correct |
1076 ms |
83572 KB |
Output is correct |
12 |
Correct |
1359 ms |
81372 KB |
Output is correct |
13 |
Correct |
1130 ms |
82820 KB |
Output is correct |
14 |
Correct |
1353 ms |
81548 KB |
Output is correct |
15 |
Correct |
1084 ms |
80004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
4 ms |
1116 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
7 ms |
1372 KB |
Output is correct |
6 |
Correct |
18 ms |
4296 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
1084 KB |
Output is correct |
9 |
Correct |
8 ms |
1628 KB |
Output is correct |
10 |
Correct |
18 ms |
3420 KB |
Output is correct |
11 |
Correct |
24 ms |
4444 KB |
Output is correct |
12 |
Correct |
21 ms |
4444 KB |
Output is correct |
13 |
Correct |
25 ms |
8916 KB |
Output is correct |
14 |
Correct |
31 ms |
10452 KB |
Output is correct |
15 |
Correct |
137 ms |
26216 KB |
Output is correct |
16 |
Correct |
79 ms |
19316 KB |
Output is correct |
17 |
Correct |
259 ms |
43600 KB |
Output is correct |
18 |
Correct |
206 ms |
56660 KB |
Output is correct |
19 |
Correct |
445 ms |
68700 KB |
Output is correct |
20 |
Correct |
257 ms |
47176 KB |
Output is correct |
21 |
Correct |
33 ms |
12500 KB |
Output is correct |
22 |
Correct |
47 ms |
15568 KB |
Output is correct |
23 |
Correct |
238 ms |
50276 KB |
Output is correct |
24 |
Correct |
268 ms |
50844 KB |
Output is correct |
25 |
Correct |
427 ms |
66816 KB |
Output is correct |
26 |
Correct |
467 ms |
68780 KB |
Output is correct |
27 |
Correct |
424 ms |
65344 KB |
Output is correct |
28 |
Correct |
26 ms |
8908 KB |
Output is correct |
29 |
Correct |
46 ms |
11760 KB |
Output is correct |
30 |
Correct |
58 ms |
14540 KB |
Output is correct |
31 |
Correct |
94 ms |
19660 KB |
Output is correct |
32 |
Correct |
253 ms |
61224 KB |
Output is correct |
33 |
Correct |
287 ms |
70320 KB |
Output is correct |
34 |
Correct |
614 ms |
78444 KB |
Output is correct |
35 |
Correct |
1016 ms |
80932 KB |
Output is correct |
36 |
Correct |
40 ms |
11980 KB |
Output is correct |
37 |
Correct |
287 ms |
68268 KB |
Output is correct |
38 |
Correct |
1076 ms |
83572 KB |
Output is correct |
39 |
Correct |
1359 ms |
81372 KB |
Output is correct |
40 |
Correct |
1130 ms |
82820 KB |
Output is correct |
41 |
Correct |
1353 ms |
81548 KB |
Output is correct |
42 |
Correct |
1084 ms |
80004 KB |
Output is correct |
43 |
Correct |
326 ms |
39580 KB |
Output is correct |
44 |
Correct |
805 ms |
80772 KB |
Output is correct |
45 |
Correct |
365 ms |
50132 KB |
Output is correct |
46 |
Correct |
993 ms |
81140 KB |
Output is correct |
47 |
Correct |
50 ms |
12476 KB |
Output is correct |
48 |
Correct |
58 ms |
13268 KB |
Output is correct |
49 |
Correct |
260 ms |
58352 KB |
Output is correct |
50 |
Correct |
347 ms |
70400 KB |
Output is correct |
51 |
Correct |
420 ms |
55736 KB |
Output is correct |
52 |
Correct |
627 ms |
80424 KB |
Output is correct |
53 |
Correct |
606 ms |
82512 KB |
Output is correct |
54 |
Correct |
823 ms |
81416 KB |
Output is correct |
55 |
Correct |
782 ms |
81728 KB |
Output is correct |
56 |
Correct |
924 ms |
83540 KB |
Output is correct |
57 |
Correct |
1040 ms |
82384 KB |
Output is correct |
58 |
Correct |
1184 ms |
81296 KB |
Output is correct |
59 |
Correct |
1135 ms |
81128 KB |
Output is correct |
60 |
Correct |
1359 ms |
81784 KB |
Output is correct |