Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define mp make_pair
const ll INF = 1e18;
typedef pair<ll, ll> pll;

struct Node {
	ll valm;
	ll valp;
	ll add;
	
	Node() {}
};

const int N = 3e5 + 10;
Node tree[4 * N];

void update_node(int tv, int tl, int tr) {
	if (tr - tl > 1) {
		tree[tv].valm = min(tree[2 * tv + 1].valm, tree[2 * tv + 2].valm);
		tree[tv].valp = min(tree[2 * tv + 1].valp, tree[2 * tv + 2].valp);
	}
}

void build(int tv, int tl, int tr) {
	tree[tv].add = 0;
	if (tr - tl == 1) {
		tree[tv].valm = INF - tl;
		tree[tv].valp = INF + tl;
		return;
	}
	int tm = (tl + tr) / 2;
	build(2 * tv + 1, tl, tm);
	build(2 * tv + 2, tm, tr);
	update_node(tv, tl, tr);
}

void push(int tv, int tl, int tr) {
	tree[tv].valm += tree[tv].add;
	tree[tv].valp += tree[tv].add;
	if (tr - tl > 1) {
		tree[2 * tv + 1].add += tree[tv].add;
		tree[2 * tv + 2].add += tree[tv].add;
	}
	tree[tv].add = 0;
}

void add_num(int tv, int tl, int tr, int l, int r, ll val) {
	push(tv, tl, tr);
	if (tr == r && tl == l) {
		tree[tv].add += val;
		push(tv, tl, tr);
		return;
	}
	if (l >= tr || r < tl)
		return;
	int tm = (tl + tr) / 2;
	add_num(2 * tv + 1, tl, tm, l, min(r, tm), val);
	add_num(2 * tv + 2, tm, tr, max(l, tm), r, val);
	update_node(tv, tl, tr);	
}

pll get_min(int tv, int tl, int tr, int l, int r) {
	push(tv, tl, tr);
	if (tr == r && tl == l) {
		return pll(tree[tv].valm, tree[tv].valp);
	}
	if (l >= tr || r < tl)
		return {INF, INF};
	int tm = (tl + tr) / 2;
	res1 = get_min(2 * tv + 1, tl, tm, l, min(r, tm));
	res2 = get_min(2 * tv + 2, tm, tr, max(l, tm), r);
	return pll(min(res1.first, res2.first), min(res1.second, res2.second));
}

void update_pos(int tv, int tl, int tr, int pos, int val) {
	push(tv, tl, tr);
	if (pos < tl || pos >= tr)
		return;
	if (tr - tl == 1) {
		tree[tv].valm = val - tl;
		tree[tv].valp = val + tl;
		return;
	}
	int tm = (tl + tr) / 2;
	update_pos(2 * tv + 1, tl, tm, pos, val);
	update_pos(2 * tv + 2, tm, tr, pos, val);
	update_node(tv, tl, tr);
}	

ll get_pos(int tv, int tl, int tr, int pos) {
	push(tv, tl, tr);
	if (tr - tl == 1) 
		return tree[tv].valm + tl;
	int tm = (tl + tr) / 2;
	if (pos < tm)
		return get_pos(2 * tv + 1, tl, tm, pos);
	else
		return get_pos(2 * tv + 2, tm, tr, pos);
}



int main() {
	//	freopen("input.txt", "r", stdin);
	//	freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(nullptr);	
	int n, q, a, b;
	cin >> n >> q >> a >> b;
	a--; b--;
	build(0, 0, n);
	ll last = b;
	update_pos(0, 0, n, a, 0);
	while (q--) {
		ll x;
		cin >> x;
		x--;
		auto p1 = get_min(0, 0, n, 0, x);
		auto p2 = get_min(0, 0, n, x, n);
		add_num(0, 0, n, 0, n, abs(last - x));
		ll cur_val = get_pos(0, 0, n, last);
		update_pos(0, 0, n, last, min(p1.first + x, p2.second - x));
		last = x;
	}
	ll res = INF;
	for (int i = 0; i < n; i++) {
		res = min(res, get_pos(0, 0, n, i));
	}
	cout << res << endl;
	return 0;
}


Compiler output:
a.cc: In function 'pll get_min(int, int, int, int, int)':
a.cc:73:9: error: 'res1' was not declared in this scope
   73 |         res1 = get_min(2 * tv + 1, tl, tm, l, min(r, tm));
      |         ^~~~
a.cc:74:9: error: 'res2' was not declared in this scope
   74 |         res2 = get_min(2 * tv + 2, tm, tr, max(l, tm), r);
      |         ^~~~