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 (Python):
from bisect import *
a,b,q=map(int,input().split())
s,t=[],[]
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
for i in range(q):
    x=int(input())
    bs=bisect_left(s,x)
    if bs!=0:
        sl=x-s[bs-1]
    else:
        sl=float("inf")
    if bs!=a:
        sr=s[bs]-x
    else:
        sr=float("inf")
    bt=bisect_left(t,x)
    if bt!=0:
        tl=x-t[bt-1]
    else:
        tl=float("inf")
    if bt!=b:
        tr=t[bt]-x
    else:
        tr=float("inf")
    print(min(max(sl,tl),max(sr,tr),2*sl+tr,2*tl+sr,sl+tr*2,tl+sr*2))

Solution (Python):


INF = 10**11
A,B,Q = map(int,input().split())
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
def binary_search(lis,x):
    ok = 0
    ng = len(lis)-1
    while ng-ok > 1:
        mid = (ok+ng)//2
        if lis[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

for _ in range(Q):
    x = int(input())
    ps = binary_search(s,x)
    pt = binary_search(t,x)
    ans = INF
    
    for i in range(ps,ps+2):
        for j in range(pt,pt+2):
            ans = min(ans,abs(s[i]-x)+abs(s[i]-t[j]), abs(t[j]-x)+abs(t[j]-s[i]))
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
INF = 10**11
A,B,Q = map(int,input().split())
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
def binary_search(lis,x):
    ok = 0
    ng = len(lis)-1
    while ng-ok > 1:
        mid = (ok+ng)//2
        if lis[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

for _ in range(Q):
    x = int(input())
    ps = binary_search(s,x)
    pt = binary_search(t,x)
    ans = INF
    
    for i in range(ps,ps+2):
        for j in range(pt,pt+2):
            ans = min(ans,abs(s[i]-x)+abs(s[i]-t[j]), abs(t[j]-x)+abs(t[j]-s[i]))
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
INF = 10**11
A,B,Q = map(int,input().split())
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
def binary_search(lis,x):
    ok = 0
    ng = len(lis)-1
    while ng-ok > 1:
        mid = (ok+ng)//2
        if lis[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

for _ in range(Q):
    x = int(input())
    ps = binary_search(s,x)
    pt = binary_search(t,x)
    ans = INF
    
    for i in range(ps,ps+2):
        for j in range(pt,pt+2):
            ds = x - s[i]
            dt = x - t[j]
            
            if (ds < 0 and dt < 0) or (ds > 0 and dt > 0):
                ans = min(ans,max(abs(ds),abs(dt)))
            else:
                ans = min(ans,abs(ds)+abs(dt)+min(abs(ds),abs(dt)))
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
INF = 10**11
A,B,Q = map(int,input().split())
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
s.sort()
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
t.sort()
def binary_search(lis,x):
    ok = 0
    ng = len(lis)-1
    while ng-ok > 1:
        mid = (ok+ng)//2
        if lis[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

for _ in range(Q):
    x = int(input())
    ps = binary_search(s,x)
    pt = binary_search(t,x)
    ans = INF
    
    for i in range(ps,ps+2):
        for j in range(pt,pt+2):
            ds = x - s[i]
            dt = x - t[j]
            
            if (ds < 0 and dt < 0) or (ds > 0 and dt > 0):
                ans = min(ans,max(abs(ds),abs(dt)))
            else:
                ans = min(ans,abs(ds)+abs(dt)+min(abs(ds),abs(dt)))
    print(ans)

Solution (Python):
'''
研究室PCでの解答
'''
import math

import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

'''
pythonにsortedsetをくれlower_boundとupper_boundをくれ
'''

def main():
    a,b,q = map(int,ipt().split())
    s = [-10**11]+[int(ipt()) for i in range(a)]+[10**11]
    t = [-10**11]+[int(ipt()) for i in range(b)]+[10**11]

    for i in range(q):
        x = int(ipt())
        ns = bisect.bisect_left(s,x)
        nt = bisect.bisect_left(t,x)
        ls = s[ns-1]-x
        rs = s[ns]-x
        lt = t[nt-1]-x
        rt = t[nt]-x
        ans = []
        ans.append(2*rs-lt)
        ans.append(2*rt-ls)
        ans.append(max(rs,rt))
        ans.append(-min(ls,lt))
        ans.append(rs-2*lt)
        ans.append(rt-2*ls)
        print(min(ans))

if __name__ == '__main__':
    main()

Solution (Python):

from bisect import bisect_right

A, B, Q = map(int, input().split())
INF = 10 ** 18
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
X = [int(input()) for _ in range(Q)]

for x in X:
    b = bisect_right(S, x)
    d = bisect_right(T, x)
    res = INF
    for s in [S[b - 1], S[b]]:
        for t in [T[d - 1], T[d]]:
            dist_0 = abs(x - s) + abs(s - t)
            dist_1 = abs(x - t) + abs(t - s)
            res = min(res, dist_0, dist_1)

    print(res)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


a,b,q = list(map(int, input().split()))
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
from bisect import bisect_left as bl
ls = [None]*a
lt = [None]*b
for i in range(a):
    ind = bl(t, s[i])
    v = float("inf")
    if ind<b:
        v = min(v, abs(t[ind]-s[i]))
    if ind>0:
        v = min(v, abs(t[ind-1]-s[i]))
    ls[i] = v
for i in range(b):
    ind = bl(s, t[i])
    v = float("inf")
    if ind<a:
        v = min(v, abs(s[ind]-t[i]))
    if ind>0:
        v = min(v, abs(s[ind-1]-t[i]))
    lt[i] = v
ans = [None]*q
for i in range(q):
    x = int(input())
    v = float("inf")
    ind = bl(s, x)
    if ind<a:
        v = min(v, abs(s[ind]-x)+ls[ind])
    if ind>0:
        v = min(v, abs(s[ind-1]-x)+ls[ind-1])
    ind = bl(t, x)
    if ind<b:
        v = min(v, abs(t[ind]-x)+lt[ind])
    if ind>0:
        v = min(v, abs(t[ind-1]-x)+lt[ind-1])
    ans[i] = v
write("\n".join(map(str, ans)))

Solution (Python):
from bisect import bisect
a,b,q=map(int,input().split())

s=[-float('inf')]+[int(input()) for i in range(a)]+[float('inf')]
t=[-float('inf')]+[int(input()) for i in range(b)]+[float('inf')]


for i in range(q):
  x=int(input())
  l=bisect(s,x)
  r=bisect(t,x)
  
  ans=float('inf')
  
  for L in [s[l],s[l-1]]:
    for R in [t[r],t[r-1]]:
      ans=min(ans,abs(x-L)+abs(L-R),abs(x-R)+abs(R-L)) 
      
  print(ans)

Solution (Python):
import bisect
A,B,Q=map(int,input().split())
INF=10**18
S=[-INF]+[int(input()) for i in range(A)]+[INF]
T=[-INF]+[int(input()) for i in range(B)]+[INF]
for q in range(Q):
    x=int(input())
    i=bisect.bisect_right(S,x)
    j=bisect.bisect_right(T,x)
    d=INF
    for s in [S[i-1],S[i]]:
        for t in [T[j-1],T[j]]:
            d1=abs(s-x)+abs(s-t)
            d2=abs(t-x)+abs(s-t)
            d=min(d,d1,d2)
    print(d)

Solution (Python):
import bisect
A,B,Q=map(int,input().split())
INF=10**18
S=[-INF]+[int(input()) for i in range(A)]+[INF]
T=[-INF]+[int(input()) for i in range(B)]+[INF]
for q in range(Q):
    x=int(input())
    i=bisect.bisect_right(S,x)
    j=bisect.bisect_right(T,x)
    d=INF
    for s in [S[i-1],S[i]]:
        for t in [T[j-1],T[j]]:
            d1=abs(s-x)+abs(s-t)
            d2=abs(t-x)+abs(s-t)
            d=min(d,d1,d2)
    print(d)

Solution (Python):
from bisect import bisect_left
import sys
input = sys.stdin.readline

a, b, q = map(int, input().split())
INF = 10**12
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]


for _ in range(q):
    x = int(input())
    k = bisect_left(s, x)
    l = bisect_left(t, x)
    sm = x - s[k - 1]
    sM = s[k] - x
    tm = x - t[l - 1]
    tM = t[l] - x
    ans = min(2 * sm + tM, 2 * sM + tm, 2 * tm + sM, 2 * tM + sm, max(sm, tm), max(sM, tM))
    print(ans)

Solution (Python):
from bisect import bisect_left

a, b, q = map(int, input().split())
INF = 10**12
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]


for _ in range(q):
    x = int(input())
    k = bisect_left(s, x)
    l = bisect_left(t, x)
    sm = x - s[k - 1]
    sM = s[k] - x
    tm = x - t[l - 1]
    tM = t[l] - x
    ans = min(2 * sm + tM, 2 * sM + tm, 2 * tm + sM, 2 * tM + sm, max(sm, tm), max(sM, tM))
    print(ans)

Solution (Python):
def near(x, X):
    i = bisect_left(X, x)
    if i == 0: return [X[i]-x]
    elif i == len(X): return [X[i-1]-x]
    else: return [X[i]-x, X[i-1]-x]
import sys
input = sys.stdin.readline
from bisect import *
from itertools import product
A, B, Q = map(int, input().split())
S, T = [], []
for _ in range(A):
    S.append(int(input()))
for _ in range(B):
    T.append(int(input()))
for _ in range(Q):
    res = 1<<40
    x = int(input())
    for a, b in product(near(x,S), near(x,T)):
            res = min(abs(a)+abs(a-b), abs(b)+abs(b-a), res)
    print(res)

Solution (Python):
def near(x, X):
    i = bisect_left(X, x)
    if i == 0: return [X[i]-x]
    elif i == len(X): return [X[i-1]-x]
    else: return [X[i]-x, X[i-1]-x]
from bisect import *
from itertools import product
A, B, Q = map(int, input().split())
S, T = [], []
for _ in range(A):
    S.append(int(input()))
for _ in range(B):
    T.append(int(input()))
for _ in range(Q):
    res = 1<<40
    x = int(input())
    for a, b in product(near(x,S), near(x,T)):
            res = min(abs(a)+abs(a-b), abs(b)+abs(b-a), res)
    print(res)

Solution (Python):
def near(x, X):
    i = bisect_left(X, x)
    if i == 0: return [X[i]-x]
    elif i == len(X): return [X[i-1]-x]
    else: return [X[i]-x, X[i-1]-x]
from bisect import *
A, B, Q = map(int, input().split())
S, T = [], []
for _ in range(A):
    S.append(int(input()))
for _ in range(B):
    T.append(int(input()))
for _ in range(Q):
    res = 1<<40
    x = int(input())
    for a in near(x,S):
        for b in near(x,T):
            res = min(abs(a)+abs(a-b), abs(b)+abs(b-a), res)
    print(res)

Solution (Python):
def near(x, Y):
    i = bisect_left(Y,x)
    if i == 0:
        return Y[i]-x
    elif i == len(Y):
        return x-Y[i-1]
    else:
        return min(Y[i]-x, x-Y[i-1])

def near2(x, Y):
    i = bisect_left(Y,x)
    if i == 0:
        return Y[i]-x + D[i][1]
    elif i == len(Y):
        return x-Y[i-1] + D[i-1][1]
    else:
        return min(Y[i]-x+D[i][1], x-Y[i-1]+D[i-1][1])

from bisect import *
A, B, Q = map(int, input().split())
S = []
T = []
for _ in range(A):
    S.append(int(input()))
for _ in range(B):
    T.append(int(input()))
D = []
for a in S:
    D.append((a, near(a, T)))
for b in T:
    D.append((b, near(b, S)))
D.sort(key=lambda x:x[0])
D2 = [d for d, _ in D]
for _ in range(Q):
    x = int(input())
    print(near2(x,D2))

Solution (Python):
import sys
from bisect import bisect_left

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A, B, Q = map(int, readline().split())
    S = [-INF]
    S.extend(int(readline()) for _ in range(A))
    S.append(INF)
    T = [-INF]
    T.extend(int(readline()) for _ in range(B))
    T.append(INF)
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    for i, x in enumerate(X):
        i1 = bisect_left(S, x)
        i2 = bisect_left(T, x)

        ans[i] = min(
            abs(x - S[i1 - 1]) + abs(S[i1 - 1] - T[i2 - 1]),
            abs(x - T[i2 - 1]) + abs(T[i2 - 1] - S[i1 - 1]),
            abs(x - S[i1 - 1]) + abs(S[i1 - 1] - T[i2]),
            abs(x - T[i2 - 1]) + abs(T[i2 - 1] - S[i1]),
            abs(x - S[i1]) + abs(S[i1] - T[i2]),
            abs(x - T[i2]) + abs(T[i2] - S[i1]),
            abs(x - S[i1]) + abs(S[i1] - T[i2 - 1]),
            abs(x - T[i2]) + abs(T[i2] - S[i1 - 1]),
        )

    print('\n'.join(map(str, ans)))

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect_left

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A, B, Q = map(int, readline().split())
    S = [-INF]
    S.extend(int(readline()) for _ in range(A))
    S.append(INF)
    T = [-INF]
    T.extend(int(readline()) for _ in range(B))
    T.append(INF)
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        for s in (S[idx_s - 1], S[idx_s]):
            for t in (T[idx_t - 1], T[idx_t]):
                ans[i] = min(ans[i], abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))

    print('\n'.join(map(str, ans)))

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect_left

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A, B, Q = map(int, readline().split())
    S = [-INF] + [int(readline()) for _ in range(A)] + [INF]
    T = [-INF] + [int(readline()) for _ in range(B)] + [INF]
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        for s in (S[idx_s - 1], S[idx_s]):
            for t in (T[idx_t - 1], T[idx_t]):
                ans[i] = min(ans[i], abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))

    print('\n'.join(map(str, ans)))

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect_left

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A, B, Q = map(int, readline().split())
    S = [int(readline()) for _ in range(A)]
    T = [int(readline()) for _ in range(B)]
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    vs = [0] * 2
    vt = [0] * 2
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        vs[0] = S[idx_s - 1] if idx_s > 0 else -INF
        vt[0] = T[idx_t - 1] if idx_t > 0 else -INF
        vs[1] = S[idx_s] if idx_s < A else INF
        vt[1] = T[idx_t] if idx_t < B else INF

        for s in vs:
            for t in vt:
                ans[i] = min(ans[i], abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))

    print('\n'.join(map(str, ans)))

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect_left

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A, B, Q = map(int, readline().split())
    S = [int(readline()) for _ in range(A)]
    T = [int(readline()) for _ in range(B)]
    X = [int(readline()) for _ in range(Q)]

    ans = [0] * Q
    vs = [0] * 2
    vt = [0] * 2
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        vs[0] = S[idx_s - 1] if idx_s > 0 else -INF
        vt[0] = T[idx_t - 1] if idx_t > 0 else -INF
        vs[1] = S[idx_s] if idx_s < A else INF
        vt[1] = T[idx_t] if idx_t < B else INF

        ans[i] = min(
            abs(x - vs[0]) + abs(vs[0] - vt[0]),
            abs(x - vt[0]) + abs(vt[0] - vs[0]),
            abs(x - vs[0]) + abs(vs[0] - vt[1]),
            abs(x - vt[0]) + abs(vt[0] - vs[1]),
            abs(x - vs[1]) + abs(vs[1] - vt[1]),
            abs(x - vt[1]) + abs(vt[1] - vs[1]),
            abs(x - vs[1]) + abs(vs[1] - vt[0]),
            abs(x - vt[1]) + abs(vt[1] - vs[0]),
        )

    print('\n'.join(map(str, ans)))

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect_left

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A, B, Q = map(int, readline().split())
    S = [int(readline()) for _ in range(A)]
    T = [int(readline()) for _ in range(B)]
    X = [int(readline()) for _ in range(Q)]

    ans = [0] * Q
    vs = [0] * 2
    vt = [0] * 2
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        vs[0] = S[idx_s - 1] if idx_s > 0 else -INF
        vt[0] = T[idx_t - 1] if idx_t > 0 else -INF
        vs[1] = S[idx_s] if idx_s < A else INF
        vt[1] = T[idx_t] if idx_t < B else INF

        ans[i] = min(
            abs(x - vs[0]) + abs(vs[0] - vt[0]),
            abs(x - vt[0]) + abs(vt[0] - vs[0]),
            abs(x - vs[0]) + abs(vs[0] - vt[1]),
            abs(x - vt[0]) + abs(vt[0] - vs[1]),
            abs(x - vs[1]) + abs(vs[1] - vt[1]),
            abs(x - vt[1]) + abs(vt[1] - vs[1]),
            abs(x - vs[1]) + abs(vs[1] - vt[0]),
            abs(x - vt[1]) + abs(vt[1] - vs[0]),
        )

    print('\n'.join(map(str, ans)))

    return


if __name__ == '__main__':
    main()

Solution (Python):
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
  x = int(input())
  b, d = bisect.bisect_left(s, x), bisect.bisect_left(t, x)
  
  res = INF
  for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
      d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
      res = min(res, d1, d2)
  print(res)

Solution (Python):
import sys
from bisect import bisect_left

A, B, Q = map(int, sys.stdin.readline().split())
S = []
for _ in range(A):
    S.append(int(sys.stdin.readline()))
T = []
for _ in range(B):
    T.append(int(sys.stdin.readline()))



for _ in range(Q):
    x = int(sys.stdin.readline())

    
    
    s_pos = bisect_left(S, x)
    if s_pos == 0:
        s_left = -float("inf")
    else:
        s_left = S[s_pos-1]
    if s_pos == A:
        s_right = float("inf")
    else:
        s_right = S[s_pos]
    

    t_pos = bisect_left(T, x)
    if t_pos == 0:
        t_left = -float("inf")
    else:
        t_left = T[t_pos-1]
    if t_pos == B:
        t_right = float("inf")
    else:
        t_right = T[t_pos]
    

    left_min = min(x - s_left, x - t_left)
    left_max = max(x - s_left, x - t_left)
    right_min = min(s_right - x, t_right - x)
    right_max = max(s_right - x, t_right - x)
    print(min(left_max, right_max,
                2 * (x - s_left) + (t_right - x), 2 * (x - t_left) + (s_right - x),
                2 * (t_right - x) + (x - s_left), 2 * (s_right - x) + (x - t_left)))

Solution (Python):
import bisect
a, b, q = map(int, input().split())

s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]

s.append(-100000000000)
s.append(100000000000)
t.append(-100000000000)
t.append(100000000000)
s.sort()
t.sort()

for _ in range(q):
    x = int(input())

    idxs = bisect.bisect_right(s, x)
    idxt = bisect.bisect_right(t, x)
    left_s = s[idxs - 1]
    right_s = s[idxs]
    left_t = t[idxt - 1]
    right_t = t[idxt]

    kouho1 = max(x - left_s, x - left_t)
    kouho2 = max(right_s - x, right_t - x)
    kouho3 = min(x - left_s, right_t - x) * 2 + max(x - left_s, right_t - x)
    kouho4 = min(x - left_t, right_s - x) * 2 + max(x - left_t, right_s - x)
    print(min(kouho1, kouho2, kouho3, kouho4))

Solution (Python):
from bisect import bisect_right
A,B,Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
S.insert(0,-10**10)
S.append(2*10**10)
T = [int(input()) for _ in range(B)]
T.insert(0,-10**10)
T.append(2*20**10)
for _ in range(Q):
    x = int(input())
    inds = bisect_right(S,x)
    indt = bisect_right(T,x)
    dmin = 10**11
    d = x-S[inds-1]+abs(S[inds-1]-T[indt-1])
    dmin = min(dmin,d)
    d = x-S[inds-1]+abs(S[inds-1]-T[indt])
    dmin = min(dmin,d)
    d = S[inds]-x+abs(S[inds]-T[indt-1])
    dmin = min(dmin,d)
    d = S[inds]-x+abs(S[inds]-T[indt])
    dmin = min(dmin,d)
    d = x-T[indt-1]+abs(T[indt-1]-S[inds-1])
    dmin = min(dmin,d)
    d = x-T[indt-1]+abs(T[indt-1]-S[inds])
    dmin = min(dmin,d)
    d = T[indt]-x+abs(T[indt]-S[inds-1])
    dmin = min(dmin,d)
    d = T[indt]-x+abs(T[indt]-S[inds])
    dmin = min(dmin,d)
    print(dmin)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

