Submission #594761

#TimeUsernameProblemLanguageResultExecution timeMemory
594761inksamuraiEvent Hopping 2 (JOI21_event2)C++17
1 / 100
148 ms70496 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 // #include <atcoder/all> int op(int l,int r){return l+r;} int e(){return 0;} using T=pair<pii,int>; signed main(){ _3uHasSr; int n,k; cin>>n>>k; vec(T) a(n); rep(i,n){ cin>>a[i].fi.fi>>a[i].fi.se; a[i].se=i; } vi tmp; int m; { rep(i,n){ tmp.pb(a[i].fi.fi); tmp.pb(a[i].fi.se); } sort(tmp.begin(), tmp.end()); tmp.erase(unique(tmp.begin(), tmp.end()),tmp.end()); m=sz(tmp); rep(i,n){ pii p=a[i].fi; p.fi=(int)(lower_bound(tmp.begin(), tmp.end(),p.fi)-tmp.begin()); p.se=(int)(lower_bound(tmp.begin(), tmp.end(),p.se)-tmp.begin()); a[i].fi=p; } } sort(a.begin(), a.end(),[&](const T&l,const T&r){ return l.fi.se<r.fi.se; }); vec(pii) rs(n); rep(i,n){ rs[a[i].se]=a[i].fi; } // rep(i,n){ // print(rs[i].fi,rs[i].se); // } // print("...."); atcoder::segtree<int,op,e> segl(m),segr(m); std::multiset<pii> stl,str; // [l,r] [r,l] int _lst=-1,cnt=0; vi usd(n,0); rep(i,n){ pii p=a[i].fi; // _lst <= p.fi if(_lst<=p.fi){ usd[a[i].se]=1; cnt+=1; _lst=p.se; segr.set(p.se,segr.get(p.se)+1); segl.set(p.fi,segl.get(p.fi)+1); stl.insert(pii(p.fi,p.se)); str.insert(pii(p.se,p.fi)); } } if(cnt<k){ cout<<(-1)<<"\n"; return 0; } std::map<pii,vi> mp; set<int> pns; rep(i,n){ if(usd[i]){ pns.insert(i); mp[rs[i]].pb(i); } } set<int> seenl,seenr; rep(i,n){ pii p=rs[i]; if(usd[i]){ seenl.insert(p.fi); seenr.insert(p.se); continue; } bool skip=0; { auto __it=seenl.lower_bound(p.fi); // if there exists some l >= p.fi and l<=p.se if(__it!=seenl.end() and *__it<p.se){ skip=1; } auto __ti=seenr.lower_bound(p.fi+1); // if there exists some r > p.fi and r<=p.se if(__ti!=seenr.end() and *__ti<=p.se){ skip=1; } } if(skip){ continue; } // auto __it=seen.lower_bound(p.fi); // if(i==4){ // print(i,*__it,p.fi,p.se); // } // if(__it!=seen.end() and *__it<p.se){ // // contains smol // continue; // } int now=0; auto it=str.lower_bound(pii(p.fi+1,-1)); if(it!=str.begin()){ it=prev(it); pii _p=*it; // previous non intersect [r,l] now+=segr.prod(0,_p.fi+1); } auto ti=stl.lower_bound(pii(p.se,-1)); if(ti!=stl.end()){ pii _p=*ti; // previous non intersect [l,r] now+=segl.prod(_p.fi,m); } // if(i==4){ // print(now); // } if(now+1>=k){ // should delete every segment inside auto ti=stl.lower_bound(pii(p.se,-1)); if(ti!=stl.begin()){ ti=prev(ti); while(1){ auto nti=ti; pii _p=*ti; if(_p.se<=p.fi){ break; } bool lok=0; if(ti!=stl.begin()){ ti=prev(ti); }else{ lok=1; } stl.erase(nti); str.erase(str.find(pii(_p.se,_p.fi))); segr.set(_p.se,segr.get(_p.se)-1); segl.set(_p.fi,segl.get(_p.fi)-1); assert(sz(mp[_p])); // print(_p.fi,_p.se); pns.erase(pns.find(mp[_p].back())); mp[_p].pop_back(); if(lok){ break; } } } pns.insert(i); seenl.insert(p.fi); seenr.insert(p.se); segl.set(p.fi,segl.get(p.fi)+1); segr.set(p.se,segr.get(p.se)+1); stl.insert(p); str.insert(pii(p.se,p.fi)); } } while(sz(pns)>k){ pns.erase(prev(pns.end())); } for(auto v:pns){ cout<<v+1<<"\n"; } // 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...