This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;}
int op0(int l,int r){return max(l,r);}
int e0(){return -1;}
int op1(int l,int r){return min(l,r);}
int e1(){return 1e9;}
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);
}
}
atcoder::segtree<int,op0,e0> seenl(m);
atcoder::segtree<int,op1,e1> seenr(m);
rep(i,m){
seenl.set(i,-1);
seenr.set(i,1e9);
}
set<pii> added;
rep(i,n){
pii p=rs[i];
if(usd[i]){
seenl.set(p.fi,op0(seenl.get(p.fi),p.se));
seenr.set(p.se,op1(seenr.get(p.se),p.fi));
// if(i==0) print(p.fi,p.se,op(seenl.get(p.fi),p.se));
continue;
}
bool skip=0;
{
int valr=seenl.prod(0,p.se);
// if(i==1) print(valr,p.fi,p.se);
if(valr>p.fi){
skip=1;
}
int vall=seenr.prod(p.fi+1,m);
if(vall<p.se){
skip=1;
}
// 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;
}
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(added.find(_p)==added.end());
if(added.find(_p)!=added.end()){
// if(_p.se>p.fi and _p.se<=p.se){
// cout<<"asdashdzxhcioxz\n";
// exit(0);
// }
// if(_p.fi>=p.fi and _p.fi<p.se){
// cout<<"asdashdzxhcioxz\n";
// exit(0);
// }
// if(_p.fi<p.fi and )
cout<<"asdashdzxhcioxz\n";
exit(0);
}
if(!sz(mp[_p])){
assert(0);
}
// print(_p.fi,_p.se);
pns.erase(pns.find(mp[_p].back()));
mp[_p].pop_back();
if(lok){
break;
}
}
}
pns.insert(i);
seenl.set(p.fi,op0(seenl.get(p.fi),p.se));
seenr.set(p.se,op1(seenr.get(p.se),p.fi));
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));
// mp[p].pb(i);
added.insert(p);
}
}
while(sz(pns)>k){
pns.erase(prev(pns.end()));
}
for(auto v:pns){
cout<<v+1<<"\n";
}
//
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |