Submission #947076

#TimeUsernameProblemLanguageResultExecution timeMemory
947076amirhoseinfar1385Event Hopping 2 (JOI21_event2)C++17
100 / 100
253 ms65692 KiB
#include<bits/stdc++.h> using namespace std; const int maxn=200000+10,lg=20; vector<long long>res; long long n,k,sp[lg][maxn+10],dp[maxn],lnk[maxn],last[maxn]; pair<long long,long long>all[maxn]; vector<long long>alll[maxn],allr[maxn]; long long ted=0; vector<pair<long long,long long>>allv; set<pair<long long,long long>>inds; long long callen(long long ind){ if(ind>=(int)allv.size()){ return 0; } long long l=allv[ind].first; auto x=inds.lower_bound(make_pair(l,l)); long long bad=(*x).first; x--; long long ghabl=(*x).second; if(l<=ghabl){ return 0; } if(lnk[l]>=bad||l>=bad){ return 0; } long long ret=1; for(int i=lg-1;i>=0;i--){ if(sp[i][l]<bad&&lnk[sp[i][l]]<bad){ ret+=(1<<i); l=sp[i][l]; } } // while(lnk[l]<bad&&l<bad){ // ret++; // l=sp[0][l]; // } return ret; } void vorod(){ cin>>n>>k; vector<long long>allind; for(long long i=1;i<=n;i++){ cin>>all[i].first>>all[i].second; all[i].second--; allind.push_back(all[i].first); allind.push_back(all[i].second); } sort(allind.begin(),allind.end()); allind.resize(unique(allind.begin(),allind.end())-allind.begin()); for(long long i=1;i<=n;i++){ all[i].first=lower_bound(allind.begin(),allind.end(),all[i].first)-allind.begin(); all[i].second=lower_bound(allind.begin(),allind.end(),all[i].second)-allind.begin(); alll[all[i].first].push_back(i); allr[all[i].second].push_back(i); } } void pre(){ inds.insert(make_pair(-1,-1)); inds.insert(make_pair(maxn,maxn)); set<pair<long long,long long>>st; for(long long i=0;i<maxn;i++){ last[i]=maxn+2; } for(long long i=0;i<maxn;i++){ long long mn=maxn+2; for(auto x:alll[i]){ mn=min(mn,all[x].second); } while((long long)st.size()>0&&(*st.rbegin()).first>=mn){ st.erase(*st.rbegin()); } for(auto x:alll[i]){ if(all[x].second==mn){ st.insert(make_pair(all[x].second,x)); mn=-1; } } for(auto x:allr[i]){ if(st.count(make_pair(i,x))==1){ st.erase(make_pair(i,x)); allv.push_back(make_pair(all[x].first,all[x].second)); last[all[x].first-1]=all[x].first; lnk[all[x].first]=all[x].second; } } } for(long long i=maxn-2;i>=0;i--){ last[i]=min(last[i],last[i+1]); } for(long long i=0;i<maxn+9;i++){ for(long long j=0;j<lg;j++){ sp[j][i]=maxn+2; } } for(long long i=1;i<=n;i++){ sp[0][all[i].first]=min(sp[0][all[i].first],last[lnk[all[i].first]]); } for(long long i=1;i<lg;i++){ for(long long j=0;j<maxn;j++){ sp[i][j]=sp[i-1][sp[i-1][j]]; } } dp[0]=callen(0); ted+=dp[0]; } bool eshy(long long l,long long r,long long ll,long long rr){ if(l>ll){ swap(l,ll); swap(r,rr); } return ll<=r; } bool esh(long long a,long long b){ return eshy(all[a].first,all[a].second,all[b].first,all[b].second); } /*long long cal(){ long long ret=0,r=-1; for(auto x:allv){ long long f=1; auto z=inds.lower_bound(make_pair(x.first,x.first)); if((long long)inds.size()>0&&(*inds.rbegin()).first>=x.first){ if(eshy(x.first,x.second,(*z).first,(*z).second)){ f=0; } } if((long long)inds.size()>0&&(*inds.begin()).first<x.first){ z--; if(eshy(x.first,x.second,(*z).first,(*z).second)){ f=0; } } if(x.first<=r||f==0){ continue; } ret++; r=x.second; } return ret; }*/ void ins(long long i){ res.push_back(i); auto x=inds.lower_bound(make_pair(all[i].first,all[i].second)); auto bad=*x; x--; auto ghabl=*x; long long p1=lower_bound(allv.begin(),allv.end(),make_pair(ghabl.second+1,-1ll))-allv.begin(); long long p2=lower_bound(allv.begin(),allv.end(),make_pair(all[i].second+1,-1ll))-allv.begin(); long long p3=lower_bound(allv.begin(),allv.end(),make_pair(bad.second+1,-1ll))-allv.begin(); inds.insert(make_pair(all[i].first,all[i].second)); if(p1!=p3){ ted-=dp[p1]; } dp[p1]=callen(p1); dp[p2]=callen(p2); if(p1!=p2){ ted+=dp[p1]; } if(p2!=p3){ ted+=dp[p2]; } } void erase(long long i){ res.pop_back(); inds.erase(make_pair(all[i].first,all[i].second)); auto x=inds.lower_bound(make_pair(all[i].first,all[i].second)); auto bad=*x; x--; auto ghabl=*x; long long p1=lower_bound(allv.begin(),allv.end(),make_pair(ghabl.second+1,-1ll))-allv.begin(); long long p2=lower_bound(allv.begin(),allv.end(),make_pair(all[i].second+1,-1ll))-allv.begin(); long long p3=lower_bound(allv.begin(),allv.end(),make_pair(bad.second+1,-1ll))-allv.begin(); if(p1!=p2){ ted-=dp[p1]; } if(p2!=p3){ ted-=dp[p2]; } dp[p1]=callen(p1); dp[p2]=callen(p2); if(p1!=p3){ ted+=dp[p1]; } } void can(long long i){ auto x=all[i]; auto z=inds.lower_bound(make_pair(x.first,x.first)); if((long long)inds.size()>0&&(*inds.rbegin()).first>=x.first){ if(eshy(x.first,x.second,(*z).first,(*z).second)){ return ; } } if((long long)inds.size()>0&&(*inds.begin()).first<x.first){ z--; if(eshy(x.first,x.second,(*z).first,(*z).second)){ return ; } } ins(i); if(ted+(long long)res.size()<k){ erase(i); } } void solve(){ for(long long i=1;i<=n;i++){ if((long long)res.size()==k){ break; } can(i); } } void khor(){ if((long long)res.size()<k){ cout<<-1<<"\n"; return ; } for(auto x:res){ cout<<x<<"\n"; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); vorod(); pre(); solve(); khor(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...