Submission #1328734

#TimeUsernameProblemLanguageResultExecution timeMemory
1328734thelegendary08Event Hopping 2 (JOI21_event2)C++17
100 / 100
128 ms33036 KiB
#include<bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define vi vector<int>
#define pii pair<int,int>
#define f0r(i,n) for(int i = 0; i < n; i++)
#define FOR(i,k,n) for(int i = k; i < n; i++)
#define dout(x) cout<<x<<' '<<#x<<endl
#define dout2(x,y) cout<<x<<' '<<#x<<' '<<y<<' '<<#y<<endl
#define vout(v) cout<<#v<<": "; for(auto u : v)cout<<u<<' '; cout<<endl
using namespace std;
const int mxn = 1e5 + 5, lg = 17; 
/*
greedily check in increasing index, given currently selected indices, is there a way to schedule the rest s.t. we can take k ranges
*/

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	//ifstream cin(".in");
	//ofstream cout(".out");
	// fact[0]=1;FOR(i,1,mxn)fact[i]=fact[i-1]*i;inv[mxn-1]=fact[mxn-1].inv();r0f(i,mxn-1)inv[i]=inv[i+1]*(i+1);
	int n,k; cin>>n>>k; vector<pair<pii,int>>w; vector<pii>v; f0r(i,n){int l,r; cin>>l>>r; v.eb(l,r); w.pb(mp(mp(l,r),i));}
	sort(w.begin(),w.end()); vector<pair<pii,int>>suf(n); suf[n-1]=w[n-1]; for(int i = n-2; i >= 0; i--){
		if(suf[i+1].F.S < w[i].F.S)suf[i]=suf[i+1]; else suf[i]=w[i];
	} 
	vi nxt(n+1); nxt[n] = suf[0].S; f0r(i,n){
		int lo = i+1, hi = n; while(lo < hi){
			int mid = lo + (hi-lo) / 2; if(w[mid].F.F >= w[i].F.S)hi=mid; else lo=mid+1;
		} if(lo==n)nxt[w[i].S]=-1; else nxt[w[i].S]=suf[lo].S; 
	}// vout(nxt);
	vector<vi> up(n+1, vi(lg)); f0r(i,n+1)up[i][0] = nxt[i]; FOR(j,1,lg)f0r(i,n+1){
		if(up[i][j-1] == -1)up[i][j] = -1; else up[i][j] = up[up[i][j-1]][j-1]; 
	}
	vi sz(n+1); set<pii>s; s.insert(mp(0,n)); int cur = n, cnt = 0; for(int i = lg-1; i >= 0; i--){
		if(up[cur][i] != -1)cnt += (1<<i), cur = up[cur][i];
	} sz[n] = cnt; if(cnt < k){cout<<-1; return 0;} vi ans; int sum = cnt; //sum is cur range count
	vi pos(n+1); pos[n] = 0; f0r(i,n)pos[w[i].S] = i+1; v.pb(mp(0,0)); 
	f0r(i,n){
		pii cmp = mp(pos[i],i); auto it = s.lower_bound(cmp); 
		int ct = 1, cur = i; int c1; 
		if(it != s.end()){
			int d = (*it).S; if(v[d].F < v[i].S)continue; 
			for(int j = lg-1; j >= 0; j--){
				if(up[cur][j] != -1 && v[up[cur][j]].S <= v[d].F)ct += (1 << j), cur = up[cur][j];
			}
		}
		else{
			for(int j = lg-1; j >= 0; j--){
				if(up[cur][j] != -1)ct += (1 << j), cur = up[cur][j];
			}
		}
		c1 = ct; 
		--it; int d = (*it).S; if(v[d].S > v[i].F)continue; int c2 = 0; if(d != n)ct++, c2++; cur = d; for(int j = lg-1; j>=0; j--){
			if(up[cur][j] != -1 && v[up[cur][j]].S <= v[i].F)ct += (1 << j), c2 += (1 << j), cur = up[cur][j];
		} //dout(ct);
		if(sum - sz[d] + ct >= k){
			sum -= sz[d], sum += ct; sz[d] = c2; sz[i] = c1; s.insert(cmp); ans.pb(i);
		}
	} while(ans.size() > k)ans.pop_back(); for(auto u : ans)cout<<u+1<<'\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...