/*
Author of all code: Pedro BIGMAN Dias
Last edit: 15/02/2021
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 500000000LL
#define EPS 0.00000001
#define pi 3.14159
ll mod=1000000007LL;
template<class A=ll>
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}
template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}}
class SucPath
{
public:
ll N;
vector<ll> fo;
vector<vector<ll> > f2; //sparse table of steps powers of 2
ll ms; //max_steps
SucPath() {N=0LL;}
SucPath(vector<ll> x, ll max_steps)
{
N=x.size(); fo=x; ms=max_steps;
vector<ll> xx;
REP(i,0,(ll) (floor(log2(ms)))+1LL) {xx.pb(0LL);}
REP(i,0,N) {f2.pb(xx);}
Conf2(0);
}
void Conf2(ll e) //O(NlogN)
{
if((1LL<<e)>ms) {return;}
if(e==0) {REP(i,0,N) {f2[i][e]=fo[i];} Conf2(e+1);}
else
{
REP(i,0,N)
{
f2[i][e]=f2[f2[i][e-1]][e-1];
}
}
Conf2(e+1);
}
ll f(ll x,ll s) //O(logN)
{
ll ind=0;
while(s>0)
{
if(s%2!=0) {x=f2[x][ind];}
s/=2; ind++;
}
return x;
}
};
ll N;
vector<ll> dp;
SucPath S;
bool check(ll L, ll R, ll val) //checks if one can fit val intervals in [L,R]
{
if(val<=0) {return true;}
ll nxt = S.f(R,val);
if(nxt!=2*N && nxt>=L) {return true;} else {return false;}
}
ll Max_Disjoint(ll L, ll R) //max disjoint intervals in [L,R]
{
bool ok = check(L,R,dp[R]-dp[L]);
if(ok) {return (dp[R]-dp[L]);} else {return (dp[R]-dp[L]-1LL);}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll K; cin>>N>>K; vector<pl> p; pl read; set<ll> vals;
REP(i,0,N) {cin>>read.ff>>read.ss; p.pb(read); vals.insert(read.ff); vals.insert(read.ss);}
unordered_map<ll,ll> comp; ll cur=0LL; set<ll>::iterator it=vals.begin();
while(it!=vals.end()) {comp[*it]=cur; cur++; it++;}
REP(i,0,N) {p[i].ff=comp[p[i].ff]; p[i].ss=comp[p[i].ss];}
vector<ll> end; REP(i,0,2*N) {end.pb(-1);}
REP(i,0,N) {end[p[i].ss]=max(end[p[i].ss],p[i].ff);}
vector<ll> f; REP(i,0,2*N+1) {f.pb(2*N);} ll curmax=-1;
REP(i,0,2*N)
{
curmax=max(curmax,end[i]);
if(curmax==-1) {f[i]=2*N;} else {f[i]=curmax;}
}
REP(i,0,2*N) {dp.pb(0);}
REP(i,0,2*N)
{
if(f[i]==2*N) {dp[i]=0;}
else {dp[i]=dp[f[i]]+1;}
}
SucPath XX(f,N+1LL); S=XX;
if(dp[2*N-1]<K) {cout<<-1<<endl; return 0;}
ll maxsum = dp[2*N-1];
set<pl> inter; //current free intervals
inter.insert({0,2*N-1});
vector<ll> ans; ll L,R; ll l,r;
set<pl>::iterator it2;
REP(i,0,N)
{
if(ans.size()==K) {break;}
L=p[i].ff; R=p[i].ss;
it2=inter.lower_bound({p[i].ff+1,0LL}); if(it2==inter.begin()) {continue;}
it2--;
if(it2->ss < R) {continue;}
l=it2->ff; r=it2->ss;
ll bef = Max_Disjoint(l,r);
ll aft = Max_Disjoint(l,L)+1LL+Max_Disjoint(R,r);
if(maxsum-bef+aft>=K)
{
ans.pb(i);
inter.erase(it2);
if(L>l) {inter.insert({l,L});}
if(r>R) {inter.insert({R,r});}
maxsum=maxsum-bef+aft;
}
else {continue;}
}
REP(i,0,ans.size()) {cout<<ans[i]+1<<"\n";}
return 0;
}
# | 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... |