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 bisect
inf=float("inf")
a,b,q=map(int,input().split())
s=[int(input()) for _ in range(a)]
t=[int(input()) for _ in range(b)]
x=[int(input()) for _ in range(q)]
for i in range(q):
    west=inf
    east=inf
    c=inf
    d=inf
    j=bisect(s,x[i])
    k=bisect(t,x[i])
    if j!=a and k!=0:
        c=s[j]-t[k-1]+min(s[j]-x[i],x[i]-t[k-1])
    if k!=b and j!=0:
        d=t[k]-s[j-1]+min(x[i]-s[j-1],t[k]-x[i])
    if j!=0 and k!=0:
        west=max(x[i]-s[j-1],x[i]-t[k-1])
    if j!=a and k!=b:
        east=max(s[j]-x[i],t[k]-x[i])
    print(min(west,east,c,d))

Solution (Python):
from bisect import bisect_left
A,B,Q=map(int,input().split())
s=[int(input()) for _ in range(A)]
t=[int(input()) for _ in range(B)]
x=[int(input()) for _ in range(Q)]
for i in x:
    si = bisect_left(s,i)
    ti = bisect_left(t,i)
    try:
        s1 = s[si-1]
    except:
        s1 = 10**11
    try:
        s0 = s[si]
    except:
        s0 = 10**11
    try:
        t1 = t[ti-1]
    except:
        t1 = 10**11
    try:
        t0 = t[ti]
    except:
        t0 = 10**11
    s0t0=abs(s0-i)+abs(t0-s0)
    s0t1=abs(s0-i)+abs(t1-s0)
    s1t0=abs(s1-i)+abs(t0-s1)
    s1t1=abs(s1-i)+abs(t1-s1)
    t0s0=abs(t0-i)+abs(t0-s0)
    t0s1=abs(t0-i)+abs(t0-s1)
    t1s0=abs(t1-i)+abs(t1-s0)
    t1s1=abs(t1-i)+abs(t1-s1)
    ans = min(s0t0,s0t1,s1t0,s1t1,t0s0,t0s1,t1s0,t1s1)
    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())
  b, d = bisect.bisect_right(s, x), bisect.bisect_right(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 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_right(s, x), bisect.bisect_right(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 bisect

A,B,Q = map(int,input().split())
shrine = [-float('inf')]+[int(input()) for _ in range(A)]+[float('inf')]
temple = [-float('inf')]+[int(input()) for _ in range(B)]+[float('inf')]
query = [int(input()) for _ in range(Q)]

for q in query:
  s = bisect.bisect(shrine,q)
  t = bisect.bisect(temple,q)
  print(
      min(
        max(shrine[s],temple[t])-q, 
        q - min(shrine[s-1],temple[t-1]),
        (shrine[s]-q)*2+(q-temple[t-1]),
        (temple[t]-q)*2+(q-shrine[s-1]),
        (shrine[s]-q)+(q-temple[t-1])*2,
        (temple[t]-q)+(q-shrine[s-1])*2,
        )
        )

Solution (Python):
import bisect
A,B,Q=map(int,input().split())
inf=10**18
s=[-inf]
t=[-inf]
for i in range(A):
  s.append(int(input()))
for i in range(B):
  t.append(int(input()))
s.append(inf)
t.append(inf)

for i in range(Q):
  x=int(input())
  a,b=bisect.bisect(s,x),bisect.bisect(t,x)
  res=inf
  for j in [s[a-1],s[a]]:
    for k in [t[b-1],t[b]]:
      res=min(res,abs(j-x)+abs(k-j),abs(k-x)+abs(k-j))
  print(res)

Solution (Python):
a, b, q = map(int, input().split())
S = [0] * a
T = [0] * b
X = [0] * q
for i in range(a):
  S[i] = int(input())
S = [-10**10*2-1] + S + [10**10*2+1]
for i in range(b):
  T[i] = int(input())
T = [-10**10*2-1] + T + [10**10*2+1]
for i in range(q):
  X[i] = int(input())


from bisect import bisect_right
for i in range(q):
  x = X[i]
  s_left = bisect_right(S, x) - 1
  s_right = bisect_right(S, x)
  t_left = bisect_right(T, x) - 1
  t_right = bisect_right(T, x)
  sl = S[s_left]
  sr = S[s_right]
  tl = T[t_left]
  tr = T[t_right]
  
  l_rr = max(sr - x, tr - x)
  l_ll = max(x - sl, x - tl)
  l_rslt = (sr - x) + (sr - tl)
  l_rtls = (tr - x) + (tr - sl)
  l_lsrt = (x - sl) + (tr - sl)
  l_ltrs = (x - tl) + (sr - tl)
  print(min(l_rr, l_ll, l_rslt, l_rtls, l_lsrt, l_ltrs))

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    A, B, Q = map(int, input().split())
    inf = float('inf')
    S = [-inf] + [int(input()) for _ in range(A)] + [inf]
    T = [-inf] + [int(input()) for _ 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)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    A, B, Q = map(int, input().split())
    inf = float('inf')
    S = [-inf] + [int(input()) for _ in range(A)] + [inf]
    T = [-inf] + [int(input()) for _ 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)


if __name__ == '__main__':
    main()

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

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

INF = 10 ** 11
for x in X:
  ids = bisect.bisect_left(S,x)
  if ids == 0:
    sl = -INF
    sr = S[0]
  elif ids == A:
    sl = S[A-1]
    sr = INF
  else:
    sl = S[ids-1]
    sr = S[ids]
  idt = bisect.bisect_left(T,x)
  if idt == 0:
    tl = -INF
    tr = T[0]
  elif idt == B:
    tl = T[B-1]
    tr = INF
  else:
    tl = T[idt-1]
    tr = T[idt]
  print(min(max(x-sl,x-tl),(x-sl)+(tr-sl),(x-tl)+(sr-tl),(tr-x)+(tr-sl),(sr-x)+(sr-tl),max(sr-x,tr-x)))

Solution (Python):
import bisect
import itertools

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


INF = 10 ** 15
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)]

