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 double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<double> point;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 5e5 + 10;
int n, val[maxn], par[maxn];
vector<int> num;
int getpar(int v){
return (par[v] == -1? v: par[v] = getpar(par[v]));
}
bool merge(int u, int v){
if ((u = getpar(u)) == (v = getpar(v))) return false;
par[u] = v;
return true;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
memset(par, -1, sizeof par);
n = s.size();
ll k = 0;
for (int i = 0; i < n; i++){
num.push_back(s[i]);
num.push_back(t[i]);
k += t[i] - s[i];
}
k -= 1e9 + 7;
num.push_back(0);
num.push_back(1e9 + 7);
sort(all(num));
num.resize(distance(num.begin(), unique(all(num))));
for (int i = 0; i < n; i++){
int ids = lower_bound(all(num), s[i]) - num.begin();
int idt = lower_bound(all(num), t[i]) - num.begin();
merge(ids, idt);
val[ids]++;
val[idt]--;
}
n = num.size();
ll ans = 0;
int cnt = 0;
vector<pair<int,pii>> edge;
for (int i = 1; i < n; i++){
if (cnt <= 0){
ans += 1ll * (1-cnt) * (num[i] - num[i-1]);
merge(i, i-1);
}
else if (cnt > 1){
ans += 1ll * (cnt-1) * (num[i] - num[i-1]);
merge(i, i-1);
}
cnt += val[i];
edge.push_back({(num[i] - num[i-1]) * 2, {i-1, i}});
}
sort(all(edge));
for (auto x: edge){
if (merge(x.S.F, x.S.S)) ans += x.F;
}
ans = (ans - k) / 2 + k;
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... |