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 Z = 2e5+2, B = 17, INF = 2e9;
int N, K, L[Z], R[Z];
int pR[Z][B], pL[Z][B];
set<array<int, 3>> intervals;
int solve(int i, int l, int r) {
int j = i, res = 1;
for(int k = B; k--; )
if(pR[j][k] <= N && R[pR[j][k]] <= r)
j = pR[j][k], res += 1<<k;
j = i;
for(int k = B; k--; )
if(pL[j][k] >= 1 && l <= L[pL[j][k]])
j = pL[j][k], res += 1<<k;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
array<int, 2> pts[N * 2];
for(int i = 1; i <= N; ++i) {
cin >> L[i] >> R[i];
--R[i];
pts[2 * i - 1] = {L[i], -i};
pts[2 * i - 2] = {R[i], +i};
}
L[N+1] = R[N+1] = INF;
sort(pts, pts + N * 2);
int j = 0;
pL[0][0] = 0;
pL[N+1][0] = max_element(L+1, L+N+1) - L;
for(auto &[x, i] : pts) {
if(i < 0)
pL[-i][0] = j;
else if(L[i] > L[j])
j = i;
}
reverse(pts, pts + N * 2);
j = N + 1;
pR[0][0] = min_element(R+1, R+N+1) - R;
pR[N+1][0] = N+1;
for(auto &[x, i] : pts) {
if(i > 0)
pR[i][0] = j;
else if(R[-i] < R[j])
j = -i;
}
for(j = 0; j + 1 < B; ++j) {
for(int i = 0; i <= N + 1; ++i) {
pL[i][j+1] = pL[pL[i][j]][j];
pR[i][j+1] = pR[pR[i][j]][j];
}
}
int sum = solve(0, 0, INF - 1) - 1, req = K;
if(sum < K) {
cout << -1;
return 0;
}
intervals.insert({L[0], 0, sum});
intervals.insert({L[N+1], N+1, 0});
for(int i = 1; i <= N && req; ++i) {
auto fR = intervals.lower_bound({R[i] + 1});
auto fL = prev(fR);
if(R[(*fL)[1]] >= L[i]) continue;
if(sum - (*fL)[2] + solve(i, R[(*fL)[1]]+1, (*fR)[0]-1) < K) continue;
--req;
array<int, 3> newL = *fL;
newL[2] = solve((*fL)[1], (*fL)[0], L[i] - 1);
int canDo = solve(i, L[i], (*fR)[0]-1);
sum += newL[2] + canDo - (*fL)[2];
intervals.erase(fL);
intervals.insert(newL);
intervals.insert({L[i], i, canDo});
cout << i << '\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... |