def solve(x):
  
  i = bisect.bisect_left(S, x)
  j = bisect.bisect_left(T, x)
  result = INF
  
  for s,t in itertools.product(S[i-1:i+1], T[j-1:j+1]):
    d = min(abs(s-x),abs(t-x)) + abs(t-s)
    result = min(result,d)
  return result

for x in X:
  answer = solve(x)
  print(answer)

Solution (Python):
import bisect
import itertools

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

for _ in range(Q):
    answer = INF
    x = int(input())
    si = bisect.bisect_left(S, x)    
    ti = bisect.bisect_left(T, x)
    for s, t in itertools.product(S[si-1:si+1], T[ti-1:ti+1]):
        d = min(abs(s-x), abs(t-x)) + abs(t-s)            
        answer = min(answer, d)
    print(answer)

Solution (Python):
import bisect

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

for _ in range(Q):
    answer = float('inf')
    x = int(input())
    si = bisect.bisect_left(S, x)    
    ti = bisect.bisect_left(T, x)
    for s in [S[si-1], S[si]]:
        for t in [T[ti-1], T[ti]]:
            d = min(abs(s-x), abs(t-x)) + abs(t-s)            
            answer = min(answer, d)
    print(answer)

Solution (Python):
import bisect

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

for _ in range(Q):
    answer = float('inf')
    x = int(input())
    si = bisect.bisect_left(S, x)    
    ti = bisect.bisect_left(T, x)
    for s in [S[si-1], S[si]]:
        for t in [T[ti-1], T[ti]]:
            d = min(abs(s-x), abs(t-x)) + abs(t-s)            
            answer = min(answer, d)
    print(answer)

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())
S = [-float('inf')] + [int(input()) for _ in range(A)] + [float('inf')]
T = [-float('inf')] + [int(input()) for _ in range(B)] + [float('inf')]

for _ in range(Q):
    answer = float('inf')
    x = int(input())
    si = bisect.bisect_left(S, x)    
    ti = bisect.bisect_left(T, x)
    for s in [S[si-1], S[si]]:
        for t in [T[ti-1], T[ti]]:
            d1 = abs(s - x) + abs(t - s)
            d2 = abs(t - x) + abs(t - s)
            answer = min(answer, d1, d2)
    print(answer)

