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>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e6+10;
const int mod = 1e9+7;
int n, a[maxn], b[maxn];
int ds[maxn], dsz[maxn], dsgrp[maxn], dsgb[maxn];
vector<int> dsvec[maxn];
set<int> dsgrpbs[maxn][2]; // mantain the B's of the ones in this component
set<pair<int,int>> stds; // the greatest B, the index
int find(int v) {
    if(v == ds[v]) return v;
    return ds[v] = find(ds[v]);
}
void createedge(int u, int v, int chg) {
    // an edge from u to v
    // dsgrp[v] = dsgrp[u]^chg, basically 0 if from the same group and 1 if not
    int u1 = u;
    int v1 = v;
    u = find(u);
    v = find(v);
    if(dsgrp[u1] != dsgrp[u]) chg^= 1;
    if(dsgrp[v1] != dsgrp[v]) chg^= 1;
    if(u == v) {
        if(chg == 1) {
            // I fail, because they are equal
            cout << 0 << endl;
            exit(0);
        }
        else {
            // stop the uniting
            return;
        }
    }
    if(dsz[u] < dsz[v]) {
        swap(u,v);
        swap(u1,v1);
    }
    if(stds.find(mp(dsgb[u],u)) != stds.end()) stds.erase(mp(dsgb[u],u));
    if(stds.find(mp(dsgb[v],v)) != stds.end()) stds.erase(mp(dsgb[v],v));
    // Now I insert all the ones from v in u
    ds[v] = u;
    dsz[u]+= dsz[v];
    dsgb[u] = max(dsgb[u],dsgb[v]);
    for(auto id : dsvec[v]) {
        // Insert id in u as dsgrp[id]^chg
        dsgrp[id]^= chg;
        dsgrpbs[u][dsgrp[id]].insert(b[id]);
        dsvec[u].pb(id);
    }
}
void solve() {
    cin >> n;
    vector<pair<int,int>> wtfsort;
    for(int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        wtfsort.pb(mp(a[i],b[i]));
    }
    sort(all(wtfsort));
    for(int i = 1; i <= n; i++) {
        a[i] = wtfsort[i-1].fr;
        b[i] = wtfsort[i-1].sc;
        ds[i] = i;
        dsz[i] = 1;
        dsvec[i].pb(i);
        dsgrp[i] = 0;
        dsgb[i] = b[i];
        dsgrpbs[i][0].insert(b[i]);
    }
    for(int i = 1; i <= n; i++) {
        // for(auto it : stds) {
        //     int id = it->sc;
        //     bool okgrp0 = false;
        //     for(auto x : dsgrpbs[id][0]) {
        //         if(x > a[i] && x < b[i]) okgrp0 = true;
        //     }
        // }
        for(int j = 1; j < i; j++) {
            if(b[j] > a[i] && b[j] < b[i]) {
                createedge(i,j,1);
            }
        }
    }
    int ans = 1;
    for(int i = 1; i <= n; i++) {
        if(find(i) == i) ans = ans*2%mod;
    }
    cout << ans << endl;
}
int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);
    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }
}
| # | 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... |