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;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e3+10;
const int mod = 1e9+7;
int n, a[maxn], b[maxn];
pair<int,int> c[maxn];
int ds[maxn], dsl[maxn], dsr[maxn], dsant[maxn];
set<int> dsbs[maxn];
set<pair<pair<int,int>,int>> st; // mp(leftmost,rightmost,index)
int find(int v) {
if(v == ds[v]) return v;
return ds[v] = find(ds[v]);
}
void join(int u, int v) {
if(u == -1 || v == -1) return;
u = find(u);
v = find(v);
if(u == v) return;
st.erase(mp(mp(dsl[u],dsr[u]),ds[u]));
st.erase(mp(mp(dsl[v],dsr[v]),ds[v]));
if(dsbs[u].size() < dsbs[v].size()) swap(u,v);
ds[v] = u;
dsl[u] = min(dsl[u],dsl[v]);
dsr[u] = max(dsr[u],dsr[v]);
for(auto x : dsbs[v]) dsbs[u].insert(x);
st.insert(mp(mp(dsl[u],dsr[u]),ds[u]));
join(dsant[u],dsant[v]);
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
c[i] = mp(a[i],b[i]);
}
sort(c+1,c+1+n);
for(int i = 1; i <= n; i++) {
a[i] = c[i].fr;
b[i] = c[i].sc;
ds[i] = i;
dsbs[i].insert(b[i]);
dsant[i] = -1;
dsl[i] = b[i];
dsr[i] = b[i];
}
for(int i = 1; i <= n; i++) {
vector<int> idscomb;
for(int j = 1; j < i; j++) {
if(a[i] < b[j] && b[j] < b[i]) {
join(i,dsant[j]);
join(dsant[i],j);
dsant[i] = j;
dsant[j] = i;
}
}
}
int ans = 1;
for(int i = 1; i <= n; i++) {
if(find(i) == find(dsant[i])) ans = 0;
}
for(int i = 1; i <= n; i++) {
// cout << i << " --- " << find(i) << endl;
}
for(int i = 1; i <= n; i++) {
join(i,dsant[i]);
// cout << i << " -> " << dsant[i] << endl;
}
for(int i = 1; i <= n; i++) {
if(find(i) == i) {
ans*= 2;
ans%= mod;
}
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# | 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... |