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 ;
const int mod = 1e9 + 7 ;
int Add(int x , int y)
{
int z = x + y ;
if(z >= mod)
z -= mod ;
return z ;
}
int Sub(int x , int y)
{
int z = x - y ;
if(z < 0)
z += mod ;
return z ;
}
int Mul(int x , int y)
{
return (1ll * x * y) % mod ;
}
int powlog(int base , int power)
{
if(power == 0)
return 1 ;
int x = powlog(base , power / 2) ;
if(power & 1)
x = Mul(x , base) ;
return x ;
}
const int MAX = 2e6 + 10 ;
int arr[MAX] ;
int n ;
int L[MAX] , R[MAX] ;
int to[MAX] , back[MAX] ;
int id[MAX] ;
pair<int , int>tree[2][4 * MAX] ;
void build(int node , int l , int r)
{
if(l == r)
{
tree[0][node].first = to[l] ;
tree[1][node].first = back[l] ;
tree[0][node].second = tree[1][node].second = id[l] ;
return ;
}
int mid = (l + r) >> 1 ;
build(node << 1 , l , mid) ;
build(node << 1 | 1 , mid+1 , r) ;
tree[0][node] = max(tree[0][node << 1] , tree[0][node << 1 | 1]) ;
tree[1][node] = min(tree[1][node << 1] , tree[1][node << 1 | 1]) ;
}
void update(int node , int l , int r , int idx , int val , bool t)
{
if(idx > r || idx < l)
return ;
if(l == r)
{
tree[t][node].first = val ;
return ;
}
int mid = (l + r) >> 1 ;
if(idx <= mid)
update(node << 1 , l , mid , idx , val , t) ;
else
update(node << 1 | 1 , mid+1 , r , idx , val , t) ;
tree[0][node] = max(tree[0][node << 1] , tree[0][node << 1 | 1]) ;
tree[1][node] = min(tree[1][node << 1] , tree[1][node << 1 | 1]) ;
}
pair<int , int>query(int node , int l , int r , int from , int to , bool t)
{
if(from > r || to < l || from > to)
{
if(!t)
return {-1e9 , -1e9} ;
else
return {1e9 , 1e9} ;
}
if(l >= from && r <= to)
return tree[t][node] ;
int mid = (l + r) >> 1 ;
pair<int , int>a = query(node << 1 , l , mid , from , to , t) ;
pair<int , int>b = query(node << 1 | 1 , mid+1 , r , from , to , t) ;
if(!t)
return max(a , b) ;
else
return min(a , b) ;
}
int vis[MAX] ;
vector< pair<int , int> >vp[2] ;
void dfs(int node , int col)
{
vis[node] = 1 ;
vp[col].emplace_back(L[node] , R[node]) ;
update(1 , 1 , 2*n , L[node] , -1e9 , 0) ;
update(1 , 1 , 2*n , R[node] , 1e9 , 1) ;
bool ok = false ;
while(!ok)
{
ok = true ;
pair<int , int>p = query(1 , 1 , 2*n , L[node] , R[node] , 0) ;
if(p.first > R[node])
dfs(p.second , !col) , ok = false ;
p = query(1 , 1 , 2*n , L[node] , R[node] , 1) ;
if(p.first < L[node])
dfs(p.second , !col) , ok = false ;
}
}
bool check(bool t)
{
sort(vp[t].begin() , vp[t].end()) ;
set<int>s ;
for(auto &p : vp[t])
{
auto it = s.lower_bound(p.second) ;
if(it != s.begin())
{
it-- ;
int x = *it ;
if(p.first < x)
return false ;
}
s.insert(p.second) ;
}
return true ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= 2*n ; ++i)
to[i] = -1e9 , back[i] = 1e9 ;
for(int i = 1 ; i <= n ; ++i)
{
cin>>L[i]>>R[i] ;
to[L[i]] = R[i] , back[R[i]] = L[i] ;
id[L[i]] = id[R[i]] = i ;
}
build(1 , 1 , 2*n) ;
int ans = 1 ;
for(int i = 1 ; i <= n ; ++i)
{
if(vis[i])
continue ;
dfs(i , 0) ;
ans = Mul(ans , 2) ;
}
if((!check(0)) || (!check(1)))
ans = 0 ;
return cout<<ans<<"\n" , 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... |