제출 #1336348

#제출 시각아이디문제언어결과실행 시간메모리
1336348MisterReaperEvent Hopping 2 (JOI21_event2)C++20
100 / 100
129 ms29272 KiB
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG
    #include "debug.h"
#else
    #define debug(...) void(23)
#endif

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

    int N, K;
    std::cin >> N >> K;

    std::vector<std::array<int, 2>> A(N);
    for (int i = 0; i < N; ++i) {
        std::cin >> A[i][0] >> A[i][1];
    }

    std::vector<int> zip;
    for (int i = 0; i < N; ++i) {
        zip.emplace_back(A[i][0]);
        zip.emplace_back(A[i][1]);
    }
    std::sort(zip.begin(), zip.end());
    zip.erase(std::unique(zip.begin(), zip.end()), zip.end());
    int n = int(zip.size());
    for (int i = 0; i < N; ++i) {
        A[i][0] = int(std::lower_bound(zip.begin(), zip.end(), A[i][0]) - zip.begin());
        A[i][1] = int(std::lower_bound(zip.begin(), zip.end(), A[i][1]) - zip.begin());
    }

    std::vector<std::vector<int>> rights(n);
    for (int i = 0; i < N; ++i) {
        rights[A[i][0]].emplace_back(A[i][1]);
    }

    const int LG = std::__lg(n);

    std::vector<std::vector<int>> par(LG + 1, std::vector<int>(n + 1));
    par[0][n] = n;
    for (int i = n - 1, j = n; i >= 0; --i) {
        for (auto k : rights[i]) {
            j = std::min(j, k);
        }
        par[0][i] = j;
    }
    debug(par[0]);
    for (int i = 1; i <= LG; ++i) {
        for (int j = 0; j <= n; ++j) {
            par[i][j] = par[i - 1][par[i - 1][j]];
        }
    }

    auto count = [&](int l, int r) -> int {
        int res = 0;
        for (int i = LG; i >= 0; --i) {
            if (par[i][l] <= r) {
                res += 1 << i;
                l = par[i][l];
            }
        }
        return res;
    };

    int now = count(0, n - 1);
    if (now < K) {
        std::cout << "-1\n";
        return 0;
    }

    std::vector<int> ans;
    std::set<std::pair<int, int>> taken;
    taken.insert({0, 0});
    taken.insert({n - 1, n - 1});

    for (int i = 0; i < N && ans.size() != K; ++i) {
        auto[l, r] = A[i];
        auto itl = --taken.lower_bound({r, 0});
        auto itr = std::next(itl);
        if (itl->second > l) {
            continue;
        }
        auto x = itl->second;
        auto y = itr->first;
        assert(x <= l && l < r && r <= y);
        int tmp = now - count(x, y) + count(x, l) + count(r, y) + 1;
        if (tmp >= K) {
            now = tmp;
            ans.emplace_back(i);
            taken.emplace(l, r);
        }
    }

    for (auto i : ans) {
        std::cout << i + 1 << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...