Solution (Python):

from bisect import bisect_left

INF = float('inf')

def f(x, s, t):
    result = INF
    i = bisect_left(s, x)
    if i > 0:
        j = bisect_left(t, s[i - 1])
        if j > 0:
            result = min(result, (x - s[i - 1]) + (s[i - 1] - t[j - 1]))
        if j < len(t):
            result = min(result, (x - s[i - 1]) + (t[j] - s[i - 1]))
    if i < len(s):
        j = bisect_left(t, s[i])
        if j > 0:
            result = min(result, (s[i] - x) + (s[i] - t[j - 1]))
        if j < len(t):
            result = min(result, (s[i] - x) + (t[j] - s[i]))
    return result


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

for i in range(Q):
    x = int(input())
    print(min(f(x, s, t), f(x, t, s)))

Solution (Python):


import bisect

A, B, Q = map(int, input().split())
S = [-float("inf")]
T = [-float("inf")]
X = []

for i in range(A):
    S.append(int(input()))
S.append(float("inf"))

for i in range(B):
    T.append(int(input()))
T.append(float("inf"))


for i in range(Q):
    X.append(int(input()))

for x in X:
    si = bisect.bisect_left(S, x)
    ti = bisect.bisect_left(T, x)
    
    s01 = [S[si-1], S[si]]
    t01 = [T[ti-1], T[ti]]
    
    ans = float("inf")
    for ss in s01:
        for tt in t01:
            l = abs(ss - tt) + min(abs(ss - x), abs(tt - x))
            ans = min(ans, l)
    print(ans)

Solution (Python):
import bisect

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

for _ in range(Q):
    x = int(input())
    si = bisect.bisect_right(S, x)
    ti = bisect.bisect_right(T, x)
    d = INF
    for s in [S[si-1], S[si]]:
        for t in [T[ti-1], T[ti]]:
            d1 = abs(s-x) + abs(t-s)
            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**12
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]

S.append(-INF)
S.append(INF)
T.append(-INF)
T.append(INF)

S.sort()
T.sort()

mini = INF

for _ in range(Q):
    x = int(input())
    
    s1 = bisect.bisect_right(S,x,)
    s2 = s1-1
    
    t1 = bisect.bisect_right(T,x,)
    t2 = t1-1
    for i in [S[s1],S[s2]]:
        mini = min(mini,abs(x-i)+min(abs(i-T[t1]),abs(i-T[t2])))
    for j in [T[t1],T[t2]]:
        mini = min(mini,abs(x-j)+min(abs(j-S[s1]),abs(j-S[s2])))
    print(mini)
    mini = INF

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

def main():
    A, B, Q = map(int, input().split())
    s = list(int(input()) for _ in range(A))
    t = list(int(input()) for _ in range(B))

    sort_s = sorted(s)
    sort_t = sorted(t)
    for _ in range(Q):
        x = int(input())
        s_near = bisect.bisect_left(sort_s, x)
        t_near = bisect.bisect_left(sort_t, x)

        s_candidate = []
        t_candidate = []

        if s_near == 0:
            s_candidate.append(sort_s[s_near])
        elif s_near == A:
            s_candidate.append(sort_s[s_near-1])
        else:
            s_candidate.append(sort_s[s_near])
            s_candidate.append(sort_s[s_near-1])
        
        if t_near == 0:
            t_candidate.append(sort_t[t_near])
        elif t_near == B:
            t_candidate.append(sort_t[t_near-1])
        else:
            t_candidate.append(sort_t[t_near])
            t_candidate.append(sort_t[t_near-1])

        ans = float('inf')
        
        for i in s_candidate:
            for j in t_candidate:
                tmp = abs(x - i) + abs(i - j)
                ans = min(ans, tmp)

        
        for k in t_candidate:
            for l in s_candidate:
                tmp = abs(x - k) + abs(k - l)
                ans = min(ans, tmp)

        print(ans)

