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;
if(st.find(mp(mp(dsl[u],dsr[u]),ds[u])) != st.end()) st.erase(mp(mp(dsl[u],dsr[u]),ds[u]));
if(st.find(mp(mp(dsl[v],dsr[v]),ds[v])) != st.end()) 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;
// }
// }
// }
// for(int i = 1; i <= n; i++) {
// vector<int> idscomb;
// st.insert(mp(mp(b[i],b[i]),i));
// for(auto it : st) {
// int u = find(it.sc);
// bool ok = false;
// for(auto x : dsbs[u]) {
// if(a[i] < x && x < b[i]) ok = true;
// }
// if(ok) {
// idscomb.pb(u);
// }
// }
// for(auto u : idscomb) {
// join(find(i),dsant[find(u)]);
// join(dsant[find(i)],find(u));
// dsant[find(i)] = find(u);
// dsant[find(u)] = find(i);
// }
// }
set<int> atvs; // the b's of the past ones
for(int i = 1; i <= n; i++) {
// cout << st.size() << endl;
// for(auto x : st) {
// cout << x.fr.fr << " " << x.fr.sc << " = " << x.sc << endl;
// }cout << endl;
// just be sure that we have someone in (a[i],b[i])
auto it1 = atvs.upper_bound(a[i]);
if(it1 == atvs.end() || *it1 > b[i]) {
st.insert(mp(mp(b[i],b[i]),i));
atvs.insert(b[i]);
continue;
}
vector<int> idscomb; // ids that I will unite
auto it = st.upper_bound(mp(mp(b[i],0),0)); // the first interval after b
// while(it != st.begin() && prev(it)->fr.sc > a[i]) {
// it = prev(it);
// idscomb.pb(it->sc);
// }
for(auto it : st) {
int u = find(it.sc);
bool ok = false;
for(auto x : dsbs[u]) {
if(a[i] < x && x < b[i]) ok = true;
}
if(ok) {
idscomb.pb(u);
}
}
st.insert(mp(mp(b[i],b[i]),i));
atvs.insert(b[i]);
if(idscomb.size()) {
// combine all this and put i in the dsant of this
int pivot = idscomb[0];
for(int i = 1; i < idscomb.size(); i++) {
join(pivot,idscomb[i]);
}
join(dsant[find(pivot)],find(i));
join(dsant[find(i)],find(pivot));
dsant[find(i)] = find(pivot);
dsant[find(pivot)] = find(i);
// cout << i << " " << pivot << " " << idscomb.size() << endl;
}
}
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();
}
}
Compilation message (stderr)
port_facility.cpp: In function 'void solve()':
port_facility.cpp:134:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for(int i = 1; i < idscomb.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
port_facility.cpp:113:14: warning: variable 'it' set but not used [-Wunused-but-set-variable]
113 | auto it = st.upper_bound(mp(mp(b[i],0),0)); // the first interval after b
| ^~
# | 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... |