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 = 1e6+10;
const int mod = 1e9+7;
int n, a[maxn], b[maxn];
int ds[maxn], dsz[maxn], dsgrp[maxn], dsgb[maxn];
vector<int> dsvec[maxn];
set<int> dsgrpbs[maxn][2]; // mantain the B's of the ones in this component
set<pair<int,int>> stds; // the greatest B, the index
vector<pair<int,int>> stdseraser;
int find(int v) {
if(v == ds[v]) return v;
return ds[v] = find(ds[v]);
}
void createedge(int u, int v, int chg) {
// an edge from comp of u to comp of v
// dsgrp[v] = dsgrp[u]^chg, basically 0 if from the same group and 1 if not
// if the edge is from u to v, chg = 1
int u1 = u;
int v1 = v;
u = find(u);
v = find(v);
if(dsgrp[u1] != dsgrp[u]) chg^= 1;
if(dsgrp[v1] != dsgrp[v]) chg^= 1;
if(u == v) {
if(chg == 1) {
// I fail, because they are equal
cout << 0 << endl;
exit(0);
}
else {
// stop the uniting
return;
}
}
if(dsz[u] < dsz[v]) {
swap(u,v);
swap(u1,v1);
}
stdseraser.pb(mp(dsgb[u],u));
stdseraser.pb(mp(dsgb[v],v));
// Now I insert all the ones from v in u
ds[v] = u;
dsz[u]+= dsz[v];
dsgb[u] = max(dsgb[u],dsgb[v]);
for(auto id : dsvec[v]) {
// Insert id in u as dsgrp[id]^chg
dsgrp[id]^= chg;
dsgrpbs[u][dsgrp[id]].insert(b[id]);
dsvec[u].pb(id);
}
}
void solve() {
cin >> n;
vector<pair<int,int>> wtfsort;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
wtfsort.pb(mp(a[i],b[i]));
}
sort(all(wtfsort));
for(int i = 1; i <= n; i++) {
a[i] = wtfsort[i-1].fr;
b[i] = wtfsort[i-1].sc;
ds[i] = i;
dsz[i] = 1;
dsvec[i].pb(i);
dsgrp[i] = 0;
dsgb[i] = b[i];
dsgrpbs[i][0].insert(b[i]);
}
stds.insert(mp(dsgb[find(1)],find(1)));
for(int i = 2; i <= n; i++) {
auto it = stds.upper_bound(mp(b[i],0));
if(it != stds.end()) {
it = next(it);
}
while(it != stds.begin()) {
it = prev(it);
int id = it->sc;
if(dsgb[id] < a[i]) break;
bool okgrp0 = false;
auto it0 = dsgrpbs[id][0].upper_bound(a[i]);
if(it0 != dsgrpbs[id][0].end() && *it0 <= b[i]) okgrp0 = true;
bool okgrp1 = false;
auto it1 = dsgrpbs[id][1].upper_bound(a[i]);
if(it1 != dsgrpbs[id][1].end() && *it1 <= b[i]) okgrp1 = true;
if(okgrp0 && okgrp1) {
cout << 0 << endl;
exit(0);
}
if(okgrp0) {
// create an edge between i and 0, so the
// edge is from i to id iff dsgrp[id] = 0
createedge(i,id,(dsgrp[id] == 0));
}
if(okgrp1) {
createedge(i,id,(dsgrp[id] == 1));
}
}
while(stdseraser.size()) {
stds.erase(stdseraser.back());
stdseraser.pop_back();
}
stds.insert(mp(dsgb[find(i)],find(i)));
// for(int j = 1; j < i; j++) {
// if(b[j] > a[i] && b[j] < b[i]) {
// createedge(i,j,1);
// }
// }
}
int ans = 1;
for(int i = 1; i <= n; i++) {
if(find(i) == i) ans = ans*2%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... |