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>
#include "railroad.h"
using namespace std;
typedef long long ll;
ll plan_roller_coaster(vector<int> s,vector<int> t)
{
s.push_back(1<<30);
t.push_back(1);
int n=s.size();
map<int,int> h;
vector<int> x;
for(int y:s) x.push_back(y);
for(int y:t) x.push_back(y);
sort(x.begin(),x.end());
x.erase(unique(x.begin(),x.end()),x.end());
int m=x.size();
for(int i=0;i<m;i++) h[x[i]]=i;
vector<int> d(m,0);
vector<int> p(m,0);
for(int i=0;i<m;i++) p[i]=i;
vector<int> sz(m,1);
function<int(int)> find_set=[&](int a)->int
{
if(a==p[a]) return a;
else return p[a]=find_set(p[a]);
};
auto merge_sets=[&](int a,int b)->int
{
a=find_set(a);
b=find_set(b);
if(a==b) return 0;
if(sz[a]<sz[b]) swap(a,b);
p[b]=a;
sz[a]+=sz[b];
return 1;
};
for(int i=0;i<n;i++)
{
int l=h[s[i]];
int r=h[t[i]];
d[l]++;
d[r]--;
merge_sets(l,r);
}
ll res=0;
int sum=0;
vector<array<int,3>> edges;
for(int i=0;i+1<m;i++)
{
sum+=d[i];
res+=(max(0,sum)*ll(x[i+1]-x[i]));
edges.push_back({x[i+1]-x[i],i,i+1});
if(sum!=0) merge_sets(i,i+1);
}
sort(edges.begin(),edges.end());
for(auto [c,a,b]:edges) res+=(merge_sets(a,b)*c);
return res;
}
# | 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... |