Language: C++

#include <bits/stdc++.h>
#include <climits>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define srep(i, begin, end) for (__typeof(end) i = begin; i != end; i++)
#define si(x) int x = scanInt();
#define sll(x) LL x = scanLong();
#define sci(x) int x; scanf("%d", &x);
#define scll(x) LL x; scanf("%lld", &x);
#define pi(x) printf("%d ", x)
#define pll(x) printf("%lld ", x)
#define nl printf("\n")
#define clr(a) memset(a, 0, sizeof(a))
#define PB push_back
#define MP make_pair
using namespace std;
typedef unsigned int UI; // 32 bit integer
typedef long int LI; // 32 bit integer
typedef unsigned long int ULI; // 32 bit unsigned integer
typedef long long int LL; // 64 bit integer
typedef unsigned long long int  ULL; // 64 bit unsigned integer
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const LL MOD = 1e9+7;

/* Fast I/O */
inline int scanInt() {
	int n = 0;
	char ch = getchar();
	int sign = 1;
	while(ch < '0' || ch > '9') {
		if(ch == '-')	sign = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		n = (n<<1)+(n<<3)+(int)(ch-'0');
		ch = getchar();
	}
	return n*sign;
}

inline LL scanLong() {
	LL n = 0;
	char ch = getchar();
	LL sign = 1;
	while(ch < '0' || ch > '9') {
		if(ch == '-')	sign = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		n = (n<<1)+(n<<3)+(LL)(ch-'0');
		ch = getchar();
	}
	return n*sign;
}

LL getDistance(LL pos, LL first, LL second) {
	if(first > second)	swap(first, second);
	if(pos <= first)	return	second - pos;
	else if(first <= pos && pos <= second)	return second - first + min(pos - first, second - pos);
	else if(pos > second)	return	pos - first;
}

int main() {
	sll(a); sll(b); sll(q);
	LL shrines[a], temples[b];
	rep(i, 0, a)	shrines[i] = scanLong();
	rep(i, 0, b)	temples[i] = scanLong();

	rep(i, 0, q) {
		sll(pos);
		LL shr = lower_bound(shrines, shrines+a, pos) - shrines;
		LL tem = lower_bound(temples, temples+b, pos) - temples;
		LL ans = LONG_MAX;
		// S - L, T - L
		if(shr > 0 && tem > 0)	ans = min(ans, shrines[shr - 1], temples[tem - 1]);
		// S - R, T - R
		if(shr < a && tem < b)	ans = min(ans, shrines[shr], temples[tem]);
		// S - R, T - L
		if(shr < a && tem > 0)	ans = min(ans, shrines[shr], temples[tem - 1]);
		// S - L, T - R
		if(shr > 0 && tem < b)	ans = min(ans, shrines[shr - 1], temples[tem]);
	}

}


Compiler output:
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h: In instantiation of 'constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare) [with _Tp = long long int; _Compare = long long int]':
a.cc:78:35:   required from here
   78 |                 if(shr > 0 && tem > 0)  ans = min(ans, shrines[shr - 1], temples[tem - 1]);
      |                                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:284:17: error: '__comp' cannot be used as a function
  284 |       if (__comp(__b, __a))
      |           ~~~~~~^~~~~~~~~~
a.cc: In function 'LL getDistance(LL, LL, LL)':
a.cc:64:1: warning: control reaches end of non-void function [-Wreturn-type]
   64 | }
      | ^