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;
const long long INF = 1e15;
long long 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 = l.size();
	vector<vector<int>> starters(n);
	vector<vector<int>> ends(n);
	for(int i=0;i<m;i++) {
		starters[l[i]].emplace_back(y[i]);
		ends[r[i]].emplace_back(y[i]);
	}
	multiset<pair<int,long long>> offers;
	ends[0].emplace_back(0);
	offers.emplace(0,0);
	for(int i=0;i<n-1;i++) {
		for(int&ht:starters[i]) {
			auto iter = offers.lower_bound({ht,0});
			long long bestopt = INF;
			if(iter!=offers.end()) {
				// try
				bestopt=min(bestopt,iter->first-ht + iter->second);
			}
			if(iter!=offers.begin()) {
				--iter;
				// try
				bestopt=min(bestopt,ht-iter->first+iter->second);
			}
			offers.emplace(ht,bestopt);
		}
		for(int&ht:ends[i]) {
			offers.erase(offers.lower_bound({ht,0}));
		}
	}
	if(offers.empty())return -1;
	long long ans = x[g]-x[s]+offers.begin()->second+offers.begin()->first;
	if(ans>=INF)return -1;
	return ans;
}
| # | 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... |