This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* code by the cute ~~Dengdualang~~ PixelCat owo */
/*     as cute as nacho neko (aka. my wife)!     */
 
/*     Nhade stay for a night here               */
/*     183234 deng deng deng pixelcat oops       */
/*     (yang wang yesu)*2                        */
/*     ^ some weird stuff. nvm =w=               */
 
// #pragma GCC optimize("O4,unroll-loops,no-stack-protector")
// #pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma")
 
#include <bits/stdc++.h>
#pragma pack(0)
#define int LL  //__int128
#define double long double
using namespace std;
using LL = long long;
using LLL = __int128_t;
using uLL = unsigned long long;
using pii = pair<int, int>;
 
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Fors(i, a, b, s) for (int i = a; i <= b; i += s)
#define Forr(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define L(id) (id * 2 + 1)
#define R(id) (id * 2 + 2)
#define LO(x) (x & (-x))
 
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define mkp make_pair
 
// #define MOD (int)(998244353)
#define MOD (int)((LL)1'000'000'007)
#define INF (int)(4e18)
// #define INF 1e9
#define EPS (1e-6)
 
#ifdef NYAOWO
#include "library/debug.hpp"
inline void USACO(const string &s) {
    cerr << "USACO: " << s << "\n";
}
 
#else
#define debug(...)
inline void timer() {}
inline void USACO(const string &s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}
 
#endif
 
inline void NYA() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
inline int gcd(int a, int b) {
    return __gcd(a, b);
}
inline int lcm(int a, int b) {
    return a / gcd(a, b) * b;
}
int fpow(int b, int p, const int &mod) {
    int ans = 1;
    while (p) {
        if (p & 1) ans = ans * b % mod;
        p >>= 1;
        b = b * b % mod;
    }
    return ans;
}
int fpow(int b, int p) {
    return fpow(b, p, MOD);
}
template <typename T>
inline void chmin(T &_a, const T &_b) {
    if (_b < _a) _a = _b;
}
template <typename T>
inline void chmax(T &_a, const T &_b) {
    if (_b > _a) _a = _b;
}
// mt19937_64 rng(
//     chrono::steady_clock::now().time_since_epoch().count());
 
struct Node{
    Node *l,*r;
    char tag;
    vector<pair<Node*,char>> adj;
    Node* par;
    char dist;
    Node():l(nullptr),r(nullptr),tag(0),par(nullptr),dist(0){}
    Node(const Node* nd){
        if(nd){
            l=nd->l; r=nd->r;
        }else{
            l=r=nullptr;
        }
        par=nullptr;
        dist=0;
        tag=0;
    }
};
bool ac=true;
pair<Node*,char> find(Node* nd){
    assert(nd);
    if(!nd->par) return make_pair(nd,0);
    auto res=find(nd->par);
    res.S^=nd->dist;
    nd->par=res.F;
    return res;
}
void uni(Node* a,Node* b,int d){
    if(!a || !b) return;
    auto r1=find(a);
    auto r2=find(b);
    int dist=(r1.S^r2.S);
    if(r1.F==r2.F){
        ac=ac&&(dist==d);
    }else{
        r1.F->par=r2.F;
        r1.F->dist=(dist!=d);
    }
}
 
Node* ptr[1000010];
Node* rt[1000010];
 
Node* update(Node *ori,int l,int r,int p,int x){
    Node* nd=new Node(ori);
    if(l==r){
        ptr[x]=nd;
        return nd;
    }
    int m=(l+r)/2;
    if(p<=m) nd->l=update(nd->l,l,m,p,x);
    else nd->r=update(nd->r,m+1,r,p,x);
    return nd;
}
 
void link(Node *seg,int l,int r,Node *nd,int L,int R){
    if(!seg) return;
    if(l>R || r<L) return;
    if(l>=L && r<=R){
        seg->adj.eb(nd,1);
        nd->adj.eb(seg,1);
        return;
    }
    int m=(l+r)/2;
    link(seg->l,l,m,nd,L,R);
    link(seg->r,m+1,r,nd,L,R);
}
 
void dfs(Node* nd){
    if(!nd) return;
    if(nd->tag>=0) return;
    nd->tag=0;
    for(auto &i:nd->adj){
        uni(nd,i.F,i.S);
        dfs(i.F);
    }
}
 
int32_t main() {
    NYA();
    // nyaacho >/////<
    // miku sama bless me >/////<
    int n; cin>>n;
    // assert(n<=2000);
    vector<pii> v(n);
    for(auto &i:v) cin>>i.F>>i.S;
    sort(all(v));
    rt[0]=nullptr;
    For(i,0,n-1){
        rt[i+1]=update(rt[i],1,n*2,v[i].S,i);
        link(rt[i+1],1,n*2,ptr[i],v[i].F+1,v[i].S-1);
    }
 
    queue<Node*> que;
    For(i,1,n) que.emplace(rt[i]);
    while(!que.empty()){
        auto now=que.front();
        que.pop();
        if(!now) continue;
        if(now->tag<0) continue;
 
        if(now->l && now->l->tag==0) que.emplace(now->l);
        if(now->r && now->r->tag==0) que.emplace(now->r);
 
        bool oao=now->tag==2 || (sz(now->adj)>0);
        if(oao){
            if(now->l){
                now->adj.eb(now->l,0);
                now->l->adj.eb(now,0);
                now->l->tag=2;
            }
            if(now->r){
                now->adj.eb(now->r,0);
                now->r->adj.eb(now,0);
                now->r->tag=2;
            }
        }
        if(now->l) chmax(now->l->tag,(char)1);
        if(now->r) chmax(now->r->tag,(char)1);
        now->tag=-1;
    }
    
    For(i,0,n-1){
        dfs(ptr[i]);
    }
    
    set<Node*> st;
    For(i,0,n-1){
        st.insert(find(ptr[i]).F);
    }
    if(ac) cout<<fpow(2,sz(st));
    else cout<<"-1\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... |