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"
#include "walk.h"
using namespace std;
const int maxNode = 2e6 + 10;
const int maxn = 1e5 + 10;
const long long inf = 1e16;
int node = 0;
vector <pair <int, int>> g[maxNode];
vector <int> start[maxn], fin[maxn];
vector <pair <int, int>> bridge[maxn];
vector <pair <int, int>> line[maxn];
struct vertex {
  int node;
  long long dist;
  vertex() {}
  vertex(int node, long long dist) : node(node), dist(dist) {}
  bool operator < (vertex v) const {
    return dist > v.dist;
  }
};
void addEdge(int i, int j, int cost) {
//   cout << i << " " << j << " " << cost << endl;
  g[i].emplace_back(j, cost);
  g[j].emplace_back(i, cost);
}
long long shortest_path(int src, int des) {
  priority_queue <vertex> Q;
  vector <long long> dist (node, inf);
  Q.emplace(src, 0);
  dist[src] = 0;
  while(not Q.empty()) {
    auto x = Q.top();
    Q.pop();
    if(x.dist != dist[x.node]) continue;
    for(auto i : g[x.node]) {
      if(i.second + x.dist < dist[i.first]) {
        dist[i.first] = i.second + x.dist;
        Q.emplace(i.first, dist[i.first]);
      }
    }
  }
  return dist[des];
}
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 from, int to) {
  for(int i = 0; i < l.size(); i++) {
    start[l[i]].push_back(i);
    fin[r[i]].push_back(i);
  }
  set <pair <int, int>> cont;
  int src = 0, des = 0;
  for(int i = 0; i < x.size(); i++) {
    for(int j : start[i]) {
      cont.insert(make_pair(y[j], j)); 
    }
    vector <pair <int, int>> can;
    if(from == i) {
      can.push_back(make_pair(0, -1));
    }
    if(to == i) {
      can.push_back(make_pair(0, -1));
    }
    for(int j : start[i]) {
      auto it = cont.find(make_pair(y[j], j));
      can.emplace_back(y[j], j);
      if(it != cont.begin()) {
        can.push_back(*prev(it));
      }
      if(next(it) != cont.end() && next(it) -> first <= h[i]) {
        can.push_back(*next(it));
      }
    }
    for(int j : fin[i]) {
      auto it = cont.find(make_pair(y[j], j));
      can.emplace_back(y[j], j);
      if(it != cont.begin()) {
        can.push_back(*prev(it));
      }
      if(next(it) != cont.end() && next(it) -> first <= h[i]) {
        can.push_back(*next(it));
      }
    }
    line[i] = can;
    for(int j : fin[i]) {
      cont.erase(make_pair(y[j], j));
    }
  }
  auto rightSide = [&] (int piv) {
    set <pair <int, int>> alive;
    for(int i = 0; i < x.size(); i++) {
      for(int j : start[i]) {
        alive.insert(make_pair(y[j], j));
      }
      if(i >= piv) {
        while(not alive.empty() && alive.begin() -> first <= h[i]) {
          line[i].emplace_back(*alive.begin());
          alive.erase(alive.begin());
        }
      }
      for(int j : fin[i]) {
        alive.erase(make_pair(y[j], j));
      }
    }
  };
  auto leftSide = [&] (int piv) {
    set <pair <int, int>> alive;
    for(int i = x.size() - 1; i >= 0; i--) {
      for(int j : fin[i]) {
        alive.insert(make_pair(y[j], j));
      }
      if(i <= piv) {
        while(not alive.empty() && alive.begin() -> first <= h[i]) {
          line[i].emplace_back(*alive.begin());
          alive.erase(alive.begin());
        }
      }
      for(int j : start[i]) {
        alive.erase(make_pair(y[j], j));
      }
    } 
  };
  leftSide(from);
  leftSide(to);
  rightSide(from);
  rightSide(to);
  for(int i = 0; i < x.size(); i++) {
    for(int j = 0; j < line[i].size(); j++) {
      if(line[i][j].second == -1) {
        if(i == from) src = node;
        else des = node;
      } else {
        bridge[line[i][j].second].emplace_back(x[i], node);
      }
      line[i][j].second = node++; 
    }
  }
  for(int i = 0; i < x.size(); i++) {
    sort(line[i].begin(), line[i].end());
    for(int j = 1; j < line[i].size(); j++) {
      addEdge(line[i][j - 1].second, line[i][j].second,
          line[i][j].first - line[i][j - 1].first);
    }
  }
  for(int i = 0; i < l.size(); i++) {
    sort(bridge[i].begin(), bridge[i].end());
    for(int j = 1; j < bridge[i].size(); j++) {
      addEdge(bridge[i][j - 1].second, bridge[i][j].second, 
          bridge[i][j].first - bridge[i][j - 1].first);
    }
  }
  auto ans = shortest_path(src, des);
  return ans >= inf ? -1 : ans;
}
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:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   for(int i = 0; i < l.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int i = 0; i < x.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp: In lambda function:
walk.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for(int i = 0; i < x.size(); i++) {
      |                    ~~^~~~~~~~~~
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:128:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |   for(int i = 0; i < x.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:129:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for(int j = 0; j < line[i].size(); j++) {
      |                    ~~^~~~~~~~~~~~~~~~
walk.cpp:139:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |   for(int i = 0; i < x.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:141:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |     for(int j = 1; j < line[i].size(); j++) {
      |                    ~~^~~~~~~~~~~~~~~~
walk.cpp:146:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |   for(int i = 0; i < l.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:148:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |     for(int j = 1; j < bridge[i].size(); j++) {
      |                    ~~^~~~~~~~~~~~~~~~~~| # | 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... |