답안 #875586

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875586 2023-11-20T05:43:20 Z vjudge1 Event Hopping 2 (JOI21_event2) C++17
7 / 100
146 ms 33232 KB
// https://oj.uz/problem/view/JOI21_event2

#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 1e8;

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        int n, k;
        cin >> n >> k;
        vector<int> l(n), r(n);
        for (int i = 0; i < n; i++) cin >> l[i] >> r[i];
        vector<int> ord(n);
        iota(ord.begin(), ord.end(), 0);

        // calc nxt

        sort(ord.begin(), ord.end(), [&](int i, int j) {
                return l[i] < l[j];
        });
        vector<int> suff(n);
        suff.back() = ord.back();
        for (int i = n - 1; i > 0; i--) suff[i - 1] = r[ord[i - 1]] < r[suff[i]] ? ord[i - 1] : suff[i];
        auto sl = l;
        vector<vector<int>> nxt(__lg(n) + 1, vector<int>(n + 1, n));
        sort(sl.begin(), sl.end());
        for (int i = 0; i < n; i++) {
                int j = lower_bound(sl.begin(), sl.end(), r[i]) - sl.begin();
                if (j == n) {
                        nxt[0][i] = n;
                } else {
                        nxt[0][i] = suff[j];
                }
        }

        // calc prv

        sort(ord.begin(), ord.end(), [&](int i, int j) {
                return r[i] < r[j];
        });
        vector<int> pref(n);
        pref[0] = ord[0];
        for (int i = 1; i < n; i++) pref[i] = l[ord[i]] > l[pref[i - 1]] ? ord[i] : pref[i - 1];
        auto sr = r;
        vector<vector<int>> prv(__lg(n) + 1, vector<int>(n + 1, n));
        sort(sr.begin(), sr.end());
        for (int i = 0; i < n; i++) {
                int j = upper_bound(sr.begin(), sr.end(), l[i]) - sr.begin() - 1;
                if (j == -1) {
                        prv[0][i] = n;
                } else {
                        prv[0][i] = pref[j];
                }
        }

        for (int i = 1; i <= __lg(n); i++) {
                for (int j = 0; j < n; j++) {
                        nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
                        prv[i][j] = prv[i - 1][prv[i - 1][j]];
                }
        }

        vector<int> cnt_left(n), cnt_right(n);
        int cnt_all = 0;

        auto try_first = [&](int x) {
                int cnt = 1;
                int l = x, r = x;
                for (int i = __lg(n); i >= 0; i--) {
                        if (nxt[i][r] != n) r = nxt[i][r], cnt += 1 << i, cnt_right[x] += 1 << i;
                        if (prv[i][l] != n) l = prv[i][l], cnt += 1 << i, cnt_left[x] += 1 << i;
                }
                return cnt >= k;
        };

        vector<int> ans;

        set<int> s, set_left, set_right;
        set<pair<int, int>> set_point;

        auto intersect = [&](int x) {
                auto ll = set_left.upper_bound(l[x]);
                auto lr = set_right.upper_bound(l[x]);
                int pos_left, pos_right;
                if (ll == set_left.begin()) {
                        pos_left = -inf - 1;
                } else {
                        pos_left = *prev(ll);
                }
                if (lr == set_right.begin()) {
                        pos_right = -inf + 1;
                } else {
                        pos_right = *prev(lr);
                }
                if (pos_right <= pos_left) return 1;
                auto rl = set_left.lower_bound(r[x]);
                auto rr = set_right.lower_bound(r[x]);
                if (rl == set_left.end()) {
                        pos_left = inf - 1;
                } else {
                        pos_left = *rl;
                }
                if (rr == set_right.end()) {
                        pos_right = inf + 1;
                } else {
                        pos_right = *rr;
                }
                if (pos_left >= pos_right) return 1;
                return 0;
        };

        auto try_add = [&](int x) {
                auto L = set_right.upper_bound(l[x]);
                auto R = set_left.lower_bound(r[x]);
                int limit_left, limit_right;
                if (L == set_right.begin()) {
                        limit_left = -inf;
                } else {
                        limit_left = *prev(L);
                }
                if (R == set_left.end()) {
                        limit_right = +inf;
                } else {
                        limit_right = *R;
                }

                auto right_it = set_point.lower_bound(make_pair(l[x], +inf));
                int cur = right_it == set_point.end() ? cnt_right[(prev(right_it))->second] : cnt_left[right_it->second];
                int le = x, ri = x;
                cnt_left[x] = cnt_right[x] = 0;
                for (int i = __lg(n); i >= 0; i--) {
                        if (nxt[i][ri] != n && r[nxt[i][ri]] <= limit_right) {
                                ri = nxt[i][ri];
                                cnt_right[x] += 1 << i;
                        }
                        if (prv[i][le] != n && l[prv[i][le]] >= limit_left) {
                                le = prv[i][le];
                                cnt_left[x] += 1 << i;
                        }
                }
                if (cnt_all + cnt_left[x] + cnt_right[x] + 1 - cur < k) return 0;
                cnt_all -= cur;
                cnt_all += cnt_left[x] + cnt_right[x] + 1;
                if (right_it != set_point.begin()) {
                        auto left_it = prev(right_it);
                        cnt_right[left_it->second] = cnt_left[x];
                }
                if (right_it != set_point.end()) {
                        cnt_left[right_it->second] = cnt_right[x];
                }
                return 1;
        };

        for (int i = 0; i < n; i++) {
                if (try_first(i)) {
                        ans.emplace_back(i);
                        set_left.emplace(l[i]), set_right.emplace(r[i]);
                        set_point.emplace(l[i], i);
                        cnt_all = cnt_left[i] + cnt_right[i] + 1;
                        break;
                }
        }

        if (ans.size() == 0) return cout << -1, 0;

        for (int i = 0; i < n; i++) {
                if (intersect(i)) continue;
                if (try_add(i) && set_point.size() < k) {
                        ans.emplace_back(i);
                        set_left.emplace(l[i]);
                        set_right.emplace(r[i]);
                        set_point.emplace(l[i], i);
                }
        }

        for (int i : ans) cout << i + 1 << '\n';
}

