# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
986939 | amine_aroua | Port Facility (JOI17_port_facility) | C++17 | 0 ms | 0 KiB |
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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define pb push_back
#define nl '\n'
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i, x, y) for(int i = x;i<=y;i++)
#define forn(i, y, x) for(int i = y; i >= x; i--)
const int MAXN = 2000010;
const int MOD = 1e9 + 7;
int ans = 1;
int n;
bool is_bipartite = 1;
int idx[MAXN] , lt[MAXN] , rt[MAXN] , col[MAXN] , vis[MAXN];
vector<pair<int ,int>> vp[2];
const int INF = 2e6 + 10;
struct segMin
{
int BASE;
vector<int> tree;
void init(int _n )
{
BASE = _n;
while(__builtin_popcount(BASE) != 1)
{
BASE++;
}
tree.assign(2*BASE , INF);
}
void upd(int x , int y)
{
tree[x + BASE] = y;
for(int j = (x + BASE)/2 ; j >= 1; j/=2)
{
tree[j] = min(tree[2*j] , tree[2*j + 1]);
}
}
int query(int l , int r)
{
if(l > r)
return INF;
l+=BASE;
r+=BASE;
if(l == r)
return tree[l];
int left = tree[l] , right = tree[r];
while(l + 1 < r)
{
if(l % 2 == 0)
left = min(left , tree[l + 1]);
if(r % 2 == 1)
right = min(tree[r - 1] , right);
l>>=1;
r>>=1;
}
return min(left , right);
}
}sL;
struct segMax
{
int BASE;
vector<int> tree;
void init(int _n )
{
BASE = _n;
while(__builtin_popcount(BASE) != 1)
{
BASE++;
}
tree.assign(2*BASE , -INF);
}
void upd(int x , int y)
{
tree[x + BASE] = y;
for(int j = (x + BASE)/2 ; j >= 1; j/=2)
{
tree[j] = max(tree[2*j] , tree[2*j + 1]);
}
}
int query(int l , int r)
{
if(l > r)
return -INF;
l+=BASE;
r+=BASE;
if(l == r)
return tree[l];
int left = tree[l] , right = tree[r];
while(l + 1 < r)
{
if(l % 2 == 0)
left = max(left , tree[l + 1]);
if(r % 2 == 1)
right = max(tree[r - 1] , right);
l>>=1;
r>>=1;
}
return max(left , right);
}
}sR;
void dfs(int x , int c)
{
if(vis[x])
{
if(c != col[x])
is_bipartite = 0;
return ;
}
vp[c].pb({lt[x] , rt[x]});
col[x] = c;
vis[x] = 1;
while(true)
{
int r = sR.query(lt[x] + 1 , rt[x] - 1);
if(r <= rt[x])
break;
int i = idx[r];
sR.upd(lt[i] , -INF);
sL.upd(rt[i] , INF);
dfs(i , 1 - c);
}
while(true)
{
int l = sL.query(lt[x] + 1 , rt[x] - 1);
if(l >= lt[x])
break;
int i = idx[l];
sR.upd(lt[i] , -INF);
sL.upd(rt[i] , INF);
dfs(i , 1 - c);
}
}
bool check(int t)
{
sort(vp[t].begin() , vp[t].end());
set<int> s;
for(auto [l , r] : vp[t])
{
auto it = s.lower_bound(r);
if(it != s.begin())
{
it--;
if(l < *it)
return 0;
}
s.insert(r);
}
return 1;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
sL.init(2*n + 1);
sR.init(2*n + 1);
fore(i , n)
{
cin>>lt[i]>>rt[i];
sL.upd(rt[i] , lt[i]);
sR.upd(lt[i] , rt[i]);
idx[lt[i]] = i;
idx[rt[i]] = i;
}
fore(i , n)
{
if(!vis[i])
{
dfs(i , 0);
ans = (ans * 2Zz)%MOD;
}
}
if(check(0) && check(1))
cout<<ans<<nl;
else
cout<<0<<nl;
}