Submission #886589

# Submission time Handle Problem Language Result Execution time Memory
886589 2023-12-12T10:59:25 Z vjudge1 Hamburg Steak (JOI20_hamburg) C++17
100 / 100
1480 ms 171976 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;
const int inf = 1e9 + 10;

int n, k;
int l[N], r[N], u[N], d[N];

namespace brute {

vector<pair<int, int>> point;
int done[N];

void dfs(int step) {
        int minR = inf, maxL = -1, minU = inf, maxD = -1;
        for (int i = 0; i < n; i++) {
                if (done[i]) continue;
                minR = min(minR, r[i]);
                maxL = max(maxL, l[i]);
                minU = min(minU, u[i]);
                maxD = max(maxD, d[i]);
        }

        if (maxL == -1) {
                for (auto [x, y] : point) cout << x << ' ' << y << '\n';
                for (int i = point.size(); i < k; i++) cout << "1 1\n";
                exit(0);
        }

        if (step == k) return;

        for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                        int x = i ? minR : maxL;
                        int y = j ? minU : maxD;
                        point.emplace_back(x, y);
                        vector<int> del;
                        for (int p = 0; p < n; p++) {
                                if (done[p]) continue;
                                if (l[p] <= x && x <= r[p] && d[p] <= y && y <= u[p]) {
                                        done[p] = 1;
                                        del.emplace_back(p);
                                }
                        }
                        dfs(step + 1);
                        for (int p : del) done[p] = 0;
                        point.pop_back();
                }
        }
}

};  // namespace brute

