#include<bits/stdc++.h>
using namespace std;
#define int long long
struct dsu {
int n, cc;
vector<int> r, s, d;
dsu(int n) {
this->n=n; cc=n;
r.reserve(n+1);
for (int i = 1; i <= n; ++i) r[i]=i;
s.assign(n+1, 1);
d.assign(n+1, 0);
}
int find(int a) {
if (a==r[a]) return a;
int p=find(r[a]);
d[a]^=d[r[a]];
return r[a]=p;
}
bool unite(int a, int b) {
int ra=find(a), rb=find(b);
if (ra==rb) return d[a]^d[b]==1;
if (s[ra]<s[rb]) swap(ra, rb);
d[rb]=1^d[a]^d[b];
r[rb]=ra; s[ra]+=s[rb]; cc--;
return true;
}
};
struct node {
int j, b=1;
node *nxt=nullptr, *prev=nullptr;
void init(int _j) { j=_j; }
};
int exp(int a, int b, int mod) {
int x=1;
while (b) {
if (b%2) x=(x*a)%mod;
a=(a*a)%mod; b/=2;
} return x;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
int n;
cin >> n;
vector<pair<int, int>> s;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
s.push_back({a[i], i+1});
s.push_back({b[i], -i-1});
} sort(s.begin(), s.end());
dsu uf(n+1);
vector<node> c(n);
vector<int> p(n+1);
for (auto i = 0, v = 0; i < 2*n; ++i) {
auto [t, j] = s[i];
if (j>0) {
p[j]=v;
c[v].init(j);
auto &t=c[v++];
if (v>1) {
t.prev=&c[v-2];
while (t.prev->prev && !t.prev->b) t.prev=t.prev->prev;
} if (t.prev) { t.prev->nxt=&t; }
} else {
auto &t=c[p[j=-j]];
auto m=t.nxt;
while (m) {
if (!uf.unite(t.j, m->j)) { cout << "0\n" << '\n'; return 0; }
m=m->nxt;
} t.b=0;
if (t.prev) t.prev->nxt=t.nxt;
if (t.nxt) t.nxt->prev=t.prev;
}
}
cout << exp(2, uf.cc-1, 1e9+7) << '\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... |