#include <bits/stdc++.h>
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define all(V) (V).begin(), (V).end()
using namespace std;
typedef vector<int> vim;
typedef pair<int, int> pii;
typedef shared_ptr<vector<pii> > pvii;
#include "collapse.h"
const int B = 300;
struct UF {
vim par, ch; bool im;
UF(int n, bool i) : par(n, -1), im(i) {}
int get(int x) { return par[x]!=-1?(par[x]=get(par[x])):x; }
bool Union(int x, int y) {
x=get(x), y=get(y);
if (x==y) return false;
if (im) ch.eb(y);
par[y]=x;
return true;
}
void resolve() { for (auto &i:ch) par[i]=-1; ch.clear(); }
};
vector<pii> solve(int N, vector<pii> Fix, vector<pair<pii, pvii> > S) {
UF U1(N, false), U2(N, true); vector<pii> R;
sort(all(Fix), [](pii &p1, pii &p2){ return p1.se<p2.se; });
sort(all(S), [](pair<pii, pvii> &p1, pair<pii, pvii> &p2){ return p1.fi.fi<p2.fi.fi; });
int tp=0, cnt=0;
for (auto &i:S) {
for (; tp<Fix.size()&&Fix[tp].se<=i.fi.fi; tp++) cnt+=U1.Union(Fix[tp].fi, Fix[tp].se);
int im=cnt;
for (auto &j:*i.se) if (j.se<=i.fi.fi) im+=U2.Union(U1.get(j.fi), U1.get(j.se));
R.eb(i.fi.se, i.fi.fi+1-im);
U2.resolve();
}
return R;
}
vim simulateCollapse(int N, vim T, vim X, vim Y, vim W, vim P) {
int D=T.size(), Q=W.size();
vim A(Q, 0); set<pii> Fix;
vector<pair<pii, int> > qu;
for (int i=0; i<Q; i++) qu.eb(pii(W[i], P[i]), i); sort(all(qu));
for (int i=0; i<D; i++) if (X[i]>Y[i]) swap(X[i], Y[i]);
for (int s=0, tp=0; s<D; s+=B) {
int e=min(D, s+B);
set<pii> nFix;
for (int i=s; i<e; i++) if (Fix.find(pii(X[i], Y[i]))!=Fix.end())
Fix.erase(pii(X[i], Y[i])), nFix.em(X[i], Y[i]);
vector<pair<pii, pvii> > S1, S2;
for (int i=s; i<e; i++) {
if (T[i]==0) nFix.em(X[i], Y[i]);
if (T[i]==1) nFix.erase(pii(X[i], Y[i]));
pvii nf1(new vector<pii>), nf2(new vector<pii>);
for (auto &j:nFix) nf1->eb(j), nf2->eb(N-1-j.se, N-1-j.fi);
for (; tp<Q&&qu[tp].fi.fi==i; tp++)
S1.eb(pii(qu[tp].fi.se, qu[tp].se), nf1), S2.eb(pii(N-2-qu[tp].fi.se, qu[tp].se), nf2);
}
vector<pii> F1, F2;
for (auto &i:Fix) F1.eb(i), F2.eb(N-1-i.se, N-1-i.fi);
vector<pii> r1 = solve(N, F1, S1);
vector<pii> r2 = solve(N, F2, S2);
for (auto &i:r1) A[i.fi]+=i.se;
for (auto &i:r2) A[i.fi]+=i.se;
for (int i=s; i<e; i++) {
if (T[i]==0) Fix.em(X[i], Y[i]);
if (T[i]==1) Fix.erase(pii(X[i], Y[i]));
}
}
return A;
}
Compilation message
collapse.cpp: In function 'std::vector<std::pair<int, int> > solve(int, std::vector<std::pair<int, int> >, std::vector<std::pair<std::pair<int, int>, std::shared_ptr<std::vector<std::pair<int, int> > > > >)':
collapse.cpp:35:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (; tp<Fix.size()&&Fix[tp].se<=i.fi.fi; tp++) cnt+=U1.Union(Fix[tp].fi, Fix[tp].se);
| ~~^~~~~~~~~~~
collapse.cpp: In function 'vim simulateCollapse(int, vim, vim, vim, vim, vim)':
collapse.cpp:48:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
48 | for (int i=0; i<Q; i++) qu.eb(pii(W[i], P[i]), i); sort(all(qu));
| ^~~
collapse.cpp:48:53: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
48 | for (int i=0; i<Q; i++) qu.eb(pii(W[i], P[i]), i); sort(all(qu));
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
856 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
3 ms |
1112 KB |
Output is correct |
4 |
Correct |
3 ms |
1116 KB |
Output is correct |
5 |
Correct |
7 ms |
848 KB |
Output is correct |
6 |
Correct |
22 ms |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1116 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
8 ms |
1108 KB |
Output is correct |
10 |
Correct |
19 ms |
1620 KB |
Output is correct |
11 |
Correct |
30 ms |
1932 KB |
Output is correct |
12 |
Correct |
32 ms |
1872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
13508 KB |
Output is correct |
2 |
Correct |
42 ms |
13840 KB |
Output is correct |
3 |
Correct |
150 ms |
7624 KB |
Output is correct |
4 |
Correct |
49 ms |
13636 KB |
Output is correct |
5 |
Correct |
262 ms |
8548 KB |
Output is correct |
6 |
Correct |
114 ms |
6588 KB |
Output is correct |
7 |
Correct |
1814 ms |
18084 KB |
Output is correct |
8 |
Correct |
240 ms |
9264 KB |
Output is correct |
9 |
Correct |
37 ms |
15244 KB |
Output is correct |
10 |
Correct |
45 ms |
15452 KB |
Output is correct |
11 |
Correct |
81 ms |
9036 KB |
Output is correct |
12 |
Correct |
245 ms |
10240 KB |
Output is correct |
13 |
Correct |
1026 ms |
13896 KB |
Output is correct |
14 |
Correct |
2328 ms |
18636 KB |
Output is correct |
15 |
Correct |
1740 ms |
18356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
13764 KB |
Output is correct |
2 |
Correct |
44 ms |
14232 KB |
Output is correct |
3 |
Correct |
47 ms |
14232 KB |
Output is correct |
4 |
Correct |
59 ms |
13820 KB |
Output is correct |
5 |
Correct |
125 ms |
11136 KB |
Output is correct |
6 |
Correct |
127 ms |
6500 KB |
Output is correct |
7 |
Correct |
1232 ms |
14440 KB |
Output is correct |
8 |
Correct |
2615 ms |
17524 KB |
Output is correct |
9 |
Correct |
48 ms |
15124 KB |
Output is correct |
10 |
Correct |
132 ms |
9464 KB |
Output is correct |
11 |
Correct |
2430 ms |
17836 KB |
Output is correct |
12 |
Correct |
3079 ms |
18036 KB |
Output is correct |
13 |
Correct |
2789 ms |
18004 KB |
Output is correct |
14 |
Correct |
3068 ms |
17804 KB |
Output is correct |
15 |
Correct |
2486 ms |
17568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
856 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
3 ms |
1112 KB |
Output is correct |
4 |
Correct |
3 ms |
1116 KB |
Output is correct |
5 |
Correct |
7 ms |
848 KB |
Output is correct |
6 |
Correct |
22 ms |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1116 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
8 ms |
1108 KB |
Output is correct |
10 |
Correct |
19 ms |
1620 KB |
Output is correct |
11 |
Correct |
30 ms |
1932 KB |
Output is correct |
12 |
Correct |
32 ms |
1872 KB |
Output is correct |
13 |
Correct |
43 ms |
13508 KB |
Output is correct |
14 |
Correct |
42 ms |
13840 KB |
Output is correct |
15 |
Correct |
150 ms |
7624 KB |
Output is correct |
16 |
Correct |
49 ms |
13636 KB |
Output is correct |
17 |
Correct |
262 ms |
8548 KB |
Output is correct |
18 |
Correct |
114 ms |
6588 KB |
Output is correct |
19 |
Correct |
1814 ms |
18084 KB |
Output is correct |
20 |
Correct |
240 ms |
9264 KB |
Output is correct |
21 |
Correct |
37 ms |
15244 KB |
Output is correct |
22 |
Correct |
45 ms |
15452 KB |
Output is correct |
23 |
Correct |
81 ms |
9036 KB |
Output is correct |
24 |
Correct |
245 ms |
10240 KB |
Output is correct |
25 |
Correct |
1026 ms |
13896 KB |
Output is correct |
26 |
Correct |
2328 ms |
18636 KB |
Output is correct |
27 |
Correct |
1740 ms |
18356 KB |
Output is correct |
28 |
Correct |
36 ms |
13764 KB |
Output is correct |
29 |
Correct |
44 ms |
14232 KB |
Output is correct |
30 |
Correct |
47 ms |
14232 KB |
Output is correct |
31 |
Correct |
59 ms |
13820 KB |
Output is correct |
32 |
Correct |
125 ms |
11136 KB |
Output is correct |
33 |
Correct |
127 ms |
6500 KB |
Output is correct |
34 |
Correct |
1232 ms |
14440 KB |
Output is correct |
35 |
Correct |
2615 ms |
17524 KB |
Output is correct |
36 |
Correct |
48 ms |
15124 KB |
Output is correct |
37 |
Correct |
132 ms |
9464 KB |
Output is correct |
38 |
Correct |
2430 ms |
17836 KB |
Output is correct |
39 |
Correct |
3079 ms |
18036 KB |
Output is correct |
40 |
Correct |
2789 ms |
18004 KB |
Output is correct |
41 |
Correct |
3068 ms |
17804 KB |
Output is correct |
42 |
Correct |
2486 ms |
17568 KB |
Output is correct |
43 |
Correct |
212 ms |
8136 KB |
Output is correct |
44 |
Correct |
1996 ms |
17748 KB |
Output is correct |
45 |
Correct |
242 ms |
8868 KB |
Output is correct |
46 |
Correct |
2515 ms |
16636 KB |
Output is correct |
47 |
Correct |
40 ms |
15044 KB |
Output is correct |
48 |
Correct |
66 ms |
15448 KB |
Output is correct |
49 |
Correct |
116 ms |
9268 KB |
Output is correct |
50 |
Correct |
173 ms |
7624 KB |
Output is correct |
51 |
Correct |
315 ms |
9908 KB |
Output is correct |
52 |
Correct |
761 ms |
11672 KB |
Output is correct |
53 |
Correct |
766 ms |
11676 KB |
Output is correct |
54 |
Correct |
1258 ms |
13280 KB |
Output is correct |
55 |
Correct |
1128 ms |
13584 KB |
Output is correct |
56 |
Correct |
1561 ms |
14784 KB |
Output is correct |
57 |
Correct |
2153 ms |
16476 KB |
Output is correct |
58 |
Correct |
2347 ms |
16476 KB |
Output is correct |
59 |
Correct |
2568 ms |
17756 KB |
Output is correct |
60 |
Correct |
2934 ms |
17844 KB |
Output is correct |