Submission #648565

# Submission time Handle Problem Language Result Execution time Memory
648565 2022-10-07T03:20:40 Z 406 Hamburg Steak (JOI20_hamburg) C++17
100 / 100
2116 ms 422004 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 50;
const int M = 2 * N + 5;
int l[N], r[N], u[N], d[N], n, k;
bitset<N> used, full;
vector<pair<int, int>> points;
vector<int> X, Y;
int cnt;

void rec();

inline bool inter(int i, int x, int y) {
        return l[i] <= x && x <= r[i] && d[i] <= y && y <= u[i];
}

inline void add_point(int x, int y) {
        vector<int> change;
        for (int i = used._Find_first(); i < n; i = used._Find_next(i)) {
                if (inter(i, x, y)) {
                        cnt--;
                        used[i] = false;
                        change.push_back(i);
                }
        }
        if (change.empty())
                return;
        points.emplace_back(x, y);

        rec();

        for (auto cc: change) used[cc] = true, cnt++;
        points.pop_back();

}
const int V = 2 * 4 * M;
vector<int> adj[V], adj_t[V];
bitset<V> mark, ass;
vector<int> order;
int comp[V];

void dfs1(int v) {
        mark[v] = true;
        for (auto u: adj[v]) if (!mark[u])
                dfs1(u);
        order.push_back(v);
}

void dfs2(int v, int cl) {
        comp[v] = cl;
        for (auto u: adj_t[v]) {
                if (comp[u] == -1)
                        dfs2(u, cl);
        }
}
void solve_2SAT() {
        mark = 0;
        for (int i = 0; i < V; i++) if (!mark[i])
                dfs1(i);
        fill(comp, comp + V, -1);

        assert(order.size() == V);

        for (int i = 0, j = 0; i < V; i++) {
                int v = order[V - i - 1];
                if (comp[v] == -1)
                        dfs2(v, j++);
        }

        for (int i = 0; i < V; i += 2)
                ass[i / 2] = comp[i] > comp[i ^ 1];
}

void add_disjunction(int a, bool na, int b, bool nb) {
        a = (2 * a) ^ na;
        b = (2 * b) ^ nb;
        int neg_a = a ^ 1;
        int neg_b = b ^ 1;
        adj[neg_a].push_back(b);
        adj[neg_b].push_back(a);

        adj_t[b].push_back(neg_a);
        adj_t[a].push_back(neg_b);
}
void add_two(int a, int b, int c, int d) {
        assert(a > 0 && c > 0);
        a--, c--;
        add_disjunction(a, 1, c, 1);
        add_disjunction(a, 1, d, 0);
        add_disjunction(b, 0, c, 1);
        add_disjunction(b, 0, d, 0);
}

