#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 between signed and unsigned integer expressions [-Wsign-compare]
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]
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'
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 |
10 ms |
632 KB |
Output is correct |
2 |
Correct |
8 ms |
1032 KB |
Output is correct |
3 |
Correct |
9 ms |
1160 KB |
Output is correct |
4 |
Correct |
9 ms |
1032 KB |
Output is correct |
5 |
Correct |
16 ms |
760 KB |
Output is correct |
6 |
Correct |
48 ms |
1784 KB |
Output is correct |
7 |
Correct |
8 ms |
1032 KB |
Output is correct |
8 |
Correct |
9 ms |
1144 KB |
Output is correct |
9 |
Correct |
19 ms |
1016 KB |
Output is correct |
10 |
Correct |
47 ms |
1528 KB |
Output is correct |
11 |
Correct |
61 ms |
1784 KB |
Output is correct |
12 |
Correct |
58 ms |
1964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
13112 KB |
Output is correct |
2 |
Correct |
69 ms |
13372 KB |
Output is correct |
3 |
Correct |
278 ms |
6256 KB |
Output is correct |
4 |
Correct |
83 ms |
13672 KB |
Output is correct |
5 |
Correct |
561 ms |
8684 KB |
Output is correct |
6 |
Correct |
188 ms |
6636 KB |
Output is correct |
7 |
Correct |
3218 ms |
18024 KB |
Output is correct |
8 |
Correct |
571 ms |
9324 KB |
Output is correct |
9 |
Correct |
68 ms |
14384 KB |
Output is correct |
10 |
Correct |
78 ms |
14824 KB |
Output is correct |
11 |
Correct |
162 ms |
8980 KB |
Output is correct |
12 |
Correct |
606 ms |
10220 KB |
Output is correct |
13 |
Correct |
1884 ms |
13636 KB |
Output is correct |
14 |
Correct |
4046 ms |
18288 KB |
Output is correct |
15 |
Correct |
3311 ms |
18608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
13104 KB |
Output is correct |
2 |
Correct |
72 ms |
13364 KB |
Output is correct |
3 |
Correct |
80 ms |
13160 KB |
Output is correct |
4 |
Correct |
89 ms |
13252 KB |
Output is correct |
5 |
Correct |
239 ms |
10292 KB |
Output is correct |
6 |
Correct |
234 ms |
6508 KB |
Output is correct |
7 |
Correct |
2081 ms |
14868 KB |
Output is correct |
8 |
Correct |
4291 ms |
17068 KB |
Output is correct |
9 |
Correct |
74 ms |
14776 KB |
Output is correct |
10 |
Correct |
248 ms |
9408 KB |
Output is correct |
11 |
Correct |
4370 ms |
18312 KB |
Output is correct |
12 |
Correct |
4993 ms |
17784 KB |
Output is correct |
13 |
Correct |
4730 ms |
18720 KB |
Output is correct |
14 |
Correct |
5071 ms |
17948 KB |
Output is correct |
15 |
Correct |
4512 ms |
18156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
632 KB |
Output is correct |
2 |
Correct |
8 ms |
1032 KB |
Output is correct |
3 |
Correct |
9 ms |
1160 KB |
Output is correct |
4 |
Correct |
9 ms |
1032 KB |
Output is correct |
5 |
Correct |
16 ms |
760 KB |
Output is correct |
6 |
Correct |
48 ms |
1784 KB |
Output is correct |
7 |
Correct |
8 ms |
1032 KB |
Output is correct |
8 |
Correct |
9 ms |
1144 KB |
Output is correct |
9 |
Correct |
19 ms |
1016 KB |
Output is correct |
10 |
Correct |
47 ms |
1528 KB |
Output is correct |
11 |
Correct |
61 ms |
1784 KB |
Output is correct |
12 |
Correct |
58 ms |
1964 KB |
Output is correct |
13 |
Correct |
61 ms |
13112 KB |
Output is correct |
14 |
Correct |
69 ms |
13372 KB |
Output is correct |
15 |
Correct |
278 ms |
6256 KB |
Output is correct |
16 |
Correct |
83 ms |
13672 KB |
Output is correct |
17 |
Correct |
561 ms |
8684 KB |
Output is correct |
18 |
Correct |
188 ms |
6636 KB |
Output is correct |
19 |
Correct |
3218 ms |
18024 KB |
Output is correct |
20 |
Correct |
571 ms |
9324 KB |
Output is correct |
21 |
Correct |
68 ms |
14384 KB |
Output is correct |
22 |
Correct |
78 ms |
14824 KB |
Output is correct |
23 |
Correct |
162 ms |
8980 KB |
Output is correct |
24 |
Correct |
606 ms |
10220 KB |
Output is correct |
25 |
Correct |
1884 ms |
13636 KB |
Output is correct |
26 |
Correct |
4046 ms |
18288 KB |
Output is correct |
27 |
Correct |
3311 ms |
18608 KB |
Output is correct |
28 |
Correct |
56 ms |
13104 KB |
Output is correct |
29 |
Correct |
72 ms |
13364 KB |
Output is correct |
30 |
Correct |
80 ms |
13160 KB |
Output is correct |
31 |
Correct |
89 ms |
13252 KB |
Output is correct |
32 |
Correct |
239 ms |
10292 KB |
Output is correct |
33 |
Correct |
234 ms |
6508 KB |
Output is correct |
34 |
Correct |
2081 ms |
14868 KB |
Output is correct |
35 |
Correct |
4291 ms |
17068 KB |
Output is correct |
36 |
Correct |
74 ms |
14776 KB |
Output is correct |
37 |
Correct |
248 ms |
9408 KB |
Output is correct |
38 |
Correct |
4370 ms |
18312 KB |
Output is correct |
39 |
Correct |
4993 ms |
17784 KB |
Output is correct |
40 |
Correct |
4730 ms |
18720 KB |
Output is correct |
41 |
Correct |
5071 ms |
17948 KB |
Output is correct |
42 |
Correct |
4512 ms |
18156 KB |
Output is correct |
43 |
Correct |
451 ms |
8428 KB |
Output is correct |
44 |
Correct |
3408 ms |
16560 KB |
Output is correct |
45 |
Correct |
582 ms |
9068 KB |
Output is correct |
46 |
Correct |
4236 ms |
16944 KB |
Output is correct |
47 |
Correct |
73 ms |
14648 KB |
Output is correct |
48 |
Correct |
85 ms |
14700 KB |
Output is correct |
49 |
Correct |
216 ms |
8976 KB |
Output is correct |
50 |
Correct |
359 ms |
7788 KB |
Output is correct |
51 |
Correct |
731 ms |
9836 KB |
Output is correct |
52 |
Correct |
1443 ms |
11660 KB |
Output is correct |
53 |
Correct |
1502 ms |
11968 KB |
Output is correct |
54 |
Correct |
2146 ms |
13276 KB |
Output is correct |
55 |
Correct |
2077 ms |
13584 KB |
Output is correct |
56 |
Correct |
2894 ms |
14656 KB |
Output is correct |
57 |
Correct |
3779 ms |
16524 KB |
Output is correct |
58 |
Correct |
3952 ms |
16940 KB |
Output is correct |
59 |
Correct |
4381 ms |
17784 KB |
Output is correct |
60 |
Correct |
5066 ms |
18052 KB |
Output is correct |