답안 #251347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
251347 2020-07-20T23:24:57 Z imeimi2000 함박 스테이크 (JOI20_hamburg) C++17
100 / 100
526 ms 28128 KB
#include <bits/stdc++.h>
#define x first
#define y second
#define x1 x.x
#define y1 x.y
#define x2 y.x
#define y2 y.y

using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppp;

const int inf = 1e9 + 5;

void flip_x(ppp &i) {
    swap(i.x1, i.x2);
    i.x1 = inf - i.x1;
    i.x2 = inf - i.x2;
}

struct get_min {
    vector<pii> V;
    void add(int p, int x) {
        V.emplace_back(p, x);
    }
    void init() {
        V.emplace_back(-inf, inf);
        sort(V.begin(), V.end());
        int mn = inf;
        for (pii &i : V) {
            mn = min(mn, i.y);
            i.y = mn;
        }
    }
    int find(int x) const {
        return (lower_bound(V.begin(), V.end(), pii(x, -inf)) - 1)->y;
    }
};

vector<pii> solve4(vector<ppp> R, int l, int d, int r, int u) {
    // u > d
    int l1 = d, l2 = u;
    int d1 = l, d2 = r;
    int r1 = d, r2 = u;
    int u1 = l, u2 = r;
    get_min ld, dr, ru, ul, du;
    vector<int> cp;
    multiset<int> Ls, Rs;
    Ls.insert(-inf);
    Rs.insert(inf);
    vector<pii> M;
    for (ppp i : R) {
        int in_l = i.x1 <= l, in_d = d >= i.y1;
        int in_r = i.x2 >= r, in_u = u <= i.y2;
        int cnt = in_l + in_d + in_r + in_u;
        if (in_l) cp.push_back(i.y1), cp.push_back(i.y2);
        assert(cnt > 0);
        if (cnt > 2) continue;
        if (cnt == 1) {
            if (in_l) {
                l1 = max(l1, i.y1);
                l2 = min(l2, i.y2);
            }
            if (in_d) {
                d1 = max(d1, i.x1);
                d2 = min(d2, i.x2);
            }
            if (in_r) {
                r1 = max(r1, i.y1);
                r2 = min(r2, i.y2);
            }
            if (in_u) {
                u1 = max(u1, i.x1);
                u2 = min(u2, i.x2);
            }
            continue;
        }
        if (in_l && in_r) {
            Ls.insert(i.y1);
            Rs.insert(i.y2);
            M.emplace_back(i.y1, i.y2);
            M.emplace_back(i.y2, -i.y1);
            continue;
        }
        if (in_d && in_u) {
            u1 = max(u1, i.x1);
            d2 = min(d2, i.x2);
            du.add(i.x2, -i.x1);
            continue;
        }
        if (in_l && in_d) ld.add(i.y2 - d, i.x2 - l);
        if (in_d && in_r) dr.add(r - i.x1, i.y2 - d);
        if (in_r && in_u) ru.add(u - i.y1, r - i.x1);
        if (in_u && in_l) ul.add(i.x2 - l, u - i.y1);
    }
    vector<pii> ret;
    if (l1 > l2 || d1 > d2 || r1 > r2 || u1 > u2) return ret;
    sort(M.rbegin(), M.rend());
    du.init();
    if (d2 < u1 && d2 < -du.find(u1)) return ret;
    ld.init();
    dr.init();
    ru.init();
    ul.init();
    sort(cp.begin(), cp.end());
    cp.erase(unique(cp.begin(), cp.end()), cp.end());
    for (int ly : cp) {
        if (ly < l1 || l2 < ly) continue;
        while (!M.empty() && M.back().x <= ly) {
            auto [x, y] = M.back();
            M.pop_back();
            if (y < 0) {
                Ls.insert(-y);
                Rs.insert(x);
            }
            else {
                Ls.erase(Ls.find(x));
                Rs.erase(Rs.find(y));
            }
        }
        int dx = min(d2, l + ld.find(ly - d));
        if (dx < d1) continue;
        int ry = min({ r2, d + dr.find(r - dx), *Rs.begin() });
        if (ry < r1 || ry < *Ls.rbegin()) continue;
        int ux = max(u1, r - ru.find(u - ry));
        if (u2 < ux) continue;
        if (dx < ux && dx < -du.find(ux)) continue;
        if (ly < u - ul.find(ux - l)) continue;
        ret.emplace_back(l, ly);
        ret.emplace_back(dx, d);
        ret.emplace_back(r, ry);
        ret.emplace_back(ux, u);
        break;
    }
    return ret;
}

