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 <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
// editorial
struct dsu {
    vector<int> p;
    vector<int> sz;
    int n;
    dsu(int _n) : n(_n) {
        p = vector<int>(n);
        iota(p.begin(), p.end(), 0);
        sz = vector<int>(n, 1);
    }
    inline int get(int x) {
        if (p[x] == x) {
            return x;
        } else {
            return p[x] = get(p[x]);
        }
    }
    inline bool unite(int x, int y) {
        x = get(x);
        y = get(y);
        if (x == y) {
            return false;
        }
        p[x] = y;
        sz[y] += sz[x];
        return true;
    }
    inline bool same(int x, int y) {
        return (get(x) == get(y));
    }
    inline int size(int x) {
        return sz[get(x)];
    }
    inline bool root(int x) {
        return (x == get(x));
    }
};
long long plan_roller_coaster(vector<int> s, vector<int> t) {
    s.emplace_back((int) 1e9);
    t.emplace_back(1);
    int n = (int) s.size();
    vector<int> p;
    for (int i = 0; i < n; i++) {
        p.emplace_back(s[i]);
        p.emplace_back(t[i]);
    }
    sort(p.begin(), p.end());
    p.resize(unique(p.begin(), p.end()) - p.begin());
    int m = (int) p.size();
    dsu uf(m);
    vector<int> a(m);
    for (int i = 0; i < m; i++) {
        int x = (int) (lower_bound(p.begin(), p.end(), s[i]) - p.begin());
        int y = (int) (lower_bound(p.begin(), p.end(), t[i]) - p.begin());
        uf.unite(x, y);
        a[x]++;
        a[y]--;
    }
    long long res = 0;
    for (int i = 0; i < m - 1; i++) {
        a[i + 1] += a[i];
        if (a[i]) {
            uf.unite(i, i + 1);
        }
        if (a[i] > 0) {
            res += a[i] * 1LL * (p[i + 1] - p[i]);
        }
    }
    vector<pair<int, int>> edges;
    for (int i = 0; i < m - 1; i++) {
        if (!uf.same(i, i + 1)) {
            edges.emplace_back(p[i + 1] - p[i], i);
        }
    }
    sort(edges.begin(), edges.end());
    for (auto [x, y] : edges) {
        if (uf.unite(y, y + 1)) {
            res += x;
        }
    }
    return res;
}
#ifdef tabr
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    debug(plan_roller_coaster({1, 4, 5, 6}, {7, 3, 8, 6}));
    return 0;
}
#endif
| # | 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... |