Submission #1368721

#TimeUsernameProblemLanguageResultExecution timeMemory
1368721AndreyEvent Hopping 2 (JOI21_event2)C++17
100 / 100
90 ms14716 KiB
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 100010;
vector<pair<int,int>> haha(1,{-1,-1});
int bruh[MAXN][18];

int calc(int p, int r) {
    int ans = 0;
    for(int i = 17; i >= 0; i--) {
        if(bruh[p][i] != -1 && haha[bruh[p][i]].second < r) {
            ans+=(1 << i);
            p = bruh[p][i];
        }
    }
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,k,l,r;
    cin >> n >> k;
    vector<pair<int,int>> wut(1,{-1,0});
    for(int i = 1; i <= n; i++) {
        cin >> l >> r;
        r--;
        haha.push_back({l,r});
        wut.push_back({r,i});
    }
    sort(wut.begin(),wut.end());
    for(int i = 0; i <= n; i++) {
        bruh[i][0] = -1;
    }
    int y = 0,big = -2,p = -1;
    for(int i = 0; i <= n; i++) {
        int c = haha[wut[i].second].first;
        if(c > big) {
            big = c;
            p = wut[i].second;
        }
        while(y <= n && wut[y].first < big) {
            bruh[wut[y].second][0] = p;
            y++;
        }
    }
    for(int i = 1; i < 18; i++) {
        for(int j = 0; j <= n; j++) {
            if(bruh[j][i-1] == -1) {
                bruh[j][i] = -1;
            }
            else {
                bruh[j][i] = bruh[bruh[j][i-1]][i-1];
            }
        }
    }
    int sb = calc(0,INT_MAX);
    if(sb < k) {
        cout << -1;
        return 0;
    }
    map<int,int> idk;
    idk[-1] = 0;
    idk[INT_MAX] = -1;
    vector<int> ans(0);
    for(int i = 1; i <= n; i++) {
        int l = haha[i].first,r = haha[i].second;
        pair<int,int> x = *(--idk.upper_bound(l-1));
        pair<int,int> y = *(idk.lower_bound(l));
        if(haha[x.second].second >= l || (y.second != -1 && haha[y.second].first <= r)) {
            continue;
        }
        int a = calc(x.second,y.first);
        int b = calc(x.second,l);
        int c = calc(i,y.first);
        if(sb-a+1+b+c >= k) {
            sb+=1+b+c-a;
            idk[l] = i;
            ans.push_back(i);
        }
    }
    for(int i = 0; i < min((int)ans.size(),k); i++) {
        cout << ans[i] << "\n";
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...