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;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 400013
#define INF 1000000007
#define LLINF 2696969696969696969
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N;
pll arr[MAXN];
ll dp[(1 << 17)][17];
ll ans = 0;
int pref[MAXN];
vl guys;
int dsu[MAXN];
vector<pair<ll, pii> > edges;
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
bool merge(int u, int v)
{
u = get(u); v = get(v);
if (u == v) return false;
dsu[u] = v;
return true;
}
ll plan_roller_coaster(vector<int> s, vector<int> t)
{
N = SZ(s);
FOR(i, 0, N)
{
arr[i] = {s[i], t[i]};
arr[i].fi--; arr[i].se--;
}
arr[N] = {INF, 0};
N++;
FOR(i, 0, N)
{
guys.PB(arr[i].fi);
guys.PB(arr[i].se);
}
sort(ALL(guys));
guys.erase(unique(ALL(guys)), guys.end());
FOR(i, 0, SZ(guys))
{
dsu[i] = i;
}
FOR(i, 0, N)
{
int lt = arr[i].fi, rt = arr[i].se;
lt = LB(ALL(guys), lt) - guys.begin();
rt = LB(ALL(guys), rt) - guys.begin();
pref[lt]++;
pref[rt]--;
merge(lt, rt);
}
FOR(i, 1, SZ(guys))
{
pref[i] += pref[i - 1];
}
FOR(i, 0, SZ(guys) - 1)
{
ll dis = guys[i + 1] - guys[i];
if (pref[i] > 0)
{
ans += pref[i] * dis;
}
if (pref[i] != 0)
{
merge(i, i + 1);
}
edges.PB({dis, {i, i + 1}});
}
sort(ALL(edges));
for (auto p : edges)
{
if (merge(p.se.fi, p.se.se))
{
ans += p.fi;
}
}
return ans;
//you cannot insert -1s. you can get +1s but it means accelerate.
//ok mark the guys that are
}
# | 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... |