Submission #898707

# Submission time Handle Problem Language Result Execution time Memory
898707 2024-01-05T03:45:11 Z box Hamburg Steak (JOI20_hamburg) C++17
100 / 100
1417 ms 189832 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif

#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;

int N;

struct SCC {
    vector<vi> g, rg;
    vi order, id, comps;
    SCC(int n) : g(n), rg(n), id(n) {}
    void make_edge(int i, int j) {
        g.at(i).push_back(j);
        rg.at(j).push_back(i);
    }
    void dfs1(int i) {
        id[i] = 1;
        for (int j : g[i]) if (!id[j]) dfs1(j);
        order.push_back(i);
    }
    int label;
    vector<int> nodes;
    void dfs2(int i) {
        nodes.push_back(i);
        id[i] = 0;
        if (i < N * 2) label = i;
        for (int j : rg[i]) if (id[j] == -1) dfs2(j);
    }
    void solve() {
        for (int i = 0; i < sz(id); i++) if (!id[i]) dfs1(i);
        reverse(all(order)), fill(all(id), -1);
        for (int i : order) if (id[i] == -1) {
            nodes = {};
            label = -1;
            dfs2(i);
            if (~label) comps.push_back(label);
            for (int j : nodes) {
                if (~label) id[j] = label;
                else id[j] = i;
            }
        }
        reverse(all(comps));
    }
};

const int INF = 1e9;

int K;
vector<ar<int, 4>> rect;
vector<bool> covered;
vector<pi> solutions;
 
ar<int, 4> get_bounds() {
    int x1 = INF, x2 = 1, y1 = INF, y2 = 1;
    for (int i = 0; i < N; i++) if (!covered[i]) {
        auto [l, d, r, u] = rect[i];
        x1 = min(x1, r);
        x2 = max(x2, l);
        y1 = min(y1, u);
        y2 = max(y2, d);
    }
    return {x1, x2, y1, y2};
}
 
void pr() {
    for (auto [x, y] : solutions) cout << x << ' ' << y << '\n';
    exit(0);
}
 
