#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bit>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
ll mod=1e9+7;
ll binPow(ll a, ll n){
ll res = 1, b=a;
while(n){
if(n&1){
res *= b;
res %= mod;
--n;
}
b *= b;
b %= mod;
n /= 2;
}
return res;
}
ll get(ll x, vector<ll>& parent){
if(parent[x] == x) return x;
return parent[x] = get(parent[x], parent);
}
void unite(ll x, ll y, ll by, vector<ll>& parent, vector<set<ll>>& another1, vector<set<ll>>& another2){
x = get(x, parent);
y = get(y, parent);
if(x == y) return;
parent[y] = x;
if(another1[x].lower_bound(by) != another1[x].begin() and another2[x].lower_bound(by) != another2[x].begin()){
cout<<0<<endl;
exit(0);
}
if(another2[x].lower_bound(by) == another2[x].begin()){
swap(another1[x], another2[x]);
}
if(another1[x].size() < another1[y].size()) swap(another1[x], another1[y]);
for(auto el: another1[y]){
another1[x].insert(el);
}
if(another2[x].size() < another2[y].size()) swap(another2[x], another2[y]);
for(auto el: another2[y]){
another2[x].insert(el);
}
}
void solve(){
ll n, i, l, r, cnt=0, id, x, id2;
cin>>n;
vector<pair<ll, ll>> add(2*n);
set<ll> s;
vector<ll> parent(n);
vector<set<ll>> another1(n);
vector<set<ll>> another2(n);
for(i=0;i<n;++i){
cin>>l>>r;
--l;
--r;
add[l] = {r, i};
add[r] = {l, i};
parent[i] = i;
}
vector<ll> del;
for(i=0;i<2*n;++i){
x = add[i].first;
id = add[i].second;
if(i < x){
s.insert(x);
another1[id].insert(x);
auto it = s.find(x);
while(it != s.begin()){
del.pb(x);
--it;
x = *it;
id2 = add[x].second;
unite(id2, id, add[i].first, parent, another1, another2);
}
for(auto el: del){
s.erase(el);
}
del.clear();
}else{
s.erase(i);
id = get(id, parent);
another1[id].erase(i);
another2[id].erase(i);
if(another1[id].size() > 0 and another2[id].size() > 0){
s.insert(min(*another1[id].begin(), *another2[id].begin()));
}else if(another1[id].size() > 0){
s.insert(*another1[id].begin());
}else if(another2[id].size() > 0){
s.insert(*another2[id].begin());
}
}
}
for(i=0;i<n;++i){
cnt += parent[i] == i;
}
cout<<binPow(2, cnt)<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(nullptr));
ll t=1;
// cin>>t;
for(;t>0;--t){
solve();
}
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... |