답안 #648564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648564 2022-10-07T03:16:20 Z 406 함박 스테이크 (JOI20_hamburg) C++17
100 / 100
1771 ms 428872 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

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:99: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]
   99 |         if (!cnt && points.size() <= k) {
      |                     ~~~~~~~~~~~~~~^~~~
hamburg.cpp:106: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]
  106 |         if (points.size() >= k)
      |             ~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 150764 KB Output is correct
2 Correct 72 ms 150720 KB Output is correct
3 Correct 80 ms 150656 KB Output is correct
4 Correct 84 ms 150708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 150784 KB Output is correct
2 Correct 77 ms 150668 KB Output is correct
3 Correct 79 ms 150732 KB Output is correct
4 Correct 83 ms 150732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 150700 KB Output is correct
2 Correct 71 ms 150680 KB Output is correct
3 Correct 75 ms 150720 KB Output is correct
4 Correct 78 ms 150776 KB Output is correct
5 Correct 74 ms 150728 KB Output is correct
6 Correct 76 ms 150728 KB Output is correct
7 Correct 83 ms 150716 KB Output is correct
8 Correct 76 ms 150676 KB Output is correct
9 Correct 79 ms 150752 KB Output is correct
10 Correct 71 ms 150740 KB Output is correct
11 Correct 76 ms 150772 KB Output is correct
12 Correct 73 ms 150772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 150728 KB Output is correct
2 Correct 77 ms 150656 KB Output is correct
3 Correct 75 ms 150820 KB Output is correct
4 Correct 76 ms 150748 KB Output is correct
5 Correct 72 ms 150736 KB Output is correct
6 Correct 83 ms 150740 KB Output is correct
7 Correct 72 ms 150716 KB Output is correct
8 Correct 72 ms 150652 KB Output is correct
9 Correct 74 ms 150748 KB Output is correct
10 Correct 72 ms 150684 KB Output is correct
11 Correct 77 ms 150780 KB Output is correct
12 Correct 74 ms 150772 KB Output is correct
13 Correct 84 ms 150732 KB Output is correct
14 Correct 511 ms 402476 KB Output is correct
15 Correct 75 ms 150732 KB Output is correct
16 Correct 79 ms 150852 KB Output is correct
17 Correct 495 ms 402420 KB Output is correct
18 Correct 83 ms 150808 KB Output is correct
19 Correct 88 ms 150768 KB Output is correct
20 Correct 504 ms 402500 KB Output is correct
21 Correct 87 ms 150732 KB Output is correct
22 Correct 86 ms 150748 KB Output is correct
23 Correct 488 ms 402424 KB Output is correct
24 Correct 83 ms 150740 KB Output is correct
25 Correct 79 ms 150744 KB Output is correct
26 Correct 75 ms 150772 KB Output is correct
27 Correct 76 ms 150936 KB Output is correct
28 Correct 84 ms 150808 KB Output is correct
29 Correct 87 ms 150828 KB Output is correct
30 Correct 75 ms 150940 KB Output is correct
31 Correct 509 ms 402420 KB Output is correct
32 Correct 524 ms 402400 KB Output is correct
33 Correct 477 ms 402460 KB Output is correct
34 Correct 481 ms 402372 KB Output is correct
35 Correct 501 ms 402392 KB Output is correct
36 Correct 478 ms 402472 KB Output is correct
37 Correct 501 ms 402408 KB Output is correct
38 Correct 478 ms 402372 KB Output is correct
39 Correct 501 ms 402532 KB Output is correct
40 Correct 511 ms 402668 KB Output is correct
41 Correct 492 ms 402440 KB Output is correct
42 Correct 465 ms 402424 KB Output is correct
43 Correct 508 ms 402444 KB Output is correct
44 Correct 500 ms 402504 KB Output is correct
45 Correct 77 ms 150852 KB Output is correct
46 Correct 519 ms 402440 KB Output is correct
47 Correct 508 ms 402544 KB Output is correct
48 Correct 480 ms 402288 KB Output is correct
49 Correct 511 ms 402348 KB Output is correct
50 Correct 507 ms 402460 KB Output is correct
51 Correct 480 ms 402448 KB Output is correct
52 Correct 501 ms 402420 KB Output is correct
53 Correct 522 ms 402528 KB Output is correct
54 Correct 490 ms 402436 KB Output is correct
55 Correct 493 ms 402444 KB Output is correct
56 Correct 501 ms 402384 KB Output is correct
57 Correct 470 ms 402372 KB Output is correct
58 Correct 495 ms 402328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 150764 KB Output is correct
2 Correct 72 ms 150720 KB Output is correct
3 Correct 80 ms 150656 KB Output is correct
4 Correct 84 ms 150708 KB Output is correct
5 Correct 340 ms 159240 KB Output is correct
6 Correct 341 ms 159264 KB Output is correct
7 Correct 347 ms 159260 KB Output is correct
8 Correct 359 ms 159380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 150784 KB Output is correct
2 Correct 77 ms 150668 KB Output is correct
3 Correct 79 ms 150732 KB Output is correct
4 Correct 83 ms 150732 KB Output is correct
5 Correct 352 ms 159028 KB Output is correct
6 Correct 358 ms 159188 KB Output is correct
7 Correct 347 ms 159008 KB Output is correct
8 Correct 356 ms 159012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 150700 KB Output is correct
2 Correct 71 ms 150680 KB Output is correct
3 Correct 75 ms 150720 KB Output is correct
4 Correct 78 ms 150776 KB Output is correct
5 Correct 74 ms 150728 KB Output is correct
6 Correct 76 ms 150728 KB Output is correct
7 Correct 83 ms 150716 KB Output is correct
8 Correct 76 ms 150676 KB Output is correct
9 Correct 79 ms 150752 KB Output is correct
10 Correct 71 ms 150740 KB Output is correct
11 Correct 76 ms 150772 KB Output is correct
12 Correct 73 ms 150772 KB Output is correct
13 Correct 348 ms 159020 KB Output is correct
14 Correct 376 ms 158992 KB Output is correct
15 Correct 392 ms 159080 KB Output is correct
16 Correct 384 ms 159096 KB Output is correct
17 Correct 407 ms 158924 KB Output is correct
18 Correct 350 ms 159144 KB Output is correct
19 Correct 373 ms 158972 KB Output is correct
20 Correct 346 ms 159052 KB Output is correct
21 Correct 457 ms 159020 KB Output is correct
22 Correct 377 ms 158984 KB Output is correct
23 Correct 379 ms 158944 KB Output is correct
24 Correct 383 ms 158988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 150728 KB Output is correct
2 Correct 77 ms 150656 KB Output is correct
3 Correct 75 ms 150820 KB Output is correct
4 Correct 76 ms 150748 KB Output is correct
5 Correct 72 ms 150736 KB Output is correct
6 Correct 83 ms 150740 KB Output is correct
7 Correct 72 ms 150716 KB Output is correct
8 Correct 72 ms 150652 KB Output is correct
9 Correct 74 ms 150748 KB Output is correct
10 Correct 72 ms 150684 KB Output is correct
11 Correct 77 ms 150780 KB Output is correct
12 Correct 74 ms 150772 KB Output is correct
13 Correct 84 ms 150732 KB Output is correct
14 Correct 511 ms 402476 KB Output is correct
15 Correct 75 ms 150732 KB Output is correct
16 Correct 79 ms 150852 KB Output is correct
17 Correct 495 ms 402420 KB Output is correct
18 Correct 83 ms 150808 KB Output is correct
19 Correct 88 ms 150768 KB Output is correct
20 Correct 504 ms 402500 KB Output is correct
21 Correct 87 ms 150732 KB Output is correct
22 Correct 86 ms 150748 KB Output is correct
23 Correct 488 ms 402424 KB Output is correct
24 Correct 83 ms 150740 KB Output is correct
25 Correct 79 ms 150744 KB Output is correct
26 Correct 75 ms 150772 KB Output is correct
27 Correct 76 ms 150936 KB Output is correct
28 Correct 84 ms 150808 KB Output is correct
29 Correct 87 ms 150828 KB Output is correct
30 Correct 75 ms 150940 KB Output is correct
31 Correct 509 ms 402420 KB Output is correct
32 Correct 524 ms 402400 KB Output is correct
33 Correct 477 ms 402460 KB Output is correct
34 Correct 481 ms 402372 KB Output is correct
35 Correct 501 ms 402392 KB Output is correct
36 Correct 478 ms 402472 KB Output is correct
37 Correct 501 ms 402408 KB Output is correct
38 Correct 478 ms 402372 KB Output is correct
39 Correct 501 ms 402532 KB Output is correct
40 Correct 511 ms 402668 KB Output is correct
41 Correct 492 ms 402440 KB Output is correct
42 Correct 465 ms 402424 KB Output is correct
43 Correct 508 ms 402444 KB Output is correct
44 Correct 500 ms 402504 KB Output is correct
45 Correct 77 ms 150852 KB Output is correct
46 Correct 519 ms 402440 KB Output is correct
47 Correct 508 ms 402544 KB Output is correct
48 Correct 480 ms 402288 KB Output is correct
49 Correct 511 ms 402348 KB Output is correct
50 Correct 507 ms 402460 KB Output is correct
51 Correct 480 ms 402448 KB Output is correct
52 Correct 501 ms 402420 KB Output is correct
53 Correct 522 ms 402528 KB Output is correct
54 Correct 490 ms 402436 KB Output is correct
55 Correct 493 ms 402444 KB Output is correct
56 Correct 501 ms 402384 KB Output is correct
57 Correct 470 ms 402372 KB Output is correct
58 Correct 495 ms 402328 KB Output is correct
59 Correct 361 ms 161620 KB Output is correct
60 Correct 378 ms 161740 KB Output is correct
61 Correct 362 ms 161676 KB Output is correct
62 Correct 357 ms 161700 KB Output is correct
63 Correct 358 ms 161688 KB Output is correct
64 Correct 371 ms 161788 KB Output is correct
65 Correct 364 ms 161672 KB Output is correct
66 Correct 373 ms 161712 KB Output is correct
67 Correct 438 ms 161676 KB Output is correct
68 Correct 397 ms 161696 KB Output is correct
69 Correct 356 ms 161760 KB Output is correct
70 Correct 386 ms 161688 KB Output is correct
71 Correct 669 ms 161676 KB Output is correct
72 Correct 1593 ms 412068 KB Output is correct
73 Correct 618 ms 166224 KB Output is correct
74 Correct 582 ms 166168 KB Output is correct
75 Correct 1293 ms 413036 KB Output is correct
76 Correct 519 ms 166328 KB Output is correct
77 Correct 630 ms 166348 KB Output is correct
78 Correct 1750 ms 412432 KB Output is correct
79 Correct 569 ms 166240 KB Output is correct
80 Correct 538 ms 166268 KB Output is correct
81 Correct 1625 ms 416272 KB Output is correct
82 Correct 497 ms 166260 KB Output is correct
83 Correct 414 ms 166244 KB Output is correct
84 Correct 460 ms 166220 KB Output is correct
85 Correct 524 ms 166196 KB Output is correct
86 Correct 481 ms 166224 KB Output is correct
87 Correct 446 ms 166336 KB Output is correct
88 Correct 499 ms 166292 KB Output is correct
89 Correct 1428 ms 416084 KB Output is correct
90 Correct 1546 ms 413356 KB Output is correct
91 Correct 1422 ms 412764 KB Output is correct
92 Correct 1549 ms 412944 KB Output is correct
93 Correct 1520 ms 416732 KB Output is correct
94 Correct 1593 ms 412116 KB Output is correct
95 Correct 1641 ms 415440 KB Output is correct
96 Correct 1598 ms 417712 KB Output is correct
97 Correct 1571 ms 418248 KB Output is correct
98 Correct 1562 ms 415148 KB Output is correct
99 Correct 1368 ms 418336 KB Output is correct
100 Correct 1674 ms 417380 KB Output is correct
101 Correct 1617 ms 415864 KB Output is correct
102 Correct 1264 ms 415112 KB Output is correct
103 Correct 1771 ms 414228 KB Output is correct
104 Correct 1326 ms 416716 KB Output is correct
105 Correct 1501 ms 414400 KB Output is correct
106 Correct 1556 ms 410396 KB Output is correct
107 Correct 1521 ms 420512 KB Output is correct
108 Correct 1644 ms 416620 KB Output is correct
109 Correct 1631 ms 417892 KB Output is correct
110 Correct 1512 ms 416196 KB Output is correct
111 Correct 1657 ms 414880 KB Output is correct
112 Correct 1574 ms 420060 KB Output is correct
113 Correct 1234 ms 414272 KB Output is correct
114 Correct 1226 ms 412488 KB Output is correct
115 Correct 1246 ms 412988 KB Output is correct
116 Correct 1280 ms 414884 KB Output is correct
117 Correct 1223 ms 428648 KB Output is correct
118 Correct 1230 ms 428720 KB Output is correct
119 Correct 1221 ms 428800 KB Output is correct
120 Correct 1207 ms 428740 KB Output is correct
121 Correct 1217 ms 428872 KB Output is correct
122 Correct 1230 ms 428668 KB Output is correct
123 Correct 1206 ms 428856 KB Output is correct
124 Correct 1207 ms 428852 KB Output is correct
125 Correct 1215 ms 428772 KB Output is correct
126 Correct 1221 ms 428644 KB Output is correct