답안 #594762

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
594762 2022-07-12T22:46:00 Z inksamurai Event Hopping 2 (JOI21_event2) C++17
1 / 100
124 ms 33900 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

// #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);
					if(!sz(mp[_p])){
						print("asdahsdasd");
						exit(0);
					}
					// 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 124 ms 33900 KB Output isn't correct
5 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Incorrect 3 ms 468 KB Output isn't correct
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 124 ms 33900 KB Output isn't correct
5 Halted 0 ms 0 KB -