A, B, Q = mapint()
Ss = [-10**18]+[int(input()) for _ in range(A)]+[10**18]
Ts = [-10**18]+[int(input()) for _ in range(B)]+[10**18]
from bisect import bisect_left

for q in range(Q):
    x = int(input())
    ls = Ss[bisect_left(Ss, x)-1]
    lt = Ts[bisect_left(Ts, x)-1]
    rs = Ss[bisect_left(Ss, x)]
    rt = Ts[bisect_left(Ts, x)]
    print(min((rs-lt)+min(rs-x, x-lt), (rt-ls)+min(rt-x, x-ls), max(x-ls, x-lt), max(rs-x, rt-x)))

Solution (Python):
import sys
from bisect import *
input = sys.stdin.buffer.readline

INF = 10 ** 13

A, B, Q = map(int, input().split())
shrines = [-INF] + [int(input()) for _ in range(A)] + [INF]
temples = [-INF] + [int(input()) for _ in range(B)] + [INF]
queries = [int(input()) for _ in range(Q)]

for x in queries:
    s_index = bisect(shrines, x)
    t_index = bisect(temples, x)
    rs = shrines[s_index]
    rt = temples[t_index]
    ls = shrines[s_index - 1]
    lt = temples[t_index - 1]
    ans = INF
    ans = min(ans, max(rs, rt) - x)
    ans = min(ans, x - min(ls, lt))
    ans = min(ans, (rt - ls)*2 - max(rt-x, x-ls))    
    ans = min(ans, (rs - lt)*2 - max(rs-x, x-lt))
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
A,B,Q = map(int,input().split())
S = []
for i in range(A):
  temp = int(input())
  S.append(temp)

