# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
594743 | inksamurai | Event Hopping 2 (JOI21_event2) | C++17 | 0 ms | 0 KiB |
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 rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3uHasSr ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
#include <atcoder/all>
int op(int l,int r){return l+r;}
int e(){return 0;}
using T=pair<pii,int>;
signed main(){
_3uHasSr;
int n,k;
cin>>n>>k;
vec(T) a(n);
rep(i,n){
cin>>a[i].fi.fi>>a[i].fi.se;
a[i].se=i;
}
vi tmp;
int m;
{
rep(i,n){
tmp.pb(a[i].fi.fi);
tmp.pb(a[i].fi.se);
}
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()),tmp.end());
m=sz(tmp);
}
rep(i,n){
pii p=a[i].fi;
p.fi=(int)(lower_bound(tmp.begin(), tmp.end(),p.fi)-tmp.begin());
p.se=(int)(lower_bound(tmp.begin(), tmp.end(),p.se)-tmp.begin());
a[i].fi=p;
}
sort(a.begin(), a.end(),[&](const T&l,const T&r){
return l.fi.se<r.fi.se;
});
vec(pii) rs(n);
rep(i,n){
rs[a[i].se]=a[i].fi;
}
// rep(i,n){
// print(rs[i].fi,rs[i].se);
// }
// print("....");
atcoder::segtree<int,op,e> segl(m),segr(m);
std::multiset<pii> stl,str;
// [l,r] [r,l]
int _lst=-1,cnt=0;
vi usd(n,0);
rep(i,n){
pii p=a[i].fi;
// _lst <= p.fi
if(_lst<=p.fi){
usd[a[i].se]=1;
cnt+=1;
_lst=p.se;
segr.set(p.se,segr.get(p.se)+1);
segl.set(p.fi,segl.get(p.fi)+1);
stl.insert(pii(p.fi,p.se));
str.insert(pii(p.se,p.fi));
}
}
if(cnt<k){
print(-1);
return 0;
}
std::map<pii,vi> mp;
set<int> pns;
rep(i,n){
if(usd[i]){
pns.insert(i);
mp[rs[i]].pb(i);
}
}
set<int> seen;
rep(i,n){
pii p=rs[i];
if(usd[i]){
seen.insert(p.fi);
seen.insert(p.se);
continue;
}
auto __it=seen.lower_bound(p.fi);
if(__it!=seen.end() and *__it<p.se){
// contains smol
continue;
}
int now=0;
auto it=str.lower_bound(pii(p.fi+1,-1));
if(it!=str.begin()){
it=prev(it);
pii _p=*it;
// previous non intersect [r,l]
now+=segr.prod(0,_p.fi+1);
}
auto ti=stl.lower_bound(pii(p.se,-1));
if(ti!=stl.end()){
pii _p=*ti;
// previous non intersect [l,r]
now+=segl.prod(_p.fi,m);
}
if(now+1>=k){
pns.insert(i);
// should delete every segment inside
auto ti=stl.lower_bound(pii(p.se,-1));
if(ti!=stl.begin()){
ti=prev(ti);
while(1){
auto nti=ti;
pii _p=*ti;
if(_p.se<=p.fi){
break;
}
bool lok=0;
if(ti!=stl.begin()){
ti=prev(ti);
}else{
lok=1;
}
stl.erase(nti);
str.erase(str.find(pii(_p.se,_p.fi)));
segr.set(_p.se,segr.get(_p.se)-1);
segl.set(_p.fi,segl.get(_p.fi)-1);
assert(sz(mp[_p]));
print(_p.fi,_p.se);
pns.erase(pns.find(mp[_p].back()));
mp[_p].pop_back();
if(lok){
break;
}
}
}
}
}
while(sz(pns)>k){
pns.erase(prev(pns.end()));
}
for(auto v:pns){
cout<<v+1<<"\n";
}
//
return 0;
}