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>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 2e6 + 10, LOG = 21;
int N;
pair<int, int> a[MAX];
vector<int> adj[MAX];
int num[MAX];
pair<int, int> ma[MAX][LOG], mi[MAX][LOG];
bool dfs(int u, int c){
if(num[u]){
return num[u] == c;
}
num[u] = c;
for(int v: adj[u]){
if(!dfs(v, 3 - c)) return false;
}
return true;
}
pair<int, int> getMin(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return min(mi[r][k], mi[l + MASK(k) - 1][k]);
}
pair<int, int> getMax(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return max(ma[r][k], ma[l + MASK(k) - 1][k]);
}
void solve(){
cin >> N;
for(int i=1; i<=N; i++){
cin >> a[i].fi >> a[i].se;
}
for(int i=1; i<=2*N; i++) mi[i][0] = {1e9, 1e9};
for(int i=1; i<=N; i++){
mi[a[i].se][0] = {a[i].fi, i};
ma[a[i].fi][0] = {a[i].se, i};
}
for(int j=1; j<LOG; j++){
for(int i=1; i<=N*2; i++){
mi[i][j] = mi[i][j - 1];
ma[i][j] = ma[i][j - 1];
if(i > MASK(j - 1)){
minimize(mi[i][j], mi[i - MASK(j - 1)][j - 1]);
maximize(ma[i][j], ma[i - MASK(j - 1)][j - 1]);
}
}
}
for(int i=1; i<=N; i++){
pair<int, int> pr1 = getMin(a[i].fi, a[i].se);
pair<int, int> pr2 = getMax(a[i].fi, a[i].se);
if(pr1.fi < a[i].fi){
adj[i].push_back(pr1.se);
adj[pr1.se].push_back(i);
}
if(a[i].se < pr2.fi){
adj[i].push_back(pr2.se);
adj[pr2.se].push_back(i);
}
}
int ans = 1;
for(int i=1; i<=N; i++) if(!num[i]){
if(!dfs(i, 1)){
cout << "0\n";
return;
}
ans = 2LL * ans % MOD;
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("terrorists.inp", "r", stdin);
// freopen("terrorists.out", "w", stdout);
int t = 1;
// cin >> t;
while(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... |