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>
#define int long long
using namespace std;
struct Inter {
int deb, fin, id;
bool operator <(const Inter& other) const {
if(deb == other.deb)
return fin < other.fin;
return deb < other.deb;
}
};
const int N = 1e5 + 42, T = 17, INF = 1e9 + 42;
set<Inter> ans;
vector<int> toPrint;
Inter inter[N], inter2[N];
int n, k, nb = 0, bl[T][N];
int getNb(int a, int b) {
int ans = 0;
for(int i = T-1; i > -1; i--)
if(inter[bl[i][a]].fin <= inter[b].deb)
a = bl[i][a], ans += (1 << i);
return ans;
}
void tryInsert(int i) {
if(((int)ans.size()) == k)
return;
auto it = ans.lower_bound(inter[i]);
if(inter[i].fin > (*it).deb && (*it).fin > inter[i].deb)
return;
int aft = (*it).id;
it--;
if(inter[i].fin > (*it).deb && (*it).fin > inter[i].deb)
return;
int pre = (*it).id,
ch = getNb(pre, i) + getNb(i, aft) + 1 - getNb(pre, aft);
if(nb + ch >= k) {
nb += ch;
toPrint.push_back(i+1);
ans.insert(inter[i]);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 0; i < n; i++)
cin >> inter[i].deb >> inter[i].fin;
inter[n].deb = (inter[n].fin = -INF) - 1;
inter[n+1].deb = (inter[n+1].fin = INF) - 1;
n += 2;
for(int i = 0; i < n; i++)
inter[i].id = i;
for(int i = 0; i < n; i++)
inter2[i].id = i,
inter2[i].deb = inter[i].deb,
inter2[i].fin = inter[i].fin;
sort(inter, inter + n,
[](Inter a, Inter b) {
return a.deb > b.deb;
});
sort(inter2, inter2 + n,
[](Inter a, Inter b) {
return a.fin > b.fin;
});
int imin = -1;
for(int i = 0, j = 0; i < n; i++) {
while(j < n && inter[j].deb >= inter2[i].fin) {
if(imin == -1 || inter[j].fin < inter[imin].fin)
imin = j;
j++;
}
bl[0][inter2[i].id] = inter[imin].id;
}
bl[0][n-1] = n-1;
for(int i = 1; i < T; i++)
for(int j = 0; j < n; j++)
bl[i][j] = bl[i-1][bl[i-1][j]];
sort(inter, inter + n,
[](Inter a, Inter b) {
return a.id < b.id;
});
n -= 2;
k += 2;
ans.insert(inter[n]);
ans.insert(inter[n+1]);
nb = 2 + getNb(n, n+1);
for(int i = 0; i < n; i++)
tryInsert(i);
if(k-2 == (int)toPrint.size()) {
for(int i : toPrint)
cout << i << '\n';
} else {
cout << "-1";
}
}
# | 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... |