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>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
#define left id<<1
#define right id<<1|1
#define _lower(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()+1
#define re exit(0);
const int mod = 1e9+7 ;
const int INF = 1e9 ;
const int LOG = 18 ;
typedef vector<int> vi ;
typedef vector<ll> vl ;
typedef pair<int,int> pii ;
typedef vector<pii> vii ;
typedef pair<ll,ll> pll ;
void add ( int &a , int b )
{
a += b ;
if ( a < 0 ) a += mod ;
if ( a >= mod ) a -= mod ;
}
template < typename T > void chkmin (T &a , T b) { if (a>b) a=b ;}
template < typename T > void chkmax (T &a , T b) { if (a<b) a=b ;}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
// freopen ("bai1.out","w",stdout) ;
}
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow(a,n/2) ;
if ( n % 2 ) return (1ll*res*res%mod*a%mod) ;
else return 1ll*res*res%mod ;
}
int n , k ;
pii a [maxn] ;
void mapping ()
{
vi v ;
for ( int i = 1 ; i <= n ; i ++ ) v . pb (a[i].fi) , v . pb (a[i].se) ;
sort (v.begin(),v.end()) ;
v.resize(unique(v.begin(),v.end())-v.begin()) ;
for ( int i = 1 ; i <= n ; i ++ )
{
a [i].fi = _lower (v,a[i].fi) ;
a [i].se = _lower (v,a[i].se) ;
}
}
bool check_sub1 ()
{
for ( int i = 1 ; i < n ; i ++ ) if ( a[i].fi > a[i+1].fi ) return false ;
return true ;
}
int bit [maxn] ;
int dp [maxn] ;
void update ( int x , int y )
{
while ( x ) chkmax (bit[x],y) , x -= (x&-x) ;
}
int get ( int x )
{
int res = 0 ;
while ( x < maxn ) chkmax (res,bit[x]) , x += (x&-x) ;
return res ;
}
void sub1 ()
{
for ( int i = n ; i >= 1 ; i -- )
{
dp [i] = get (a[i].se) + 1 ;
update (a[i].fi,dp[i]) ;
}
if ( *max_element(dp+1,dp+n+1) < k )
{
cout << - 1 ; re
}
int cur = 0 ;
vi res ;
for ( int i = 1 ; i <= n ; i ++ )
{
if ( a[i].fi >= cur && dp [i] + (int)res.size () >= k )
{
cur = a[i].se ;
res . pb (i) ;
}
if ( res.size () == k ) break ;
}
for ( auto x : res ) cout << x << "\n" ;
}
void sub2 ()
{
vi res ;
for ( int mask = 0 ; mask < (1<<n) ; mask ++ )
{
if ( __builtin_popcount (mask) != k ) continue ;
vii arr ;
vi order ;
for ( int i = 0 ; i < n ; i ++ )
{
if ( mask >> i & 1 ) arr . pb (a[i+1]) , order . pb (i+1) ;
}
sort (arr.begin(),arr.end()) ;
int last = 0 ;
bool ok = true ;
for ( auto x : arr )
{
if ( x.fi < last ) ok = false ;
last = x.se ;
}
if ( ok == true )
{
if ( res.empty())
{
res = order ;
continue ;
}
for ( int i = 0 ; i < k ; i ++ )
{
if ( order [i] != res [i] )
{
if ( order [i] < res [i] )
{
ok = true ;
}
else ok = false ;
break ;
}
}
if ( ok == true ) res = order ;
}
}
if ( res.empty() ) cout << -1 ;
else
{
for ( auto x : res ) cout << x << "\n" ;
}
}
const int N = 3e3 ;
const int maxN = N + 9 ;
int T [maxN*4] , lazy [maxN*4] ;
void down ( int id )
{
int &t = lazy [id] ;
T [left] += t , T [right] += t ;
lazy [left] += t , lazy [right] += t ;
t = 0 ;
}
void update ( int id , int l , int r , int u , int v , int w )
{
if ( l > v || r < u ) return ;
if ( u <= l && r <= v )
{
T [id] += w ;
lazy [id] += w ;
return ;
}
down (id) ;
int mid = (l+r)/2 ;
update (left,l,mid,u,v,w) ;
update (right,mid+1,r,u,v,w) ;
T [id] = max (T[left],T[right]) ;
}
int get ( int id , int l , int r , int u , int v )
{
if ( l > v || r < u ) return 0 ;
if ( u <= l && r <= v ) return T [id] ;
int mid = (l+r)/2 ;
down (id) ;
return max (get(left,l,mid,u,v),get(right,mid+1,r,u,v)) ;
}
bool check_conquer ( int i )
{
return ( get(1,1,N,a[i].fi,a[i].se-1) ) ;
}
void sub3 ()
{
vi res ;
for ( int i = 1 ; i <= n ; i ++ )
{
if ( check_conquer(i) ) continue ;
update (1,1,N,a[i].fi,a[i].se-1,1) ;
vii arr ;
for ( int j = i+1 ; j <= n ; j ++ )
{
if ( !check_conquer (j) ) arr . pb (a[j]) ;
}
sort (arr.begin(),arr.end()) ;
int last = 0 , num = 0 ;
for ( auto x : arr )
{
if ( x.fi >= last ) num ++ , last = x.se ;
else last = min (last,x.se) ;
}
if ( num + 1 + res.size () >= k ) res . pb (i) ;
else update (1,1,N,a[i].fi,a[i].se-1,-1) ;
if ( res.size () == k ) break ;
}
if ( res.size () < k ) cout << - 1 ;
else
{
for ( auto x : res ) cout << x << "\n" ;
}
}
int main ()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ; cout.tie(0) ;
// rf () ;
cin >> n >> k ;
for ( int i = 1 ; i <= n ; i ++ ) cin >> a[i].fi >> a[i].se ;
mapping () ;
if ( check_sub1 () ) sub1 () ;
else if ( n <= 20 ) sub2 () ;
else if ( n <= 3000 ) sub3 () ;
}
Compilation message (stderr)
event2.cpp: In function 'void sub1()':
event2.cpp:103:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
103 | if ( res.size () == k ) break ;
| ~~~~~~~~~~~~^~~~
event2.cpp: In function 'void sub3()':
event2.cpp:219:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
219 | if ( num + 1 + res.size () >= k ) res . pb (i) ;
| ~~~~~~~~~~~~~~~~~~~~~~^~~~
event2.cpp:221:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
221 | if ( res.size () == k ) break ;
| ~~~~~~~~~~~~^~~~
event2.cpp:224:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
224 | if ( res.size () < k ) cout << - 1 ;
| ~~~~~~~~~~~~^~~
event2.cpp: In function 'void rf()':
event2.cpp:35:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |