This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, k; cin>>n>>k;
    vector<int> l(n + 1), r(n + 1), all1;
    for (int i = 1; i <= n; i++){
        cin>>l[i]>>r[i];
        all1.pb(l[i]);
        all1.pb(r[i]);
    }
    
    sort(all1.begin(), all1.end());
    vector<int> all;
    for (int i: all1){
        if (all.empty() || all.back() != i){
            all.pb(i);
        }
    }
    
    vector<int> :: iterator it;
    for (int i = 1; i <= n; i++){
        it = lower_bound(all.begin(), all.end(), l[i]);
        l[i] = (int) (it - all.begin()) + 1;
        it = lower_bound(all.begin(), all.end(), r[i]);
        r[i] = (int) (it - all.begin()) + 1;
    }
    
    // i -> j: r[i] <= l[j]
    
    int N = (int) all.size();
    vector<int> st[N + 1];
    for (int i = 1; i <= n; i++){
        st[l[i]].pb(r[i]);
    }
    
    vector<int> h(N + 1);
    for (int i = N - 1; i > 0; i--){
        h[i] = h[i + 1];
        for (int r: st[i]){
            h[i] = max(h[i], 1 + h[r]);
        }
    }
    vector<int> out;
    int pre = 0, tt = k;
    while (tt--){
        bool ind = 1;
        for (int i = 1; i <= n; i++){
            if (l[i] < pre) continue;
            int x = k - (int) out.size() - 1;
            if (h[r[i]] >= x){
                out.pb(i);
                pre = r[i];
                ind = 0;
                break;
            }
        }
        if (ind){
            cout<<-1<<"\n";
            return 0;
        }
    }
    sort(out.begin(), out.end());
    for (int i: out) cout<<i<<" ";
    cout<<"\n";
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |