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)1e10];
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 = 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);
}
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);
}
}
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);
g.resize(n);
seg_mi = seg_min(2 * n);
seg_ma = seg_max(2 * 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);
}
long long ans = 1ll;
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)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `void std::__facet_shims::(anonymous namespace)::__destroy_string<wchar_t>(void*)':
(.text._ZNSt13__facet_shims12_GLOBAL__N_116__destroy_stringIwEEvPv+0x1e): relocation truncated to fit: R_X86_64_32S against symbol `std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_Rep::_S_empty_rep_storage' defined in .bss._ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE[_ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-wstring-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `void std::__facet_shims::(anonymous namespace)::__destroy_string<char>(void*)':
(.text._ZNSt13__facet_shims12_GLOBAL__N_116__destroy_stringIcEEvPv+0x1e): relocation truncated to fit: R_X86_64_32S against symbol `std::string::_Rep::_S_empty_rep_storage' defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-string-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `std::__facet_shims::(anonymous namespace)::money_get_shim<wchar_t>::do_get(std::istreambuf_iterator<wchar_t, std::char_traits<wchar_t> >, std::istreambuf_iterator<wchar_t, std::char_traits<wchar_t> >, bool, std::ios_base&, std::_Ios_Iostate&, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&) const':
(.text._ZNKSt13__facet_shims12_GLOBAL__N_114money_get_shimIwE6do_getESt19istreambuf_iteratorIwSt11char_traitsIwEES6_bRSt8ios_baseRSt12_Ios_IostateRSbIwS5_SaIwEE+0xfe): relocation truncated to fit: R_X86_64_32S against symbol `std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_Rep::_S_empty_rep_storage' defined in .bss._ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE[_ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-wstring-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `std::__facet_shims::(anonymous namespace)::money_get_shim<wchar_t>::do_get(std::istreambuf_iterator<wchar_t, std::char_traits<wchar_t> >, std::istreambuf_iterator<wchar_t, std::char_traits<wchar_t> >, bool, std::ios_base&, std::_Ios_Iostate&, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&) const':
(.text._ZNKSt13__facet_shims12_GLOBAL__N_114money_get_shimIwE6do_getESt19istreambuf_iteratorIwSt11char_traitsIwEES6_bRSt8ios_baseRSt12_Ios_IostateRSbIwS5_SaIwEE+0x17b): relocation truncated to fit: R_X86_64_32S against symbol `std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_Rep::_S_empty_rep_storage' defined in .bss._ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE[_ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-wstring-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `std::__facet_shims::(anonymous namespace)::money_get_shim<char>::do_get(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, bool, std::ios_base&, std::_Ios_Iostate&, std::string&) const':
(.text._ZNKSt13__facet_shims12_GLOBAL__N_114money_get_shimIcE6do_getESt19istreambuf_iteratorIcSt11char_traitsIcEES6_bRSt8ios_baseRSt12_Ios_IostateRSs+0xfe): relocation truncated to fit: R_X86_64_32S against symbol `std::string::_Rep::_S_empty_rep_storage' defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-string-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `std::__facet_shims::(anonymous namespace)::money_get_shim<char>::do_get(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, bool, std::ios_base&, std::_Ios_Iostate&, std::string&) const':
(.text._ZNKSt13__facet_shims12_GLOBAL__N_114money_get_shimIcE6do_getESt19istreambuf_iteratorIcSt11char_traitsIcEES6_bRSt8ios_baseRSt12_Ios_IostateRSs+0x17b): relocation truncated to fit: R_X86_64_32S against symbol `std::string::_Rep::_S_empty_rep_storage' defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-string-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `void std::__facet_shims::__numpunct_fill_cache<char>(std::integral_constant<bool, false>, std::locale::facet const*, std::__numpunct_cache<char>*)':
(.text._ZNSt13__facet_shims21__numpunct_fill_cacheIcEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E[_ZNSt13__facet_shims21__numpunct_fill_cacheIcEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E]+0xa1): relocation truncated to fit: R_X86_64_32S against symbol `std::string::_Rep::_S_empty_rep_storage' defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-string-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `void std::__facet_shims::__numpunct_fill_cache<char>(std::integral_constant<bool, false>, std::locale::facet const*, std::__numpunct_cache<char>*)':
(.text._ZNSt13__facet_shims21__numpunct_fill_cacheIcEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E[_ZNSt13__facet_shims21__numpunct_fill_cacheIcEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E]+0x24f): relocation truncated to fit: R_X86_64_32S against symbol `std::string::_Rep::_S_empty_rep_storage' defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-string-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `void std::__facet_shims::__numpunct_fill_cache<wchar_t>(std::integral_constant<bool, false>, std::locale::facet const*, std::__numpunct_cache<wchar_t>*)':
(.text._ZNSt13__facet_shims21__numpunct_fill_cacheIwEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E[_ZNSt13__facet_shims21__numpunct_fill_cacheIwEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E]+0xa9): relocation truncated to fit: R_X86_64_32S against symbol `std::string::_Rep::_S_empty_rep_storage' defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-string-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `void std::__facet_shims::__numpunct_fill_cache<wchar_t>(std::integral_constant<bool, false>, std::locale::facet const*, std::__numpunct_cache<wchar_t>*)':
(.text._ZNSt13__facet_shims21__numpunct_fill_cacheIwEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E[_ZNSt13__facet_shims21__numpunct_fill_cacheIwEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E]+0x114): relocation truncated to fit: R_X86_64_32S against symbol `std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_Rep::_S_empty_rep_storage' defined in .bss._ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE[_ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-wstring-inst.o)
/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a(cow-shim_facets.o): In function `void std::__facet_shims::__numpunct_fill_cache<wchar_t>(std::integral_constant<bool, false>, std::locale::facet const*, std::__numpunct_cache<wchar_t>*)':
(.text._ZNSt13__facet_shims21__numpunct_fill_cacheIwEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E[_ZNSt13__facet_shims21__numpunct_fill_cacheIwEEvSt17integral_constantIbLb0EEPKNSt6locale5facetEPSt16__numpunct_cacheIT_E]+0x294): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status