Submission #587959

#TimeUsernameProblemLanguageResultExecution timeMemory
587959AriaHRoller Coaster Railroad (IOI16_railroad)C++17
100 / 100
195 ms18620 KiB
#include "railroad.h"

/* Ignore Others Only Focus On Yourself! */
/* Im the Best! */

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;

#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;

const int N = 1e6 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;

ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }

mt19937 rng(time(nullptr));

vector < int > cm;

int ps[N], par[N];

int get(int x)
{
    return (x == par[x]? x : par[x] = get(par[x]));
}

int unify(int v, int u)
{
    v = get(v), u = get(u);
    if(v == u) return 0;
    par[u] = v;
    return 1;
}

int lwr(int x)
{
    return lower_bound(all(cm), x) - cm.begin();
}

ll plan_roller_coaster(vector < int > s, vector < int > t)
{
    iota(par, par + N, 0);
    int n = SZ(s);
    for(int i = 0; i < n; i ++)
    {
        cm.push_back(s[i]);
        cm.push_back(t[i]);
    }
    cm.push_back(-1e9 - 5);
    cm.push_back(1e9 + 5);
    sort(all(cm));
    cm.resize(unique(all(cm)) - cm.begin());
    for(int i = 0; i < n; i ++)
    {
        s[i] = lwr(s[i]);
        t[i] = lwr(t[i]);
        ps[s[i]] --;
        ps[t[i]] ++;
        unify(s[i], t[i]);
    }
    ll tot = 0;
    vector < pll > E;
    for(int i = 0; i < SZ(cm) - 1; i ++)
    {
        ps[i + 1] += ps[i];
        tot += 1ll * max(0, -1 - ps[i]) * (cm[i + 1] - cm[i]);
        if(ps[i] == -1)
        {
            E.push_back(Mp(cm[i + 1] - cm[i], i));
        }
        else
        {
            unify(i, i + 1);
        }
    }
    sort(all(E));
    for(auto [c, i] : E)
    {
        if(unify(i, i + 1))
        {
            tot += c;
        }
    }
    return tot;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...