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 TASK "lca"
#pragma GCC optimaze("Ofast");
#pragma target("sse3,sse4");
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second
#define pb push_back
#define int long long
#define pii pair<int, int>
typedef long long ll;
//typedef long double ld;
using namespace std;
const int INF = 1e18;
const int MAXN = 30;
const int MOD = 1e9 + 7;
const double EPS = 1e-12;
ll power(ll x, ll n, ll mod = 1e9 + 7) {
if (n == 0) return 1ll;
if (n & 1ll) return power(x, n - 1ll, mod) * x % mod;
ll tmp = power(x, n >> 1ll, mod);
return (tmp * tmp) % mod;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd (b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
struct seg_max {
vector<int> d;
int SZ = 1;
seg_max(int n = 0) {
while(SZ < n) SZ *= 2;
d.resize(SZ * 2, -INF);
}
void update(int x, int dx, int v, int l, int r) {
if (x < l || x >= r) return;
if (l + 1 >= r) {
d[v] = dx;
return;
}
int c = (r + l) / 2;
update(x, dx, v * 2 + 1, l, c);
update(x, dx, v * 2 + 2, c, r);
d[v] = max(d[v * 2 + 1], d[v * 2 + 2]);
}
void update(int x, int dx) {
update(x, dx, 0, 0, SZ);
}
int query(int ql, int qr, int v, int l, int r) {
if (qr <= l || r <= ql) return -INF;
if (ql <= l && r <= qr) return d[v];
int c = (l + r) / 2;
return max(query(ql, qr, 2 * v + 1, l, c), query(ql, qr, 2 * v + 2, c, r));
}
int query(int ql, int qr) {
return query(ql, qr, 0, 0, SZ);
}
};
struct seg_min {
vector<int> d;
int SZ = 1;
seg_min(int n = 0) {
while(SZ < n) SZ *= 2;
d.resize(SZ * 2, INF);
}
void update(int x, int dx, int v, int l, int r) {
if (x < l || x >= r) return;
if (l + 1 >= r) {
d[v] = dx;
return;
}
int c = (r + l) / 2;
update(x, dx, v * 2 + 1, l, c);
update(x, dx, v * 2 + 2, c, r);
d[v] = min(d[v * 2 + 1], d[v * 2 + 2]);
}
void update(int x, int dx) {
update(x, dx, 0, 0, SZ);
}
int query(int ql, int qr, int v, int l, int r) {
if (qr <= l || r <= ql) return INF;
if (ql <= l && r <= qr) return d[v];
int c = (l + r) / 2;
return min(query(ql, qr, 2 * v + 1, l, c), query(ql, qr, 2 * v + 2, c, r));
}
int query(int ql, int qr) {
return query(ql, qr, 0, 0, SZ);
}
};
vector<pair<int, int>> a;
vector<vector<int>> g;
vector<int> used;
unordered_map<int, int> num1, num2;
seg_max seg_ma;
seg_min seg_mi;
void solve(int i, int color = 1) {
if (used[i]) return;
seg_ma.update(a[i].F, -INF);
seg_mi.update(a[i].S, INF);
used[i] = color;
vector<int> next;
int l = a[i].F;
int r = a[i].S;
int ans = INF;
while (ans > r) {
ans = seg_ma.query(l, r + 1);
if (ans <= r) break;
int p = num1[ans];
if (!used[p]) {
g[i].pb(p);
g[p].pb(i);
next.pb(p);
}
seg_ma.update(a[p].F, -INF);
//seg_mi.update(a[p].S, INF);
}
ans = -INF;
while (ans < l) {
ans = seg_mi.query(l, r + 1);
if (ans >= l) break;
int p = num2[ans];
if (!used[p]) {
g[i].pb(p);
g[p].pb(i);
next.pb(p);
}
//seg_ma.update(a[p].F, -INF);
seg_mi.update(a[p].S, INF);
}
for (auto x: next) {
solve(x, color % 2 + 1);
}
}
void dfs(int v, int color = 1) {
used[v] = color;
for (auto u: g[v]) {
if (u != v && used[u] && used[u] == color) {
cout << 0 << "\n";
exit(0);
}
if (!used[u]) dfs(u, color % 2 + 1);
}
}
signed main() {
#ifndef LOCAL
#ifdef TASK
freopen(TASK".in", "r", stdin);
freopen(TASK".out", "w", stdout);
#endif
#endif
#ifdef LOCAL
//freopen("/Users/alekseygolub/Desktop/с++/ABS/ABS/input.txt", "r", stdin);
#endif
iostream::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(924653523);
// == SOLUTION == //
int n;
cin >> n;
a.resize(n);
seg_mi = seg_min(2 * n);
seg_ma = seg_max(2 * n);
g.resize(n);
int i = -1;
for (auto &x: a) {
i++;
cin >> x.F >> x.S;
x.F--; x.S--;
num1[x.S] = i;
num2[x.F] = i;
seg_ma.update(x.F, x.S);
seg_mi.update(x.S, x.F);
}
used.resize(n);
ll ans = 0;
for (int i = 0; i < n; i++) {
if (!used[i]) {
solve(i);
ans++;
}
}
fill(all(used), 0);
for (int i = 0; i < n; i++) {
if (!used[i]) {
dfs(i);
}
}
cout << power(2, ans) << '\n';
}
Compilation message (stderr)
port_facility.cpp:5:0: warning: ignoring #pragma GCC optimaze [-Wunknown-pragmas]
#pragma GCC optimaze("Ofast");
port_facility.cpp:6:0: warning: ignoring #pragma target [-Wunknown-pragmas]
#pragma target("sse3,sse4");
# | 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... |