vector<pii> solve(vector<ppp> R, int k) {
    if (R.empty()) return vector<pii>(k, pii(1, 1));
    int l = inf, d = inf, r = 0, u = 0;
    for (ppp i : R) {
        l = min(l, i.x2);
        d = min(d, i.y2);
        r = max(r, i.x1);
        u = max(u, i.y1);
    }
    if (k == 1) {
        vector<pii> ret;
        if (r <= l && u <= d) ret.emplace_back(l, d);
        return ret;
    }
    for (int x : { l, r }) for (int y : { d, u }) {
        vector<ppp> nxt;
        for (ppp i : R) {
            if (i.x1 <= x && x <= i.x2 && i.y1 <= y && y <= i.y2) continue;
            nxt.push_back(i);
        }
        vector<pii> ret = solve(nxt, k - 1);
        if (ret.empty()) continue;
        ret.emplace_back(x, y);
        return ret;
    }
    if (k <= 3) return vector<pii>();
    assert(l < r && d < u);
    for (int i = 0; i < 2; ++i) {
        vector<pii> ans = solve4(R, l, d, r, u);
        if (!ans.empty()) {
            for (pii &p : ans) if (i) p.x = inf - p.x;
            return ans;
        }
        for (ppp &i : R) flip_x(i);
        swap(l, r);
        l = inf - l;
        r = inf - r;
    }
    return vector<pii>();
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<ppp> R(n);
    for (int i = 0; i < n; ++i) {
        cin >> R[i].x1 >> R[i].y1 >> R[i].x2 >> R[i].y2;
    }
    vector<pii> ans = solve(R, k);
    assert(!ans.empty());
    for (pii i : ans) printf("%d %d\n", i.x, i.y);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 3 ms 600 KB Output is correct
14 Correct 4 ms 596 KB Output is correct
15 Correct 3 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 4 ms 600 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 3 ms 632 KB Output is correct
20 Correct 3 ms 624 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 600 KB Output is correct
23 Correct 4 ms 640 KB Output is correct
24 Correct 3 ms 568 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 2 ms 584 KB Output is correct
31 Correct 3 ms 588 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 576 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 640 KB Output is correct
36 Correct 3 ms 592 KB Output is correct
37 Correct 4 ms 652 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 3 ms 600 KB Output is correct
41 Correct 6 ms 604 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 3 ms 620 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 4 ms 616 KB Output is correct
47 Correct 3 ms 640 KB Output is correct
48 Correct 5 ms 612 KB Output is correct
49 Correct 4 ms 628 KB Output is correct
50 Correct 4 ms 596 KB Output is correct
51 Correct 4 ms 640 KB Output is correct
52 Correct 4 ms 588 KB Output is correct
53 Correct 5 ms 616 KB Output is correct
54 Correct 4 ms 600 KB Output is correct
55 Correct 4 ms 604 KB Output is correct
56 Correct 4 ms 596 KB Output is correct
57 Correct 4 ms 600 KB Output is correct
58 Correct 4 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 107 ms 6596 KB Output is correct
6 Correct 92 ms 6648 KB Output is correct
7 Correct 109 ms 6648 KB Output is correct
8 Correct 102 ms 6600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 104 ms 9152 KB Output is correct
6 Correct 109 ms 8808 KB Output is correct
7 Correct 104 ms 8984 KB Output is correct
8 Correct 114 ms 11708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 95 ms 10044 KB Output is correct
14 Correct 97 ms 10220 KB Output is correct
15 Correct 106 ms 8896 KB Output is correct
16 Correct 103 ms 9072 KB Output is correct
17 Correct 121 ms 11500 KB Output is correct
18 Correct 101 ms 8892 KB Output is correct
19 Correct 99 ms 11840 KB Output is correct
20 Correct 135 ms 13036 KB Output is correct
21 Correct 195 ms 19932 KB Output is correct
22 Correct 128 ms 13700 KB Output is correct
23 Correct 134 ms 17340 KB Output is correct
24 Correct 129 ms 18536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 3 ms 600 KB Output is correct
14 Correct 4 ms 596 KB Output is correct
15 Correct 3 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 4 ms 600 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 3 ms 632 KB Output is correct
20 Correct 3 ms 624 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 600 KB Output is correct
23 Correct 4 ms 640 KB Output is correct
24 Correct 3 ms 568 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 2 ms 584 KB Output is correct
31 Correct 3 ms 588 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 576 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 640 KB Output is correct
36 Correct 3 ms 592 KB Output is correct
37 Correct 4 ms 652 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 3 ms 600 KB Output is correct
41 Correct 6 ms 604 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 3 ms 620 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 4 ms 616 KB Output is correct
47 Correct 3 ms 640 KB Output is correct
48 Correct 5 ms 612 KB Output is correct
49 Correct 4 ms 628 KB Output is correct
50 Correct 4 ms 596 KB Output is correct
51 Correct 4 ms 640 KB Output is correct
52 Correct 4 ms 588 KB Output is correct
53 Correct 5 ms 616 KB Output is correct
54 Correct 4 ms 600 KB Output is correct
55 Correct 4 ms 604 KB Output is correct
56 Correct 4 ms 596 KB Output is correct
57 Correct 4 ms 600 KB Output is correct
58 Correct 4 ms 588 KB Output is correct
59 Correct 107 ms 13804 KB Output is correct
60 Correct 101 ms 11200 KB Output is correct
61 Correct 110 ms 11884 KB Output is correct
62 Correct 102 ms 10604 KB Output is correct
63 Correct 98 ms 12392 KB Output is correct
64 Correct 97 ms 9536 KB Output is correct
65 Correct 108 ms 12864 KB Output is correct
66 Correct 143 ms 11888 KB Output is correct
67 Correct 200 ms 19176 KB Output is correct
68 Correct 142 ms 16664 KB Output is correct
69 Correct 120 ms 14060 KB Output is correct
70 Correct 121 ms 18408 KB Output is correct
71 Correct 375 ms 26008 KB Output is correct
72 Correct 419 ms 26012 KB Output is correct
73 Correct 339 ms 26216 KB Output is correct
74 Correct 259 ms 25812 KB Output is correct
75 Correct 298 ms 24976 KB Output is correct
76 Correct 220 ms 28128 KB Output is correct
77 Correct 285 ms 25440 KB Output is correct
78 Correct 503 ms 25968 KB Output is correct
79 Correct 308 ms 26716 KB Output is correct
80 Correct 265 ms 26788 KB Output is correct
81 Correct 411 ms 27052 KB Output is correct
82 Correct 232 ms 22476 KB Output is correct
83 Correct 178 ms 23736 KB Output is correct
84 Correct 154 ms 16488 KB Output is correct
85 Correct 243 ms 26256 KB Output is correct
86 Correct 162 ms 21560 KB Output is correct
87 Correct 226 ms 25336 KB Output is correct
88 Correct 214 ms 24792 KB Output is correct
89 Correct 393 ms 27484 KB Output is correct
90 Correct 480 ms 25984 KB Output is correct
91 Correct 311 ms 27084 KB Output is correct
92 Correct 398 ms 26452 KB Output is correct
93 Correct 423 ms 26460 KB Output is correct
94 Correct 488 ms 26372 KB Output is correct
95 Correct 437 ms 25940 KB Output is correct
96 Correct 386 ms 26836 KB Output is correct
97 Correct 421 ms 25840 KB Output is correct
98 Correct 365 ms 26580 KB Output is correct
99 Correct 373 ms 26620 KB Output is correct
100 Correct 457 ms 25304 KB Output is correct
101 Correct 412 ms 26044 KB Output is correct
102 Correct 286 ms 18340 KB Output is correct
103 Correct 526 ms 25968 KB Output is correct
104 Correct 305 ms 22136 KB Output is correct
105 Correct 429 ms 26220 KB Output is correct
106 Correct 432 ms 26600 KB Output is correct
107 Correct 422 ms 26556 KB Output is correct
108 Correct 399 ms 25948 KB Output is correct
109 Correct 468 ms 26032 KB Output is correct
110 Correct 402 ms 26584 KB Output is correct
111 Correct 376 ms 27732 KB Output is correct
112 Correct 492 ms 26612 KB Output is correct
113 Correct 311 ms 20620 KB Output is correct
114 Correct 371 ms 20664 KB Output is correct
115 Correct 333 ms 20636 KB Output is correct
116 Correct 303 ms 20636 KB Output is correct
117 Correct 335 ms 26540 KB Output is correct
118 Correct 339 ms 26548 KB Output is correct
119 Correct 346 ms 26564 KB Output is correct
120 Correct 355 ms 26552 KB Output is correct
121 Correct 339 ms 26540 KB Output is correct
122 Correct 339 ms 26616 KB Output is correct
123 Correct 343 ms 26540 KB Output is correct
124 Correct 348 ms 26536 KB Output is correct
125 Correct 360 ms 26668 KB Output is correct
126 Correct 348 ms 26548 KB Output is correct