#include "collapse.h"
#include <bits/stdc++.h>
using namespace std;
class disjoint_set {
public:
disjoint_set() {}
disjoint_set(int n) : n_(n), comp_count(n), comp(n, -1) {}
int find_root(int x) {
return (comp[x] < 0) ? x : find_root(comp[x]);
}
int merge(int x, int y) {
x = find_root(x);
y = find_root(y);
if (x == y) {
return 0;
}
if (-comp[x] > -comp[y]) {
swap(x, y);
}
history.push_back({x, comp[x]});
history.push_back({y, comp[y]});
comp[y] += comp[x];
comp[x] = y;
comp_count--;
return 1;
}
void rollback(int x) {
comp_count += x;
for (int i = 0; i < 2 * x; i++) {
comp[history.back().first] = history.back().second;
history.pop_back();
}
}
void make_set() {
n_ += 1;
comp.emplace_back(-1);
}
int size(int x) {
return -comp[find_root(x)];
}
int size() {
return comp_count;
}
private:
int n_;
int comp_count;
vector<int> comp;
vector<pair<int, int>> history;
};
vector<int> simulateCollapse(int N, vector<int> T,
vector<int> X,
vector<int> Y,
vector<int> W,
vector<int> P) {
int C = (int) T.size();
int Q = (int) W.size();
vector<int> ans(Q);
vector<array<int, 4>> events;
for (int i = 0; i < C; i++) {
if (X[i] > Y[i]) {
swap(X[i], Y[i]);
}
if (T[i] == 0) {
events.push_back({i, -1, X[i], Y[i]});
} else {
events.push_back({i, -2, X[i], Y[i]});
}
}
for (int i = 0; i < Q; i++) {
events.push_back({W[i], i, P[i], P[i] + 1});
}
for (int rep = 0; rep < 2; rep++) {
struct edge {
int u, v;
edge() : u(0), v(0) {}
edge(int u, int v) : u(u), v(v) {}
const bool operator < (const edge &o) const {
return make_pair(u, v) < make_pair(o.u, o.v);
}
};
struct interval {
int l, r;
interval() : l(0), r(0) {}
interval(int l, int r) : l(l), r(r) {}
};
const int BLOCK = 1000;
const int INF = 1e8;
set<edge> active_edges;
sort(begin(events), end(events));
for (int current = 0; current < (int) events.size(); current += BLOCK) {
set<edge> changed_edges;
for (int i = current; i < min(int(events.size()), current + BLOCK); i++) {
if (events[i][1] < 0) {
changed_edges.insert({events[i][2], events[i][3]});
}
}
map<edge, int> active_time;
vector<edge> edges_permanent;
for (auto e : active_edges) {
if (changed_edges.count(e) == 0) {
edges_permanent.push_back(e);
} else {
active_time[e] = 0;
}
}
vector<array<int, 3>> queries; // (x-coord, time, id)
vector<pair<edge, interval>> edges_temp;
for (int i = current; i < min(int(events.size()), current + BLOCK); i++) {
if (events[i][1] == -1) {
active_time[edge(events[i][2], events[i][3])] = events[i][0];
} else if (events[i][1] == -2) {
edge e = edge(events[i][2], events[i][3]);
edges_temp.push_back(make_pair(e, interval(active_time[e], events[i][0])));
active_time.erase(e);
} else {
queries.push_back({events[i][2], events[i][0], events[i][1]});
}
}
for (auto e : active_time) {
edges_temp.push_back(make_pair(e.first, interval(e.second, INF)));
}
sort(begin(edges_permanent), end(edges_permanent), [](const edge &e1, const edge &e2) {
return e1.v < e2.v;
});
reverse(begin(edges_permanent), end(edges_permanent));
sort(begin(queries), end(queries));
disjoint_set dsu(N);
for (auto q : queries) {
while (!edges_permanent.empty() && edges_permanent.back().v <= q[0]) {
int u = edges_permanent.back().u;
int v = edges_permanent.back().v;
edges_permanent.pop_back();
dsu.merge(u, v);
}
int rollbacks = 0;
for (auto e : edges_temp) {
if (e.first.v <= q[0] && e.second.l <= q[1] && q[1] < e.second.r) {
rollbacks += dsu.merge(e.first.u, e.first.v);
}
}
ans[q[2]] += dsu.size() - (N - q[0] - 1);
dsu.rollback(rollbacks);
}
for (int i = current; i < min(int(events.size()), current + BLOCK); i++) {
if (events[i][1] == -1) {
assert(active_edges.count({events[i][2], events[i][3]}) == 0);
active_edges.insert({events[i][2], events[i][3]});
} else if (events[i][1] == -2) {
assert(active_edges.count({events[i][2], events[i][3]}) == 1);
active_edges.erase({events[i][2], events[i][3]});
}
}
}
for (int i = 0; i < (int) events.size(); i++) {
events[i][2] = N - events[i][2] - 1;
events[i][3] = N - events[i][3] - 1;
swap(events[i][2], events[i][3]);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1024 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
6 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
16 ms |
1024 KB |
Output is correct |
6 |
Correct |
37 ms |
1148 KB |
Output is correct |
7 |
Correct |
4 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
640 KB |
Output is correct |
9 |
Correct |
18 ms |
1024 KB |
Output is correct |
10 |
Correct |
39 ms |
1024 KB |
Output is correct |
11 |
Correct |
58 ms |
1308 KB |
Output is correct |
12 |
Correct |
52 ms |
1308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
4844 KB |
Output is correct |
2 |
Correct |
70 ms |
4844 KB |
Output is correct |
3 |
Correct |
310 ms |
10216 KB |
Output is correct |
4 |
Correct |
69 ms |
4980 KB |
Output is correct |
5 |
Correct |
485 ms |
10492 KB |
Output is correct |
6 |
Correct |
148 ms |
5360 KB |
Output is correct |
7 |
Correct |
3421 ms |
16404 KB |
Output is correct |
8 |
Correct |
514 ms |
11140 KB |
Output is correct |
9 |
Correct |
75 ms |
5236 KB |
Output is correct |
10 |
Correct |
82 ms |
5228 KB |
Output is correct |
11 |
Correct |
108 ms |
5484 KB |
Output is correct |
12 |
Correct |
543 ms |
11368 KB |
Output is correct |
13 |
Correct |
1875 ms |
13640 KB |
Output is correct |
14 |
Correct |
4737 ms |
17788 KB |
Output is correct |
15 |
Correct |
3602 ms |
18652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
4852 KB |
Output is correct |
2 |
Correct |
80 ms |
4844 KB |
Output is correct |
3 |
Correct |
82 ms |
4844 KB |
Output is correct |
4 |
Correct |
81 ms |
4972 KB |
Output is correct |
5 |
Correct |
92 ms |
5100 KB |
Output is correct |
6 |
Correct |
172 ms |
5360 KB |
Output is correct |
7 |
Correct |
2313 ms |
13828 KB |
Output is correct |
8 |
Correct |
4437 ms |
16880 KB |
Output is correct |
9 |
Correct |
81 ms |
5228 KB |
Output is correct |
10 |
Correct |
118 ms |
5736 KB |
Output is correct |
11 |
Correct |
4440 ms |
20156 KB |
Output is correct |
12 |
Correct |
5691 ms |
20076 KB |
Output is correct |
13 |
Correct |
4679 ms |
20076 KB |
Output is correct |
14 |
Correct |
5966 ms |
20388 KB |
Output is correct |
15 |
Correct |
4443 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1024 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
6 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
16 ms |
1024 KB |
Output is correct |
6 |
Correct |
37 ms |
1148 KB |
Output is correct |
7 |
Correct |
4 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
640 KB |
Output is correct |
9 |
Correct |
18 ms |
1024 KB |
Output is correct |
10 |
Correct |
39 ms |
1024 KB |
Output is correct |
11 |
Correct |
58 ms |
1308 KB |
Output is correct |
12 |
Correct |
52 ms |
1308 KB |
Output is correct |
13 |
Correct |
60 ms |
4844 KB |
Output is correct |
14 |
Correct |
70 ms |
4844 KB |
Output is correct |
15 |
Correct |
310 ms |
10216 KB |
Output is correct |
16 |
Correct |
69 ms |
4980 KB |
Output is correct |
17 |
Correct |
485 ms |
10492 KB |
Output is correct |
18 |
Correct |
148 ms |
5360 KB |
Output is correct |
19 |
Correct |
3421 ms |
16404 KB |
Output is correct |
20 |
Correct |
514 ms |
11140 KB |
Output is correct |
21 |
Correct |
75 ms |
5236 KB |
Output is correct |
22 |
Correct |
82 ms |
5228 KB |
Output is correct |
23 |
Correct |
108 ms |
5484 KB |
Output is correct |
24 |
Correct |
543 ms |
11368 KB |
Output is correct |
25 |
Correct |
1875 ms |
13640 KB |
Output is correct |
26 |
Correct |
4737 ms |
17788 KB |
Output is correct |
27 |
Correct |
3602 ms |
18652 KB |
Output is correct |
28 |
Correct |
62 ms |
4852 KB |
Output is correct |
29 |
Correct |
80 ms |
4844 KB |
Output is correct |
30 |
Correct |
82 ms |
4844 KB |
Output is correct |
31 |
Correct |
81 ms |
4972 KB |
Output is correct |
32 |
Correct |
92 ms |
5100 KB |
Output is correct |
33 |
Correct |
172 ms |
5360 KB |
Output is correct |
34 |
Correct |
2313 ms |
13828 KB |
Output is correct |
35 |
Correct |
4437 ms |
16880 KB |
Output is correct |
36 |
Correct |
81 ms |
5228 KB |
Output is correct |
37 |
Correct |
118 ms |
5736 KB |
Output is correct |
38 |
Correct |
4440 ms |
20156 KB |
Output is correct |
39 |
Correct |
5691 ms |
20076 KB |
Output is correct |
40 |
Correct |
4679 ms |
20076 KB |
Output is correct |
41 |
Correct |
5966 ms |
20388 KB |
Output is correct |
42 |
Correct |
4443 ms |
20052 KB |
Output is correct |
43 |
Correct |
457 ms |
10728 KB |
Output is correct |
44 |
Correct |
3539 ms |
16476 KB |
Output is correct |
45 |
Correct |
579 ms |
11112 KB |
Output is correct |
46 |
Correct |
4457 ms |
16712 KB |
Output is correct |
47 |
Correct |
85 ms |
5236 KB |
Output is correct |
48 |
Correct |
92 ms |
5236 KB |
Output is correct |
49 |
Correct |
122 ms |
5736 KB |
Output is correct |
50 |
Correct |
387 ms |
6636 KB |
Output is correct |
51 |
Correct |
685 ms |
11388 KB |
Output is correct |
52 |
Correct |
1668 ms |
13024 KB |
Output is correct |
53 |
Correct |
1479 ms |
13024 KB |
Output is correct |
54 |
Correct |
2515 ms |
15284 KB |
Output is correct |
55 |
Correct |
2124 ms |
15092 KB |
Output is correct |
56 |
Correct |
2843 ms |
16660 KB |
Output is correct |
57 |
Correct |
3644 ms |
18144 KB |
Output is correct |
58 |
Correct |
4555 ms |
19096 KB |
Output is correct |
59 |
Correct |
4335 ms |
20472 KB |
Output is correct |
60 |
Correct |
5684 ms |
19984 KB |
Output is correct |