이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define f first
#define s second
#define maxn 1000001
#define mod 1000000007
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef set<ii> sii;
typedef long long int ll;
int dsu[maxn], flag=0, opo[maxn], vis[maxn];
sii ini, fim;
sii::iterator initial, last;
vii truck, ordered;
int find(int x){
if(x == -1) return -1;
if(dsu[x]==x) return x;
else{
dsu[x] = find(dsu[x]);
return dsu[x];
}
}
void merge(int a, int b){
a = find(a);
b = find(b);
opo[b] = find(opo[b]);
opo[a] = find(opo[a]);
if(a == b) flag=1;
else{
if(opo[a]==-1 && opo[b]==-1){
opo[a] = b;
opo[b] = a;
}
else if(opo[a]==-1){
dsu[a] = opo[b];
}
else if(opo[b]==-1){
dsu[b] = opo[a];
}
else{
dsu[b] = opo[a];
dsu[opo[b]] = a;
}
}
}
int main(){
int n, i, a, b, u, tam=1;
cin >> n;
for(i=0;i<n;i++){
cin >> a >> b;
a--;b--;
truck.push_back({a, b});
ordered.push_back({b-a, i});
ini.insert({a, i});
fim.insert({b, i});
dsu[i]=i;
opo[i]=-1;
}
sort(ordered.begin(), ordered.end());
for(i=0;i<n;i++){
u = ordered[i].s;
initial = ini.upper_bound({truck[u].f, u});
last = ini.upper_bound({truck[u].s, u});
while(initial!=last){
if((*initial).s!=u) merge(u, (*initial).s);
if(flag) break;
initial++;
}
initial = fim.upper_bound({truck[u].f, u});
last = fim.upper_bound({truck[u].s, u});
while(initial!=last){
if((*initial).s!=u) merge(u, (*initial).s);
if(flag) break;
initial++;
}
if(flag) break;
ini.erase({truck[u].f, u});
fim.erase({truck[u].s, u});
}
if(flag) cout << "0";
else{
for(i=0;i<n;i++){
a = find(i);
if(!vis[a] && (opo[a]==-1 || !vis[opo[a]])) tam+=tam;
if(tam>=mod) tam-=mod;
vis[a]=1;
}
cout << tam;
}
return (0);
}
# | 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... |