T = []
for i in range(B):
  temp = int(input())
  T.append(temp)



def jinja(x): 
  if x <= S[0]:
    ret = [S[0]-x,0]
  elif x >= S[-1]:
    ret = [x-S[-1],A-1]
  else:
    ok = 0
    ng = A
    while abs(ok-ng) > 1:
      mid = (ok+ng)//2
      if S[mid] <= x:
        ok = mid
      else:
        ng = mid
    ret = [x - S[ok],ok,S[ng] - x,ng]
  return ret

def tera(x): 
  if x <= T[0]:
    ret = [T[0]-x,0]
  elif x >= T[-1]:
    ret = [x-T[-1],B-1]
  else:
    ok = 0
    ng = B
    while abs(ok-ng) > 1:
      mid = (ok+ng)//2
      if T[mid] <= x:
        ok = mid
      else:
        ng = mid
    ret = [x - T[ok],ok,T[ng] - x,ng]
  return ret
      



for i in range(Q):
  INPUT = int(input())
  j = jinja(INPUT)
  
  if len(j) == 2:
    x = j[0]; loc = S[j[1]]
    jt = tera(loc)
    if len(jt) == 2:
      x += jt[0]
    else:
      x += min(jt[0],jt[2])
  else:
    x1 = j[0]; loc1 = S[j[1]]; x2 = j[2]; loc2 = S[j[3]]
    jt1 = tera(loc1); jt2 = tera(loc2)
    if len(jt1) == 2:
      x1 += jt1[0]
    else:
      x1 += min(jt1[0],jt1[2])
    if len(jt2) == 2:
      x2 += jt2[0]
    else:
      x2 += min(jt2[0],jt2[2])
    x = min(x1, x2)
  t = tera(INPUT)
  
  if len(t) == 2:
    y = t[0]; loc = T[t[1]]
    tj = jinja(loc)
    
    if len(tj) == 2:
      y += tj[0]
    else:
      y += min(tj[0],tj[2])
  else:
    y1 = t[0]; loc1 = T[t[1]]; y2 = t[2]; loc2 = T[t[3]]
    tj1 = jinja(loc1); tj2 = jinja(loc2)
    if len(tj1) == 2:
      y1 += tj1[0]
    else:
      y1 += min(tj1[0],tj1[2])
    if len(tj2) == 2:
      y2 += tj2[0]
    else:
      y2 += min(tj2[0],tj2[2])
    y = min(y1, y2)
  
  print(min(x,y))

