답안 #414575

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414575 2021-05-30T16:42:11 Z usachevd0 함박 스테이크 (JOI20_hamburg) C++17
100 / 100
704 ms 66184 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
    return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
    return y > x ? (x = y, true) : false;
}
void debug_out() {
    cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
    cerr << ' ' << A;
    debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
    for (int i = 0; i < n; ++i)
        cerr << a[i] << ' ';
    cerr << endl;
}
#ifdef LOCAL
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
    #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
    #define debug(...) 1337
    #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
    for (auto& x : v)
        stream << x << ' ';
    return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
    return stream << p.first << ' ' << p.second;
}

const int INF32 = 1e9;

using Point = array<int, 2>;

using Segm = Point;
Segm universe_segm;
Segm intersect(const Segm& s1, const Segm& s2) {
    return {max(s1[0], s2[0]), min(s1[1], s2[1])};
}
void uintersect(Segm& s1, const Segm& s2) {
    chkmax(s1[0], s2[0]);
    chkmin(s1[1], s2[1]);
}
bool in(int x, const Segm& s) {
    return s[0] <= x && x <= s[1];
}
bool empt(const Segm& s) {
    return s[0] > s[1];
}

struct Rect {
    Segm s[2];
    
    Rect() {}
    
    void read() {
        cin >> s[0][0] >> s[1][0] >> s[0][1] >> s[1][1];
    }
    
    bool in(Point P) {
        return ::in(P[0], s[0]) && ::in(P[1], s[1]);
    }
} universe_rect;
ostream& operator << (ostream& stream, const Rect& r) {
    return stream << r.s[0][0] << ' ' << r.s[1][0] << ' ' << r.s[0][1] << ' ' << r.s[1][1];
}

Rect intersect(const Rect& r1, const Rect& r2) {
    Rect r3;
    for (int a = 0; a < 2; ++a) {
        r3.s[a][0] = max(r1.s[a][0], r2.s[a][0]);
        r3.s[a][1] = min(r1.s[a][1], r2.s[a][1]);
    }
    return r3;
}





vector<Point> solve(int k, vector<Rect> rects, vector<Point> points) {
    if (rects.empty()) {
        while (k--)
            points.push_back({0, 0});
        return points;
    }
    Rect I = universe_rect;
    for (Rect& r : rects)
        I = intersect(I, r);
    if (k == 1) {
        if (I.s[0][0] > I.s[0][1] || I.s[1][0] > I.s[1][1]) {
            return {};
        } else {
            points.push_back({I.s[0][0], I.s[1][0]});
            return points;
        }
    }
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            Point P{I.s[0][i], I.s[1][j]};
            points.push_back(P);
            vector<Rect> new_rects;
            for (auto r : rects)
                if (!r.in(P))
                    new_rects.push_back(r);
            auto res = solve(k - 1, new_rects, points);
            if (!res.empty()) {
                return res;
            }
            points.pop_back();
        }
    }
    return {};
}

int32_t main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    universe_rect.s[0][0] = universe_rect.s[1][0] = 0;
    universe_rect.s[0][1] = universe_rect.s[1][1] = +INF32;
    universe_segm = {0, +INF32};
    
    int n, k;
    cin >> n >> k;
    vector<int> rv;
    vector<Rect> rects(n);
    for (auto& r : rects) {
        r.read();
        for (int a = 0; a < 2; ++a)
            for (int b = 0; b < 2; ++b)
                rv.push_back(r.s[a][b]);
    }
    sort(all(rv));
    rv.resize(unique(all(rv)) - rv.begin());
    int X = rv.size();
    for (auto& r : rects)
        for (int a = 0; a < 2; ++a) {
            assert(r.s[a][0] <= r.s[a][1]);
            for (int b = 0; b < 2; ++b)
                r.s[a][b] = lower_bound(all(rv), r.s[a][b]) - rv.begin();
            assert(r.s[a][0] <= r.s[a][1]);
        }
    auto easy = solve(k, rects, {});
    if (!easy.empty()) {
        for (auto P : easy)
            cout << rv[P[0]] << ' ' << rv[P[1]] << '\n';
        exit(0);
    }
    assert(k == 4);
    Rect I = universe_rect;
    for (Rect r : rects)
        I = intersect(I, r);
    int R = I.s[0][0], L = I.s[0][1];
    int U = I.s[1][0], D = I.s[1][1];
    assert(L < R);
    assert(D < U);
    
    Segm baseL = {D + 1, U - 1};
    Segm baseR = {D + 1, U - 1};
    Segm baseD = {L + 1, R - 1};
    Segm baseU = {L + 1, R - 1};
    
    vector<Segm> prefLR(X + 1, universe_segm);
    vector<Segm> suffLR(X + 1, universe_segm);
    vector<Segm> prefDU(X + 1, universe_segm); // also prefUD
    vector<Segm> prefLU(X + 1, universe_segm);
    vector<Segm> suffLD(X + 1, universe_segm);
    vector<Segm> prefUR(X + 1, universe_segm);
    vector<Segm> prefDR(X + 1, universe_segm);
    
    int minxUD = +INF32;
    for (Rect r : rects) {
        Segm rx = r.s[0], ry = r.s[1];
        int touchL = in(L, rx);
        int touchR = in(R, rx);
        int touchD = in(D, ry);
        int touchU = in(U, ry);
        int sum = touchL + touchR + touchD + touchU;
        if (sum == 0) throw;
        if (sum >= 3) continue;
        if (sum == 1) {
            if (touchL) {
                uintersect(baseL, ry);
            } else if (touchR) {
                uintersect(baseR, ry);
            } else if (touchD) {
                uintersect(baseD, rx);
            } else { // touchU
                uintersect(baseU, rx);
            }
        } else { // sum = 2
            if (touchL && touchR) {
                uintersect(prefLR[ry[0]], ry);
                uintersect(suffLR[ry[1] + 1], ry);
            } else if (touchD && touchU) {
                chkmin(minxUD, rx[1]);
                uintersect(prefDU[rx[0]], rx);
            } else if (touchL && touchU) {
                uintersect(prefLU[ry[0]], rx);
            } else if (touchL && touchD) {
                uintersect(suffLD[ry[1] + 1], rx);
            } else if (touchU && touchR) {
                uintersect(prefUR[rx[0]], ry);
            } else if (touchD && touchR) {
                uintersect(prefDR[rx[0]], ry);
            }
        }
    }
    
    for (int x = 0; x < X; ++x) {
        uintersect(suffLR[x + 1], suffLR[x]);
        uintersect(suffLD[x + 1], suffLD[x]);
    }
    for (int x = X - 1; x >= 0; --x) {
        uintersect(prefLR[x], prefLR[x + 1]);
        uintersect(prefDU[x], prefDU[x + 1]);
        uintersect(prefLU[x], prefLU[x + 1]);
        uintersect(prefUR[x], prefUR[x + 1]);
        uintersect(prefDR[x], prefDR[x + 1]);
    }
    
    for (int l = baseL[0]; l <= baseL[1]; ++l) {
        for (int rotUD = 0; rotUD < 2; ++rotUD) {
            auto segmU = intersect(baseU, prefLU[l + 1]);
            auto segmD = intersect(baseD, suffLD[l]);
            auto segmR = intersect(intersect(baseR, prefLR[l + 1]), suffLR[l]);
            int d, u;
            if (rotUD == 0) {
                // U <= D
                u = min(segmU[1], minxUD);
                if (u < segmU[0]) continue;
                uintersect(segmD, prefDU[u + 1]);
                uintersect(segmR, prefUR[u + 1]);
                if (empt(segmD)) continue;
                d = segmD[1];
                if (d < u) continue;
                uintersect(segmR, prefDR[d + 1]);
            } else {
                // U >= D
                d = min(segmD[1], minxUD);
                if (d < segmD[0]) continue;
                uintersect(segmU, prefDU[d + 1]);
                uintersect(segmR, prefDR[d + 1]);
                if (empt(segmU)) continue;
                u = segmU[1];
                if (u < d) continue;
                uintersect(segmR, prefUR[u + 1]);
            }
            if (empt(segmR)) continue;
            int r = segmR[1];
            cout << rv[L] << ' ' << rv[l] << '\n';
            cout << rv[R] << ' ' << rv[r] << '\n';
            cout << rv[d] << ' ' << rv[D] << '\n';
            cout << rv[u] << ' ' << rv[U] << '\n';
            exit(0);
        }
    }
    throw;
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 408 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 404 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 3 ms 332 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 4 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 3 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 4 ms 588 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 4 ms 596 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 4 ms 544 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 4 ms 844 KB Output is correct
15 Correct 3 ms 456 KB Output is correct
16 Correct 4 ms 588 KB Output is correct
17 Correct 5 ms 868 KB Output is correct
18 Correct 4 ms 584 KB Output is correct
19 Correct 3 ms 580 KB Output is correct
20 Correct 5 ms 804 KB Output is correct
21 Correct 3 ms 588 KB Output is correct
22 Correct 4 ms 584 KB Output is correct
23 Correct 6 ms 812 KB Output is correct
24 Correct 4 ms 588 KB Output is correct
25 Correct 4 ms 588 KB Output is correct
26 Correct 4 ms 684 KB Output is correct
27 Correct 4 ms 588 KB Output is correct
28 Correct 4 ms 588 KB Output is correct
29 Correct 4 ms 676 KB Output is correct
30 Correct 4 ms 588 KB Output is correct
31 Correct 5 ms 844 KB Output is correct
32 Correct 4 ms 844 KB Output is correct
33 Correct 5 ms 804 KB Output is correct
34 Correct 5 ms 872 KB Output is correct
35 Correct 5 ms 844 KB Output is correct
36 Correct 5 ms 844 KB Output is correct
37 Correct 5 ms 884 KB Output is correct
38 Correct 6 ms 904 KB Output is correct
39 Correct 5 ms 832 KB Output is correct
40 Correct 5 ms 944 KB Output is correct
41 Correct 5 ms 836 KB Output is correct
42 Correct 5 ms 796 KB Output is correct
43 Correct 5 ms 940 KB Output is correct
44 Correct 5 ms 936 KB Output is correct
45 Correct 4 ms 588 KB Output is correct
46 Correct 5 ms 824 KB Output is correct
47 Correct 5 ms 824 KB Output is correct
48 Correct 5 ms 788 KB Output is correct
49 Correct 5 ms 844 KB Output is correct
50 Correct 5 ms 812 KB Output is correct
51 Correct 5 ms 908 KB Output is correct
52 Correct 5 ms 912 KB Output is correct
53 Correct 5 ms 844 KB Output is correct
54 Correct 5 ms 884 KB Output is correct
55 Correct 5 ms 844 KB Output is correct
56 Correct 5 ms 844 KB Output is correct
57 Correct 5 ms 900 KB Output is correct
58 Correct 5 ms 928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 408 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 322 ms 9824 KB Output is correct
6 Correct 329 ms 9784 KB Output is correct
7 Correct 326 ms 9780 KB Output is correct
8 Correct 322 ms 9784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 404 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 332 ms 13308 KB Output is correct
6 Correct 334 ms 13492 KB Output is correct
7 Correct 326 ms 13108 KB Output is correct
8 Correct 345 ms 15820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 3 ms 332 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 4 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 3 ms 460 KB Output is correct
13 Correct 334 ms 14856 KB Output is correct
14 Correct 330 ms 14948 KB Output is correct
15 Correct 336 ms 15540 KB Output is correct
16 Correct 325 ms 13184 KB Output is correct
17 Correct 328 ms 13892 KB Output is correct
18 Correct 324 ms 11956 KB Output is correct
19 Correct 331 ms 15876 KB Output is correct
20 Correct 381 ms 21700 KB Output is correct
21 Correct 333 ms 15956 KB Output is correct
22 Correct 350 ms 21060 KB Output is correct
23 Correct 359 ms 20704 KB Output is correct
24 Correct 358 ms 20280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 4 ms 588 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 4 ms 596 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 4 ms 544 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 4 ms 844 KB Output is correct
15 Correct 3 ms 456 KB Output is correct
16 Correct 4 ms 588 KB Output is correct
17 Correct 5 ms 868 KB Output is correct
18 Correct 4 ms 584 KB Output is correct
19 Correct 3 ms 580 KB Output is correct
20 Correct 5 ms 804 KB Output is correct
21 Correct 3 ms 588 KB Output is correct
22 Correct 4 ms 584 KB Output is correct
23 Correct 6 ms 812 KB Output is correct
24 Correct 4 ms 588 KB Output is correct
25 Correct 4 ms 588 KB Output is correct
26 Correct 4 ms 684 KB Output is correct
27 Correct 4 ms 588 KB Output is correct
28 Correct 4 ms 588 KB Output is correct
29 Correct 4 ms 676 KB Output is correct
30 Correct 4 ms 588 KB Output is correct
31 Correct 5 ms 844 KB Output is correct
32 Correct 4 ms 844 KB Output is correct
33 Correct 5 ms 804 KB Output is correct
34 Correct 5 ms 872 KB Output is correct
35 Correct 5 ms 844 KB Output is correct
36 Correct 5 ms 844 KB Output is correct
37 Correct 5 ms 884 KB Output is correct
38 Correct 6 ms 904 KB Output is correct
39 Correct 5 ms 832 KB Output is correct
40 Correct 5 ms 944 KB Output is correct
41 Correct 5 ms 836 KB Output is correct
42 Correct 5 ms 796 KB Output is correct
43 Correct 5 ms 940 KB Output is correct
44 Correct 5 ms 936 KB Output is correct
45 Correct 4 ms 588 KB Output is correct
46 Correct 5 ms 824 KB Output is correct
47 Correct 5 ms 824 KB Output is correct
48 Correct 5 ms 788 KB Output is correct
49 Correct 5 ms 844 KB Output is correct
50 Correct 5 ms 812 KB Output is correct
51 Correct 5 ms 908 KB Output is correct
52 Correct 5 ms 912 KB Output is correct
53 Correct 5 ms 844 KB Output is correct
54 Correct 5 ms 884 KB Output is correct
55 Correct 5 ms 844 KB Output is correct
56 Correct 5 ms 844 KB Output is correct
57 Correct 5 ms 900 KB Output is correct
58 Correct 5 ms 928 KB Output is correct
59 Correct 336 ms 23748 KB Output is correct
60 Correct 337 ms 24496 KB Output is correct
61 Correct 337 ms 23604 KB Output is correct
62 Correct 334 ms 23648 KB Output is correct
63 Correct 330 ms 23096 KB Output is correct
64 Correct 330 ms 18996 KB Output is correct
65 Correct 337 ms 25460 KB Output is correct
66 Correct 447 ms 36476 KB Output is correct
67 Correct 362 ms 31412 KB Output is correct
68 Correct 523 ms 37052 KB Output is correct
69 Correct 558 ms 36664 KB Output is correct
70 Correct 479 ms 37800 KB Output is correct
71 Correct 343 ms 27020 KB Output is correct
72 Correct 560 ms 62440 KB Output is correct
73 Correct 350 ms 27776 KB Output is correct
74 Correct 409 ms 36516 KB Output is correct
75 Correct 483 ms 63812 KB Output is correct
76 Correct 422 ms 35812 KB Output is correct
77 Correct 348 ms 26168 KB Output is correct
78 Correct 678 ms 58300 KB Output is correct
79 Correct 347 ms 28580 KB Output is correct
80 Correct 391 ms 30388 KB Output is correct
81 Correct 627 ms 58252 KB Output is correct
82 Correct 406 ms 34740 KB Output is correct
83 Correct 434 ms 38120 KB Output is correct
84 Correct 455 ms 35136 KB Output is correct
85 Correct 514 ms 36812 KB Output is correct
86 Correct 385 ms 31104 KB Output is correct
87 Correct 531 ms 36952 KB Output is correct
88 Correct 442 ms 36292 KB Output is correct
89 Correct 533 ms 64268 KB Output is correct
90 Correct 631 ms 58392 KB Output is correct
91 Correct 514 ms 63912 KB Output is correct
92 Correct 641 ms 61720 KB Output is correct
93 Correct 597 ms 58368 KB Output is correct
94 Correct 630 ms 58280 KB Output is correct
95 Correct 620 ms 58312 KB Output is correct
96 Correct 563 ms 58460 KB Output is correct
97 Correct 596 ms 58288 KB Output is correct
98 Correct 587 ms 58348 KB Output is correct
99 Correct 524 ms 62280 KB Output is correct
100 Correct 648 ms 58456 KB Output is correct
101 Correct 606 ms 58384 KB Output is correct
102 Correct 479 ms 66184 KB Output is correct
103 Correct 704 ms 58308 KB Output is correct
104 Correct 498 ms 65708 KB Output is correct
105 Correct 656 ms 58336 KB Output is correct
106 Correct 620 ms 64400 KB Output is correct
107 Correct 576 ms 61956 KB Output is correct
108 Correct 644 ms 64412 KB Output is correct
109 Correct 656 ms 58408 KB Output is correct
110 Correct 599 ms 58232 KB Output is correct
111 Correct 578 ms 58420 KB Output is correct
112 Correct 643 ms 58388 KB Output is correct
113 Correct 537 ms 59176 KB Output is correct
114 Correct 521 ms 59532 KB Output is correct
115 Correct 516 ms 59268 KB Output is correct
116 Correct 522 ms 59268 KB Output is correct
117 Correct 415 ms 37440 KB Output is correct
118 Correct 423 ms 37452 KB Output is correct
119 Correct 440 ms 37532 KB Output is correct
120 Correct 416 ms 37540 KB Output is correct
121 Correct 430 ms 37456 KB Output is correct
122 Correct 414 ms 37460 KB Output is correct
123 Correct 425 ms 37400 KB Output is correct
124 Correct 410 ms 37452 KB Output is correct
125 Correct 413 ms 37588 KB Output is correct
126 Correct 407 ms 37484 KB Output is correct