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,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 1e6 + 7;
ll A [N], B [N], arr [N << 1], n, ans;
vector <ll> vec [2];
bool mark [N];
set <ll> st;
struct segtree {
ll mx [N << 2], mn [N << 2], n;
segtree () { memset(mx, -63, sizeof mx); memset(mn, 63, sizeof mn); }
inline void updmx (ll pos, cl &x) { for (mx[pos += n] = x; pos > 1; pos >>= 1) mx[pos >> 1] = max(mx[pos], mx[pos ^ 1]); }
inline void updmn (ll pos, cl &x) { for (mn[pos += n] = x; pos > 1; pos >>= 1) mn[pos >> 1] = min(mn[pos], mn[pos ^ 1]); }
inline ll askmx (ll l, ll r) {
ll ret = -1;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret = max(ret, mx[l++]);
if (r & 1) ret = max(ret, mx[--r]);
}
return ret;
}
inline ll askmn (ll l, ll r) {
ll ret = inf;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret = min(ret, mn[l++]);
if (r & 1) ret = min(ret, mn[--r]);
}
return ret;
}
} seg;
void dfs (cl &v, cl &col = 0) {
mark[v] = 1;
seg.updmx(A[v], -1);
seg.updmn(B[v], inf);
vec[col].push_back(v);
for (ll u;;) {
u = seg.askmx(A[v], B[v]);
if (u < B[v]) break;
u = arr[u] - n;
dfs(u, col ^ 1);
}
for (ll u;;) {
u = seg.askmn(A[v], B[v]);
if (u > A[v]) break;
u = arr[u];
dfs(u, col ^ 1);
}
}
bool check (vector <ll> &v) {
st.clear();
sort(All(v), [&](cl &x, cl &y) { return A[x] < A[y]; });
for (auto &x : v) {
if (st.lower_bound(A[x]) != st.lower_bound(B[x])) return false;
st.insert(B[x]);
}
return true;
}
int main ()
{
cin.tie(0)->sync_with_stdio(false);
cin>> n;
seg.n = n + n;
for (ll i = 0; i < n; i++) {
cin>> A[i] >> B[i], arr[--A[i]] = i, arr[--B[i]] = i + n;
seg.updmx(A[i], B[i]); seg.updmn(B[i], A[i]);
}
for (ll i = 0; i < n + n; i++) if (arr[i] < n && !mark[arr[i]]) ans++, dfs(arr[i]);
if (!check(vec[0])) kill(0);
if (!check(vec[1])) kill(0);
for (n = ans, ans = 1; n--;) (ans <<= 1) %= mod;
cout<< ans << endl;
cerr<< "\n" __TIMESTAMP__ " - Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
4
1 3
2 5
4 8
6 7
*/
# | 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... |