This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
#include "railroad.h"
using namespace std;
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int INF = 1e9;
const int SIZE = 4e5 + 5;
int n, m;
ll ans;
int d[SIZE];
vector<int> lis;
int to[SIZE], h[SIZE];
int dsu(int x) {
return x == to[x] ? x : (to[x] = dsu(to[x]));
}
bool mer(int a, int b) {
a = dsu(a), b = dsu(b);
if (a == b) return 0;
if (h[a] < h[b]) swap(a, b);
to[b] = a;
h[a] += (h[a] == h[b]);
return 1;
}
ll plan_roller_coaster(vector<int> s, vector<int> t) {
n = s.size();
lis.clear();
lis.pb(1), lis.pb(INF);
lis.insert(lis.end(), s.begin(), s.end());
lis.insert(lis.end(), t.begin(), t.end());
sort(lis.begin(), lis.end());
lis.erase(unique(lis.begin(), lis.end()), lis.end());
m = lis.size();
iota(to, to + m, 0);
fill(h, h + m, 0);
fill(d, d + m, 0);
ans = 0;
mer(0, m - 1);
d[m - 1]++, d[0]--;
FOR (i, 0, n - 1) {
int l = lower_bound(lis.begin(), lis.end(), s[i]) - lis.begin();
int r = lower_bound(lis.begin(), lis.end(), t[i]) - lis.begin();
d[l]++, d[r]--;
mer(l, r);
}
vector<pair<int, int>> e;
FOR (i, 0, m - 2) {
if (d[i] != 0) {
mer(i, i + 1);
if (d[i] > 0) ans += 1ll * d[i] * (lis[i + 1] - lis[i]);
}
e.pb(lis[i + 1] - lis[i], i);
d[i + 1] += d[i];
}
sort(e.begin(), e.end());
for (auto [w, i] : e) if (mer(i, i + 1)) ans += w;
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... |