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 i64 = long long;
constexpr int P = 1E9 + 7;
struct DSU {
std::vector<int> f, siz;
DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); }
int leader(int x) {
while (x != f[x]) x = f[x] = f[f[x]];
return x;
}
bool same(int x, int y) { return leader(x) == leader(y); }
bool merge(int x, int y) {
x = leader(x);
y = leader(y);
if (x == y) return false;
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x) { return siz[leader(x)]; }
int count() {
int cnt = 0;
for (int i = 0; i < int(f.size()); i++) {
cnt += f[i] == i;
}
return cnt;
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<std::pair<int, int>> a(n);
std::vector<int> p(2 * n), id(2 * n);
for (int i = 0; i < n; i++) {
std::cin >> a[i].first >> a[i].second;
a[i].first--;
a[i].second--;
p[a[i].first] = a[i].second;
p[a[i].second] = a[i].first;
id[a[i].first] = id[a[i].second] = i;
}
DSU d(n);
int m = 2 * n;
std::vector mx(21, std::vector<int>(m));
for (int i = 0; i < m; i++) {
mx[0][i] = p[i];
}
for (int j = 0; (1 << (j + 1)) <= m; j++) {
for (int i = 0; i + (1 << (j + 1)) <= m; i++) {
if (mx[j][i] > mx[j][i + (1 << j)]) {
mx[j + 1][i] = mx[j][i];
} else {
mx[j + 1][i] = mx[j][i + (1 << j)];
}
}
}
auto query = [&](int l, int r) {
int k = std::__lg(r - l);
if (mx[k][l] > mx[k][r - (1 << k)]) {
return mx[k][l];
} else {
return mx[k][r - (1 << k)];
}
};
std::vector<bool> vis(n);
for (int i = 0; i < m; i++) {
if (!vis[id[i]]) {
std::function<void(int)> dfs = [&](int u) {
vis[id[u]] = true;
int l = u + 1;
int r = p[u];
while (l < r) {
int k = query(l, r);
k = p[k];
if (p[k] > r) {
if (l < k && query(l, k) > r) {
std::cout << "0\n";
std::exit(0);
}
if (!d.merge(id[u], id[k])) {
std::cout << "0\n";
std::exit(0);
}
dfs(k);
l = k + 1;
} else {
break;
}
}
};
dfs(i);
}
}
int ans = 1;
int c = d.count();
for (int i = 0; i < c; i++) {
ans = 2 * ans % P;
}
std::cout << ans << "\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... |