이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include<bits/stdc++.h>
using namespace std;
long long p[2000005],a[2000005];
long long fp(long long a){
return p[a]==a?a:p[a]=fp(p[a]);
}
void un(long long a,long long b){
p[fp(a)]=fp(b);
}
vector<long long>ids;
map<long long,long long>mp;
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
long long n = (long long) s.size();
vector<long long> ids;
for(long long i=0;i<n;i++){
ids.push_back(s[i]);
ids.push_back(t[i]);
}
ids.push_back(LLONG_MIN);
sort(ids.begin(),ids.end());
ids.erase(unique(ids.begin(),ids.end()),ids.end());
long long m=ids.size();
for(long long i=1;i<=m;i++)p[i]=i;
for(long long i=0;i<n;i++){
s[i]=lower_bound(ids.begin(),ids.end(),s[i])-ids.begin();
t[i]=lower_bound(ids.begin(),ids.end(),t[i])-ids.begin();
un(s[i],t[i]);
a[s[i]]++,a[t[i]]--;
}
long long val=0;
long long ans=0;
for(long long i=1;i<m;i++){
val+=a[i];
if(val>1)ans+=(val-1)*(ids[i+1]-ids[i]),un(i,i+1);
else if(val<1)un(i,i+1);
}
vector<pair<long long,long long>>edges;
for(long long i=1;i<m;i++){
edges.push_back({ids[i+1]-ids[i],i});
}
sort(edges.begin(),edges.end());
for(auto x:edges){
if(fp(x.second)!=fp(x.second+1))un(x.second,x.second+1),ans+=(ids[x.second+1]-ids[x.second]);
}
return ans;
}
# | 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... |