#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD=1e9+7;
const int N=(int)1e6;
int mul(int a,int b){
return (LL)a*b%MOD;
}
int n;
pair<int,int>p[N+2];
namespace subtask1{
bool check(){
return n<=2000;
}
bool intersec(pair<int,int>x,pair<int,int>y){
if (x.second<y.first || y.second<x.first) return false;
if (x.first<=y.first && y.second<=x.second) return false;
if (y.first<=x.first && x.second<=y.second) return false;
return true;
}
vector<int>ke[N+2];
vector<int>order;
bool vis[N+2]={};
void dfs(int u){
vis[u]=true;
order.push_back(u);
for(auto&v:ke[u]){
if (vis[v]==false) {
dfs(v);
}
}
return;
}
bool process(){
vector<pair<int,int>>a[2];
for(auto&x:order){
// case 1
if (a[1].size()==0) a[1].push_back(p[x]);
else {
bool can=false;
for(auto&e:a[1]) can|=intersec(p[x],e);
if (can){
for(auto&e:a[0]) if (intersec(p[x],e)) return false;
a[0].push_back(p[x]);
}
else a[1].push_back(p[x]);
}
}
return true;
}
void main_code(){
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if (intersec(p[i],p[j])) {
ke[i].push_back(j);
ke[j].push_back(i);
}
}
}
int ans=1;
for(int i=1;i<=n;++i){
if (vis[i]==false){
order.clear();
dfs(i);
if (process()==false){
cout<<0;
return;
}
ans=mul(ans,2);
}
}
cout<<ans;
}
}
namespace subtask2{
bool check(){
return true;
}
const int LIMIT=2*N;
int par[LIMIT+2],id[LIMIT+2],pre[LIMIT+2],nxt[LIMIT+2],v[LIMIT+2];
int a[LIMIT+2];
int Find(int x){
return par[x]==x?par[x]:par[x]=Find(par[x]);
}
vector<int>ke[LIMIT+2];
void add_canh(int u,int v){
ke[u].push_back(v),ke[v].push_back(u);
return;
}
int vis[N+2];
bool dfs(int u){
for(auto&v:ke[u]){
if (vis[v]==0){
vis[v]=-vis[u];
if (dfs(v)==false) return false;
}
else if (vis[u]==vis[v]) return false;
}
return true;
}
void main_code(){
for(int i=1;i<=N;++i) par[i]=i,nxt[i]=i;
for(int i=1;i<=n;++i) id[p[i].first]=id[p[i].second]=i;
int cnt=0,comp=0;
for(int i=1;i<=2*n;++i){
if (id[i]==0) continue;
if (pre[id[i]]==0) pre[id[i]]=++cnt,v[cnt]=id[i];
else {
int u=pre[id[i]];
par[u]=Find(u+1);
for(int j=par[u],k; j<=cnt;j=k){
add_canh(v[j],id[i]);
k=Find(nxt[j]+1);
nxt[j]=cnt;
}
}
}
for(int i=1;i<=n;++i) if (vis[i]==0){
vis[i]=1;
if (dfs(i)) ++comp;
else {
cout<<0<<'\n';
return;
}
}
int ans=1;
for(int i=1;i<=comp;++i) ans=mul(ans,2);
cout<<ans<<'\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0) ; cout.tie(0);
#define task "main"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>n;
for(int i=1;i<=n;++i) cin>>p[i].first>>p[i].second;
return subtask2::main_code(),0;
return 0;
}
Compilation message (stderr)
port_facility.cpp: In function 'int main()':
port_facility.cpp:151:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
port_facility.cpp:152:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |