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 = 1e5+2, B = 17, INF = 2e9;
int N, K, L[Z], R[Z], p[Z][B];
set<array<int, 3>> s;
int solve(int i, int r) {
int res = 1;
for(int k = B; k--; )
if(p[i][k] <= N && R[p[i][k]] <= r)
i = p[i][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 = N + 1;
p[0][0] = min_element(R+1, R+N+1) - R;
p[N+1][0] = N+1;
for(auto &[x, i] : pts) {
if(i < 0)
p[-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)
p[i][j+1] = p[p[i][j]][j];
int sum = solve(0, INF - 1) - 1, req = K;
if(sum < K) {
cout << -1;
return 0;
}
s.insert({L[0], 0, sum+1});
s.insert({L[N+1], N+1, 0});
for(int i = 1; i <= N && req; ++i) {
auto f = s.lower_bound({R[i] + 1});
auto fR = *f, fL = *prev(f);
if(R[fL[1]] >= L[i]) continue;
int canL = solve(fL[1], L[i] - 1);
int canR = solve(i, fR[0] - 1);
if(sum - fL[2] + canL + canR < K) continue;
--req;
sum += canL + canR - fL[2];
s.erase(prev(f));
s.insert({fL[0], fL[1], canL});
s.insert({L[i], i, canR});
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... |