main()

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


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

    for x in X:
        i_s = bisect.bisect_left(S, x)
        i_t = bisect.bisect_left(T, x)
        Cs = []
        Ct = []
        if i_s!=0:
            s_l = S[i_s - 1]
            Cs.append(s_l)
        if i_s!=len(S):
            s_r = S[i_s]
            Cs.append(s_r)
        if i_t!=0:
            t_l = T[i_t - 1]
            Ct.append(t_l)
        if i_t!=len(T):
            t_r = T[i_t]
            Ct.append(t_r)
        
        
        
        ans = float('inf')
        for C in itertools.product(Cs, Ct):
            ans = min(ans, abs(x-C[0])+abs(C[0]-C[1]))
            ans = min(ans, abs(x-C[1])+abs(C[1]-C[0]))
            
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect
 
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]
ans = inf
for _ in range(q):
  x = int(input())
  b,d = bisect.bisect_right(S,x),bisect.bisect_right(T,x)
  ans = inf
  for s in [S[b-1],S[b]]:
    for t in [T[d-1],T[d]]:
      tmp1,tmp2 = abs(s-x)+abs(s-t) ,abs(t-x)+abs(t-s)
      ans = min(ans ,tmp1,tmp2)
  print(ans)

Solution (Python):
import bisect 

def getlist():
	return list(map(int, input().split()))

INF = float("inf")

A, B, Q = getlist()
S = [-INF]
for i in range(A):
	s = int(input())
	S.append(s)
S.append(INF)

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

for i in range(Q):
	x = int(input())
	a = bisect.bisect_left(S, x)
	b = bisect.bisect_left(T, x)
	print(min(max(x - S[a - 1], x - T[b - 1]), max(S[a] - x, T[b] - x), (S[a] - x) * 2 + x - T[b - 1], (T[b] - x) * 2 + x - S[a - 1], S[a] - x + (x - T[b - 1]) * 2, T[b] - x + (x - S[a - 1]) * 2))

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    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]
    X=[int(input()) for _ in range(q)]
    from bisect import bisect
    for x in X:
        i=bisect(S,x)
        j=bisect(T,x)
        sleft=x-S[i-1]
        sright=S[i]-x
        tleft=x-T[j-1]
        tright=T[j]-x
        LL=max(sleft,tleft)
        RR=max(sright,tright)
        LR=sleft+tright+min(sleft,tright)
        RL=sright+tleft+min(sright,tleft)
        print(min(LL,RR,LR,RL))

resolve()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    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]
    X=[int(input()) for _ in range(q)]
    from bisect import bisect
    for x in X:
        i=bisect(S,x)
        j=bisect(T,x)
        sleft=x-S[i-1]
        sright=S[i]-x
        tleft=x-T[j-1]
        tright=T[j]-x
        LL=max(sleft,tleft)
        RR=max(sright,tright)
        LR=sleft+tright+min(sleft,tright)
        RL=sright+tleft+min(sright,tleft)
        print(min(LL,RR,LR,RL))

resolve()

Solution (Python):
import bisect
import sys
input=sys.stdin.readline
a,b,q=map(int,input().split())
s=[-999999999999]+[int(input()) for i in range(a)]+[999999999999]
t=[-999999999999]+[int(input()) for i in range(b)]+[999999999999]
x=[int(input()) for i in range(q)]
for i in range(q):
  a1=s[bisect.bisect_left(s,x[i])-1]
  a2=s[bisect.bisect_left(s,x[i])]
  b1=t[bisect.bisect_left(t,x[i])-1]
  b2=t[bisect.bisect_left(t,x[i])]
  l=[]
  for j in [a1,a2]:
    for k in [b1,b2]:
      l.append(abs(j-k)+min(abs(x[i]-j),abs(x[i]-k)))
  print(min(l))

Solution (Python):
import bisect
a,b,q=map(int,input().split())
s=[-999999999999]+[int(input()) for i in range(a)]+[999999999999]
t=[-999999999999]+[int(input()) for i in range(b)]+[999999999999]
x=[int(input()) for i in range(q)]
for i in range(q):
  a1=s[bisect.bisect_left(s,x[i])-1]
  a2=s[bisect.bisect_left(s,x[i])]
  b1=t[bisect.bisect_left(t,x[i])-1]
  b2=t[bisect.bisect_left(t,x[i])]
  l=[]
  for j in [a1,a2]:
    for k in [b1,b2]:
      l.append(abs(j-k)+min(abs(x[i]-j),abs(x[i]-k)))
  print(min(l))