Solution (Python):
import bisect

A,B,Q = map(int,input().split())

shira = [-100000000000] + [int(input()) for _ in range(A)] + [10000000000000]
temp = [-100000000000] + [int(input()) for _ in range(B)] + [10000000000000]

shira.sort()
temp.sort()

for _ in range(Q):
    x = int(input())
    shira_s = bisect.bisect_left(shira,x)
    temp_s = bisect.bisect_left(temp,x)
    ans = 100000000000
    for i in [shira_s-1,shira_s]:
        for j in [temp_s-1,temp_s]:
            ans = min(ans, abs(shira[i]-x)+abs(shira[i]-temp[j]))
            ans = min(ans, abs(temp[j]-x)+abs(temp[j]-shira[i]))
    print(ans)

Solution (Python):
from bisect import bisect_left

A, B, Q = map(int, input().split())
INF = 10**12
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]

for _ in range(Q):
    x = int(input())
    si = bisect_left(s,x)
    ti = bisect_left(t,x)
    sl = [s[si-1],s[si]]
    tl = [t[ti-1],t[ti]]
    ans = INF
    for i in range(2):
        for j in range(2):
            ans = min(ans, abs(x-sl[i])+abs(sl[i]-tl[j]), abs(x-tl[j])+abs(tl[j]-sl[i]))
    print(ans)

