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 N 200005
#define ff first
#define ss second
vector<pair<int, pair<int, int> > > f;
int p[N];
int find(int v){
return (v == p[v] ? v : p[v] = find(p[v]));
}
bool unite(int v, int u){
v = find(v);
u = find(u);
if(v == u) return 0;
p[v] = u;
return 1;
}
map<int, int> sw;
map<int, vector<int> > in;
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = s.size();
s.push_back(1000000001);
t.push_back(1);
for(int i = 0; i <= n; i++){
sw[s[i]]++;
p[i] = i;
sw[t[i]]--;
in[s[i]].push_back(i);
in[t[i]].push_back(i);
}
long long res = 0;
int bal = 0, last = 0, lastid = n;
for(auto x : sw){
if(bal > 0){
res += 1LL * bal * (x.ff - last);
}
for(int y : in[x.ff]){
f.push_back({ bal == 0 ? x.ff - last : 0, {y, lastid}});
lastid = y;
last = x.ff;
}
bal += x.ss;
}
sort(f.begin(), f.end());
for(auto x : f)
res += x.ff * unite(x.ss.ff, x.ss.ss);
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... |