#include <bits/stdc++.h>
#include "collapse.h"
using namespace std;
vector<int> par;
vector<int> par2;
vector<int> last;
int tt;
int find(int u) {
if (par[u] == u) {
return u;
} else {
return par[u] = find(par[u]);
}
}
bool merge(int u, int v) {
u = find(u);
v = find(v);
if (u == v) {
return false;
} else {
par[v] = u;
return true;
}
}
int find2(int u) {
if (last[u] != tt) {
last[u] = tt;
par2[u] = par[u];
}
if (par2[u] == u) {
return u;
} else {
return par2[u] = find2(par2[u]);
}
}
bool merge2(int u, int v) {
u = find2(u);
v = find2(v);
if (u == v) {
return false;
} else {
par2[v] = u;
return true;
}
}
vector<int> simulateCollapse(int n, vector<int> type, vector<int> from, vector<int> to, vector<int> time, vector<int> pos) {
int m = type.size();
int q = time.size();
par.resize(n);
par2.resize(n);
last.assign(n, 0);
for (int i = 0; i < m; ++i) {
if (from[i] > to[i]) {
swap(from[i], to[i]);
}
}
vector<int> go(m);
{
map<pair<int, int>, int> st;
for (int i = 0; i < m; ++i) {
auto e = make_pair(from[i], to[i]);
if (type[i] == 0) {
st[e] = i;
} else {
go[st[e]] = i;
}
}
}
for (int i = 0; i < m; ++i) {
if (type[i] == 0 && go[i] == 0) {
go[i] = m;
}
}
vector<int> pref, suf;
for (int i = 0; i < m; ++i) {
if (type[i] == 0) {
pref.emplace_back(i);
suf.emplace_back(i);
}
}
sort(pref.begin(), pref.end(), [&](int u, int v) {
return to[u] < to[v];
});
sort(suf.begin(), suf.end(), [&](int u, int v) {
return from[u] > from[v];
});
vector<vector<int>> qs(n);
for (int i = 0; i < q; ++i) {
qs[pos[i]].emplace_back(i);
}
vector<int> ans(q, n);
int block = 320;
for (int l = 0; l < m; l += block) {
int r = min(m, l + block);
vector<int> pref_all;
vector<int> suf_all;
vector<int> pref_cand;
vector<int> suf_cand;
for (int id : pref) {
if (id <= l && r <= go[id]) {
pref_all.emplace_back(id);
} else if (id < r && l < go[id]) {
pref_cand.emplace_back(id);
}
}
for (int id : suf) {
if (id <= l && r <= go[id]) {
suf_all.emplace_back(id);
} else if (id < r && l < go[id]) {
suf_cand.emplace_back(id);
}
}
vector<int> pref_qs;
vector<int> suf_qs;
for (int i = 0; i < n; ++i) {
for (int id : qs[i]) {
if (l <= time[id] && time[id] < r) {
pref_qs.emplace_back(id);
}
}
}
for (int i = n - 1; i >= 0; --i) {
for (int id : qs[i]) {
if (l <= time[id] && time[id] < r) {
suf_qs.emplace_back(id);
}
}
}
{
for (int i = 0; i < n; ++i) {
par[i] = i;
}
int ptr = 0;
int cnt = 0;
for (int id : pref_qs) {
while (ptr < pref_all.size() && to[pref_all[ptr]] <= pos[id]) {
cnt += merge(from[pref_all[ptr]], to[pref_all[ptr]]);
++ptr;
}
int cnt2 = cnt;
++tt;
for (int jd : pref_cand) {
if (jd <= time[id] && time[id] < go[jd] && to[jd] <= pos[id]) {
cnt2 += merge2(from[jd], to[jd]);
}
}
ans[id] -= cnt2;
}
}
{
for (int i = 0; i < n; ++i) {
par[i] = i;
}
int ptr = 0;
int cnt = 0;
for (int id : suf_qs) {
while (ptr < suf_all.size() && from[suf_all[ptr]] > pos[id]) {
cnt += merge(from[suf_all[ptr]], to[suf_all[ptr]]);
++ptr;
}
int cnt2 = cnt;
++tt;
for (int jd : suf_cand) {
if (jd <= time[id] && time[id] < go[jd] && from[jd] > pos[id]) {
cnt2 += merge2(from[jd], to[jd]);
}
}
ans[id] -= cnt2;
}
}
}
return ans;
}
Compilation message
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:142:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (ptr < pref_all.size() && to[pref_all[ptr]] <= pos[id]) {
~~~~^~~~~~~~~~~~~~~~~
collapse.cpp:163:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (ptr < suf_all.size() && from[suf_all[ptr]] > pos[id]) {
~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
636 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
14 ms |
640 KB |
Output is correct |
6 |
Correct |
29 ms |
768 KB |
Output is correct |
7 |
Correct |
4 ms |
896 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
17 ms |
868 KB |
Output is correct |
10 |
Correct |
32 ms |
944 KB |
Output is correct |
11 |
Correct |
36 ms |
968 KB |
Output is correct |
12 |
Correct |
33 ms |
968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
3832 KB |
Output is correct |
2 |
Correct |
32 ms |
3824 KB |
Output is correct |
3 |
Correct |
600 ms |
6000 KB |
Output is correct |
4 |
Correct |
57 ms |
4368 KB |
Output is correct |
5 |
Correct |
819 ms |
7696 KB |
Output is correct |
6 |
Correct |
344 ms |
3900 KB |
Output is correct |
7 |
Correct |
1422 ms |
12920 KB |
Output is correct |
8 |
Correct |
819 ms |
10232 KB |
Output is correct |
9 |
Correct |
33 ms |
8176 KB |
Output is correct |
10 |
Correct |
41 ms |
8184 KB |
Output is correct |
11 |
Correct |
333 ms |
7780 KB |
Output is correct |
12 |
Correct |
1027 ms |
12048 KB |
Output is correct |
13 |
Correct |
1649 ms |
13092 KB |
Output is correct |
14 |
Correct |
1696 ms |
14556 KB |
Output is correct |
15 |
Correct |
1548 ms |
14584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
3832 KB |
Output is correct |
2 |
Correct |
32 ms |
3980 KB |
Output is correct |
3 |
Correct |
42 ms |
4000 KB |
Output is correct |
4 |
Correct |
62 ms |
4028 KB |
Output is correct |
5 |
Correct |
354 ms |
3456 KB |
Output is correct |
6 |
Correct |
421 ms |
3268 KB |
Output is correct |
7 |
Correct |
1421 ms |
8696 KB |
Output is correct |
8 |
Correct |
2862 ms |
13304 KB |
Output is correct |
9 |
Correct |
51 ms |
10868 KB |
Output is correct |
10 |
Correct |
398 ms |
9344 KB |
Output is correct |
11 |
Correct |
3817 ms |
15664 KB |
Output is correct |
12 |
Correct |
4566 ms |
15604 KB |
Output is correct |
13 |
Correct |
4718 ms |
15720 KB |
Output is correct |
14 |
Correct |
4715 ms |
15716 KB |
Output is correct |
15 |
Correct |
3944 ms |
15744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
636 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
14 ms |
640 KB |
Output is correct |
6 |
Correct |
29 ms |
768 KB |
Output is correct |
7 |
Correct |
4 ms |
896 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
17 ms |
868 KB |
Output is correct |
10 |
Correct |
32 ms |
944 KB |
Output is correct |
11 |
Correct |
36 ms |
968 KB |
Output is correct |
12 |
Correct |
33 ms |
968 KB |
Output is correct |
13 |
Correct |
26 ms |
3832 KB |
Output is correct |
14 |
Correct |
32 ms |
3824 KB |
Output is correct |
15 |
Correct |
600 ms |
6000 KB |
Output is correct |
16 |
Correct |
57 ms |
4368 KB |
Output is correct |
17 |
Correct |
819 ms |
7696 KB |
Output is correct |
18 |
Correct |
344 ms |
3900 KB |
Output is correct |
19 |
Correct |
1422 ms |
12920 KB |
Output is correct |
20 |
Correct |
819 ms |
10232 KB |
Output is correct |
21 |
Correct |
33 ms |
8176 KB |
Output is correct |
22 |
Correct |
41 ms |
8184 KB |
Output is correct |
23 |
Correct |
333 ms |
7780 KB |
Output is correct |
24 |
Correct |
1027 ms |
12048 KB |
Output is correct |
25 |
Correct |
1649 ms |
13092 KB |
Output is correct |
26 |
Correct |
1696 ms |
14556 KB |
Output is correct |
27 |
Correct |
1548 ms |
14584 KB |
Output is correct |
28 |
Correct |
25 ms |
3832 KB |
Output is correct |
29 |
Correct |
32 ms |
3980 KB |
Output is correct |
30 |
Correct |
42 ms |
4000 KB |
Output is correct |
31 |
Correct |
62 ms |
4028 KB |
Output is correct |
32 |
Correct |
354 ms |
3456 KB |
Output is correct |
33 |
Correct |
421 ms |
3268 KB |
Output is correct |
34 |
Correct |
1421 ms |
8696 KB |
Output is correct |
35 |
Correct |
2862 ms |
13304 KB |
Output is correct |
36 |
Correct |
51 ms |
10868 KB |
Output is correct |
37 |
Correct |
398 ms |
9344 KB |
Output is correct |
38 |
Correct |
3817 ms |
15664 KB |
Output is correct |
39 |
Correct |
4566 ms |
15604 KB |
Output is correct |
40 |
Correct |
4718 ms |
15720 KB |
Output is correct |
41 |
Correct |
4715 ms |
15716 KB |
Output is correct |
42 |
Correct |
3944 ms |
15744 KB |
Output is correct |
43 |
Correct |
821 ms |
9628 KB |
Output is correct |
44 |
Correct |
2221 ms |
12792 KB |
Output is correct |
45 |
Correct |
1049 ms |
10104 KB |
Output is correct |
46 |
Correct |
3009 ms |
13212 KB |
Output is correct |
47 |
Correct |
71 ms |
10872 KB |
Output is correct |
48 |
Correct |
64 ms |
9840 KB |
Output is correct |
49 |
Correct |
410 ms |
9376 KB |
Output is correct |
50 |
Correct |
732 ms |
9692 KB |
Output is correct |
51 |
Correct |
1854 ms |
13484 KB |
Output is correct |
52 |
Correct |
2369 ms |
14180 KB |
Output is correct |
53 |
Correct |
2312 ms |
14120 KB |
Output is correct |
54 |
Correct |
2772 ms |
14628 KB |
Output is correct |
55 |
Correct |
2829 ms |
14608 KB |
Output is correct |
56 |
Correct |
3426 ms |
14792 KB |
Output is correct |
57 |
Correct |
3734 ms |
15444 KB |
Output is correct |
58 |
Correct |
3856 ms |
15432 KB |
Output is correct |
59 |
Correct |
4038 ms |
15672 KB |
Output is correct |
60 |
Correct |
4809 ms |
15780 KB |
Output is correct |