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>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2e6 + 7, MOD = 1000 * 1000 * 1000 + 7;
vector <int> g[N];
int l[N], r[N];
bool intr(int i, int j) {
int a = max(l[i], l[j]);
int b = min(r[i], r[j]);
return a <= b;
}
bool in(int i, int j) {
return l[j] <= l[i] && r[i] <= r[j];
}
bool used[N];
void dfs(int u) {
used[u] = 1;
for (int v : g[u]) {
if (!used[v])
dfs(v);
}
}
bool edge(int i, int j) {
return intr(i, j) && !in(i, j) && !in(j, i);
}
int rin[N], lin[N];
int who[N];
bool comp(ii a, ii b) {
return a.s > b.s;
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> l[i] >> r[i];
who[l[i]] = who[r[i]] = i;
}
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
if (edge(i, j)) {
g[i].app(j);
g[j].app(i);
}
}
}
/*
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
for (int k = j + 1; k <= n; ++k) {
if (edge(i, j) && edge(j, k) && edge(i, k)) {
cout << 0 << endl;
exit(0);
}
}
}
}
*/
vector <ii> a;
{
for (int i = 1; i <= n; ++i)
a.app(mp(l[i], r[i]));
sort(all(a));
set <int> ms;
for (auto e : a) {
int i = who[e.f];
auto t = ms.upper_bound(e.s);
if (t == ms.begin())
rin[i] = -1;
else {
--t;
rin[i] = *t;
}
ms.insert(e.s);
}
}
{
sort(all(a), comp);
set <int> ms;
for (auto e : a) {
int i = who[e.f];
auto t = ms.lower_bound(e.f);
if (t == ms.end())
lin[i] = N;
else
lin[i] = *t;
ms.insert(e.f);
}
}
for (int i = 1; i <= n; ++i) {
if (lin[i] < rin[i]) {
cout << 0 << endl;
exit(0);
}
}
int comp = 0;
for (int i = 1; i <= n; ++i) {
if (!used[i]) {
dfs(i);
++comp;
}
}
int ans = 1;
for (int i = 0; i < comp; ++i)
ans = (ans << 1) % MOD;
cout << ans << endl;
}
# | 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... |