namespace two_sat {

int timer = 0;
int id[N][2];
const int C = 6;
vector<int> G[N * C];
vector<int> R[N * C];
int to[N], from[N];
int good[N];
int mask[N];
int LL, RR, DD, UU;

void init() {
        int minR = inf, maxL = -1, minU = inf, maxD = -1;
        for (int i = 0; i < n; i++) {
                minR = min(minR, r[i]);
                maxL = max(maxL, l[i]);
                minU = min(minU, u[i]);
                maxD = max(maxD, d[i]);
        }

        assert(minR <= maxL && minU <= maxD);

        LL = minR, RR = maxL, UU = maxD, DD = minU;

        vector<vector<pair<int, int>>> ranges(4);

        for (int i = 0; i < n; i++) {
                if (l[i] <= LL && LL <= r[i]) mask[i] ^= 1;
                if (l[i] <= RR && RR <= r[i]) mask[i] ^= 2;
                if (d[i] <= DD && DD <= u[i]) mask[i] ^= 4;
                if (d[i] <= UU && UU <= u[i]) mask[i] ^= 8;

                if (__builtin_popcount(mask[i]) > 2) continue;
                good[i] = 1;

                id[i][0] = timer++, id[i][1] = timer++;  // 2
                                                         // 0: must in that range, 1: vice versa

                if (__builtin_popcount(mask[i]) == 1) {
                        G[id[i][1]].emplace_back(id[i][0]);  // must in that range
                }
                int p = 0;
                for (int j = 0; j < 4; j++) {
                        if (mask[i] >> j & 1) ranges[j].emplace_back(i, p++);
                }
                assert(p > 0);
        }

        auto solve = [&](vector<pair<int, int>>& ord, int x) {
                auto& a = x < 2 ? d : l;
                auto& b = x < 2 ? u : r;

                sort(ord.begin(), ord.end(), [&](auto x, auto y) {
                        return b[x.first] < b[y.first];
                });

                int m = ord.size();
                for (int i = 0; i < m; i++) {
                        to[i] = timer++, from[i] = timer++;  // 2
                        int p = ord[i].second;
                        G[to[i]].emplace_back(id[ord[i].first][p] ^ 1);
                        G[id[ord[i].first][p]].emplace_back(from[i]);
                        if (i > 0) {
                                G[to[i]].emplace_back(to[i - 1]);
                                G[from[i - 1]].emplace_back(from[i]);
                        }
                }

                vector<int> sorted_b(m);
                for (int i = 0; i < m; i++) sorted_b[i] = b[ord[i].first];

                for (auto [i, p] : ord) {
                        int j = lower_bound(sorted_b.begin(), sorted_b.end(), a[i]) - sorted_b.begin() - 1;
                        if (j >= 0) {
                                G[id[i][p]].emplace_back(to[j]);
                                G[from[j]].emplace_back(id[i][p] ^ 1);
                        }
                }
        };
        for (int i = 0; i < 4; i++) solve(ranges[i], i);

        assert(timer < N * C);
}

vector<int> topo;
int done[N * C];
int comp[N * C];

void dfs(int u) {
        done[u] = 1;
        for (int v : G[u]) {
                if (!done[v]) dfs(v);
        }
        topo.emplace_back(u);
}

void scc(int u, int id) {
        done[u] = 0;
        comp[u] = id;
        for (int v : R[u]) {
                if (done[v]) scc(v, id);
        }
}

void solve() {
        for (int i = 0; i < timer; i++) {
                for (int j : G[i]) R[j].emplace_back(i);
        }

        for (int i = 0; i < timer; i++) {
                if (!done[i]) dfs(i);
        }

        reverse(topo.begin(), topo.end());

        int P = 0;
        for (int i : topo) {
                if (done[i]) scc(i, P++);
        }

        vector<pair<int, int>> point(4);
        point[0].first = LL, point[1].first = RR;
        point[2].second = DD, point[3].second = UU;

        for (int i = 0; i < n; i++) {
                if (!good[i]) continue;
                int which = comp[id[i][0]] < comp[id[i][1]];
                int j = 0, p = 0;
                for (j = 0; j < 4; j++) {
                        if (mask[i] >> j & 1) {
                                if (p++ == which) break;
                        }
                }
                auto& x = (j < 2) ? (j == 0 ? point[0].second : point[1].second)
                                  : (j == 2 ? point[2].first : point[3].first);
                auto& a = j < 2 ? d : l;

                x = max(x, a[i]);
        }

        for (int i = 0; i < 4; i++) {
                auto [x, y] = point[i];
                cout << x << ' ' << y << '\n';
        }
}
};  // namespace two_sat

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        cin >> n >> k;
        for (int i = 0; i < n; i++) cin >> l[i] >> d[i] >> r[i] >> u[i];

        brute::dfs(0);

        two_sat::init();
        two_sat::solve();
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 64092 KB Output is correct
2 Correct 15 ms 64092 KB Output is correct
3 Correct 16 ms 64348 KB Output is correct
4 Correct 14 ms 64092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 64092 KB Output is correct
2 Correct 15 ms 64092 KB Output is correct
3 Correct 14 ms 63992 KB Output is correct
4 Correct 15 ms 63964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 64092 KB Output is correct
2 Correct 14 ms 64092 KB Output is correct
3 Correct 14 ms 64092 KB Output is correct
4 Correct 14 ms 64348 KB Output is correct
5 Correct 14 ms 64092 KB Output is correct
6 Correct 14 ms 63996 KB Output is correct
7 Correct 14 ms 64092 KB Output is correct
8 Correct 15 ms 64092 KB Output is correct
9 Correct 14 ms 64088 KB Output is correct
10 Correct 14 ms 64092 KB Output is correct
11 Correct 15 ms 64092 KB Output is correct
12 Correct 15 ms 64092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 64092 KB Output is correct
2 Correct 14 ms 64092 KB Output is correct
3 Correct 14 ms 64080 KB Output is correct
4 Correct 14 ms 64092 KB Output is correct
5 Correct 14 ms 64092 KB Output is correct
6 Correct 14 ms 64092 KB Output is correct
7 Correct 14 ms 64156 KB Output is correct
8 Correct 19 ms 64092 KB Output is correct
9 Correct 15 ms 64092 KB Output is correct
10 Correct 18 ms 64092 KB Output is correct
11 Correct 19 ms 64092 KB Output is correct
12 Correct 17 ms 64092 KB Output is correct
13 Correct 15 ms 64092 KB Output is correct
14 Correct 21 ms 66652 KB Output is correct
15 Correct 14 ms 64088 KB Output is correct
16 Correct 15 ms 64092 KB Output is correct
17 Correct 21 ms 66648 KB Output is correct
18 Correct 15 ms 64088 KB Output is correct
19 Correct 14 ms 64092 KB Output is correct
20 Correct 23 ms 66640 KB Output is correct
21 Correct 14 ms 64084 KB Output is correct
22 Correct 16 ms 64092 KB Output is correct
23 Correct 23 ms 66908 KB Output is correct
24 Correct 16 ms 64092 KB Output is correct
25 Correct 17 ms 64004 KB Output is correct
26 Correct 18 ms 64036 KB Output is correct
27 Correct 18 ms 64092 KB Output is correct
28 Correct 16 ms 64328 KB Output is correct
29 Correct 17 ms 64092 KB Output is correct
30 Correct 18 ms 64344 KB Output is correct
31 Correct 22 ms 66572 KB Output is correct
32 Correct 20 ms 66396 KB Output is correct
33 Correct 22 ms 66644 KB Output is correct
34 Correct 21 ms 66396 KB Output is correct
35 Correct 22 ms 66936 KB Output is correct
36 Correct 22 ms 66396 KB Output is correct
37 Correct 23 ms 66896 KB Output is correct
38 Correct 22 ms 66900 KB Output is correct
39 Correct 22 ms 66652 KB Output is correct
40 Correct 21 ms 66640 KB Output is correct
41 Correct 23 ms 66652 KB Output is correct
42 Correct 23 ms 66652 KB Output is correct
43 Correct 23 ms 66640 KB Output is correct
44 Correct 22 ms 66892 KB Output is correct
45 Correct 17 ms 64092 KB Output is correct
46 Correct 23 ms 66640 KB Output is correct
47 Correct 22 ms 66652 KB Output is correct
48 Correct 22 ms 66904 KB Output is correct
49 Correct 23 ms 66652 KB Output is correct
50 Correct 22 ms 66652 KB Output is correct
51 Correct 23 ms 66904 KB Output is correct
52 Correct 21 ms 66396 KB Output is correct
53 Correct 22 ms 66792 KB Output is correct
54 Correct 22 ms 66712 KB Output is correct
55 Correct 21 ms 66764 KB Output is correct
56 Correct 20 ms 66652 KB Output is correct
57 Correct 20 ms 66640 KB Output is correct
58 Correct 20 ms 66652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 64092 KB Output is correct
2 Correct 15 ms 64092 KB Output is correct
3 Correct 16 ms 64348 KB Output is correct
4 Correct 14 ms 64092 KB Output is correct
5 Correct 78 ms 73172 KB Output is correct
6 Correct 84 ms 73156 KB Output is correct
7 Correct 77 ms 73124 KB Output is correct
8 Correct 77 ms 73256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 64092 KB Output is correct
2 Correct 15 ms 64092 KB Output is correct
3 Correct 14 ms 63992 KB Output is correct
4 Correct 15 ms 63964 KB Output is correct
5 Correct 84 ms 73428 KB Output is correct
6 Correct 89 ms 73420 KB Output is correct
7 Correct 80 ms 73280 KB Output is correct
8 Correct 92 ms 73208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 64092 KB Output is correct
2 Correct 14 ms 64092 KB Output is correct
3 Correct 14 ms 64092 KB Output is correct
4 Correct 14 ms 64348 KB Output is correct
5 Correct 14 ms 64092 KB Output is correct
6 Correct 14 ms 63996 KB Output is correct
7 Correct 14 ms 64092 KB Output is correct
8 Correct 15 ms 64092 KB Output is correct
9 Correct 14 ms 64088 KB Output is correct
10 Correct 14 ms 64092 KB Output is correct
11 Correct 15 ms 64092 KB Output is correct
12 Correct 15 ms 64092 KB Output is correct
13 Correct 82 ms 73100 KB Output is correct
14 Correct 93 ms 73120 KB Output is correct
15 Correct 83 ms 73392 KB Output is correct
16 Correct 80 ms 73172 KB Output is correct
17 Correct 80 ms 73172 KB Output is correct
18 Correct 85 ms 73228 KB Output is correct
19 Correct 79 ms 72916 KB Output is correct
20 Correct 236 ms 73140 KB Output is correct
21 Correct 94 ms 73188 KB Output is correct
22 Correct 124 ms 73232 KB Output is correct
23 Correct 174 ms 73196 KB Output is correct
24 Correct 135 ms 73124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 64092 KB Output is correct
2 Correct 14 ms 64092 KB Output is correct
3 Correct 14 ms 64080 KB Output is correct
4 Correct 14 ms 64092 KB Output is correct
5 Correct 14 ms 64092 KB Output is correct
6 Correct 14 ms 64092 KB Output is correct
7 Correct 14 ms 64156 KB Output is correct
8 Correct 19 ms 64092 KB Output is correct
9 Correct 15 ms 64092 KB Output is correct
10 Correct 18 ms 64092 KB Output is correct
11 Correct 19 ms 64092 KB Output is correct
12 Correct 17 ms 64092 KB Output is correct
13 Correct 15 ms 64092 KB Output is correct
14 Correct 21 ms 66652 KB Output is correct
15 Correct 14 ms 64088 KB Output is correct
16 Correct 15 ms 64092 KB Output is correct
17 Correct 21 ms 66648 KB Output is correct
18 Correct 15 ms 64088 KB Output is correct
19 Correct 14 ms 64092 KB Output is correct
20 Correct 23 ms 66640 KB Output is correct
21 Correct 14 ms 64084 KB Output is correct
22 Correct 16 ms 64092 KB Output is correct
23 Correct 23 ms 66908 KB Output is correct
24 Correct 16 ms 64092 KB Output is correct
25 Correct 17 ms 64004 KB Output is correct
26 Correct 18 ms 64036 KB Output is correct
27 Correct 18 ms 64092 KB Output is correct
28 Correct 16 ms 64328 KB Output is correct
29 Correct 17 ms 64092 KB Output is correct
30 Correct 18 ms 64344 KB Output is correct
31 Correct 22 ms 66572 KB Output is correct
32 Correct 20 ms 66396 KB Output is correct
33 Correct 22 ms 66644 KB Output is correct
34 Correct 21 ms 66396 KB Output is correct
35 Correct 22 ms 66936 KB Output is correct
36 Correct 22 ms 66396 KB Output is correct
37 Correct 23 ms 66896 KB Output is correct
38 Correct 22 ms 66900 KB Output is correct
39 Correct 22 ms 66652 KB Output is correct
40 Correct 21 ms 66640 KB Output is correct
41 Correct 23 ms 66652 KB Output is correct
42 Correct 23 ms 66652 KB Output is correct
43 Correct 23 ms 66640 KB Output is correct
44 Correct 22 ms 66892 KB Output is correct
45 Correct 17 ms 64092 KB Output is correct
46 Correct 23 ms 66640 KB Output is correct
47 Correct 22 ms 66652 KB Output is correct
48 Correct 22 ms 66904 KB Output is correct
49 Correct 23 ms 66652 KB Output is correct
50 Correct 22 ms 66652 KB Output is correct
51 Correct 23 ms 66904 KB Output is correct
52 Correct 21 ms 66396 KB Output is correct
53 Correct 22 ms 66792 KB Output is correct
54 Correct 22 ms 66712 KB Output is correct
55 Correct 21 ms 66764 KB Output is correct
56 Correct 20 ms 66652 KB Output is correct
57 Correct 20 ms 66640 KB Output is correct
58 Correct 20 ms 66652 KB Output is correct
59 Correct 80 ms 73040 KB Output is correct
60 Correct 87 ms 73388 KB Output is correct
61 Correct 82 ms 73004 KB Output is correct
62 Correct 84 ms 73352 KB Output is correct
63 Correct 83 ms 73000 KB Output is correct
64 Correct 79 ms 73280 KB Output is correct
65 Correct 84 ms 73040 KB Output is correct
66 Correct 662 ms 73500 KB Output is correct
67 Correct 216 ms 73248 KB Output is correct
68 Correct 709 ms 73104 KB Output is correct
69 Correct 681 ms 73684 KB Output is correct
70 Correct 688 ms 73268 KB Output is correct
71 Correct 90 ms 73196 KB Output is correct
72 Correct 1316 ms 142220 KB Output is correct
73 Correct 127 ms 73344 KB Output is correct
74 Correct 270 ms 73068 KB Output is correct
75 Correct 878 ms 114544 KB Output is correct
76 Correct 392 ms 73340 KB Output is correct
77 Correct 131 ms 73168 KB Output is correct
78 Correct 1312 ms 147816 KB Output is correct
79 Correct 131 ms 73684 KB Output is correct
80 Correct 280 ms 73208 KB Output is correct
81 Correct 1277 ms 142844 KB Output is correct
82 Correct 305 ms 73472 KB Output is correct
83 Correct 449 ms 73324 KB Output is correct
84 Correct 560 ms 73328 KB Output is correct
85 Correct 423 ms 73416 KB Output is correct
86 Correct 298 ms 73408 KB Output is correct
87 Correct 539 ms 73080 KB Output is correct
88 Correct 343 ms 73320 KB Output is correct
89 Correct 1079 ms 128352 KB Output is correct
90 Correct 1287 ms 147284 KB Output is correct
91 Correct 1063 ms 125580 KB Output is correct
92 Correct 1286 ms 155496 KB Output is correct
93 Correct 1248 ms 147952 KB Output is correct
94 Correct 1282 ms 139048 KB Output is correct
95 Correct 1286 ms 145664 KB Output is correct
96 Correct 1105 ms 145856 KB Output is correct
97 Correct 1242 ms 147916 KB Output is correct
98 Correct 1265 ms 138728 KB Output is correct
99 Correct 1008 ms 131264 KB Output is correct
100 Correct 1293 ms 147652 KB Output is correct
101 Correct 1331 ms 146316 KB Output is correct
102 Correct 831 ms 107448 KB Output is correct
103 Correct 1334 ms 150756 KB Output is correct
104 Correct 962 ms 121828 KB Output is correct
105 Correct 1286 ms 154124 KB Output is correct
106 Correct 1334 ms 146780 KB Output is correct
107 Correct 1201 ms 139284 KB Output is correct
108 Correct 1324 ms 148796 KB Output is correct
109 Correct 1279 ms 147752 KB Output is correct
110 Correct 1243 ms 146236 KB Output is correct
111 Correct 1315 ms 138648 KB Output is correct
112 Correct 1321 ms 150200 KB Output is correct
113 Correct 855 ms 128748 KB Output is correct
114 Correct 850 ms 130032 KB Output is correct
115 Correct 852 ms 130120 KB Output is correct
116 Correct 854 ms 129900 KB Output is correct
117 Correct 1459 ms 170072 KB Output is correct
118 Correct 1470 ms 170428 KB Output is correct
119 Correct 1464 ms 170192 KB Output is correct
120 Correct 1466 ms 171460 KB Output is correct
121 Correct 1462 ms 171692 KB Output is correct
122 Correct 1466 ms 171064 KB Output is correct
123 Correct 1460 ms 170560 KB Output is correct
124 Correct 1464 ms 171344 KB Output is correct
125 Correct 1466 ms 170288 KB Output is correct
126 Correct 1480 ms 171976 KB Output is correct