이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*input
5
1 4
2 9
3 6
5 8
7 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 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];
mxout[u]=max(mxout[u],mxout[v]);
edge[_u].pb(_v);
edge[_v].pb(_u);
}
vector<int> v;
pii tg[maxn*2];
int a[maxn],b[maxn];
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(){
// 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]=b[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,v.back());
v.pop_back();
}
if(i==mxout[find(tg[i].F)]){
v.pop_back();
cnt++;
}
}
}
REP(i,n) if(!vis[i]) dfs(i);
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... |