Submission #223862

# Submission time Handle Problem Language Result Execution time Memory
223862 2020-04-16T15:34:02 Z Minnakhmetov Hamburg Steak (JOI20_hamburg) C++14
100 / 100
661 ms 34296 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 4e5 + 5, INF = 1e9 + 5;

struct R {
    int xl, yl, xr, yr;

    bool contains(int x, int y) const {
        return x >= xl && x <= xr && y >= yl && y <= yr;
    }
    bool intersects_hor(int y) const {
        return y >= yl && y <= yr;
    }
    bool intersects_ver(int x) const {
        return x >= xl && x <= xr;
    }
};

vector<R> rcs;
vector<int> vx, vy;
vector<pair<int, int>> ans;
pair<int, int> suf_hor[N], pref_ver[N], suf_ver[N];
int pref[N], suf[N], suf_lt[N], suf_rt[N];

pair<int, int> intersection(pair<int, int> a, pair<int, int> b) {
    return {max(a.first, b.first), min(a.second, b.second)};
}

bool solve_for_line(vector<pair<int, int>> v, int k, bool hor, int x) {
    sort(all(v));
    int last = N;
    vector<int> w;
    for (int i = (int)v.size() - 1; i >= 0; i--) {
        if (v[i].second < last) {
            if (k == 0)
                return false;
            k--;
            last = v[i].first;
            w.push_back(last);
        }
    }
    for (int y : w) {
        ans.push_back(hor ? make_pair(y, x) : make_pair(x, y));
    }
    return true;
}

vector<R> remove_covered(vector<R> v, int x, int y) {
    vector<R> nv;
    for (const R &rc : v) {
        if (!rc.contains(x, y)) {
            nv.push_back(rc);
        }
    }
    return nv;
}

bool solve(vector<R> v, int k) {
    int mn_xr = N, mx_xl = -1, mn_yr = N, mx_yl = -1;

    for (R &rc : v) {
        mn_xr = min(mn_xr, rc.xr);
        mn_yr = min(mn_yr, rc.yr);
        mx_xl = max(mx_xl, rc.xl);
        mx_yl = max(mx_yl, rc.yl);
    }

    if (mn_xr >= mx_xl && mn_yr >= mx_yl) {
        ans.push_back({mn_xr, mn_yr});
        return true;
    }
    else if (mn_xr >= mx_xl) {
        vector<pair<int, int>> sgs;
        for (R &rc : v) {
            sgs.push_back({rc.yl, rc.yr});
        }
        if (solve_for_line(sgs, k, false, mn_xr))
            return true;
        return false;
    }
    else if (mn_yr >= mx_yl) {
        vector<pair<int, int>> sgs;
        for (R &rc : v) {
            sgs.push_back({rc.xl, rc.xr});
        }
        if (solve_for_line(sgs, k, true, mn_yr))
            return true;
        return false;
    }
    else if (k == 2) {
        for (int y : {mn_yr, mx_yl}) {
            if (solve(remove_covered(v, mn_xr, y), k - 1)) {
                ans.push_back({mn_xr, y});
                return true;
            }
        }
        return false;
    }
    else if (k >= 2) {
        for (int x : {mn_xr, mx_xl}) {
            for (int y : {mn_yr, mx_yl}) {
                if (solve(remove_covered(v, x, y), k - 1)) {
                    ans.push_back({x, y});
                    return true;
                }
            }
        }
        if (k == 3)
            return false;

        pair<int, int> up, rt, lt, dn;
        dn = up = {1, vx.size() - 2};
        lt = rt = {1, vy.size() - 2};

        fill(suf_hor, suf_hor + N, make_pair(1, vy.size() - 2));
        fill(pref_ver, pref_ver + N, make_pair(1, vx.size() - 2));
        fill(suf_ver, suf_ver + N, make_pair(1, vx.size() - 2));
        fill(pref, pref + N, vy.size() - 2);
        fill(suf, suf + N, vy.size() - 2);
        fill(suf_lt, suf_lt + N, vx.size() - 2);
        fill(suf_rt, suf_rt + N, 1);

        for (const R &rc : v) {
            bool d = rc.intersects_hor(mn_yr),
                u = rc.intersects_hor(mx_yl),
                l = rc.intersects_ver(mn_xr),
                r = rc.intersects_ver(mx_xl);

            int ct = d + u + l + r;
            if (ct == 1) {
                if (l)
                    lt = intersection(lt, {rc.yl, rc.yr});
                else if (r)
                    rt = intersection(rt, {rc.yl, rc.yr});
                else if (u)
                    up = intersection(up, {rc.xl, rc.xr});
                else
                    dn = intersection(dn, {rc.xl, rc.xr});
            }
            else if (ct == 2) {
                if (l && r) {
                    suf_hor[rc.yl] = intersection(suf_hor[rc.yl], 
                        make_pair(rc.yl, rc.yr));
                }
                else if (d && l) {
                    pref[rc.xr] = min(pref[rc.xr], rc.yr);
                }
                else if (d && r) {
                    suf[rc.xl] = min(suf[rc.xl], rc.yr);
                }
                else if (l && u) {
                    suf_lt[rc.yl] = min(rc.xr, suf_lt[rc.yl]);
                }
                else if (r && u) {
                    suf_rt[rc.yl] = max(rc.xl, suf_rt[rc.yl]);
                }
                else {
                    pref_ver[rc.xr] = intersection(pref_ver[rc.xr], {rc.xl, rc.xr});
                    suf_ver[rc.xl] = intersection(suf_ver[rc.xl], {rc.xl, rc.xr});
                }
            }
        }

        for (int i = N - 2; i >= 0; i--) {
            suf_hor[i] = intersection(suf_hor[i], suf_hor[i + 1]);
            suf_lt[i] = min(suf_lt[i], suf_lt[i + 1]);
            suf_rt[i] = max(suf_rt[i], suf_rt[i + 1]);
            suf_ver[i] = intersection(suf_ver[i], suf_ver[i + 1]);
            suf[i] = min(suf[i], suf[i + 1]);
        }

        for (int i = 1; i < N; i++) {
            pref_ver[i] = intersection(pref_ver[i], pref_ver[i - 1]);
            pref[i] = min(pref[i], pref[i - 1]);
        }

        for (int i = dn.first; i <= dn.second; i++) {
            int mx_lt = pref[i - 1],
                mx_rt = suf[i + 1];

            pair<int, int> nup = intersection(up, 
                intersection(pref_ver[i - 1], suf_ver[i + 1]));

            if (nup.first > nup.second) {
                continue;
            }

            int y_lt = min(mx_lt, suf_hor[0].second);
            y_lt = min(y_lt, lt.second);
            if (y_lt < lt.first) {
                continue;
            }

            pair<int, int> nrt = intersection(rt, suf_hor[y_lt + 1]);
            if (nrt.first > nrt.second) {
                continue;
            }

            int y_rt = min(mx_rt, nrt.second);

            if (y_rt < nrt.first)
                continue;

            nup = intersection(nup, make_pair(suf_rt[y_rt + 1], suf_lt[y_lt + 1]));

            if (nup.first > nup.second) {
                continue;
            }

            ans.push_back({i, mn_yr});
            ans.push_back({mn_xr, y_lt});
            ans.push_back({mx_xl, y_rt});
            ans.push_back({nup.first, mx_yl});

            return true;
        }

        for (int i = dn.first; i <= dn.second; i++) {
            int mx_lt = pref[i - 1],
                mx_rt = suf[i + 1];

            pair<int, int> nup = intersection(up, 
                intersection(pref_ver[i - 1], suf_ver[i + 1]));

            if (nup.first > nup.second) {
                continue;
            }

            int y_rt = min(mx_rt, suf_hor[0].second);
            y_rt = min(y_rt, rt.second);
            if (y_rt < rt.first) {
                continue;
            }

            pair<int, int> nlt = intersection(lt, suf_hor[y_rt + 1]);
            if (nlt.first > nlt.second) {
                continue;
            }

            int y_lt = min(mx_lt, nlt.second);

            if (y_lt < nlt.first)
                continue;

            nup = intersection(nup, make_pair(suf_rt[y_rt + 1], suf_lt[y_lt + 1]));

            if (nup.first > nup.second) {
                continue;
            }

            ans.push_back({i, mn_yr});
            ans.push_back({mn_xr, y_lt});
            ans.push_back({mx_xl, y_rt});
            ans.push_back({nup.first, mx_yl});

            return true;
        }
    }
    return false;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++) {
        R r;
        cin >> r.xl >> r.yl >> r.xr >> r.yr;
        vx.push_back(r.xl);
        vx.push_back(r.xr);
        vy.push_back(r.yl);
        vy.push_back(r.yr);
        rcs.push_back(r);
    }

    vx.push_back(-1);
    vx.push_back(INF);

    vy.push_back(-1);
    vy.push_back(INF);

    sort(all(vx));
    vx.erase(unique(all(vx)), vx.end());
    sort(all(vy));
    vy.erase(unique(all(vy)), vy.end());

    for (int i = 0; i < n; i++) {
        rcs[i].xl = lower_bound(all(vx), rcs[i].xl) - vx.begin();
        rcs[i].xr = lower_bound(all(vx), rcs[i].xr) - vx.begin();
        rcs[i].yl = lower_bound(all(vy), rcs[i].yl) - vy.begin();
        rcs[i].yr = lower_bound(all(vy), rcs[i].yr) - vy.begin();
    }

    solve(rcs, k);

    while (ans.size() < k)
        ans.push_back({1, 1});

    for (auto p : ans) {
        cout << vx[p.first] << " " << vy[p.second] << "\n";
    }

    return 0;
}       

