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;
#define ll long long
#define pb push_back
#define vll vector<ll>
#define fr first
#define bc back()
#define sc second
const ll inf=2e9;
const ll N=4e5+7;
vector<vector<int>> g(N);
int p[N],siz[N];
int anc(ll a){
return (a==p[a] ? a : p[a]=anc(p[a]));
}
bool uni(ll a,ll b){
a=anc(a),b=anc(b);
if(a==b)return false;
if(siz[a]>siz[b])swap(a,b);
siz[b]+=siz[a];
p[a]=b;
return true;
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
ll i;
ll n=s.size();
//compress;
set<int> st;
map<int,ll> pos,mp;
for(i=0;i<n;i++){
st.insert(s[i]);
st.insert(t[i]);
}
st.insert(inf);
ll c=0;
for(auto i : st){
pos[i]=c;
mp[c]=i;
c++;
}
vll val(c);
for(i=0;i<c;i++){
val[i]=mp[i];
p[i]=i;
siz[i]=1;
}
vll vs,vt;
vll pref(c);
pref[c-1]++;
pref[0]--;
for(i=0;i<n;i++){
vs.pb(pos[s[i]]);
vt.pb(pos[t[i]]);
g[vs[i]].pb(vt[i]);
pref[vs[i]]++;
pref[vt[i]]--;
}
ll ans=0;
for(i=0;i<c-1;i++){
if(i>0)pref[i]+=pref[i-1];
if(pref[i]>0){
ans+=pref[i]*(val[i+1]-val[i]);
g[i+1].pb(i);
}
if(pref[i]<0){
g[i].pb(i+1);
}
}
uni(0,c-1);
for(i=0;i<c;i++){
for(auto j : g[i]){
uni(i,j);
}
}
vector<pair<ll,ll>> edges;
for(i=1;i<c;i++){
edges.pb({val[i]-val[i-1],i});
}
sort(edges.begin(),edges.end());
for(i=0;i<(ll)edges.size();i++){
if(uni(edges[i].sc,edges[i].sc-1)){
ans+=edges[i].fr;
}
}
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... |