void rec() {
        if (!cnt && points.size() <= k) {
                for (int i = 0; i < (int) points.size(); i++)
                        cout << X[points[i].first] << ' ' << Y[points[i].second] << '\n';
                for (int i = points.size(); i < k; i++)
                        cout << 1 << ' ' << 1 << '\n';
                exit(0);
        }
        if (points.size() >= k)
                return;

        int minR = 2 * n;
        int minU = 2 * n;
        int maxD = 0;
        int maxL = 0;

        for (int i = used._Find_first(); i < n; i = used._Find_next(i)) {
                minR = min(minR, r[i]);
                maxL = max(maxL, l[i]);

                minU = min(minU, u[i]);
                maxD = max(maxD, d[i]);
        }

        if (maxL <= minR || maxD <= minU) {
                add_point(minR, minU);
        }
        else {
                add_point(minR, minU);
                add_point(minR, maxD);
                add_point(maxL, minU);
                add_point(maxL, maxD);
                if (points.size())
                        return;

                for (int i = 0; i < n; i++) {
                        l[i] = max(l[i], minR);
                        r[i] = min(r[i], maxL);
                        d[i] = max(d[i], minU);
                        u[i] = min(u[i], maxD);
                        int q = (l[i] == minR) + (r[i] == maxL) + (d[i] == minU) + (u[i] == maxD);
                        if (q >= 3)
                                continue;
                        assert(q);

                        if (q == 1) {
                                if (l[i] == minR)
                                        add_disjunction(d[i] - 1, 1, d[i] - 1, 1),
                                        add_disjunction(u[i], 0, u[i], 0);
                                else if (r[i] == maxL)
                                        add_disjunction(2 * M + d[i] - 1, 1, 2 * M + d[i] - 1, 1),
                                        add_disjunction(2 * M + u[i], 0, 2 * M + u[i], 0);
                                else if (d[i] == minU)
                                        add_disjunction(3 * M + l[i] - 1, 1, 3 * M + l[i] - 1, 1), 
                                        add_disjunction(3 * M + r[i], 0, 3 * M + r[i], 0);
                                else  //u[i] == maxD
                                        add_disjunction(M + l[i] - 1, 1, M + l[i] - 1, 1),
                                        add_disjunction(M + r[i], 0, M + r[i], 0);
                        }
                        else {
                                if (l[i] == minR && r[i] == maxL)
                                        add_two(d[i], u[i], 2 * M + d[i], 2 * M + u[i]);
                                else if (l[i] == minR && d[i] == minU)
                                        add_two(d[i], u[i], 3 * M + l[i], 3 * M + r[i]);
                                else if (l[i] == minR && u[i] == maxD)
                                        add_two(d[i], u[i], M + l[i], M + r[i]);
                                else if (r[i] == maxL && d[i] == minU)
                                        add_two(2 * M + d[i], 2 * M + u[i], 3 * M + l[i], 3 * M + r[i]);
                                else if (r[i] == maxL && u[i] == maxD)
                                        add_two(2 * M + d[i], 2 * M + u[i], M + l[i], M + r[i]);
                                else if (d[i] == minU && u[i] == maxD) 
                                        add_two(3 * M + l[i], 3 * M + r[i], M + l[i], M + r[i]);
                        }
                }
                //init edges
                for (int j = 0; j < 4; j++)
                        for (int i = j * M; i < (j + 1) * M - 1; i++)
                                add_disjunction(i, true, i + 1, false);
                solve_2SAT();
                //cout << "SOLVED 2SAT\n";
                //exit(0);

                for (int i = 0; i < M; i++)
                        if (ass[i]) {
                                add_point(minR, i);
                                break;
                        }
                cout << "IMPOSSIBLE\n";
                exit(0);
        }
}


signed main() {
        ios::sync_with_stdio(0);
        cin.tie(0);

        cin >> n >> k;
        X.reserve(2 * n), Y.reserve(2 * n);
        X.push_back(-1), Y.push_back(-1);
        for (int i = 0; i < n; i++) {
                cin >> l[i] >> d[i] >> r[i] >> u[i];
                X.push_back(l[i]);
                X.push_back(r[i]);
                Y.push_back(d[i]);
                Y.push_back(u[i]);
        }
        sort(X.begin(), X.end());
        X.resize(unique(X.begin(), X.end()) - X.begin());

        sort(Y.begin(), Y.end());
        Y.resize(unique(Y.begin(), Y.end()) - Y.begin());

        for (int i = 0; i < n; i++) {
                l[i] = lower_bound(X.begin(), X.end(), l[i]) - X.begin();
                r[i] = lower_bound(X.begin(), X.end(), r[i]) - X.begin();

                u[i] = lower_bound(Y.begin(), Y.end(), u[i]) - Y.begin();
                d[i] = lower_bound(Y.begin(), Y.end(), d[i]) - Y.begin();
        }

        for (int i = 0; i < n; i++)
                used[i] = true;
        cnt = n;

        rec();
        cout << "NO I DID NOT FIND ANYTHING\n";
        return 0;
}

Compilation message

