This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* https://ioi2019.az/source/Tasks/Day2/Walk/review.pdf */
#include "walk.h"
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef vector<int> vi;
const int N = 100000, M = 100000, N_ = 1 << 17, N1 = N * 13, M1 = N1 * 2;	/* N_ = pow2(ceil(log2(N))) */
const long long INF = 0x3f3f3f3f3f3f3f3f;
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
	return (X *= 3) >> 1;
}
int *aa;
void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
		while (j < k)
			if (aa[ii[j]] == aa[i_])
				j++;
			else if (aa[ii[j]] < aa[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}
int xx[N], yy[N], n;
int hh[M], ii[M], jj[M], zz[M], *ei[M], eo[M], m;
int *eh[N1], eo_[N1], n1;
int ij[M1], ww[M1], m1;
void append(int **eh, int *eo, int i, int h) {
	int o = eo[i]++;
	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}
int st[N_ * 2], n_;
void update(int l, int r, int h) {
	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			st[l++] = h;
		if ((r & 1) == 0)
			st[r--] = h;
	}
}
int query(int i) {
	int h = -1;
	for (i += n_; i > 0; i >>= 1)
		h = max(h, st[i]);
	return h == -1 ? -1 : hh[h];
}
void add(int h, int i) {
	int h_;
	append(ei, eo, h, i);
	if ((h_ = query(i)) != -1)
		append(ei, eo, h_, i);
}
int top[N], yy1[N];
int newnode() {
	eh[n1] = (int *) malloc(2 * sizeof *eh[n1]);
	return n1++;
}
void add_(int i, int j, int w) {
	int h = m1++;
	ij[h] = i ^ j, ww[h] = w;
	append(eh, eo_, i, h), append(eh, eo_, j, h);
}
void push(int i, int y) {
	int t = top[i], t_ = newnode();
	add_(t, t_, y - yy1[i]);
	top[i] = t_, yy1[i] = y;
}
long long dd[N1]; int pq[N1], iq[1 + N1], cnt;
int lt(int i, int j) { return dd[i] < dd[j]; }
int p2(int p) {
	return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
	int p, q, j;
	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}
void pq_dn(int i) {
	int p, q, j;
	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
	iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
	int i = iq[1], j = iq[cnt--];
	if (j != i)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}
long long dijkstra(int s, int t) {
	memset(dd, 0x3f, n1 * sizeof *dd);
	dd[s] = 0, pq_add_last(s);
	while (cnt) {
		int i = pq_remove_first(), o;
		if (i == t)
			return dd[i];
		for (o = eo_[i]; o--; ) {
			int h = eh[i][o], j = i ^ ij[h];
			long long d = dd[i] + ww[h];
			if (dd[j] > d) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = d, pq_up(j);
			}
		}
	}
	return -1;
}
long long min_distance(vi xx_, vi yy_, vi ii_, vi jj_, vi zz_, int s, int t) {
	int h, i, j, p1, q1, p2, q2, tmp;
	n = xx_.size(), m = zz_.size();
	for (i = 0; i < n; i++)
		xx[i] = xx_[i], yy[i] = yy_[i];
	for (h = 0; h < m; h++) {
		ii[h] = ii_[h], jj[h] = jj_[h], zz[h] = zz_[h];
		hh[h] = h;
	}
	aa = zz, sort(hh, 0, m);
	if (s > t)
		tmp = s, s = t, t = tmp;
	for (h = 0; h < m; h++)
		ei[h] = (int *) malloc(2 * sizeof *ei[h]);
	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	memset(st, -1, n_ * 2 * sizeof *st);
	for (h = 0, p1 = s, q1 = s, p2 = t, q2 = t; h < m; h++) {
		int h_ = hh[h];
		while (p1 >= 0 && yy[p1] < zz[h_])
			p1--;
		while (q1 < n && yy[q1] < zz[h_])
			q1++;
		while (p2 >= 0 && yy[p2] < zz[h_])
			p2--;
		while (q2 < n && yy[q2] < zz[h_])
			q2++;
		add(h_, ii[h_]), add(h_, jj[h_]);
		if (ii[h_] < s && s < jj[h_])
			add(h_, p1), add(h_, q1);
		if (ii[h_] < t && t < jj[h_])
			add(h_, p2), add(h_, q2);
		update(ii[h_], jj[h_], h);
	}
	for (i = 0; i < n; i++)
		top[i] = newnode(), yy1[i] = 0;
	for (h = 0, p1 = s, q1 = s, p2 = t, q2 = t; h < m; h++) {
		int h_ = hh[h], o, o_;
		aa = xx, sort(ei[h_], 0, eo[h_]);
		for (o = 0, o_ = 0; o < eo[h_]; o++)
			if (o_ == 0 || ei[h_][o_ - 1] != ei[h_][o])
				ei[h_][o_++] = ei[h_][o];
		eo[h_] = o_;
		for (o = 0; o < eo[h_]; o++) {
			i = ei[h_][o];
			push(i, zz[h_]);
		}
		for (o = 0; o + 1 < eo[h_]; o++) {
			i = ei[h_][o], j = ei[h_][o + 1];
			add_(top[i], top[j], xx[j] - xx[i]);
		}
	}
	return dijkstra(s, t);
}
Compilation message (stderr)
walk.cpp: In function 'void append(int**, int*, int, int)':
walk.cpp:50:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   50 |  if (o >= 2 && (o & o - 1) == 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... |