이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll inf = 1e18;
ll min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int s, int g) {
    int N = X.size();
    int M = Y.size();
    if (s == 0 && g == N - 1 && (*min_element(H.begin(), H.end())) == (*max_element(H.begin(), H.end()))) {
        vector<pair<int, int>> p(M);
        for (int i = 0; i < M; i++) {
            p[i] = {L[i], i};
        }
        sort(p.begin(), p.end());
        vector<int> ord(M);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int i, int j) {
            return R[i] < R[j];
        });
        vector<ll> dp(M, inf);
        int j = 0;
        set<pair<int, int>> S;
        for (int i : ord) {
            if (L[i] == 0) {
                dp[i] = Y[i] + X[R[i]] - X[L[i]];
            }
            while (j < M && p[j].first <= R[i]) {
                int x = p[j].second;
                S.emplace(Y[x], x);
                j++;
            }
            S.erase({Y[i], i});
            auto it = S.lower_bound({Y[i], -1});
            if (it != S.end()) {
                auto [y, z] = *it;
                dp[z] = min(dp[z], dp[i] + y - Y[i] + X[R[z]] - X[R[i]]);
            }
            if (it != S.begin()) {
                auto [y, z] = *prev(it);
                dp[z] = min(dp[z], dp[i] + Y[i] - y + X[R[z]] - X[R[i]]);
            }
        }
        ll ans = inf;
        for (int i = 0; i < M; i++) {
            if (R[i] == N - 1) {
                ans = min(ans, dp[i] + Y[i]);
            }
        }
        if (ans == inf) {
            ans = -1;
        }
        return ans;
    }
    map<pair<int, int>, int> mp;
    vector<pair<int, int>> p;
    for (int i = 0; i < N; i++) {
        mp[{X[i], 0}] = i;
        p.emplace_back(X[i], 0);
    }
    vector<vector<pair<int, int>>> adj(N);
    vector<tuple<int, int, int>> T(N + M);
    for (int i = 0; i < N; i++) {
        T[i] = {H[i], 1, i};
    }
    for (int i = 0; i < M; i++) {
        T[N + i] = {Y[i], 0, i};
    }
    sort(T.rbegin(), T.rend());
    set<int> S;
    int q;
    for (auto [y, t, k] : T) {
        if (t == 1) {
            S.insert(k);
        } else {
            q = -1;
            auto it = S.lower_bound(L[k]);
            while (it != S.end() && (*it) <= R[k]) {
                int z = *it, w; it++;
                if (!mp.count({X[z], y})) {
                    mp[{X[z], y}] = w = p.size();
                    p.emplace_back(X[z], y);
                    adj.push_back({});
                } else {
                    w = mp[{X[z], y}];
                }
                if (q != -1) {
                    adj[q].emplace_back(w, X[z] - p[q].first);
                    adj[w].emplace_back(q, X[z] - p[q].first);
                }
                q = w;
            }
        }
    }
    q = -1;
    for (auto [P, k] : mp) {
        auto [x, y] = P;
        if (q != -1 && p[q].first == x) {
            adj[k].emplace_back(q, y - p[q].second);
            adj[q].emplace_back(k, y - p[q].second);
        }
        q = k;
    }
    int K = adj.size();
    vector<ll> dis(K, -1);
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
    pq.emplace(0, s);
    while (!pq.empty()) {
        auto [d, x] = pq.top();
        pq.pop();
        if (dis[x] != -1) {
            continue;
        }
        dis[x] = d;
        for (auto [y, z] : adj[x]) {
            pq.emplace(d + z, y);
        }
    }
    return dis[g];
}
| # | 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... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |