이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 ;
update(node << 1 , l , mid , idx , val , t) ;
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< pair<int , int> , pair<int , int> >query(int node , int l , int r , int from , int to)
{
if(from > r || to < l || from > to)
return {{-1e9 , -1e9} , {1e9 , 1e9}} ;
if(l >= from && r <= to)
return {tree[0][node] , tree[1][node]} ;
int mid = (l + r) >> 1 ;
pair< pair<int , int> , pair<int , int> >a = query(node << 1 , l , mid , from , to) ;
pair< pair<int , int> , pair<int , int> >b = query(node << 1 | 1 , mid+1 , r , from , to) ;
return {max(a.first , b.first) , min(a.second , b.second)} ;
}
int vis[MAX] , col[MAX] ;
vector< pair<int , int> >vp[2] ;
queue<int>q ;
void bfs(int src)
{
q.push(src) ;
update(1 , 1 , 2*n , L[src] , -1e9 , 0) ;
update(1 , 1 , 2*n , R[src] , 1e9 , 1) ;
while(!q.empty())
{
int node = q.front() ;
q.pop() ;
vis[node] = 1 ;
vp[col[node]].emplace_back(L[node] , R[node]) ;
bool ok = false ;
while(!ok)
{
ok = true ;
pair< pair<int , int> , pair<int , int> >p = query(1 , 1 , 2*n , L[node] , R[node]) ;
if(p.first.first > R[node])
{
ok = false ;
int node2 = p.first.second ;
col[node2] = !col[node] ;
update(1 , 1 , 2*n , L[node2] , -1e9 , 0) ;
update(1 , 1 , 2*n , R[node2] , 1e9 , 1) ;
q.push(node2) ;
}
if(p.second.first < L[node])
{
ok = false ;
int node2 = p.second.second ;
col[node2] = !col[node] ;
update(1 , 1 , 2*n , L[node2] , -1e9 , 0) ;
update(1 , 1 , 2*n , R[node2] , 1e9 , 1) ;
q.push(node2) ;
}
}
}
}
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 ;
bfs(i) ;
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... |