This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//persistent segtree with lazy propagation?!?!?!
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define eb emplace_back
#define pii pair<int,int>
#define f first
#define s second
pii vals[100005];
int lef[100005];
struct node{
int s,e,v;//range sum persistent segtree :)
node *l,*r;
node(int ss=-1,int ee=-1){
s=ss;e=ee;
v=0;l=r=NULL;
if(ss!=-1){//build whole thing for real...
if(s==e)l=r=NULL;
else{
l=new node(s,(s+e)/2);
r=new node((s+e)/2+1,e);
}
}
}
void copy(node *t){
v=t->v;
l=t->l;
r=t->r;
s=t->s;e=t->e;
}
void update(int p,node *t,int d){//copy t...
copy(t);
if(s==e){
v+=d;
return;
}
if(p<=(s+e)/2){
l=new node();
l->update(p,t->l,d);
}else{
r=new node();
r->update(p,t->r,d);
}
v=l->v+r->v;
}
void upd(int p,int d){
if(s==e){
v+=d;return;
}
node *tmp=new node();
if(p<=(s+e)/2){
tmp->update(p,l,d);
l=tmp;
}else{
tmp->update(p,r,d);
r=tmp;
}
v=l->v+r->v;
}
int query(int a,int b){
if(a<=s && e<=b)return v;
int ans=0;
if(a<=(s+e)/2)ans+=l->query(a,b);
if((s+e)/2<b)ans+=r->query(a,b);
return ans;
}
} *st[100005];
vector<pii> v;int it;
void init_sts(int s,int e){//1, a
v.clear();v.eb(s,0);
st[s]=new node(1,e);
for(int i=s+1;i<=e;i++){
st[i]=new node();
if(lef[i]<s)goto end;
it=upper_bound(v.begin(),v.end(),mp(lef[i],LLONG_MAX))-v.begin()-1;
it=v[it].s+1;
if(v.back().s>it)goto end;
if(v.back().s==it){
st[i]->update(max(lef[i],lef[v.back().f]),st[i-1],1);
st[i]->upd(lef[v.back().f],-1);
if(lef[i]>lef[v.back().f])v.back().f=i;//push it to the back...
continue;
}
st[i]->update(lef[i],st[i-1],1);
v.eb(i,it);//increment at i...
continue;
end:;
st[i]->copy(st[i-1]);
}
}
int qq(int l,int r){
return st[r]->query(l,r);
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n,k,a,b;
cin>>n>>k;
vector<pii>disc;//discretisation: raw value, index...
for(int i=0;i<n;i++){
cin>>a>>b;
disc.eb(a,i*2);
disc.eb(b,i*2+1);
}
sort(disc.begin(),disc.end());
a=1;
for(int i=0;i<2*n;i++){
if(i>0 && disc[i].f>disc[i-1].f)a++;
if(disc[i].s&1)vals[disc[i].s/2].s=a;
else vals[disc[i].s/2].f=a;
}
for(int i=0;i<n;i++){
//cout<<vals[i].f<<' '<<vals[i].s<<'\n';
lef[vals[i].s]=max(lef[vals[i].s],vals[i].f);//both should be discretised...
}
init_sts(1,a);//initialise segtrees...
int cur=qq(1,a),tmp;
if(cur<k){
cout<<-1;
return 0;
}//else cout<<cur<<'\n';
set<pii> ss;ss.insert(mp(1,1));ss.insert(mp(a,a));
set<pii>::iterator it,it2;
bool mk[n];memset(mk,0,sizeof(mk));
for(int i=0;i<n;i++){
it=ss.lower_bound(vals[i]);
it2=it--;
if(it2==ss.end())continue;
if(it->s<=vals[i].f && vals[i].s<=it2->f){
tmp=cur;
tmp+=1+(qq(it->s,vals[i].f)+qq(vals[i].s,it2->f) ) - qq(it->s,it2->f);
if(tmp>=k){
mk[i]=true;
cur=tmp;
ss.insert(vals[i]);
}
}
}
int cnt=0;
for(int i=0;i<n && cnt<k;i++){
if(mk[i]){
cout<<i+1<<'\n';
cnt++;
}
}
return 0;
}
/*
5 4
1 3
2 5
8 9
6 8
10 15
*/
# | 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... |