Compilation message

event2.cpp: In function 'int32_t main()':
event2.cpp:171:52: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  171 |                 if (try_add(i) && set_point.size() < k) {
      |                                   ~~~~~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 146 ms 33232 KB Output is correct
5 Correct 143 ms 32716 KB Output is correct
6 Correct 136 ms 31856 KB Output is correct
7 Correct 129 ms 30412 KB Output is correct
8 Correct 140 ms 33168 KB Output is correct
9 Correct 137 ms 32420 KB Output is correct
10 Correct 136 ms 31692 KB Output is correct
11 Correct 132 ms 30232 KB Output is correct
12 Correct 101 ms 27852 KB Output is correct
13 Correct 101 ms 27340 KB Output is correct
14 Correct 100 ms 26908 KB Output is correct
15 Correct 98 ms 26020 KB Output is correct
16 Correct 59 ms 20940 KB Output is correct
17 Correct 54 ms 20940 KB Output is correct
18 Correct 57 ms 20736 KB Output is correct
19 Correct 46 ms 19484 KB Output is correct
20 Correct 46 ms 19232 KB Output is correct
21 Correct 48 ms 19280 KB Output is correct
22 Correct 46 ms 19104 KB Output is correct
23 Correct 47 ms 19264 KB Output is correct
24 Correct 47 ms 19148 KB Output is correct
25 Correct 49 ms 19228 KB Output is correct
26 Correct 53 ms 19660 KB Output is correct
27 Correct 50 ms 19016 KB Output is correct
28 Correct 44 ms 19156 KB Output is correct
29 Correct 43 ms 19160 KB Output is correct
30 Correct 43 ms 19148 KB Output is correct
31 Correct 45 ms 19112 KB Output is correct
32 Correct 50 ms 19132 KB Output is correct
33 Correct 53 ms 19032 KB Output is correct
34 Correct 97 ms 24268 KB Output is correct
35 Correct 91 ms 22644 KB Output is correct
36 Correct 86 ms 21196 KB Output is correct
37 Correct 96 ms 26572 KB Output is correct
38 Correct 97 ms 26488 KB Output is correct
39 Correct 95 ms 26140 KB Output is correct
40 Correct 95 ms 25808 KB Output is correct
41 Correct 94 ms 25332 KB Output is correct
42 Correct 53 ms 19112 KB Output is correct
43 Correct 103 ms 26316 KB Output is correct
44 Correct 100 ms 26060 KB Output is correct
45 Correct 97 ms 25748 KB Output is correct
46 Correct 95 ms 25548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 452 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 452 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 146 ms 33232 KB Output is correct
5 Correct 143 ms 32716 KB Output is correct
6 Correct 136 ms 31856 KB Output is correct
7 Correct 129 ms 30412 KB Output is correct
8 Correct 140 ms 33168 KB Output is correct
9 Correct 137 ms 32420 KB Output is correct
10 Correct 136 ms 31692 KB Output is correct
11 Correct 132 ms 30232 KB Output is correct
12 Correct 101 ms 27852 KB Output is correct
13 Correct 101 ms 27340 KB Output is correct
14 Correct 100 ms 26908 KB Output is correct
15 Correct 98 ms 26020 KB Output is correct
16 Correct 59 ms 20940 KB Output is correct
17 Correct 54 ms 20940 KB Output is correct
18 Correct 57 ms 20736 KB Output is correct
19 Correct 46 ms 19484 KB Output is correct
20 Correct 46 ms 19232 KB Output is correct
21 Correct 48 ms 19280 KB Output is correct
22 Correct 46 ms 19104 KB Output is correct
23 Correct 47 ms 19264 KB Output is correct
24 Correct 47 ms 19148 KB Output is correct
25 Correct 49 ms 19228 KB Output is correct
26 Correct 53 ms 19660 KB Output is correct
27 Correct 50 ms 19016 KB Output is correct
28 Correct 44 ms 19156 KB Output is correct
29 Correct 43 ms 19160 KB Output is correct
30 Correct 43 ms 19148 KB Output is correct
31 Correct 45 ms 19112 KB Output is correct
32 Correct 50 ms 19132 KB Output is correct
33 Correct 53 ms 19032 KB Output is correct
34 Correct 97 ms 24268 KB Output is correct
35 Correct 91 ms 22644 KB Output is correct
36 Correct 86 ms 21196 KB Output is correct
37 Correct 96 ms 26572 KB Output is correct
38 Correct 97 ms 26488 KB Output is correct
39 Correct 95 ms 26140 KB Output is correct
40 Correct 95 ms 25808 KB Output is correct
41 Correct 94 ms 25332 KB Output is correct
42 Correct 53 ms 19112 KB Output is correct
43 Correct 103 ms 26316 KB Output is correct
44 Correct 100 ms 26060 KB Output is correct
45 Correct 97 ms 25748 KB Output is correct
46 Correct 95 ms 25548 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Incorrect 0 ms 452 KB Output isn't correct
52 Halted 0 ms 0 KB -