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 optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unroll-all-loops")
#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;
char buf[(long long)1e9];
size_t p_ = 0;
inline void *operator new(size_t n_) {
p_ += n_;
return buf + p_ - n_;
}
inline void operator delete(void*) {};
using namespace std;
const int INF = 1e9 + 100;
const int MAXN = 1e6;
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(MAXN);
seg_min seg_mi(MAXN);
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);
}
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_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);
}
}
int n;
int ans = 1ll;
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 == //
scanf("%d", &n);
a.resize(n);
used.resize(n);
g.resize(n);
for (int i = 0; i < n; i++) {
pair<int, int> x;
scanf("%d%d", &x.F, &x.S);
x.F--; x.S--;
a[i] = x;
num1[x.S] = i;
num2[x.F] = i;
seg_ma.update(x.F, x.S);
seg_mi.update(x.S, x.F);
}
for (int i = 0; i < n; i++) {
if (!used[i]) {
solve(i);
ans = ans << 1ll;
ans %= MOD;
}
}
fill(all(used), 0);
for (int i = 0; i < n; i++) {
if (!used[i]) {
dfs(i);
}
}
cout << ans << '\n';
}
Compilation message (stderr)
port_facility.cpp: In function 'int main()':
port_facility.cpp:205:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
port_facility.cpp:213:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x.F, &x.S);
~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |