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 = 1e6 + 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);
}
}
}
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];
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
if (intr(i, j) && !in(i, j) && !in(j, i)) {
g[i].app(j);
g[j].app(i);
}
}
}
vector <ii> a;
for (int i = 1; i <= n; ++i)
a.app(mp(l[i], r[i]));
sort(all(a));
vector <int> s1, s2;
s1.app(N); s2.app(N);
for (auto e : a) {
/*
cout << "add " << e.f << ' ' << e.s << endl;
for (auto e : s1)
cout << e << ' ';
cout << endl;
for (auto e : s2)
cout << e << ' ';
cout << endl;
*/
while (s1.back() < e.f)
s1.pop_back();
while (s2.back() < e.f)
s2.pop_back();
if (s1.back() < e.s && s2.back() < e.s) {
cout << 0 << endl;
exit(0);
}
else if (s1.back() < e.s) {
s2.app(e.s);
}
else if (s2.back() < e.s) {
s1.app(e.s);
}
else if (s1.back() < s2.back())
s1.app(e.s);
else
s2.app(e.s);
}
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... |