This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
5
1 7
4 9
2 5
6 8
3 10
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<'('<<P.F<<','<<P.S<<')';
return out;
}
//}}}
const ll maxn=1000005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
//const ll p=880301;
//const ll P=31;
ll mypow(ll a,ll b){
ll res=1LL;
while(b){
if(b&1) res=res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
int dsu[maxn];
int sz[maxn];
int mxout[maxn];
int a[maxn],b[maxn];
int find(int u){
if(dsu[u]==u) return u;
return dsu[u]=find(dsu[u]);
}
vector<int> edge[maxn];
void mrg(int u,int v){
int _u=u,_v=v;
u=find(u);
v=find(v);
if(u==v) return;
if(sz[u]<sz[v]) swap(u,v);
dsu[v]=u;
sz[u]+=sz[v];
if(b[mxout[u]]<b[mxout[v]]) mxout[u]=mxout[v];
edge[_u].pb(_v);
edge[_v].pb(_u);
// cout<<"mrg: "<<_u<<' '<<_v<<'\n';
}
vector<int> v;
pii tg[maxn*2];
bool type[maxn];
bool vis[maxn];
void dfs(int u){
vis[u]=1;
for(int v:edge[u]) if(!vis[v]){
type[v]=!type[u];
dfs(v);
}
}
vector<int> A,B;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
IOS;
int n;
cin>>n;
REP(i,n) cin>>a[i]>>b[i];
REP(i,n) a[i]--,b[i]--;
REP(i,n) dsu[i]=i,sz[i]=1,mxout[i]=i;
REP(i,n) tg[a[i]]=MP(i,1);
REP(i,n) tg[b[i]]=MP(i,-1);
int cnt=0;
REP(i,n+n){
if(tg[i].S==1){
v.pb(tg[i].F);
}
else{
while(find(v.back())!=find(tg[i].F)){
mrg(tg[i].F,mxout[find(v.back())]);
v.pop_back();
}
if(i==b[mxout[find(tg[i].F)]]){
v.pop_back();
cnt++;
}
}
// for(int i:v) cout<<i<<' ';
// cout<<'\n';
// cout<<"dsu: \n";
// REP(i,n) cout<<find(i)<<' '<<mxout[find(i)]<<'\n';
}
REP(i,n) if(!vis[i]) dfs(i);
// REP(i,n) cout<<type[i]<<'\n';
REP(i,n+n){
if(tg[i].S==1){
if(type[tg[i].F]){
B.pb(tg[i].F);
}
else A.pb(tg[i].F);
}
else{
if(type[tg[i].F]){
if(B.back()!=tg[i].F){
cout<<"0\n";
return 0;
}
B.pop_back();
}
else{
if(A.back()!=tg[i].F){
cout<<"0\n";
return 0;
}
A.pop_back();
}
}
}
cout<<mypow(2,cnt)<<'\n';
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... |