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;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define mp make_pair
#define pb push_back
#define f first
#define s second
struct DSU{
vi e;
void init(int n){
e = vi(n, -1);
}
int get(int a){
if(e[a] < 0) return a;
e[a] = get(e[a]);
return e[a];
}
bool unite(int a, int b){
a = get(a);
b = get(b);
if(a == b) return 0;
if(-e[a] < -e[b]) swap(a, b);
e[a]+=e[b];
e[b] = a;
return 1;
}
};
ll plan_roller_coaster(vi s, vi t) {
s.pb(1000000000);
t.pb(0);
int n = sz(s);
vpi v;
for(int i = 0; i < n; i++){
v.pb(mp(s[i], -1));
v.pb(mp(t[i], 1));
}
sort(all(v));
map<int, int> rv;
for(int i = 0; i < 2*n; i++){
//cout << i << " " << v[i].f << "\n";
rv[v[i].f] = i;
}
DSU dsu;
dsu.init(2*n);
for(int i = 0; i < n; i++){
dsu.unite(rv[s[i]], rv[t[i]]);
}
ll ans = 0;
for(int i = 0; i < 2*n; i++){
if(i-1 >= 0) v[i].s+=v[i-1].s;
if(i+1 < 2*n){
if(v[i].s < 0){
dsu.unite(i, i+1);
ans+=ll(v[i+1].f-v[i].f)*ll(-v[i].s);
//cout << "<: " << i << " " << i+1 << "\n";
}
else if(v[i].s > 0){
dsu.unite(i, i+1);
//cout << ">: " << i << " " << i+1 << "\n";
}
}
}
vector<pair<int, pi>> eds;
for(int i = 0; i+1 < 2*n; i++){
eds.pb(mp(v[i+1].f-v[i].f, mp(i, i+1)));
}
sort(all(eds));
for(auto u: eds){
if(dsu.unite(u.s.f, u.s.s)){
ans+=u.f;
}
}
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... |