Solution (Python):
import sys
from bisect import bisect_left as bl
input = sys.stdin.readline
A, B, Q = map(int, input().split())
a = [-10 ** 18] + [int(input()) for _ in range(A)] + [10 ** 18]
b = [-10 ** 18] + [int(input()) for _ in range(B)] + [10 ** 18]
for _ in range(Q):
  x = int(input())
  ra = bl(a, x)
  rb = bl(b, x)
  la = ra - 1
  lb = rb - 1
  res = 10 ** 18
  res = min(res, max(abs(a[ra] - x), abs(b[rb] - x)))
  res = min(res, max(abs(a[la] - x), abs(b[lb] - x)))
  res = min(res, max(abs(a[la] - x), abs(b[rb] - x)) + min(abs(a[la] - x), abs(b[rb] - x)) * 2)
  res = min(res, max(abs(a[ra] - x), abs(b[lb] - x)) + min(abs(a[ra] - x), abs(b[lb] - x)) * 2)
  print(res)

Solution (Python):
import bisect
A, B, Q = map(int, input().split())
S = list(int(input()) for _ in range(A)) 
T = list(int(input()) for _ in range(B))


for _ in range(Q):
    x = int(input())
    s = bisect.bisect_right(S, x)
    t = bisect.bisect_right(T, x)
    s %= A
    t %= B
    ans = 2*10**10
    for i in range(2):
        for j in range(2):
            ans = min(ans, min(abs(x-S[s-i])+abs(S[s-i]-T[t-j]), abs(x-T[t-j])+abs(T[t-j]-S[s-i])))
    print(ans)

