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 "walk.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 2000005;
const int N = 100005;
const ll INF = 2000000000000000000LL;
vector<int> adj[M];
vector<array<int, 2>> isAt[N];
int cnode = 0;
ll dp[N];
///usr/include/c++/10/bits/stl_heap.h|209|error: ‘bool Comparator::operator()(const std::__debug::array<long long int, 2>&, const std::__debug::array<long long int, 2>&) const’ is private within this context|
struct Comparator
{
    bool operator() (const array<ll, 2>& ar1, const array<ll, 2>& ar2) const
    {
        return tie(ar1[1], ar1[0]) < tie(ar2[1], ar2[0]);
    }
};
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
    vector<vector<int>> startsAt(x.size());
    vector<vector<int>> endsAt(x.size());
    for (int i = 0; i < l.size(); i++)
    {
        startsAt[l[i]].push_back(i);
        endsAt[r[i]].push_back(i);
    }
    multiset<array<int, 2>> sett;
    vector<int> in(y.size());
    for (int i = 0; i < x.size(); i++)
    {
        for (int j : endsAt[i])
        {
            sett.erase(sett.find({y[j], j}));
        }
        for (int j : startsAt[i])
        {
            sett.insert({y[j], j});
        }
        for (int j : endsAt[i])
        {
            auto it = sett.upper_bound({y[j], -1});
            if (it != sett.end())
            {
                adj[(*it)[1]].push_back(j);
                in[j]++;
            }
            if (it != sett.begin())
            {
                it--;
                adj[(*it)[1]].push_back(j);
                in[j]++;
            }
        }
    }
    for (int i = 0; i <N; i++) dp[i] = INF;
    for (int i : endsAt[x.size() - 1])
    {
        dp[i] = y[i];
    }
    vector<int> noIn;
    for (int i = 0; i < in.size(); i++)
    {
        if (in[i] == 0)
        {
            noIn.push_back(i);
        }
    }
    while (!noIn.empty())
    {
        int cur = noIn.back();
        noIn.pop_back();
        for (int next : adj[cur])
        {
            in[next]--;
            dp[next] = min(dp[cur] + abs(y[cur] - y[next]), dp[next]);
            if (in[next] == 0) noIn.push_back(next);
        }
    }
    ll res = INF;
    for (int i : startsAt[0])
    {
        res = min(res, dp[i] + y[i]);
    }
    if (res == INF) return -1;
    return res + x.back() - x.front();
}
Compilation message (stderr)
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for (int i = 0; i < l.size(); i++)
      |                     ~~^~~~~~~~~~
walk.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int i = 0; i < x.size(); i++)
      |                     ~~^~~~~~~~~~
walk.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for (int i = 0; i < in.size(); i++)
      |                     ~~^~~~~~~~~~~| # | 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... |