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>
#pragma GCC optimize("O2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define debug(x) {cerr<<#x<<"="<<x<<"\n";}
#define debug2(x, y) {cerr<<"{"<<#x<<", "<<#y<<"}={"<<x<<", "<<y<<"}\n";}
#define debugp(p) {cerr<<#p<<"={"<<p.first<<", "<<p.second<<"}\n";}
#define debugv(abcd) {cerr<<#abcd<<": ";for (auto dcba:abcd) cerr<<dcba<<", ";cerr<<"\n";}
#define pb push_back
#define SZ(x) ((int)x.size())
#define all(x) x.begin(), x.end()
const int inf=1000001000; // 1e9
const ll INF=10000000010000000; // 1e16
const int mod=1000000007;
const int MAXN=100010, LOG=9;
int n, m, k, x, y, u, v;
ll dist[51][101];
bool mark[51][101];
vi comp;
priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> pq;
inline void upd(int x, int y, ll d){ if (dist[x][y]>d) pq.push({dist[x][y]=d, {x, y}});}
ll min_distance(vi X, vi H, vi L, vi R, vi Y, int s, int g){
	n=SZ(X);
	m=SZ(L);
	for (int i=0; i<n; i++) comp.pb(H[i]);
	for (int i=0; i<m; i++) comp.pb(Y[i]);
	comp.pb(0);
	sort(all(comp));
	comp.resize(unique(all(comp))-comp.begin());
	memset(dist, 63, sizeof(dist));
	pq.push({dist[s][0]=0, {s, 0}});
	while (pq.size()){
		pii p=pq.top().second;
		pq.pop();
		int x=p.first, h=p.second;
		if (mark[x][h]) continue ;
		// cerr<<X[x]<<" "<<comp[h]<<"  "<<dist[x][h]<<"\n";
		mark[x][h]=1;
		if (H[x]>comp[h]) upd(x, h+1, dist[x][h]+comp[h+1]-comp[h]);
		if (H[x]>=comp[h] && h) upd(x, h-1, dist[x][h]+comp[h]-comp[h-1]);
		for (int i=0; i<m; i++) if (Y[i]==comp[h] && L[i]<x && x<=R[i]){
			upd(x-1, h, dist[x][h]+X[x]-X[x-1]);
			break ;
		}
		for (int i=0; i<m; i++) if (Y[i]==comp[h] && L[i]<=x && x<R[i]){
			upd(x+1, h, dist[x][h]+X[x+1]-X[x]);
			break ;
		}
	}
	if (dist[g][0]>=INF) return -1;
	return dist[g][0];
}
| # | 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... |