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 ;
typedef long long ll ;
typedef unsigned long long ull ;
#define fi first
#define se second
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MAXN = 2e6 + 7 ;
const int MOD = 1e9 + 7 ;
int n ;
pair < int , int > a[ MAXN ] ;
int prv[ MAXN ] , rnk[ MAXN ] ;
int get ( int x ) {
if ( prv[ x ] == -1 ) { return x ; }
int y = get ( prv[ x ] ) ;
prv[ x ] = y ;
return y ;
}
void unite ( int x , int y ) {
int k1 = get ( x ) , k2 = get ( y ) ;
if ( k1 != k2 ) {
if ( rnk[ k1 ] >= rnk[ k2 ] ) {
rnk[ k1 ] += ( rnk[ k1 ] == rnk[ k2 ] ) ;
prv[ k2 ] = k1 ;
}
else {
prv[ k1 ] = k2 ;
}
}
}
void solve ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ].fi >> a[ i ].se ;
}
sort ( a + 1 , a + n + 1 ) ;
for ( int i = 1 ; i <= 2 * n ; ++ i ) {
prv[ i ] = -1 , rnk[ i ] = 0 ;
}
set < pair < int , int > > s ;
for ( int i = 1 ; i <= n ; ++ i ) {
auto it = s.lower_bound ( { a[ i ].fi , 0 } ) ;
auto en = s.lower_bound ( { a[ i ].se , 0 } ) ;
auto aux = en ; -- aux ;
while ( it != en ) {
unite ( i , it->se + n ) ;
unite ( i + n , it->se ) ;
if ( get ( it->se ) == get ( aux->se ) ) { break ; }
++ it ;
}
if ( get ( i ) == get ( n + i ) ) {
cout << "0\n" ;
return ;
}
s.insert ( { a[ i ].se , i } ) ;
}
ll ans = 1 ;
for ( int i = 1 ; i <= n ; ++ i ) {
if ( get ( i ) == i ) {
ans = ( ans * 2 ) % MOD ;
}
}
cout << ans << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
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... |