Compilation message

hamburg.cpp: In function 'int main()':
hamburg.cpp:303:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  303 |     while (ans.size() < k)
      |            ~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 416 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 616 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 5 ms 616 KB Output is correct
14 Correct 19 ms 16216 KB Output is correct
15 Correct 4 ms 728 KB Output is correct
16 Correct 3 ms 732 KB Output is correct
17 Correct 18 ms 16288 KB Output is correct
18 Correct 3 ms 768 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 18 ms 16372 KB Output is correct
21 Correct 4 ms 768 KB Output is correct
22 Correct 4 ms 736 KB Output is correct
23 Correct 18 ms 16364 KB Output is correct
24 Correct 4 ms 720 KB Output is correct
25 Correct 3 ms 768 KB Output is correct
26 Correct 3 ms 768 KB Output is correct
27 Correct 4 ms 768 KB Output is correct
28 Correct 5 ms 640 KB Output is correct
29 Correct 5 ms 640 KB Output is correct
30 Correct 3 ms 768 KB Output is correct
31 Correct 18 ms 16316 KB Output is correct
32 Correct 18 ms 16252 KB Output is correct
33 Correct 20 ms 16352 KB Output is correct
34 Correct 21 ms 16348 KB Output is correct
35 Correct 19 ms 16324 KB Output is correct
36 Correct 21 ms 16308 KB Output is correct
37 Correct 29 ms 16308 KB Output is correct
38 Correct 19 ms 16296 KB Output is correct
39 Correct 19 ms 16288 KB Output is correct
40 Correct 19 ms 16324 KB Output is correct
41 Correct 19 ms 16292 KB Output is correct
42 Correct 18 ms 16260 KB Output is correct
43 Correct 23 ms 16272 KB Output is correct
44 Correct 22 ms 16372 KB Output is correct
45 Correct 4 ms 640 KB Output is correct
46 Correct 18 ms 16256 KB Output is correct
47 Correct 20 ms 16260 KB Output is correct
48 Correct 19 ms 16320 KB Output is correct
49 Correct 24 ms 16380 KB Output is correct
50 Correct 21 ms 16280 KB Output is correct
51 Correct 24 ms 16332 KB Output is correct
52 Correct 18 ms 16336 KB Output is correct
53 Correct 18 ms 16264 KB Output is correct
54 Correct 18 ms 16332 KB Output is correct
55 Correct 20 ms 16352 KB Output is correct
56 Correct 19 ms 16364 KB Output is correct
57 Correct 19 ms 16248 KB Output is correct
58 Correct 19 ms 16372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 329 ms 10004 KB Output is correct
6 Correct 331 ms 10056 KB Output is correct
7 Correct 320 ms 10000 KB Output is correct
8 Correct 318 ms 10016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 343 ms 16184 KB Output is correct
6 Correct 346 ms 15968 KB Output is correct
7 Correct 330 ms 16072 KB Output is correct
8 Correct 350 ms 19272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 351 ms 13380 KB Output is correct
14 Correct 340 ms 13380 KB Output is correct
15 Correct 349 ms 13408 KB Output is correct
16 Correct 367 ms 13400 KB Output is correct
17 Correct 378 ms 13356 KB Output is correct
18 Correct 353 ms 13380 KB Output is correct
19 Correct 337 ms 18332 KB Output is correct
20 Correct 324 ms 19652 KB Output is correct
21 Correct 440 ms 25492 KB Output is correct
22 Correct 354 ms 20016 KB Output is correct
23 Correct 337 ms 23620 KB Output is correct
24 Correct 383 ms 24900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 416 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 616 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 5 ms 616 KB Output is correct
14 Correct 19 ms 16216 KB Output is correct
15 Correct 4 ms 728 KB Output is correct
16 Correct 3 ms 732 KB Output is correct
17 Correct 18 ms 16288 KB Output is correct
18 Correct 3 ms 768 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 18 ms 16372 KB Output is correct
21 Correct 4 ms 768 KB Output is correct
22 Correct 4 ms 736 KB Output is correct
23 Correct 18 ms 16364 KB Output is correct
24 Correct 4 ms 720 KB Output is correct
25 Correct 3 ms 768 KB Output is correct
26 Correct 3 ms 768 KB Output is correct
27 Correct 4 ms 768 KB Output is correct
28 Correct 5 ms 640 KB Output is correct
29 Correct 5 ms 640 KB Output is correct
30 Correct 3 ms 768 KB Output is correct
31 Correct 18 ms 16316 KB Output is correct
32 Correct 18 ms 16252 KB Output is correct
33 Correct 20 ms 16352 KB Output is correct
34 Correct 21 ms 16348 KB Output is correct
35 Correct 19 ms 16324 KB Output is correct
36 Correct 21 ms 16308 KB Output is correct
37 Correct 29 ms 16308 KB Output is correct
38 Correct 19 ms 16296 KB Output is correct
39 Correct 19 ms 16288 KB Output is correct
40 Correct 19 ms 16324 KB Output is correct
41 Correct 19 ms 16292 KB Output is correct
42 Correct 18 ms 16260 KB Output is correct
43 Correct 23 ms 16272 KB Output is correct
44 Correct 22 ms 16372 KB Output is correct
45 Correct 4 ms 640 KB Output is correct
46 Correct 18 ms 16256 KB Output is correct
47 Correct 20 ms 16260 KB Output is correct
48 Correct 19 ms 16320 KB Output is correct
49 Correct 24 ms 16380 KB Output is correct
50 Correct 21 ms 16280 KB Output is correct
51 Correct 24 ms 16332 KB Output is correct
52 Correct 18 ms 16336 KB Output is correct
53 Correct 18 ms 16264 KB Output is correct
54 Correct 18 ms 16332 KB Output is correct
55 Correct 20 ms 16352 KB Output is correct
56 Correct 19 ms 16364 KB Output is correct
57 Correct 19 ms 16248 KB Output is correct
58 Correct 19 ms 16372 KB Output is correct
59 Correct 339 ms 14820 KB Output is correct
60 Correct 345 ms 14788 KB Output is correct
61 Correct 337 ms 14784 KB Output is correct
62 Correct 353 ms 14784 KB Output is correct
63 Correct 338 ms 14788 KB Output is correct
64 Correct 355 ms 14788 KB Output is correct
65 Correct 350 ms 20352 KB Output is correct
66 Correct 337 ms 19780 KB Output is correct
67 Correct 360 ms 26220 KB Output is correct
68 Correct 337 ms 24672 KB Output is correct
69 Correct 359 ms 22212 KB Output is correct
70 Correct 361 ms 26104 KB Output is correct
71 Correct 517 ms 34080 KB Output is correct
72 Correct 525 ms 32888 KB Output is correct
73 Correct 535 ms 33308 KB Output is correct
74 Correct 477 ms 34296 KB Output is correct
75 Correct 464 ms 30060 KB Output is correct
76 Correct 439 ms 30960 KB Output is correct
77 Correct 486 ms 32076 KB Output is correct
78 Correct 560 ms 32820 KB Output is correct
79 Correct 477 ms 32040 KB Output is correct
80 Correct 451 ms 31876 KB Output is correct
81 Correct 520 ms 31000 KB Output is correct
82 Correct 444 ms 30120 KB Output is correct
83 Correct 379 ms 31428 KB Output is correct
84 Correct 366 ms 24512 KB Output is correct
85 Correct 461 ms 33124 KB Output is correct
86 Correct 397 ms 29236 KB Output is correct
87 Correct 426 ms 32928 KB Output is correct
88 Correct 409 ms 32448 KB Output is correct
89 Correct 475 ms 30596 KB Output is correct
90 Correct 573 ms 33248 KB Output is correct
91 Correct 468 ms 30340 KB Output is correct
92 Correct 550 ms 33876 KB Output is correct
93 Correct 534 ms 33264 KB Output is correct
94 Correct 556 ms 33012 KB Output is correct
95 Correct 558 ms 32444 KB Output is correct
96 Correct 517 ms 32228 KB Output is correct
97 Correct 521 ms 33760 KB Output is correct
98 Correct 516 ms 32420 KB Output is correct
99 Correct 501 ms 32180 KB Output is correct
100 Correct 566 ms 33736 KB Output is correct
101 Correct 547 ms 33344 KB Output is correct
102 Correct 451 ms 26972 KB Output is correct
103 Correct 612 ms 33156 KB Output is correct
104 Correct 467 ms 30096 KB Output is correct
105 Correct 571 ms 33796 KB Output is correct
106 Correct 544 ms 34216 KB Output is correct
107 Correct 509 ms 32572 KB Output is correct
108 Correct 551 ms 32552 KB Output is correct
109 Correct 566 ms 33600 KB Output is correct
110 Correct 537 ms 33480 KB Output is correct
111 Correct 522 ms 30848 KB Output is correct
112 Correct 562 ms 33508 KB Output is correct
113 Correct 633 ms 30064 KB Output is correct
114 Correct 661 ms 30064 KB Output is correct
115 Correct 643 ms 30040 KB Output is correct
116 Correct 634 ms 30052 KB Output is correct
117 Correct 459 ms 29236 KB Output is correct
118 Correct 455 ms 29224 KB Output is correct
119 Correct 434 ms 29228 KB Output is correct
120 Correct 461 ms 29164 KB Output is correct
121 Correct 432 ms 29252 KB Output is correct
122 Correct 450 ms 29296 KB Output is correct
123 Correct 444 ms 29216 KB Output is correct
124 Correct 436 ms 29252 KB Output is correct
125 Correct 446 ms 29220 KB Output is correct
126 Correct 447 ms 29204 KB Output is correct