Solution (Python):
import bisect

A, B, Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]

for _ in range(Q):
    x = int(input())
    a = bisect.bisect_left(S,x)
    b = bisect.bisect_left(T,x)
    a1 = float("inf")
    a2 = float("inf")
    b1 = float("inf")
    b2 = float("inf")
    
    if a == 0:
        a2 = S[a] - x
    elif a == A:
        a1 = x - S[a-1]
    else:
        a1 = x - S[a-1]
        a2 = S[a] - x
    if b == 0:
        b2 = T[b] - x
    elif b == B:
        b1 = x - T[b-1]
    else:
        b1 = x - T[b-1]
        b2 = T[b] - x
    
    print(min(max(a1,b1),max(a2,b2), min(a1,b2)*2+max(a1,b2), min(a2,b1)*2+max(a2,b1)))

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

mx = 3*10**10
a,b,q = ma()
S = [0 for i in range(a+2)]
T = [0 for i in range(b+2)]
S[0] = -mx;S[a+1] = mx
T[0] = -mx;T[b+1] = mx
for i in range(a):
    S[i+1]=ni()
for i in range(b):
    T[i+1]=ni()

def bs(x,ls):
    ok =0
    ng = len(ls)-1
    while ng-ok > 1:
        idx = (ok+ng)//2
        if ls[idx] < x:
            ok = idx
        else:
            ng = idx
    return ok  
def solve(x):
    ids = bs(x,S)
    s1 = S[ids] 
    s2 = S[ids+1] 
    idt = bs(x,T)
    t1 = T[idt]
    t2 = T[idt+1]

    s1t1 = max(x-s1,x-t1)
    s1t2 = 2*min(x-s1,t2-x) + max(x-s1,t2-x)
    s2t1 = 2*min(s2-x,x-t1) + max(s2-x,x-t1)
    s2t2 = max(s2-x,t2-x)
    print(min(s1t1,s1t2,s2t1,s2t2))

for i in range(q):
    x = ni()
    solve(x)

Solution (Python):
import sys
from bisect import bisect


def read():
    return sys.stdin.readline().rstrip()


def main():
    a, b, q = map(int, read().split())
    s = [-10**12] + [int(read()) for _ in range(a)] + [10**12]
    t = [-10**12] + [int(read()) for _ in range(b)] + [10**12]
    for _ in range(q):
        x = int(read())
        si = bisect(s, x)
        ti = bisect(t, x)
        sa, sb = s[si - 1], s[si]
        ta, tb = t[ti - 1], t[ti]
        d = 10**12
        for sx in (sa, sb):
            for tx in (ta, tb):
                d = min(abs(x - sx) + abs(sx - tx), abs(x - tx) + abs(tx - sx), d)
        print(d)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect, bisect_left 

A, B, Q = map(int, input().split())
S, T, X = [0]*(A+2), [0]*(B+2), [0]*Q

INF = 10**10
S[0], T[0] = -INF, -INF
S[-1], T[-1] = 2*INF, 2*INF

for i in range(A):
  S[i+1] = int(input())

for i in range(B):
  T[i+1] = int(input())

for i in range(Q):
  X[i] = int(input())

def dis(x):
  RS = bisect_left(S, x)
  LS = RS - 1
  RT = bisect_left(T, x)
  LT = RT - 1
  ans = [0]*4
  ans[0] = max(S[RS], T[RT]) - x
  ans[1] = x - min(S[LS], T[LT])
  ans[2] = S[RS] - T[LT] + min(S[RS] - x, x - T[LT])
  ans[3] = T[RT] - S[LS] + min(T[RT] - x, x - S[LS])
  return min(ans)

for x in X:
  print(dis(x))