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], bit[maxn];
int ds[maxn], dsz[maxn];
pair<int,int> c[maxn];
int find(int v) {
if(v == ds[v]) return v;
return ds[v] = find(ds[v]);
}
void join(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return;
if(dsz[u] < dsz[v]) swap(u,v);
dsz[u]+= dsz[v];
ds[v] = u;
}
void attb1(int pos, int val) {
for(int i = pos; i <= 2*n; i+= i&-i) bit[i] = max(bit[i],val);
}
int qryb1(int pos) {
int val = 0;
for(int i = pos; i > 0; i-= i&-i) val = max(val,bit[i]);
return val;
}
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;
dsz[i] = 1;
}
// The graph will not be bipartite only if there exist some b1 < b2 < b3
set<int> stb1;
for(int i = 1; i <= n; i++) {
stb1.insert(b[i]);
auto it = stb1.find(b[i]);
if(it != stb1.begin()) {
int b1 = *prev(it);
// adiciona o par b1,b[i]
attb1(b[i],b1);
}
// If the max b1 with b2 < b[i] is > a[i], I found it
if(qryb1(b[i]-1) > a[i]) {
cout << 0 << endl;
return;
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j < i; j++) {
if(a[i] < b[j] && b[j] < b[i]) join(i,j);
}
}
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... |