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;
const int N=2e5+10, LG=18;
int n, k;
int l[N], r[N];
vector<int> vv[N];
int jump[LG][N];
int get(int l, int r){
int ans=0;
for (int k=LG-1; k>=0; --k){
if (jump[k][l]<=r){
ans+=1<<k;
l=jump[k][l];
}
}
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
vector<int> vals{-1};
for (int i=1; i<=n; ++i) cin >> l[i] >> r[i], vals.push_back(l[i]), vals.push_back(r[i]);
sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end())-vals.begin());
for (int i=1; i<=n; ++i){
l[i]=lower_bound(vals.begin(), vals.end(), l[i])-vals.begin();
r[i]=lower_bound(vals.begin(), vals.end(), r[i])-vals.begin();
vv[l[i]].push_back(r[i]);
}
int m=(int)vals.size()-1;
jump[0][m+1]=m+1;
for (int i=m; i>=1; --i){
jump[0][i]=jump[0][i+1];
for (auto &j:vv[i]) jump[0][i]=min(jump[0][i], j);
}
for (int k=1; k<LG; ++k) for (int i=1; i<=m+1; ++i) jump[k][i]=jump[k-1][jump[k-1][i]];
set<pair<pair<int, int>, int>> st;
st.insert({{1, m}, get(1, m)});
int sum=st.begin()->second;
if (sum<k){
cout << -1 << '\n';
return 0;
}
for (int i=1; i<=n && k; ++i){
auto it=st.lower_bound({{l[i], (int)1e9}, (int)1e9});
if (it!=st.begin()){
--it;
int ll=it->first.first, rr=it->first.second;
if (ll<=l[i] && r[i]<=rr){
int tl=get(ll, l[i]);
int tr=get(r[i], rr);
if (sum-it->second+tl+tr+1>=k){
sum=sum-it->second+tl+tr+1;
st.erase(it);
if (ll!=l[i]) st.insert({{ll, l[i]}, tl});
if (r[i]!=rr) st.insert({{r[i], rr}, tr});
cout << i << '\n';
--k;
}
}
}
}
return 0;
}
# | 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... |