#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
struct fountain
{
int x, y;
size_t i;
};
template <size_t N>
struct dsu
{
int t[N];
dsu() { memset(t, 255, sizeof t); }
int repr(int u) { return t[u] < 0 ? u : t[u] = repr(t[u]); }
bool same_component(int u, int v) { return repr(u) == repr(v); }
int component_size(int u) { return -t[repr(u)]; }
void merge(int u, int v)
{
u = repr(u), v = repr(v);
if (u == v)
return;
if (t[u] > t[v])
swap(u, v);
t[u] += t[v];
t[v] = u;
}
};
constexpr size_t N = 200000;
dsu<N> d;
bitset<N> occupied[8];
int construct_roads(vector<int> x, vector<int> y)
{
size_t const n = x.size();
vector<fountain> f;
for (size_t i = 0; i < n; ++i)
f.push_back({x[i], y[i], i});
sort(f.begin(), f.end(), [](auto const &a, auto const &b)
{ return a.y == b.y ? a.x < b.x : a.y < b.y; });
vector<int> u, v, a, b;
auto add_edge = [&](size_t i, size_t j)
{
u.push_back(i);
v.push_back(j);
d.merge(i, j);
};
auto add_bench = [&](size_t x, size_t y)
{
assert(!occupied[x][y]);
a.push_back(x);
b.push_back(y);
occupied[x][y] = 1;
};
for (size_t i = 0; i < n;)
{
size_t j = i;
while (j < n && f[j].y == f[i].y)
++j;
if (j - i >= 2 && f[i + 1].x - f[i].x == 2 && !d.same_component(f[i].i, f[i + 1].i))
{
add_edge(f[i].i, f[i + 1].i);
if (!occupied[f[i].x + 1][f[i].y - 1])
add_bench(f[i].x + 1, f[i].y - 1);
else
add_bench(f[i].x + 1, f[i].y + 1);
}
if (j - i >= 3 && f[i + 2].x - f[i + 1].x == 2 && !d.same_component(f[i + 1].i, f[i + 2].i))
{
add_edge(f[i + 1].i, f[i + 2].i);
if (!occupied[f[i + 1].x + 1][f[i + 1].y - 1])
add_bench(f[i + 1].x + 1, f[i + 1].y - 1);
else
add_bench(f[i + 1].x + 1, f[i + 1].y + 1);
}
if (j < n && f[j].y == f[i].y + 2)
{
size_t k = j;
while (i < j && k < n && f[k].y == f[j].y)
{
if (f[i].x < f[k].x)
++i;
else if (f[i].x > f[k].x)
++k;
else
{
add_edge(f[i].i, f[k].i);
if (f[i].x == 2)
add_bench(f[i].x - 1, f[i].y + 1);
else if (f[i].x == 6)
add_bench(f[i].x + 1, f[i].y + 1);
else
{
if (!occupied[f[i].x - 1][f[i].y + 1])
add_bench(f[i].x - 1, f[i].y + 1);
else
add_bench(f[i].x + 1, f[i].y + 1);
}
++i;
++k;
}
}
}
i = j;
}
if (d.component_size(0) != n)
return 0;
build(u, v, a, b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:123:29: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
123 | if (d.component_size(0) != n)
| ~~~~~~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1088 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1084 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
54 ms |
10032 KB |
Output is correct |
10 |
Correct |
5 ms |
2004 KB |
Output is correct |
11 |
Correct |
24 ms |
5844 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
9 ms |
4096 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
50 ms |
10040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1088 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1084 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
54 ms |
10032 KB |
Output is correct |
10 |
Correct |
5 ms |
2004 KB |
Output is correct |
11 |
Correct |
24 ms |
5844 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
9 ms |
4096 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
50 ms |
10040 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
103 ms |
19216 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
40 ms |
8580 KB |
Output is correct |
29 |
Correct |
60 ms |
11596 KB |
Output is correct |
30 |
Correct |
82 ms |
16188 KB |
Output is correct |
31 |
Correct |
102 ms |
19140 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
980 KB |
Output is correct |
35 |
Correct |
1 ms |
980 KB |
Output is correct |
36 |
Correct |
1 ms |
980 KB |
Output is correct |
37 |
Correct |
1 ms |
980 KB |
Output is correct |
38 |
Correct |
1 ms |
980 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
1 ms |
980 KB |
Output is correct |
42 |
Correct |
1 ms |
980 KB |
Output is correct |
43 |
Correct |
1 ms |
1236 KB |
Output is correct |
44 |
Correct |
1 ms |
1364 KB |
Output is correct |
45 |
Correct |
50 ms |
10192 KB |
Output is correct |
46 |
Correct |
76 ms |
15116 KB |
Output is correct |
47 |
Correct |
74 ms |
15032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1088 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1084 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
54 ms |
10032 KB |
Output is correct |
10 |
Correct |
5 ms |
2004 KB |
Output is correct |
11 |
Correct |
24 ms |
5844 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
9 ms |
4096 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
50 ms |
10040 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
103 ms |
19216 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
40 ms |
8580 KB |
Output is correct |
29 |
Correct |
60 ms |
11596 KB |
Output is correct |
30 |
Correct |
82 ms |
16188 KB |
Output is correct |
31 |
Correct |
102 ms |
19140 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
980 KB |
Output is correct |
35 |
Correct |
1 ms |
980 KB |
Output is correct |
36 |
Correct |
1 ms |
980 KB |
Output is correct |
37 |
Correct |
1 ms |
980 KB |
Output is correct |
38 |
Correct |
1 ms |
980 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
1 ms |
980 KB |
Output is correct |
42 |
Correct |
1 ms |
980 KB |
Output is correct |
43 |
Correct |
1 ms |
1236 KB |
Output is correct |
44 |
Correct |
1 ms |
1364 KB |
Output is correct |
45 |
Correct |
50 ms |
10192 KB |
Output is correct |
46 |
Correct |
76 ms |
15116 KB |
Output is correct |
47 |
Correct |
74 ms |
15032 KB |
Output is correct |
48 |
Correct |
1 ms |
980 KB |
Output is correct |
49 |
Correct |
1 ms |
980 KB |
Output is correct |
50 |
Correct |
1 ms |
1108 KB |
Output is correct |
51 |
Correct |
1 ms |
1108 KB |
Output is correct |
52 |
Correct |
1 ms |
980 KB |
Output is correct |
53 |
Correct |
1 ms |
1084 KB |
Output is correct |
54 |
Correct |
1 ms |
1108 KB |
Output is correct |
55 |
Correct |
111 ms |
19140 KB |
Output is correct |
56 |
Correct |
1 ms |
1108 KB |
Output is correct |
57 |
Correct |
2 ms |
1228 KB |
Output is correct |
58 |
Correct |
4 ms |
1748 KB |
Output is correct |
59 |
Correct |
3 ms |
1872 KB |
Output is correct |
60 |
Correct |
53 ms |
10032 KB |
Output is correct |
61 |
Correct |
88 ms |
14136 KB |
Output is correct |
62 |
Correct |
85 ms |
16564 KB |
Output is correct |
63 |
Correct |
101 ms |
19124 KB |
Output is correct |
64 |
Correct |
1 ms |
980 KB |
Output is correct |
65 |
Correct |
2 ms |
1148 KB |
Output is correct |
66 |
Correct |
2 ms |
980 KB |
Output is correct |
67 |
Correct |
108 ms |
19204 KB |
Output is correct |
68 |
Correct |
102 ms |
19124 KB |
Output is correct |
69 |
Correct |
105 ms |
19128 KB |
Output is correct |
70 |
Correct |
3 ms |
1360 KB |
Output is correct |
71 |
Correct |
2 ms |
1612 KB |
Output is correct |
72 |
Correct |
52 ms |
10108 KB |
Output is correct |
73 |
Correct |
82 ms |
15528 KB |
Output is correct |
74 |
Correct |
103 ms |
19260 KB |
Output is correct |
75 |
Correct |
102 ms |
19092 KB |
Output is correct |
76 |
Correct |
109 ms |
19124 KB |
Output is correct |
77 |
Correct |
2 ms |
1408 KB |
Output is correct |
78 |
Correct |
3 ms |
1744 KB |
Output is correct |
79 |
Correct |
50 ms |
10128 KB |
Output is correct |
80 |
Correct |
77 ms |
15408 KB |
Output is correct |
81 |
Correct |
103 ms |
19264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1088 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1084 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
54 ms |
10032 KB |
Output is correct |
10 |
Correct |
5 ms |
2004 KB |
Output is correct |
11 |
Correct |
24 ms |
5844 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
9 ms |
4096 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
50 ms |
10040 KB |
Output is correct |
17 |
Runtime error |
2 ms |
2004 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1088 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1084 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
54 ms |
10032 KB |
Output is correct |
10 |
Correct |
5 ms |
2004 KB |
Output is correct |
11 |
Correct |
24 ms |
5844 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
9 ms |
4096 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
50 ms |
10040 KB |
Output is correct |
17 |
Runtime error |
64 ms |
16976 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1088 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1084 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
54 ms |
10032 KB |
Output is correct |
10 |
Correct |
5 ms |
2004 KB |
Output is correct |
11 |
Correct |
24 ms |
5844 KB |
Output is correct |
12 |
Correct |
7 ms |
2432 KB |
Output is correct |
13 |
Correct |
9 ms |
4096 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
50 ms |
10040 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
103 ms |
19216 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
40 ms |
8580 KB |
Output is correct |
29 |
Correct |
60 ms |
11596 KB |
Output is correct |
30 |
Correct |
82 ms |
16188 KB |
Output is correct |
31 |
Correct |
102 ms |
19140 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
980 KB |
Output is correct |
35 |
Correct |
1 ms |
980 KB |
Output is correct |
36 |
Correct |
1 ms |
980 KB |
Output is correct |
37 |
Correct |
1 ms |
980 KB |
Output is correct |
38 |
Correct |
1 ms |
980 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
1 ms |
980 KB |
Output is correct |
42 |
Correct |
1 ms |
980 KB |
Output is correct |
43 |
Correct |
1 ms |
1236 KB |
Output is correct |
44 |
Correct |
1 ms |
1364 KB |
Output is correct |
45 |
Correct |
50 ms |
10192 KB |
Output is correct |
46 |
Correct |
76 ms |
15116 KB |
Output is correct |
47 |
Correct |
74 ms |
15032 KB |
Output is correct |
48 |
Correct |
1 ms |
980 KB |
Output is correct |
49 |
Correct |
1 ms |
980 KB |
Output is correct |
50 |
Correct |
1 ms |
1108 KB |
Output is correct |
51 |
Correct |
1 ms |
1108 KB |
Output is correct |
52 |
Correct |
1 ms |
980 KB |
Output is correct |
53 |
Correct |
1 ms |
1084 KB |
Output is correct |
54 |
Correct |
1 ms |
1108 KB |
Output is correct |
55 |
Correct |
111 ms |
19140 KB |
Output is correct |
56 |
Correct |
1 ms |
1108 KB |
Output is correct |
57 |
Correct |
2 ms |
1228 KB |
Output is correct |
58 |
Correct |
4 ms |
1748 KB |
Output is correct |
59 |
Correct |
3 ms |
1872 KB |
Output is correct |
60 |
Correct |
53 ms |
10032 KB |
Output is correct |
61 |
Correct |
88 ms |
14136 KB |
Output is correct |
62 |
Correct |
85 ms |
16564 KB |
Output is correct |
63 |
Correct |
101 ms |
19124 KB |
Output is correct |
64 |
Correct |
1 ms |
980 KB |
Output is correct |
65 |
Correct |
2 ms |
1148 KB |
Output is correct |
66 |
Correct |
2 ms |
980 KB |
Output is correct |
67 |
Correct |
108 ms |
19204 KB |
Output is correct |
68 |
Correct |
102 ms |
19124 KB |
Output is correct |
69 |
Correct |
105 ms |
19128 KB |
Output is correct |
70 |
Correct |
3 ms |
1360 KB |
Output is correct |
71 |
Correct |
2 ms |
1612 KB |
Output is correct |
72 |
Correct |
52 ms |
10108 KB |
Output is correct |
73 |
Correct |
82 ms |
15528 KB |
Output is correct |
74 |
Correct |
103 ms |
19260 KB |
Output is correct |
75 |
Correct |
102 ms |
19092 KB |
Output is correct |
76 |
Correct |
109 ms |
19124 KB |
Output is correct |
77 |
Correct |
2 ms |
1408 KB |
Output is correct |
78 |
Correct |
3 ms |
1744 KB |
Output is correct |
79 |
Correct |
50 ms |
10128 KB |
Output is correct |
80 |
Correct |
77 ms |
15408 KB |
Output is correct |
81 |
Correct |
103 ms |
19264 KB |
Output is correct |
82 |
Runtime error |
2 ms |
2004 KB |
Execution killed with signal 11 |
83 |
Halted |
0 ms |
0 KB |
- |