void rec(int k) {
    if (!k) {
        if (count(all(covered), 1) == N) pr();
        return;
    }
    auto [x1, x2, y1, y2] = get_bounds();
    for (auto x : {x1, x2}) for (auto y : {y1, y2}) {
        solutions.push_back({x, y});
        vector<int> add;
        for (int i = 0; i < N; i++) if (!covered[i]) {
            auto [l, d, r, u] = rect[i];
            if (l <= x && x <= r && d <= y && y <= u) add.push_back(i);
        }
        for (int i : add) covered[i] = 1;
        rec(k - 1);
        for (int i : add) covered[i] = 0;
        solutions.pop_back();
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> K;
    rect.resize(N);
    for (auto &[l, d, r, u] : rect) cin >> l >> d >> r >> u;
    covered.resize(N);
    rec(K);
    auto [x1, x2, y1, y2] = get_bounds();
    vector<pi> edges;
    int n = 0;
    vector<ar<int, 3>> A, B, C, D;
    vector<int> must;
    for (int i = 0; i < N; i++) {
        auto [l, d, r, u] = rect[i];
        if (l <= x1 && x1 <= r && d <= y1 && y2 <= u) continue;
        if (l <= x2 && x2 <= r && d <= y1 && y2 <= u) continue;
        if (l <= x1 && x2 <= r && d <= y1 && y1 <= u) continue;
        if (l <= x1 && x2 <= r && d <= y2 && y2 <= u) continue;
        int cnt = 0;
        int bm = 0;
        if (d <= y1 && y1 <= u) {
            assert(max(x1, l) <= min(x2, r));
            A.push_back({max(x1, l), min(x2, r), i * 2 + cnt});
            cnt++;
            bm |= 1;
        }
        if (d <= y2 && y2 <= u) {
            assert(max(x1, l) <= min(x2, r));
            B.push_back({max(x1, l), min(x2, r), i * 2 + cnt});
            cnt++;
            bm |= 2;
        }
        if (l <= x1 && x1 <= r) {
            assert(max(y1, d) <= min(y2, u));
            C.push_back({max(y1, d), min(y2, u), i * 2 + cnt});
            cnt++;
            bm |= 4;
        }
        if (l <= x2 && x2 <= r) {
            assert(max(y1, d) <= min(y2, u));
            D.push_back({max(y1, d), min(y2, u), i * 2 + cnt});
            cnt++;
            bm |= 8;
        }
        // bug(bm);
        if (cnt == 1) {
            bug(i);
            must.push_back(i * 2);
            edges.push_back({i * 2 + 1, i * 2});
        } else assert(cnt == 2);
    }
    vector<pi> ban;
    auto process = [&](const vector<ar<int, 3>> v) {

        auto one = v, two = v;
        sort(all(one), [&](auto x, auto y) {
            return x[1] < y[1];
        });
        sort(all(two), [&](auto x, auto y) {
            return x[0] > y[0];
        });
        vi f(sz(v)), g(sz(v));
        for (int i = 0; i < sz(v); i++) {
            if (i == 0) {
                f[i] = one[i][2] ^ 1;
                g[i] = two[i][2] ^ 1;
            } else {
                f[i] = N * 2 + (n++);
                g[i] = N * 2 + (n++);
                edges.push_back({f[i], one[i][2] ^ 1});
                edges.push_back({f[i], f[i - 1]});
                edges.push_back({g[i], two[i][2] ^ 1});
                edges.push_back({g[i], g[i - 1]});
            }
        }
        for (auto [l, r, i] : v) {
            if (one[0][1] < l) {
                int low = 0, hi = sz(one) - 1;
                while (low < hi) {
                    int m = (low + hi) / 2 + 1;
                    one[m][1] < l ? low = m : hi = m - 1;
                }
                edges.push_back({i, f[low]});
            }
            if (two[0][0] > r) {
                int low = 0, hi = sz(two) - 1;
                while (low < hi) {
                    int m = (low + hi) / 2 + 1;
                    two[m][0] > r ? low = m : hi = m - 1;
                }
                edges.push_back({i, g[low]});
            }
        }
    };
    process(A);
    process(B);
    process(C);
    process(D);
    SCC S(N * 2 + n);
    for (auto [i, j] : edges) S.make_edge(i, j);
    S.solve();
    for (int i = 0; i < N; i++) {
        if (S.id[i * 2] == S.id[i * 2 + 1])
            bug(i, S.id[i * 2], S.id[i * 2 + 1]);
        assert(S.id[i * 2] != S.id[i * 2 + 1]);
    }
    vi what(N * 2 + n, -1);
    for (int i : S.comps) if (what[i] == -1) {
        what[i] = 1;
        what[S.id[i ^ 1]] = 0;
    }
    for (int i : must) {
        assert(what[S.id[i]]);
        assert(!what[S.id[i ^ 1]]);
    }
    for (auto [i, j] : ban) assert(!what[S.id[i]] || !what[S.id[j]]);
    auto go = [&](const vector<ar<int, 3>> v, int x1, int x2) {
        for (auto [l, r, i] : v) if (what.at(S.id[i])) x1 = max(x1, l), x2 = min(x2, r);
        bug(x1, x2);
        assert(x1 <= x2);
        return x1;
    };
    solutions.push_back({go(A, x1, x2), y1});
    solutions.push_back({go(B, x1, x2), y2});
    solutions.push_back({x1, go(C, y1, y2)});
    solutions.push_back({x2, go(D, y1, y2)});
    pr();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 500 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 7 ms 1260 KB Output is correct
15 Correct 5 ms 348 KB Output is correct
16 Correct 4 ms 344 KB Output is correct
17 Correct 9 ms 1372 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 9 ms 1624 KB Output is correct
21 Correct 7 ms 856 KB Output is correct
22 Correct 5 ms 348 KB Output is correct
23 Correct 8 ms 1884 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 3 ms 348 KB Output is correct
26 Correct 3 ms 580 KB Output is correct
27 Correct 4 ms 348 KB Output is correct
28 Correct 3 ms 444 KB Output is correct
29 Correct 3 ms 348 KB Output is correct
30 Correct 3 ms 472 KB Output is correct
31 Correct 8 ms 1364 KB Output is correct
32 Correct 7 ms 1356 KB Output is correct
33 Correct 8 ms 1364 KB Output is correct
34 Correct 7 ms 1416 KB Output is correct
35 Correct 9 ms 1852 KB Output is correct
36 Correct 8 ms 1372 KB Output is correct
37 Correct 9 ms 1884 KB Output is correct
38 Correct 7 ms 1744 KB Output is correct
39 Correct 8 ms 1488 KB Output is correct
40 Correct 8 ms 1372 KB Output is correct
41 Correct 8 ms 1492 KB Output is correct
42 Correct 8 ms 1628 KB Output is correct
43 Correct 9 ms 1628 KB Output is correct
44 Correct 9 ms 1840 KB Output is correct
45 Correct 4 ms 344 KB Output is correct
46 Correct 9 ms 1628 KB Output is correct
47 Correct 8 ms 1624 KB Output is correct
48 Correct 8 ms 1880 KB Output is correct
49 Correct 9 ms 1624 KB Output is correct
50 Correct 8 ms 1604 KB Output is correct
51 Correct 9 ms 1884 KB Output is correct
52 Correct 7 ms 1368 KB Output is correct
53 Correct 8 ms 1540 KB Output is correct
54 Correct 10 ms 1884 KB Output is correct
55 Correct 7 ms 1604 KB Output is correct
56 Correct 7 ms 1628 KB Output is correct
57 Correct 7 ms 1372 KB Output is correct
58 Correct 7 ms 1604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 59 ms 4724 KB Output is correct
6 Correct 59 ms 4752 KB Output is correct
7 Correct 59 ms 4980 KB Output is correct
8 Correct 60 ms 4728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 62 ms 4820 KB Output is correct
6 Correct 69 ms 4916 KB Output is correct
7 Correct 72 ms 4728 KB Output is correct
8 Correct 73 ms 4660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 64 ms 4564 KB Output is correct
14 Correct 70 ms 4472 KB Output is correct
15 Correct 75 ms 4564 KB Output is correct
16 Correct 77 ms 4564 KB Output is correct
17 Correct 69 ms 4816 KB Output is correct
18 Correct 62 ms 4560 KB Output is correct
19 Correct 87 ms 4564 KB Output is correct
20 Correct 81 ms 4472 KB Output is correct
21 Correct 180 ms 4848 KB Output is correct
22 Correct 137 ms 4652 KB Output is correct
23 Correct 108 ms 4768 KB Output is correct
24 Correct 109 ms 4772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 500 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 7 ms 1260 KB Output is correct
15 Correct 5 ms 348 KB Output is correct
16 Correct 4 ms 344 KB Output is correct
17 Correct 9 ms 1372 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 9 ms 1624 KB Output is correct
21 Correct 7 ms 856 KB Output is correct
22 Correct 5 ms 348 KB Output is correct
23 Correct 8 ms 1884 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 3 ms 348 KB Output is correct
26 Correct 3 ms 580 KB Output is correct
27 Correct 4 ms 348 KB Output is correct
28 Correct 3 ms 444 KB Output is correct
29 Correct 3 ms 348 KB Output is correct
30 Correct 3 ms 472 KB Output is correct
31 Correct 8 ms 1364 KB Output is correct
32 Correct 7 ms 1356 KB Output is correct
33 Correct 8 ms 1364 KB Output is correct
34 Correct 7 ms 1416 KB Output is correct
35 Correct 9 ms 1852 KB Output is correct
36 Correct 8 ms 1372 KB Output is correct
37 Correct 9 ms 1884 KB Output is correct
38 Correct 7 ms 1744 KB Output is correct
39 Correct 8 ms 1488 KB Output is correct
40 Correct 8 ms 1372 KB Output is correct
41 Correct 8 ms 1492 KB Output is correct
42 Correct 8 ms 1628 KB Output is correct
43 Correct 9 ms 1628 KB Output is correct
44 Correct 9 ms 1840 KB Output is correct
45 Correct 4 ms 344 KB Output is correct
46 Correct 9 ms 1628 KB Output is correct
47 Correct 8 ms 1624 KB Output is correct
48 Correct 8 ms 1880 KB Output is correct
49 Correct 9 ms 1624 KB Output is correct
50 Correct 8 ms 1604 KB Output is correct
51 Correct 9 ms 1884 KB Output is correct
52 Correct 7 ms 1368 KB Output is correct
53 Correct 8 ms 1540 KB Output is correct
54 Correct 10 ms 1884 KB Output is correct
55 Correct 7 ms 1604 KB Output is correct
56 Correct 7 ms 1628 KB Output is correct
57 Correct 7 ms 1372 KB Output is correct
58 Correct 7 ms 1604 KB Output is correct
59 Correct 69 ms 12260 KB Output is correct
60 Correct 69 ms 12268 KB Output is correct
61 Correct 68 ms 12264 KB Output is correct
62 Correct 70 ms 12392 KB Output is correct
63 Correct 68 ms 12276 KB Output is correct
64 Correct 65 ms 12396 KB Output is correct
65 Correct 68 ms 12388 KB Output is correct
66 Correct 72 ms 12268 KB Output is correct
67 Correct 190 ms 12336 KB Output is correct
68 Correct 136 ms 12624 KB Output is correct
69 Correct 96 ms 12336 KB Output is correct
70 Correct 118 ms 12180 KB Output is correct
71 Correct 641 ms 12420 KB Output is correct
72 Correct 1032 ms 142700 KB Output is correct
73 Correct 575 ms 12760 KB Output is correct
74 Correct 372 ms 12344 KB Output is correct
75 Correct 775 ms 100100 KB Output is correct
76 Correct 392 ms 12664 KB Output is correct
77 Correct 533 ms 12736 KB Output is correct
78 Correct 1012 ms 146280 KB Output is correct
79 Correct 580 ms 12724 KB Output is correct
80 Correct 477 ms 12732 KB Output is correct
81 Correct 1026 ms 142456 KB Output is correct
82 Correct 424 ms 12512 KB Output is correct
83 Correct 218 ms 12500 KB Output is correct
84 Correct 218 ms 12272 KB Output is correct
85 Correct 308 ms 12684 KB Output is correct
86 Correct 280 ms 12468 KB Output is correct
87 Correct 244 ms 12392 KB Output is correct
88 Correct 270 ms 12424 KB Output is correct
89 Correct 946 ms 121576 KB Output is correct
90 Correct 1006 ms 145036 KB Output is correct
91 Correct 903 ms 114168 KB Output is correct
92 Correct 1036 ms 166536 KB Output is correct
93 Correct 1020 ms 149348 KB Output is correct
94 Correct 1008 ms 138768 KB Output is correct
95 Correct 1020 ms 147012 KB Output is correct
96 Correct 960 ms 141312 KB Output is correct
97 Correct 1016 ms 149476 KB Output is correct
98 Correct 1025 ms 136540 KB Output is correct
99 Correct 872 ms 121956 KB Output is correct
100 Correct 1086 ms 149468 KB Output is correct
101 Correct 1012 ms 148240 KB Output is correct
102 Correct 738 ms 89628 KB Output is correct
103 Correct 1036 ms 151056 KB Output is correct
104 Correct 848 ms 111864 KB Output is correct
105 Correct 1004 ms 156568 KB Output is correct
106 Correct 1037 ms 148944 KB Output is correct
107 Correct 994 ms 137120 KB Output is correct
108 Correct 1046 ms 153692 KB Output is correct
109 Correct 1012 ms 149248 KB Output is correct
110 Correct 1033 ms 145368 KB Output is correct
111 Correct 1115 ms 136476 KB Output is correct
112 Correct 1054 ms 154864 KB Output is correct
113 Correct 849 ms 115820 KB Output is correct
114 Correct 825 ms 116508 KB Output is correct
115 Correct 827 ms 115064 KB Output is correct
116 Correct 843 ms 115060 KB Output is correct
117 Correct 1356 ms 188376 KB Output is correct
118 Correct 1345 ms 189508 KB Output is correct
119 Correct 1339 ms 188652 KB Output is correct
120 Correct 1388 ms 188312 KB Output is correct
121 Correct 1361 ms 189612 KB Output is correct
122 Correct 1347 ms 189408 KB Output is correct
123 Correct 1334 ms 189832 KB Output is correct
124 Correct 1417 ms 188912 KB Output is correct
125 Correct 1390 ms 188836 KB Output is correct
126 Correct 1361 ms 188832 KB Output is correct