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;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 100001;
const int K = 20;
const int INF = (int)1e9;
int n, k, nxt[N][K];
pair <int, int> tree[4 * N];
vector <pair <int, int> > a;
void update(int idx){
int p = a[idx].first + 2 * n;
tree[p] = min(tree[p], make_pair(a[idx].second, idx));
for(; p > 1 ; p >>= 1){
tree[p >> 1] = min(tree[p], tree[p ^ 1]);
}
}
pair <int, int> query(int l, int r){
r++;
auto ret = make_pair(INF, INF);
for(l += 2 * n, r += 2 * n ; l < r ; l >>= 1, r >>= 1){
if(l & 1) ret = min(ret, tree[l++]);
if(r & 1) ret = min(ret, tree[--r]);
}
return ret;
}
int calc(int l, int r){
if(r < l) return 0;
int idx = query(l, r).second;
if(idx == INF) return 0;
int ret = 1;
for(int i = K - 1 ; i >= 0 ; i--){
if(nxt[idx][i] == INF) continue;
if(a[nxt[idx][i]].second > r) continue;
ret += (1 << i);
idx = nxt[idx][i];
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
for(int i = 0 ; i < 4 * N ; i++){
tree[i] = make_pair(INF, INF);
}
cin >> n >> k;
a.resize(n);
for(auto &i : a) cin >> i.first >> i.second;
map <int, int> mp;
for(auto &i : a) mp[i.first], mp[i.second];
int x = 0;
for(auto &i : mp) i.second = x++;
for(auto &i : a){
i.first = mp[i.first];
i.second = mp[i.second];
}
for(int i = 0 ; i < n ; i++){
update(i);
}
for(int i = 0 ; i < n ; i++){
nxt[i][0] = query(a[i].second, 2 * n - 1).second;
}
vector <int> all;
for(int i = 0 ; i < n ; i++){
all.push_back(i);
}
sort(all.begin(), all.end(), [&](int l, int r){
return a[l].first > a[r].first;
});
for(auto &i : all){
for(int k = 1 ; k < K ; k++){
if(nxt[i][k - 1] == INF) nxt[i][k] = INF;
else nxt[i][k] = nxt[nxt[i][k - 1]][k - 1];
}
}
set <pair <int, int> > cur;
cur.insert(make_pair(0, 2 * n - 1));
int cnt = calc(0, 2 * n - 1);
if(cnt < k) finish(-1);
vector <int> res;
for(int i = 0 ; i < n ; i++){
auto it = cur.upper_bound(make_pair(a[i].first, INF));
it--;
if(!(it->first <= a[i].first && a[i].second <= it->second)) continue;
cnt -= calc(it->first, it->second);
if(cnt + (int)res.size() + 1 + calc(it->first, a[i].first) + calc(a[i].second, it->second) < k){
cnt += calc(it->first, it->second);
continue;
}
res.push_back(i);
cur.insert(make_pair(it->first, a[i].first));
cnt += calc(it->first, a[i].first);
cur.insert(make_pair(a[i].second, it->second));
cnt += calc(a[i].second, it->second);
cur.erase(it);
}
while((int)res.size() > k) res.pop_back();
//~ assert((int)res.size() == k);
for(auto &i : res) cout << i + 1 << "\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... |