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;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
const int MAXN = 1e5 + 10;
int N, M;
vi l, r, y, cost;
ll min_distance(vi x, vi h, vi iL, vi iR, vi iY, int s, int g) {
    N = x.size();
    M = l.size();
    priority_queue<pi, vector<pi>, greater<pi>> start;
    priority_queue<pi, vector<pi>, greater<pi>> end;
    l = iL;
    r = iR;
    y = iY;
    cost.resize(M + 2, 1e9);
    for (int i = 0; i < M; i++) {
        start.push({l[i], i});
        end.push({r[i], i});
    }
    l.pb(0);
    r.pb(0);
    cost[M] = 0;
    end.push({0, M});
    bool sameH = true;
    int h0 = h[0];
    for (int i = 1; i < N; i++) {
        if (h[i] != h0) {
            sameH = false;
            break;
        }
    }
    if (s == 0 && g == N - 1 && sameH) {
        set<pi> active;
        for (int i = 0; i < N; i++) {
            while (end.top().f < i) {
                pi tR = end.top();
                end.pop();
                active.erase({y[tR.s], tR.s});
            }
            if (active.size() == 0) {
                return -1;
            }
            while (start.top().f == i) {
                pi tI = start.top();
                start.pop();
                int cBridge = tI.s;
                pi cP = {y[cBridge], cBridge};
                auto above = active.lower_bound(cP);
                int bestC = 1e9;
                if (above != active.end()) {
                    int pBridge = (*above).s;
                    bestC = min(bestC, cost[pBridge] + x[i] - l[pBridge] + abs(y[pBridge] - y[cBridge]));
                }
                if (above != active.begin()) {
                    auto below = --above;
                    int pBridge = (*below).s;
                    bestC = min(bestC, cost[pBridge] + x[i] - l[pBridge] + abs(y[pBridge] - y[cBridge]));
                }
                active.insert(cP);
                cost[tI.s] = bestC;
            }
        }
        int fBridge = (*active.begin()).s;
        int ans = cost[fBridge] + x[N - 1] - l[fBridge] + y[fBridge];
        return ans;
    } else {
        return -1;
    }
}
| # | 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... |