Submission #799999

# Submission time Handle Problem Language Result Execution time Memory
799999 2023-08-01T09:16:04 Z finn__ Fountain Parks (IOI21_parks) C++17
30 / 100
2371 ms 2097152 KB
#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;
vector<uint32_t> g[N];
set<pair<int, int>> occupied;
vector<int> u, v, a, b;

void dfs(uint32_t node, uint32_t p, vector<int> const &x, vector<int> const &y)
{
    for (uint32_t const &child : g[node])
        if (child != p)
        {
            dfs(child, node, x, y);
            pair<int, int> bench[2];
            if (x[node] == x[child])
            {
                bench[0] = {x[node] - 1, (y[node] + y[child]) / 2};
                bench[1] = {x[node] + 1, (y[node] + y[child]) / 2};
            }
            else
            {
                assert(y[node] == y[child]);
                bench[0] = {(x[node] + x[child]) / 2, y[node] - 1};
                bench[1] = {(x[node] + x[child]) / 2, y[node] + 1};
            }

            u.push_back(node);
            v.push_back(child);
            for (size_t i = 0; i < 2; ++i)
                if (occupied.find(bench[i]) == occupied.end())
                {
                    occupied.insert(bench[i]);
                    a.push_back(bench[i].first);
                    b.push_back(bench[i].second);
                    break;
                }
            assert(a.size() == u.size());
        }
}

int construct_roads(vector<int> x, vector<int> y)
{
    size_t const n = x.size();

    int x_max = 2;
    vector<fountain> f;
    for (size_t i = 0; i < n; ++i)
        f.push_back({x[i], y[i], i}), x_max = max(x_max, x[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; });

    if (x_max <= 6)
    {

        auto add_edge = [&](size_t i, size_t j)
        {
            u.push_back(i);
            v.push_back(j);
            d.merge(i, j);
        };

        auto add_bench = [&](int x, int y)
        {
            a.push_back(x);
            b.push_back(y);
            occupied.insert(make_pair(x, y));
        };

        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.find(make_pair(f[i].x + 1, f[i].y - 1)) == occupied.end())
                    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.find(make_pair(f[i + 1].x + 1, f[i + 1].y - 1)) == occupied.end())
                    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.find(make_pair(f[i].x - 1, f[i].y + 1)) == occupied.end())
                                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;
    }
    else
    {
        for (size_t i = 0; i < n;)
        {
            size_t j = i;
            while (j < n && f[j].y == f[i].y)
                ++j;
            for (size_t k = i + 1; k < j; ++k)
                if (f[k].x == f[k - 1].x + 2)
                {
                    g[f[k - 1].i].push_back(f[k].i);
                    g[f[k].i].push_back(f[k - 1].i);
                    d.merge(f[k - 1].i, f[k].i);
                }

            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
                    {
                        g[f[i].i].push_back(f[k].i);
                        g[f[k].i].push_back(f[i].i);
                        d.merge(f[i].i, f[k].i);
                        ++i;
                        ++k;
                    }
                }
            }

            i = j;
        }

        if (d.component_size(0) != n)
            return 0;
        dfs(0, -1, x, y);
        build(u, v, a, b);
        return 1;
    }
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:159:33: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  159 |         if (d.component_size(0) != n)
      |             ~~~~~~~~~~~~~~~~~~~~^~~~
parks.cpp:203:33: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  203 |         if (d.component_size(0) != n)
      |             ~~~~~~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5656 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5776 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 65 ms 19156 KB Output is correct
