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 ll long long
const int N = 1e6+15,mod = 1e9+7;
int L[N],R[N];
int par[N],enemy[N];
int find(int a){
if(a==-1)return -1;
if(a==par[a])return a;
return par[a]= find(par[a]);
}
set<int> dsuid[N];
set<pair<int,int> >minid;
int sz[N];
void push(int x,int y){
minid.erase(make_pair(*dsuid[y].begin(),y));
minid.erase(make_pair(*dsuid[x].begin(),x));
while(dsuid[y].size()>0){
dsuid[x].insert(*dsuid[y].begin());
dsuid[y].erase(dsuid[y].begin());
}
minid.insert(make_pair(*dsuid[x].begin(),x) );
}
void make(int v,int a){
if(v!=-1 && sz[v]>sz[a]){
par[a]= v;
sz[v]+=sz[a];
push(v,a);
}
else if(v!=-1){
par[v]= a;
sz[a]+=sz[v];
push(a,v);
}
}
void make_enemy(int a,int b){
a = find(a),b = find(b);
int u = find(enemy[a]), v = find(enemy[b]);
if(a==b || (u!=-1 && u==v)){
printf("0\n");
exit(0);
}
make(v,a);
make(u,b);
enemy[a]= b;
enemy[b]= a;
}
int main(){
int n;
cin>>n;
vector<pair<int,int> >v;
for(int i=1;i<=n;++i){
scanf("%d%d",&L[i],&R[i]);
v.push_back(make_pair(L[i],i));
}
sort(v.begin(),v.end());
for(int i=1;i<=n;++i){
par[i]= i,enemy[i]= -1,sz[i]= 1;
dsuid[i].insert(R[i]);
}
set<pair<int,int> >s;
set<pair<int,int> >::iterator it,jt;
set<int>::iterator nx;
int u;
for(int i=0;i<v.size();++i){
int l = v[i].first, id = v[i].second;
while(minid.size()>0){
it = minid.begin();
if((*it).first>l)break;
u = (*it).second;
minid.erase(it);
while(dsuid[u].size()>0){
if( *dsuid[u].begin()>l)break;
else
dsuid[u].erase(dsuid[u].begin());
}
if(dsuid[u].size()>0)
minid.insert(make_pair(*dsuid[u].begin(),u));
}
minid.insert(make_pair(R[id],id));
vector<int> Q;
for(it = minid.begin();it!= minid.end();++it){
if( (*it).first>=R[id])break;
Q.push_back((*it).second);
}
while(!Q.empty()){
make_enemy(Q.back(),id);
Q.pop_back();
}
}
long long ret= 1;
for(int i=1;i<=n;++i){
if(find(i)==i&& find(enemy[i])<i){
ret= (ret*2)%mod;
}
}
cout<<ret<<endl;
return 0;
}
Compilation message (stderr)
port_facility.cpp: In function 'int main()':
port_facility.cpp:77:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i=0;i<v.size();++i){
| ~^~~~~~~~~
port_facility.cpp:62:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%d",&L[i],&R[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |