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 "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... |