Solution (Python):
a,b,q = map(int,input().split())
inf = 10 ** 18
s = [-inf]
for _ in range(a):
  s.append(int(input()))
s.append(inf)
t = [-inf]
for _ in range(b):
  t.append(int(input()))
t.append(inf)
import bisect
st = [inf]
for i in range(1,a+1):
  here = bisect.bisect_left(t,s[i])
  st.append(min(abs(t[here-1]-s[i]),abs(t[here]-s[i])))
st.append(inf)
ts = [inf]
for j in range(1,b+1):
  here = bisect.bisect_left(s,t[j])
  ts.append(min(abs(s[here-1]-t[j]),abs(s[here]-t[j])))
ts.append(inf)
for _ in range(q):
  now = int(input())
  shere = bisect.bisect_left(s,now)
  there = bisect.bisect_left(t,now)
  z1 = abs(now-s[shere-1]) + st[shere-1]
  z2 = abs(now-s[shere  ]) + st[shere  ]
  z3 = abs(now-t[there-1]) + ts[there-1]
  z4 = abs(now-t[there  ]) + ts[there  ]
  print(min(z1,z2,z3,z4))

Solution (Python):
import bisect

a,b,q=map(int,input().split())
tem=[-10**18]+[int(input()) for _ in range(a)]+[10**18]
shr=[-10**18]+[int(input()) for _ in range(b)]+[10**18]
ans=[]

for _ in range(q):
    x=int(input())
    t=bisect.bisect_right(tem, x)
    s=bisect.bisect_right(shr, x)
    dt=[tem[t-1], tem[t]]
    ds=[shr[s-1], shr[s]]

    res=10**18
    for i in dt:
        for j in ds:
            d1=abs(x-i)+abs(i-j)
            d2=abs(x-j)+abs(i-j)
            res=min(res, d1, d2)
    ans.append(res)

print(*ans, sep="\n")

Solution (Python):
import bisect

a, b, q = map(int, input().split())
ss = sorted([int(input()) for _ in range(a)] + [float('inf'), -float('inf')])
ts = sorted([int(input()) for _ in range(b)] + [float('inf'), -float('inf')])
xs = [int(input()) for _ in range(q)]

for x in xs:
    sindex = bisect.bisect(ss, x)
    sl = x - ss[sindex - 1]
    sr = ss[sindex] - x
    tindex = bisect.bisect(ts, x)
    tl = x - ts[tindex - 1]
    tr = ts[tindex] - x
    print(
        min([
            max(sl, tl),
            max(sr, tr),
            2 * sl + tr,
            2 * tl + sr,
            2 * sr + tl,
            2 * tr + sl,
        ]))

Solution (Python):
import bisect

a, b, q = map(int, input().split())
ss = sorted([int(input()) for _ in range(a)])
ts = sorted([int(input()) for _ in range(b)])
xs = [int(input()) for _ in range(q)]

for x in xs:
    sindex = bisect.bisect(ss, x)
    if sindex == 0:
        sl = float('inf')
        sr = ss[sindex] - x
    elif sindex == a:
        sl = x - ss[sindex - 1]
        sr = float('inf')
    else:
        sl = x - ss[sindex - 1]
        sr = ss[sindex] - x
    tindex = bisect.bisect(ts, x)
    if tindex == 0:
        tl = float('inf')
        tr = ts[tindex] - x
    elif tindex == b:
        tl = x - ts[tindex - 1]
        tr = float('inf')
    else:
        tl = x - ts[tindex - 1]
        tr = ts[tindex] - x
    print(
        min([
            max(sl, tl),
            max(sr, tr),
            2 * sl + tr,
            2 * tl + sr,
            2 * sr + tl,
            2 * tr + sl,
        ]))

Solution (Python):
import sys
import bisect
MAX_INT = int(10e12)
MIN_NUM = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def distance(list, index, x):
  res = abs(x - list[index][0])
  return res

