Problem:
Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

template <class T>
using lim = numeric_limits<T>;

typedef long long ll;
typedef long double ld;

pair<ll,int> getCloser(ll a, ll b, int aIndex, int bIndex, ll target){
	if (abs(target-a)>abs(target-b)) return make_pair(b,bIndex);
	else return make_pair(a,aIndex);
}

// binary search the closest
pair<ll,int> findClosest(ll arr[], int n, ll target){
	if (target <= arr[0]) return make_pair(arr[0],0);
	if (target >= arr[n-1]) return make_pair(arr[n-1],n-1);
	
	int i = 0, j = n, mid = 0;
	while (i < j){
		mid = (i+j)/2;
		
		if (target == arr[mid]) return make_pair(arr[mid],mid);
		
		if (target < arr[mid]){
			if (mid > 0 && target > arr[mid-1]) return getCloser(arr[mid-1], arr[mid], mid-1, mid, target);
			
			j = mid;
		}
		else{
			if(mid < n-1 && target < arr[mid+1]) return getCloser(arr[mid+1], arr[mid], mid+1, mid, target);
			
			j = mid;
		}
	}
	
	return make_pair(arr[mid],mid);
}

const int maxNum = 10e5;

ll closestDistS[maxNum];
ll closestDistT[maxNum];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	memset(closestDistS, 0 , sizeof closestDistS);
	memset(closestDistT, 0 , sizeof closestDistT);
	
	int a, b, q; cin >> a >> b >> q;
	
	ll s[a];
	ll t[b];
	
	for (int i = 0; i < a; i++){
		cin >> s[i];
	}

	for (int i = 0; i < b; i++){
		cin >> t[i];
	}
	
	// sorting 
	sort(s, s+a);
	sort(t, t+b);
	
	// pre-compute S
	for (int i = 0; i < a; i++){
		closestDistS[i] = abs(s[i]-findClosest(t, b, s[i]).first);
	}
	
	// pre-compute T
	for (int i = 0; i < b; i++){
		closestDistT[i] = abs(t[i]-findClosest(s, a, t[i]).first);
	}
	
	while(q--){
		ll x; cin >> x;
		
		auto closestS = findClosest(s, a, x);
		auto closestT = findClosest(t, b, x);
		
		ll ans = closestDistS[closestS.second] + abs(x-closestS.first);
		ans = min(ans, closestDistT[closestT.second] + abs(x-closestT.first));
		cout << ans << endl;
	}
}