hamburg.cpp: In function 'void rec()':
hamburg.cpp:96:35: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   96 |         if (!cnt && points.size() <= k) {
      |                     ~~~~~~~~~~~~~~^~~~
hamburg.cpp:103:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  103 |         if (points.size() >= k)
      |             ~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 73 ms 150948 KB Output is correct
2 Correct 76 ms 150816 KB Output is correct
3 Correct 72 ms 150756 KB Output is correct
4 Correct 73 ms 150732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 150848 KB Output is correct
2 Correct 87 ms 150816 KB Output is correct
3 Correct 74 ms 150736 KB Output is correct
4 Correct 74 ms 150736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 150752 KB Output is correct
2 Correct 81 ms 150760 KB Output is correct
3 Correct 75 ms 150840 KB Output is correct
4 Correct 86 ms 150804 KB Output is correct
5 Correct 74 ms 150732 KB Output is correct
6 Correct 74 ms 150756 KB Output is correct
7 Correct 80 ms 150808 KB Output is correct
8 Correct 77 ms 150760 KB Output is correct
9 Correct 75 ms 150728 KB Output is correct
10 Correct 75 ms 150816 KB Output is correct
11 Correct 81 ms 150764 KB Output is correct
12 Correct 77 ms 150744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 150744 KB Output is correct
2 Correct 75 ms 150732 KB Output is correct
3 Correct 76 ms 150768 KB Output is correct
4 Correct 78 ms 150840 KB Output is correct
5 Correct 79 ms 150780 KB Output is correct
6 Correct 75 ms 150780 KB Output is correct
7 Correct 87 ms 150852 KB Output is correct
8 Correct 77 ms 150808 KB Output is correct
9 Correct 75 ms 150740 KB Output is correct
10 Correct 79 ms 150768 KB Output is correct
11 Correct 76 ms 150828 KB Output is correct
12 Correct 81 ms 150852 KB Output is correct
13 Correct 75 ms 150816 KB Output is correct
14 Correct 512 ms 402516 KB Output is correct
15 Correct 87 ms 150860 KB Output is correct
16 Correct 75 ms 150736 KB Output is correct
17 Correct 499 ms 402428 KB Output is correct
18 Correct 74 ms 150728 KB Output is correct
19 Correct 75 ms 150764 KB Output is correct
20 Correct 488 ms 402376 KB Output is correct
21 Correct 88 ms 150788 KB Output is correct
22 Correct 78 ms 150860 KB Output is correct
23 Correct 461 ms 402388 KB Output is correct
24 Correct 81 ms 150728 KB Output is correct
25 Correct 79 ms 150832 KB Output is correct
26 Correct 81 ms 150848 KB Output is correct
27 Correct 75 ms 150732 KB Output is correct
28 Correct 74 ms 150796 KB Output is correct
29 Correct 73 ms 150748 KB Output is correct
30 Correct 77 ms 150748 KB Output is correct
31 Correct 478 ms 402492 KB Output is correct
32 Correct 514 ms 402456 KB Output is correct
33 Correct 479 ms 402388 KB Output is correct
34 Correct 560 ms 402400 KB Output is correct
35 Correct 473 ms 402424 KB Output is correct
36 Correct 471 ms 402488 KB Output is correct
37 Correct 510 ms 402476 KB Output is correct
38 Correct 506 ms 402336 KB Output is correct
39 Correct 511 ms 402444 KB Output is correct
40 Correct 482 ms 402372 KB Output is correct
41 Correct 514 ms 402444 KB Output is correct
42 Correct 493 ms 402508 KB Output is correct
43 Correct 499 ms 402484 KB Output is correct
44 Correct 516 ms 402480 KB Output is correct
45 Correct 73 ms 150764 KB Output is correct
46 Correct 514 ms 402448 KB Output is correct
47 Correct 481 ms 402452 KB Output is correct
48 Correct 556 ms 402360 KB Output is correct
49 Correct 554 ms 402380 KB Output is correct
50 Correct 489 ms 402376 KB Output is correct
51 Correct 475 ms 402500 KB Output is correct
52 Correct 500 ms 402528 KB Output is correct
53 Correct 477 ms 402472 KB Output is correct
54 Correct 574 ms 402428 KB Output is correct
55 Correct 527 ms 402448 KB Output is correct
56 Correct 533 ms 402444 KB Output is correct
57 Correct 529 ms 402376 KB Output is correct
58 Correct 541 ms 402468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 150948 KB Output is correct
2 Correct 76 ms 150816 KB Output is correct
3 Correct 72 ms 150756 KB Output is correct
4 Correct 73 ms 150732 KB Output is correct
5 Correct 369 ms 160148 KB Output is correct
6 Correct 385 ms 160688 KB Output is correct
7 Correct 356 ms 160884 KB Output is correct
8 Correct 357 ms 160772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 150848 KB Output is correct
2 Correct 87 ms 150816 KB Output is correct
3 Correct 74 ms 150736 KB Output is correct
4 Correct 74 ms 150736 KB Output is correct
5 Correct 350 ms 159884 KB Output is correct
6 Correct 358 ms 160616 KB Output is correct
7 Correct 361 ms 160588 KB Output is correct
8 Correct 409 ms 160644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 150752 KB Output is correct
2 Correct 81 ms 150760 KB Output is correct
3 Correct 75 ms 150840 KB Output is correct
4 Correct 86 ms 150804 KB Output is correct
5 Correct 74 ms 150732 KB Output is correct
6 Correct 74 ms 150756 KB Output is correct
7 Correct 80 ms 150808 KB Output is correct
8 Correct 77 ms 150760 KB Output is correct
9 Correct 75 ms 150728 KB Output is correct
10 Correct 75 ms 150816 KB Output is correct
11 Correct 81 ms 150764 KB Output is correct
12 Correct 77 ms 150744 KB Output is correct
13 Correct 377 ms 160452 KB Output is correct
14 Correct 377 ms 160524 KB Output is correct
15 Correct 366 ms 160528 KB Output is correct
16 Correct 362 ms 160516 KB Output is correct
17 Correct 359 ms 160500 KB Output is correct
18 Correct 363 ms 160524 KB Output is correct
19 Correct 358 ms 160664 KB Output is correct
20 Correct 364 ms 160456 KB Output is correct
21 Correct 473 ms 160544 KB Output is correct
22 Correct 392 ms 160544 KB Output is correct
23 Correct 398 ms 160592 KB Output is correct
24 Correct 393 ms 160496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 150744 KB Output is correct
2 Correct 75 ms 150732 KB Output is correct
3 Correct 76 ms 150768 KB Output is correct
4 Correct 78 ms 150840 KB Output is correct
5 Correct 79 ms 150780 KB Output is correct
6 Correct 75 ms 150780 KB Output is correct
7 Correct 87 ms 150852 KB Output is correct
8 Correct 77 ms 150808 KB Output is correct
9 Correct 75 ms 150740 KB Output is correct
10 Correct 79 ms 150768 KB Output is correct
11 Correct 76 ms 150828 KB Output is correct
12 Correct 81 ms 150852 KB Output is correct
13 Correct 75 ms 150816 KB Output is correct
14 Correct 512 ms 402516 KB Output is correct
15 Correct 87 ms 150860 KB Output is correct
16 Correct 75 ms 150736 KB Output is correct
17 Correct 499 ms 402428 KB Output is correct
18 Correct 74 ms 150728 KB Output is correct
19 Correct 75 ms 150764 KB Output is correct
20 Correct 488 ms 402376 KB Output is correct
21 Correct 88 ms 150788 KB Output is correct
22 Correct 78 ms 150860 KB Output is correct
23 Correct 461 ms 402388 KB Output is correct
24 Correct 81 ms 150728 KB Output is correct
25 Correct 79 ms 150832 KB Output is correct
26 Correct 81 ms 150848 KB Output is correct
27 Correct 75 ms 150732 KB Output is correct
28 Correct 74 ms 150796 KB Output is correct
29 Correct 73 ms 150748 KB Output is correct
30 Correct 77 ms 150748 KB Output is correct
31 Correct 478 ms 402492 KB Output is correct
32 Correct 514 ms 402456 KB Output is correct
33 Correct 479 ms 402388 KB Output is correct
34 Correct 560 ms 402400 KB Output is correct
35 Correct 473 ms 402424 KB Output is correct
36 Correct 471 ms 402488 KB Output is correct
37 Correct 510 ms 402476 KB Output is correct
38 Correct 506 ms 402336 KB Output is correct
39 Correct 511 ms 402444 KB Output is correct
40 Correct 482 ms 402372 KB Output is correct
41 Correct 514 ms 402444 KB Output is correct
42 Correct 493 ms 402508 KB Output is correct
43 Correct 499 ms 402484 KB Output is correct
44 Correct 516 ms 402480 KB Output is correct
45 Correct 73 ms 150764 KB Output is correct
46 Correct 514 ms 402448 KB Output is correct
47 Correct 481 ms 402452 KB Output is correct
48 Correct 556 ms 402360 KB Output is correct
49 Correct 554 ms 402380 KB Output is correct
50 Correct 489 ms 402376 KB Output is correct
51 Correct 475 ms 402500 KB Output is correct
52 Correct 500 ms 402528 KB Output is correct
53 Correct 477 ms 402472 KB Output is correct
54 Correct 574 ms 402428 KB Output is correct
55 Correct 527 ms 402448 KB Output is correct
56 Correct 533 ms 402444 KB Output is correct
57 Correct 529 ms 402376 KB Output is correct
58 Correct 541 ms 402468 KB Output is correct
59 Correct 346 ms 160040 KB Output is correct
60 Correct 354 ms 160016 KB Output is correct
61 Correct 375 ms 160028 KB Output is correct
62 Correct 387 ms 160104 KB Output is correct
63 Correct 343 ms 160076 KB Output is correct
64 Correct 356 ms 160044 KB Output is correct
65 Correct 357 ms 159948 KB Output is correct
66 Correct 369 ms 159980 KB Output is correct
67 Correct 444 ms 160112 KB Output is correct
68 Correct 410 ms 160220 KB Output is correct
69 Correct 392 ms 160084 KB Output is correct
70 Correct 402 ms 159996 KB Output is correct
71 Correct 806 ms 160040 KB Output is correct
72 Correct 1662 ms 410520 KB Output is correct
73 Correct 668 ms 160036 KB Output is correct
74 Correct 652 ms 160020 KB Output is correct
75 Correct 1344 ms 406688 KB Output is correct
76 Correct 587 ms 160020 KB Output is correct
77 Correct 645 ms 159964 KB Output is correct
78 Correct 1907 ms 406068 KB Output is correct
79 Correct 641 ms 159812 KB Output is correct
80 Correct 610 ms 159880 KB Output is correct
81 Correct 1740 ms 410076 KB Output is correct
82 Correct 557 ms 159876 KB Output is correct
83 Correct 443 ms 159892 KB Output is correct
84 Correct 430 ms 159888 KB Output is correct
85 Correct 542 ms 159832 KB Output is correct
86 Correct 499 ms 159992 KB Output is correct
87 Correct 501 ms 159844 KB Output is correct
88 Correct 553 ms 159904 KB Output is correct
89 Correct 1582 ms 409560 KB Output is correct
90 Correct 1569 ms 407172 KB Output is correct
91 Correct 1524 ms 406720 KB Output is correct
92 Correct 1640 ms 406768 KB Output is correct
93 Correct 1596 ms 410476 KB Output is correct
94 Correct 1699 ms 405848 KB Output is correct
95 Correct 1728 ms 409448 KB Output is correct
96 Correct 1470 ms 411300 KB Output is correct
97 Correct 1547 ms 411948 KB Output is correct
98 Correct 1622 ms 408800 KB Output is correct
99 Correct 1400 ms 412208 KB Output is correct
100 Correct 1774 ms 411176 KB Output is correct
101 Correct 1655 ms 409536 KB Output is correct
102 Correct 1289 ms 408460 KB Output is correct
103 Correct 2116 ms 407964 KB Output is correct
104 Correct 1438 ms 410668 KB Output is correct
105 Correct 1560 ms 408076 KB Output is correct
106 Correct 1613 ms 404140 KB Output is correct
107 Correct 1582 ms 414336 KB Output is correct
108 Correct 1755 ms 410752 KB Output is correct
109 Correct 1697 ms 411532 KB Output is correct
110 Correct 1570 ms 409784 KB Output is correct
111 Correct 1711 ms 408556 KB Output is correct
112 Correct 1632 ms 413704 KB Output is correct
113 Correct 1253 ms 408132 KB Output is correct
114 Correct 1250 ms 406228 KB Output is correct
115 Correct 1293 ms 406764 KB Output is correct
116 Correct 1280 ms 408676 KB Output is correct
117 Correct 1265 ms 421860 KB Output is correct
118 Correct 1217 ms 422004 KB Output is correct
119 Correct 1231 ms 421852 KB Output is correct
120 Correct 1235 ms 421868 KB Output is correct
121 Correct 1243 ms 421780 KB Output is correct
122 Correct 1247 ms 421780 KB Output is correct
123 Correct 1233 ms 421880 KB Output is correct
124 Correct 1213 ms 421700 KB Output is correct
125 Correct 1245 ms 421592 KB Output is correct
126 Correct 1212 ms 421444 KB Output is correct