def distance2(list, index, x):
  res = abs(x - list[index])
  return res


def nibutan(list,n):
    left = -1
    right = len(list)
    while left+1 != right:
        middle = (left+right)//2
        if list[middle][0] <= n:
            left = middle
        else:
            right = middle
    return right

A,B,Q = IL()
a = [I() for i in range(A)] 
b = [I() for i in range(B)] 
q = [I() for i in range(Q)]
ab = []

a.sort()
b.sort()

for i in a:
  ab.append([i,0])
for i in b:
  ab.append([i,1])
ab.sort()

for x in q:
  ans = MAX_INT
  ID = nibutan(ab, x)
  for i in [-1,0]:
    if 0 <= ID+i < len(ab):
      if ab[ID+i][1] == 0: 
        for j in [-1,0]:
          ID2 = bisect.bisect_left(b, ab[ID+i][0])
          if 0 <= ID2+j < len(b):
            dis = distance(ab, ID+i, x)
            dis += distance2(b, ID2+j, ab[ID+i][0])
            ans = min(ans, dis)
      else: 
        for j in [-1,0]:
          ID2 = bisect.bisect_left(a, ab[ID+i][0])
          if 0 <= ID2+j < len(a):
            dis = distance(ab, ID+i, x)
            dis += distance2(a, ID2+j, ab[ID+i][0])
            ans = min(ans, dis)
  print(ans)

Solution (Python):

a,b,q = map(int,input().split())
s = []
for _ in range(a):
  s.append(int(input()))
t = []
for _ in range(b):
  t.append(int(input()))
import bisect
st = []
for i in range(a):
  tmp = bisect.bisect_left(t,s[i])
  if tmp == 0:
    st.append(abs(s[i]-t[tmp]))
  elif tmp == b:
    st.append(abs(s[i]-t[tmp-1]))
  else:
    st.append(min(abs(s[i]-t[tmp-1]),abs(s[i]-t[tmp])))
ts = []
for j in range(b):
  tmp = bisect.bisect_left(s,t[j])
  if tmp == 0:
    ts.append(abs(t[j]-s[tmp]))
  elif tmp == a:
    ts.append(abs(t[j]-s[tmp-1]))
  else:
    ts.append(min(abs(t[j]-s[tmp-1]),abs(t[j]-s[tmp])))
'''
print(s)
print(t)
print(st)
print(ts)
'''
for _ in range(q):
  x = int(input())
  can = []
  xs = bisect.bisect_left(s,x)
  xt = bisect.bisect_left(t,x)
  try:
    can.append(abs(x-s[xs-1]) + st[xs-1])
  except:
    pass
  try:
    can.append(abs(x-s[xs])   + st[xs])
  except:
    pass
  try:
    can.append(abs(x-t[xt-1]) + ts[xt-1])
  except:
    pass
  try:
    can.append(abs(x-t[xt])   + ts[xt])
  except:
    pass
  print(min(can))

Solution (Python):
import bisect
a,b,q = map(int,input().split())
s = []
ans = []
for i in range(a):
    s0 = int(input())
    s.append(s0)
t = []
for i in range(b):
    t0 = int(input())
    t.append(t0)
for i in range(q):
    x = int(input())
    m = bisect.bisect_left(s,x)
    n = bisect.bisect_left(t,x)
    ai = []
    bi = []
    ansl = []
    if m > 0 and m < a:
        ai.append(s[m])
        ai.append(s[m-1])
    elif m == 0:
        ai.append(s[m])
    else:
        ai.append(s[m-1])
    if n > 0 and n < b:
        bi.append(t[n])
        bi.append(t[n-1])
    elif n == 0:
        bi.append(t[n])
    else:
        bi.append(t[n-1])
    for j in ai:
        for k in bi:
            if (j-x)*(k-x) > 0:
                ansl.append(max(abs(j-x),abs(k-x)))
            else:
                ansl.append(abs(j-k)+abs(j-x))
                ansl.append(abs(j-k)+abs(k-x))
    ans.append(min(ansl))
for i in ans:
    print(i)