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;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
struct range{ int l,r,i; };
vector<range> sl;
int n,k,p[100005][20];
ii sfx[100005];
vi ans;
vii a;
set<ii> s;
int num(int l,int r){ //counts number of things in this range
int lo=0,hi=n-1,mid,res=-1;
while(lo<=hi){
mid=(lo+hi)/2;
if(l<=sl[mid].l)res=mid,hi=mid-1;
else lo=mid+1;
}
if(res==-1)return 0;
int st=sfx[res].se;
if(sfx[res].fi>r)return 0;
int ans=1;
for(int k=19;k>=0;--k){
if(p[st][k]!=-1&&a[p[st][k]].se<=r){
ans+=(1<<k);
st=p[st][k];
}
}
return ans;
}
int main(){
sf("%d%d",&n,&k);
for(int i=0;i<n;++i){
int l,r;sf("%d%d",&l,&r);
a.pb(l,r);
sl.push_back({l,r,i});
}
sort(all(sl),[&](range &a,range &b){return a.l<b.l;});
sfx[n]=ii(INF,-1);
for(int i=n-1;i>=0;--i){
sfx[i]=min(sfx[i+1],ii(sl[i].r,sl[i].i));
}
for(int i=0;i<n;++i){
int lo=0,hi=n-1,mid,res=-1;
while(lo<=hi){
mid=(lo+hi)/2;
if(a[i].se<=sl[mid].l)res=mid,hi=mid-1;
else lo=mid+1;
}
if(res==-1)p[i][0]=-1;
else p[i][0]=sfx[res].se;
}
for(int k=1;k<20;++k){
for(int i=0;i<n;++i){
if(p[i][k-1]!=-1)p[i][k]=p[p[i][k-1]][k-1];
else p[i][k]=-1;
}
}
int tot=num(0,1e9);
if(tot<k)pf("-1\n"),exit(0);
s.insert({0,1e9});
for(int i=0;i<n;++i){
auto it=s.upper_bound({a[i].fi,INF});
if(it==s.begin())continue;
else --it;
int l,r;tie(l,r)=*it;
if(r<a[i].se)continue;
int nw=tot-num(l,r)+num(l,a[i].fi)+num(a[i].se,r)+1;
if(nw>=k){
tot=nw;
s.erase(it);
s.insert({l,a[i].fi});
s.insert({a[i].se,r});
ans.pb(i);
}
}
if(sz(ans)<k)pf("-1\n");
else{
for(int i=0;i<k;++i)pf("%d\n",ans[i]+1);
}
}
Compilation message (stderr)
event2.cpp: In function 'int main()':
event2.cpp:71:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | sf("%d%d",&n,&k);
| ^
event2.cpp:73:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | int l,r;sf("%d%d",&l,&r);
| ^
# | 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... |