10 Correct 10 ms 7120 KB Output is correct
11 Correct 38 ms 13008 KB Output is correct
12 Correct 11 ms 7760 KB Output is correct
13 Correct 16 ms 10328 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 66 ms 19208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5656 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5776 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 65 ms 19156 KB Output is correct
10 Correct 10 ms 7120 KB Output is correct
11 Correct 38 ms 13008 KB Output is correct
12 Correct 11 ms 7760 KB Output is correct
13 Correct 16 ms 10328 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 66 ms 19208 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5780 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 4 ms 5716 KB Output is correct
21 Correct 3 ms 5776 KB Output is correct
22 Correct 3 ms 5716 KB Output is correct
23 Correct 147 ms 32508 KB Output is correct
24 Correct 4 ms 5716 KB Output is correct
25 Correct 4 ms 5972 KB Output is correct
26 Correct 4 ms 6100 KB Output is correct
27 Correct 4 ms 6100 KB Output is correct
28 Correct 70 ms 16560 KB Output is correct
29 Correct 87 ms 21824 KB Output is correct
30 Correct 115 ms 27512 KB Output is correct
31 Correct 146 ms 32512 KB Output is correct
32 Correct 3 ms 5716 KB Output is correct
33 Correct 3 ms 5716 KB Output is correct
34 Correct 3 ms 5760 KB Output is correct
35 Correct 3 ms 5712 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 3 ms 5776 KB Output is correct
38 Correct 4 ms 5780 KB Output is correct
39 Correct 4 ms 5784 KB Output is correct
40 Correct 3 ms 5776 KB Output is correct
41 Correct 3 ms 5716 KB Output is correct
42 Correct 3 ms 5716 KB Output is correct
43 Correct 3 ms 5972 KB Output is correct
44 Correct 4 ms 6100 KB Output is correct
45 Correct 73 ms 19200 KB Output is correct
46 Correct 102 ms 25948 KB Output is correct
47 Correct 100 ms 25904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5656 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5776 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 65 ms 19156 KB Output is correct
10 Correct 10 ms 7120 KB Output is correct
11 Correct 38 ms 13008 KB Output is correct
12 Correct 11 ms 7760 KB Output is correct
13 Correct 16 ms 10328 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 66 ms 19208 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5780 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 4 ms 5716 KB Output is correct
21 Correct 3 ms 5776 KB Output is correct
22 Correct 3 ms 5716 KB Output is correct
23 Correct 147 ms 32508 KB Output is correct
24 Correct 4 ms 5716 KB Output is correct
25 Correct 4 ms 5972 KB Output is correct
26 Correct 4 ms 6100 KB Output is correct
27 Correct 4 ms 6100 KB Output is correct
28 Correct 70 ms 16560 KB Output is correct
29 Correct 87 ms 21824 KB Output is correct
30 Correct 115 ms 27512 KB Output is correct
31 Correct 146 ms 32512 KB Output is correct
32 Correct 3 ms 5716 KB Output is correct
33 Correct 3 ms 5716 KB Output is correct
34 Correct 3 ms 5760 KB Output is correct
35 Correct 3 ms 5712 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 3 ms 5776 KB Output is correct
38 Correct 4 ms 5780 KB Output is correct
39 Correct 4 ms 5784 KB Output is correct
40 Correct 3 ms 5776 KB Output is correct
41 Correct 3 ms 5716 KB Output is correct
42 Correct 3 ms 5716 KB Output is correct
43 Correct 3 ms 5972 KB Output is correct
44 Correct 4 ms 6100 KB Output is correct
45 Correct 73 ms 19200 KB Output is correct
46 Correct 102 ms 25948 KB Output is correct
47 Correct 100 ms 25904 KB Output is correct
48 Correct 3 ms 5716 KB Output is correct
49 Correct 3 ms 5780 KB Output is correct
50 Correct 6 ms 5716 KB Output is correct
51 Correct 4 ms 5716 KB Output is correct
52 Correct 3 ms 5716 KB Output is correct
53 Correct 3 ms 5716 KB Output is correct
54 Correct 3 ms 5716 KB Output is correct
55 Correct 153 ms 32528 KB Output is correct
56 Correct 3 ms 5716 KB Output is correct
57 Correct 4 ms 5972 KB Output is correct
58 Correct 6 ms 6648 KB Output is correct
59 Correct 8 ms 6736 KB Output is correct
60 Correct 69 ms 19208 KB Output is correct
61 Correct 113 ms 25088 KB Output is correct
62 Correct 118 ms 28268 KB Output is correct
63 Correct 176 ms 32564 KB Output is correct
64 Correct 3 ms 5716 KB Output is correct
65 Correct 3 ms 5780 KB Output is correct
66 Correct 3 ms 5780 KB Output is correct
67 Correct 143 ms 32588 KB Output is correct
68 Correct 135 ms 32524 KB Output is correct
69 Correct 138 ms 32264 KB Output is correct
70 Correct 4 ms 6100 KB Output is correct
71 Correct 6 ms 6616 KB Output is correct
72 Correct 69 ms 19204 KB Output is correct
73 Correct 109 ms 26372 KB Output is correct
74 Correct 146 ms 32568 KB Output is correct
75 Correct 139 ms 32400 KB Output is correct
76 Correct 137 ms 32400 KB Output is correct
77 Correct 5 ms 6228 KB Output is correct
78 Correct 8 ms 6736 KB Output is correct
79 Correct 69 ms 19164 KB Output is correct
80 Correct 105 ms 26252 KB Output is correct
81 Correct 139 ms 32140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5656 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5776 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 65 ms 19156 KB Output is correct
10 Correct 10 ms 7120 KB Output is correct
11 Correct 38 ms 13008 KB Output is correct
12 Correct 11 ms 7760 KB Output is correct
13 Correct 16 ms 10328 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 66 ms 19208 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5780 KB Output is correct
19 Correct 4 ms 5780 KB Output is correct
20 Runtime error 203 ms 127296 KB Execution killed with signal 6
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5656 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5776 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 65 ms 19156 KB Output is correct
10 Correct 10 ms 7120 KB Output is correct
11 Correct 38 ms 13008 KB Output is correct
12 Correct 11 ms 7760 KB Output is correct
13 Correct 16 ms 10328 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 66 ms 19208 KB Output is correct
17 Runtime error 2371 ms 2097152 KB Execution killed with signal 9
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5656 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5776 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 65 ms 19156 KB Output is correct
10 Correct 10 ms 7120 KB Output is correct
11 Correct 38 ms 13008 KB Output is correct
12 Correct 11 ms 7760 KB Output is correct
13 Correct 16 ms 10328 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 66 ms 19208 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5780 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 4 ms 5716 KB Output is correct
21 Correct 3 ms 5776 KB Output is correct
22 Correct 3 ms 5716 KB Output is correct
23 Correct 147 ms 32508 KB Output is correct
24 Correct 4 ms 5716 KB Output is correct
25 Correct 4 ms 5972 KB Output is correct
26 Correct 4 ms 6100 KB Output is correct
27 Correct 4 ms 6100 KB Output is correct
28 Correct 70 ms 16560 KB Output is correct
29 Correct 87 ms 21824 KB Output is correct
30 Correct 115 ms 27512 KB Output is correct
31 Correct 146 ms 32512 KB Output is correct
32 Correct 3 ms 5716 KB Output is correct
33 Correct 3 ms 5716 KB Output is correct
34 Correct 3 ms 5760 KB Output is correct
35 Correct 3 ms 5712 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 3 ms 5776 KB Output is correct
38 Correct 4 ms 5780 KB Output is correct
39 Correct 4 ms 5784 KB Output is correct
40 Correct 3 ms 5776 KB Output is correct
41 Correct 3 ms 5716 KB Output is correct
42 Correct 3 ms 5716 KB Output is correct
43 Correct 3 ms 5972 KB Output is correct
44 Correct 4 ms 6100 KB Output is correct
45 Correct 73 ms 19200 KB Output is correct
46 Correct 102 ms 25948 KB Output is correct
47 Correct 100 ms 25904 KB Output is correct
48 Correct 3 ms 5716 KB Output is correct
49 Correct 3 ms 5780 KB Output is correct
50 Correct 6 ms 5716 KB Output is correct
51 Correct 4 ms 5716 KB Output is correct
52 Correct 3 ms 5716 KB Output is correct
53 Correct 3 ms 5716 KB Output is correct
54 Correct 3 ms 5716 KB Output is correct
55 Correct 153 ms 32528 KB Output is correct
56 Correct 3 ms 5716 KB Output is correct
57 Correct 4 ms 5972 KB Output is correct
58 Correct 6 ms 6648 KB Output is correct
59 Correct 8 ms 6736 KB Output is correct
60 Correct 69 ms 19208 KB Output is correct
61 Correct 113 ms 25088 KB Output is correct
62 Correct 118 ms 28268 KB Output is correct
63 Correct 176 ms 32564 KB Output is correct
64 Correct 3 ms 5716 KB Output is correct
65 Correct 3 ms 5780 KB Output is correct
66 Correct 3 ms 5780 KB Output is correct
67 Correct 143 ms 32588 KB Output is correct
68 Correct 135 ms 32524 KB Output is correct
69 Correct 138 ms 32264 KB Output is correct
70 Correct 4 ms 6100 KB Output is correct
71 Correct 6 ms 6616 KB Output is correct
72 Correct 69 ms 19204 KB Output is correct
73 Correct 109 ms 26372 KB Output is correct
74 Correct 146 ms 32568 KB Output is correct
75 Correct 139 ms 32400 KB Output is correct
76 Correct 137 ms 32400 KB Output is correct
77 Correct 5 ms 6228 KB Output is correct
78 Correct 8 ms 6736 KB Output is correct
79 Correct 69 ms 19164 KB Output is correct
80 Correct 105 ms 26252 KB Output is correct
81 Correct 139 ms 32140 KB Output is correct
82 Correct 3 ms 5716 KB Output is correct
83 Correct 3 ms 5780 KB Output is correct
84 Correct 4 ms 5780 KB Output is correct
85 Runtime error 203 ms 127296 KB Execution killed with signal 6
86 Halted 0 ms 0 KB -