답안 #595699

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
595699 2022-07-14T01:34:24 Z inksamurai Event Hopping 2 (JOI21_event2) C++17
7 / 100
152 ms 60036 KB
#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);
		rep(i,n){
			pii p=_a[i];
			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.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;
		// print(i,now_prefix,idl,taken_l);
		// print(i,taken_l+taken_r+nowr+nowl+now_prefix+1);
		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{
				seg.set(_a[idl].fi,1+nowl);
			}
			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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 140 ms 58092 KB Output is correct
5 Correct 141 ms 60020 KB Output is correct
6 Correct 147 ms 59996 KB Output is correct
7 Correct 141 ms 59876 KB Output is correct
8 Correct 150 ms 60036 KB Output is correct
9 Correct 152 ms 59936 KB Output is correct
10 Correct 146 ms 59960 KB Output is correct
11 Correct 146 ms 59840 KB Output is correct
12 Correct 130 ms 57404 KB Output is correct
13 Correct 130 ms 57304 KB Output is correct
14 Correct 130 ms 57460 KB Output is correct
15 Correct 124 ms 57340 KB Output is correct
16 Correct 95 ms 54084 KB Output is correct
17 Correct 103 ms 53988 KB Output is correct
18 Correct 97 ms 54080 KB Output is correct
19 Correct 90 ms 54060 KB Output is correct
20 Correct 89 ms 53972 KB Output is correct
21 Correct 89 ms 54008 KB Output is correct
22 Correct 97 ms 53956 KB Output is correct
23 Correct 97 ms 53972 KB Output is correct
24 Correct 95 ms 53984 KB Output is correct
25 Correct 101 ms 53972 KB Output is correct
26 Correct 101 ms 53968 KB Output is correct
27 Correct 102 ms 53952 KB Output is correct
28 Correct 90 ms 54000 KB Output is correct
29 Correct 91 ms 53940 KB Output is correct
30 Correct 91 ms 53980 KB Output is correct
31 Correct 98 ms 54056 KB Output is correct
32 Correct 105 ms 53988 KB Output is correct
33 Correct 109 ms 53980 KB Output is correct
34 Correct 122 ms 57280 KB Output is correct
35 Correct 124 ms 57148 KB Output is correct
36 Correct 130 ms 57060 KB Output is correct
37 Correct 122 ms 56800 KB Output is correct
38 Correct 120 ms 56832 KB Output is correct
39 Correct 122 ms 56760 KB Output is correct
40 Correct 117 ms 56820 KB Output is correct
41 Correct 117 ms 56828 KB Output is correct
42 Correct 100 ms 53952 KB Output is correct
43 Correct 133 ms 56768 KB Output is correct
44 Correct 129 ms 56540 KB Output is correct
45 Correct 133 ms 56444 KB Output is correct
46 Correct 135 ms 56256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Incorrect 0 ms 212 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Incorrect 0 ms 212 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 140 ms 58092 KB Output is correct
5 Correct 141 ms 60020 KB Output is correct
6 Correct 147 ms 59996 KB Output is correct
7 Correct 141 ms 59876 KB Output is correct
8 Correct 150 ms 60036 KB Output is correct
9 Correct 152 ms 59936 KB Output is correct
10 Correct 146 ms 59960 KB Output is correct
11 Correct 146 ms 59840 KB Output is correct
12 Correct 130 ms 57404 KB Output is correct
13 Correct 130 ms 57304 KB Output is correct
14 Correct 130 ms 57460 KB Output is correct
15 Correct 124 ms 57340 KB Output is correct
16 Correct 95 ms 54084 KB Output is correct
17 Correct 103 ms 53988 KB Output is correct
18 Correct 97 ms 54080 KB Output is correct
19 Correct 90 ms 54060 KB Output is correct
20 Correct 89 ms 53972 KB Output is correct
21 Correct 89 ms 54008 KB Output is correct
22 Correct 97 ms 53956 KB Output is correct
23 Correct 97 ms 53972 KB Output is correct
24 Correct 95 ms 53984 KB Output is correct
25 Correct 101 ms 53972 KB Output is correct
26 Correct 101 ms 53968 KB Output is correct
27 Correct 102 ms 53952 KB Output is correct
28 Correct 90 ms 54000 KB Output is correct
29 Correct 91 ms 53940 KB Output is correct
30 Correct 91 ms 53980 KB Output is correct
31 Correct 98 ms 54056 KB Output is correct
32 Correct 105 ms 53988 KB Output is correct
33 Correct 109 ms 53980 KB Output is correct
34 Correct 122 ms 57280 KB Output is correct
35 Correct 124 ms 57148 KB Output is correct
36 Correct 130 ms 57060 KB Output is correct
37 Correct 122 ms 56800 KB Output is correct
38 Correct 120 ms 56832 KB Output is correct
39 Correct 122 ms 56760 KB Output is correct
40 Correct 117 ms 56820 KB Output is correct
41 Correct 117 ms 56828 KB Output is correct
42 Correct 100 ms 53952 KB Output is correct
43 Correct 133 ms 56768 KB Output is correct
44 Correct 129 ms 56540 KB Output is correct
45 Correct 133 ms 56444 KB Output is correct
46 Correct 135 ms 56256 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Incorrect 0 ms 212 KB Output isn't correct
54 Halted 0 ms 0 KB -