This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unroll-all-loops")
#include <bits/stdc++.h>
//#define TASK "lca"
#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 = 1e9 + 100;
const int MAXN = 30;
const int MOD = 1e9 + 7;
const double EPS = 1e-12;
//char buf[(int)1e9];
//size_t p_ = 0;
//
//inline void *operator new(size_t n_) {
// p_ += n_;
// return buf + p_ - n_;
//}
//inline void operator delete(void*) {};
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<int> used;
unordered_map<int, int> num1, num2;
seg_max seg_ma;
seg_min seg_mi;
void solve(int i, int color = 1) {
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] && used[p] == color) {
cout << 0 << "\n";
exit(0);
}
used[p] = color % 2 + 1;
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] && used[p] == color) {
cout << 0 << "\n";
exit(0);
}
used[p] = color % 2 + 1;
next.pb(p);
seg_mi.update(a[p].S, INF);
}
for (auto x: next) {
solve(x, 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);
used.resize(n);
seg_mi = seg_min(2 * n);
seg_ma = seg_max(2 * n);
for (int i = 0; i < n; i++) {
pii x;
cin >> 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);
}
ll ans = 1;
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';
}
# | 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... |