제출 #595705

#제출 시각아이디문제언어결과실행 시간메모리
595705inksamuraiEvent Hopping 2 (JOI21_event2)C++17
100 / 100
246 ms59992 KiB
#include <bits/stdc++.h> // cut here #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder namespace atcoder { template <class S, S (*op)(S, S), S (*e)()> struct segtree { public: segtree() : segtree(0) {} segtree(int n) : segtree(std::vector<S>(n, e())) {} segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } template <bool (*f)(S)> int max_right(int l) { return max_right(l, [](S x) { return f(x); }); } template <class F> int max_right(int l, F f) { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*f)(S)> int min_left(int r) { return min_left(r, [](S x) { return f(x); }); } template <class F> int min_left(int r, F f) { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder // cut here 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 int op(int l,int r){return l+r;} int e(){return 0;} const int inf=1e9+11; using T=pair<int,pii>; signed main(){ _3uHasSr; int n,k; cin>>n>>k; vec(T) a; vec(pii) _a; int m; { rep(i,n){ pii p; cin>>p.fi>>p.se; a.pb(T{p.fi,{p.se,i}}); _a.pb(p); } vi tmp; rep(i,n){ tmp.pb(_a[i].fi); tmp.pb(_a[i].se); } sort(tmp.begin(), tmp.end()); tmp.erase(unique(tmp.begin(), tmp.end()),tmp.end()); m=sz(tmp)+4; rep(i,n){ pii p=_a[i]; p.fi=(int)(lower_bound(tmp.begin(), tmp.end(),p.fi)-tmp.begin())+2; p.se=(int)(lower_bound(tmp.begin(), tmp.end(),p.se)-tmp.begin())+2; a[i].fi=p.fi; a[i].se.fi=p.se; _a[i]=p; } } // rep(i,n){ // print(_a[i].fi,_a[i].se); // } // create double lift vec(vec(pii)) gl(30,vec(pii)(n,{-1,-1})); { sort(a.begin(), a.end()); priority_queue<pii> pq; per(i,n){ int id=a[i].se.se; int l=a[i].fi; int r=a[i].se.fi; while(sz(pq)){ auto top=pq.top(); if(top.fi>=r){ gl[0][top.se].fi=id; pq.pop(); }else{ break; } } pq.push(pii(l,id)); } rng(j,1,30){ rep(i,n){ gl[j][i].fi=(gl[j-1][i].fi==-1?-1:gl[j-1][gl[j-1][i].fi].fi); } } rep(j,30){ rep(i,n){ gl[j][i].se=(gl[j][i].fi==-1?-1:_a[gl[j][i].fi].fi); } } } vec(vec(pii)) gr(30,vec(pii)(n,{inf,inf})); { sort(a.begin(), a.end(),[&](const T&l,const T&r){ return l.se.fi<r.se.fi; }); priority_queue<pii,vec(pii),greater<pii>> pq; rep(i,n){ int id=a[i].se.se; int l=a[i].fi; int r=a[i].se.fi; while(sz(pq)){ auto top=pq.top(); if(top.fi<=l){ gr[0][top.se].fi=id; pq.pop(); }else{ break; } } pq.push(pii(r,id)); } rng(j,1,30){ rep(i,n){ gr[j][i].fi=(gr[j-1][i].fi==inf?inf:gr[j-1][gr[j-1][i].fi].fi); } } rep(j,30){ rep(i,n){ gr[j][i].se=(gr[j][i].fi==inf?inf:_a[gr[j][i].fi].se); } } } multiset<T> st; atcoder::segtree<int,op,e> seg(m); int prefix=0; vi pns; rep(i,n){ pii p=_a[i]; // check if intersects with previously added segment int _l=0,idl=-1,_r=inf-1,idr=-1; bool pok=1; if(sz(st)){ auto it=st.lower_bound(T{p.fi,{-1,-1}}); if(it!=st.end()){ if((*it).fi<p.se){ pok=0; } _r=(*it).fi; idr=(*it).se.se; } if(it!=st.begin()){ it=prev(it); if((*it).se.fi>p.fi){ pok=0; } _l=(*it).se.fi; idl=(*it).se.se; } } if(!pok){ continue; } // search first element to the left such that it's l < _l int nowl=0; { int s=i; per(j,30){ int t=gl[j][s].se; // print(s,t); if(t>=_l){ s=gl[j][s].fi; nowl+=(1<<j); } } } // search first element to the right such that it's r > _r int nowr=0; { int s=i; per(j,30){ // print(_r,gr[j][s].fi,s); int t=gr[j][s].se; if(t<=_r){ s=gr[j][s].fi; nowr+=(1<<j); } } } int now_prefix=(idl!=-1?prefix:0); int taken_r=(idr!=-1?seg.prod(_r,m):0); int taken_l=(idl!=-1?seg.prod(0,_a[idl].fi)+1:0); if(taken_l+taken_r+nowr+nowl+now_prefix+1>=k){ st.insert(T{p.fi,{p.se,i}}); if(idl==-1){ prefix=nowl; }else{ // print(1+nowl); seg.set(_a[idl].fi,1+nowl); } // print(p.fi,1+nowr); seg.set(p.fi,1+nowr); pns.pb(i); } } if(sz(pns)<k){ print(-1); return 0; } while(sz(pns)>k){ pns.pop_back(); } for(auto v:pns){ print(v+1); } // return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...