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):
import bisect
import sys
readline = sys.stdin.buffer.readline


def main():
    A, B, Q = map(int, readline().split())
    inf = 10 ** 18
    s = [-inf] + [int(readline()) for _ in range(A)] + [inf]
    t = [-inf] + [int(readline()) for _ in range(B)] + [inf]
    for q in range(Q):
        x = int(readline())
        b, d = bisect.bisect_right(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)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
a,b,q=map(int,input().split())
s=[-10**11]
t=[-10**11]
for i in range(a):
	s.append(int(input()))
s.append(10**11)
for i in range(b):
	t.append(int(input()))
t.append(10**11)
import bisect
for i in range(q):
	x=int(input())
	s_l=bisect.bisect_left(s,x)
	t_l=bisect.bisect_left(t,x)
	ans1=x-min(s[s_l-1],t[t_l-1])
	ans2=max(s[s_l],t[t_l])-x
	ans3=min(s[s_l]-x,x-t[t_l-1])+s[s_l]-t[t_l-1]
	ans4=min(t[t_l]-x,x-s[s_l-1])+t[t_l]-s[s_l-1]
	print(min(ans1,ans2,ans3,ans4))

Solution (Python):
from bisect import bisect_left

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]
x = [int(input()) for _ in range(q)]

for e in x:
    si = bisect_left(s, e)
    ti = bisect_left(t, e)

    ans = INF
    for ss in [s[si-1], s[si]]:
        for tt in [t[ti-1], t[ti]]:
            d1 = abs(e - ss) + abs(ss - tt)
            d2 = abs(e - tt) + abs(tt - ss)
            ans = min(ans, d1, d2)

    print(ans)

Solution (Python):
from bisect import bisect_left

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]
x = [int(input()) for _ in range(q)]

for e in x:
    si = bisect_left(s, e)
    ti = bisect_left(t, e)
    sl, sr = s[si-1], s[si]
    tl, tr = t[ti-1], t[ti]

    ans = INF
    ans = min(ans, e - min(sl, tl))
    ans = min(ans, max(sr, tr) - e)
    ans = min(ans, e - sl + tr - sl)
    ans = min(ans, e - tl + sr - tl)
    ans = min(ans, sr - e + sr - tl)
    ans = min(ans, tr - e + tr - sl)

    print(ans)

Solution (Python):
def main():
    INF=10**11
    from bisect import bisect_left
    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]

    for _ in range(q):
        x=int(input())
        s=bisect_left(S,x)
        t=bisect_left(T,x)
        ans=max(T[t],S[s])-x
        ans=min(ans,S[s]-T[t-1]+min(S[s]-x,x-T[t-1]))
        ans=min(ans,x-min(S[s-1],T[t-1]))
        ans=min(ans,T[t]-S[s-1]+min(T[t]-x,x-S[s-1]))
        print(ans)
if __name__=='__main__':
    main()

Solution (Python):
import sys
import bisect
input=sys.stdin.readline
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())
  sr=bisect.bisect_left(s,x)
  sr=sr-1 if sr==len(s) else sr
  sl=sr-1 if sr>0 else sr 
  tr=bisect.bisect_left(t,x)
  tr=tr-1 if tr==len(t) else tr
  tl=tr-1 if tr>0 else tr
 
  def f(i,j):
    
    return min(abs(x-s[i]),abs(x-t[j]))+abs(t[j]-s[i])


  ans=min(f(sl,tl),f(sl,tr),f(sr,tl),f(sr,tr))
  print(ans)

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)]

for _ in range(q):
  x = int(input())
  s_bi = bisect.bisect_right(S, x)
  t_bi = bisect.bisect_right(T, x)
  s_left = S[s_bi-1] if s_bi>0 else -float("inf")
  s_right = S[s_bi] if s_bi<a else float("inf")
  t_left = T[t_bi-1] if t_bi>0 else -float("inf")
  t_right = T[t_bi] if t_bi<b else float("inf")
  c = x - min(s_left, t_left)
  d = max(s_right, t_right) - x
  e = t_right - s_left + min(x-s_left, t_right-x)
  f = s_right - t_left + min(x-t_left, s_right-x)
  ans = min(c, d, e, f)
  print(ans)

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

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 _ in range(Q):
    x = int(input())
    i = bisect(s, x)
    j = bisect(t, x)
    ans = INF
    for a in [s[i-1], s[i]]:
        for b in [t[j-1], t[j]]:
            ans = min(ans, abs(a-x)+abs(b-a), abs(b-x)+abs(a-b))
    print(ans)

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

A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
s.sort()
t = [int(input()) for _ in range(B)]
t.sort()
for _ in range(Q):
    x = int(input())
    i = bisect(s, x)
    sl = sr = 0
    if i == 0:
        sl = -float('inf')
        sr = s[i]
    elif i == A:
        sl = s[i-1]
        sr = float('inf')
    else:
        sl = s[i-1]
        sr = s[i]
    j = bisect(t, x)
    tl = tr = 0
    if j == 0:
        tl = -float('inf')
        tr = t[j]
    elif j == B:
        tl = t[j-1]
        tr = float('inf')
    else:
        tl = t[j-1]
        tr = t[j]
    ans = min(max(sr, tr)-x, x-min(sl, tl), x+sr-2*tl, x+tr-2*sl, 2*sr-x-tl, 2*tr-x-sl)
    print(ans)

Solution (Python):
from bisect import bisect_left

INF = 2 * 10**10

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)]
ans = []
for x in X:
    sr = bisect_left(S, x) 
    sl = sr if S[sr] == x else sr -1
    tr = bisect_left(T, x)
    tl = tr if T[tr] == x else tr -1

    ll = x - min(S[sl], T[tl])
    rr = max(S[sr], T[tr]) - x
    lsrt = 2*(x-S[sl]) + (T[tr] - x)
    ltrs = 2*(x-T[tl]) + (S[sr] - x)
    rslt = 2*(S[sr] - x) + (x - T[tl])
    rtls = 2*(T[tr] - x) + (x - S[sl])

    ans.append(min(ll, rr, lsrt, ltrs, rslt, rtls))
print(*ans, sep='\n')

Solution (Python):
from bisect import bisect_left

INF = 2 * 10**10

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)]
ans = []
for x in X:
    sr = bisect_left(S, x) 
    sl = sr -1
    if sr != len(S) and S[sr] == x:
        sl = sr
    tr = bisect_left(T, x)
    tl = tr - 1
    if tr != len(T) and T[tr] == x:
        tl = tr

    ll = INF if sl == -1 or tl == -1 else x - min(S[sl], T[tl])
    rr = INF if sr == len(S) or tr == len(T) else max(S[sr], T[tr]) - x
    lsrt = INF if sl == -1 or tr == len(T) else 2*(x-S[sl]) + (T[tr] - x)
    ltrs = INF if tl == -1 or sr == len(S) else 2*(x-T[tl]) + (S[sr] - x)
    rslt = INF if tl == -1 or sr == len(S) else 2*(S[sr] - x) + (x - T[tl])
    rtls = INF if sl == -1 or tr == len(T) else 2*(T[tr] - x) + (x - S[sl])

    ans.append(min(ll, rr, lsrt, ltrs, rslt, rtls))
print(*ans, sep='\n')

Solution (Python):
from bisect import bisect_left,bisect_right
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
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())
    values = []
    p1 = bisect_left(S,x)
    if p1 == 0:
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    elif p1 == A:
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    else:
        
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        p1 += 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))

    p1 = bisect_left(T,x)
    if p1 == 0:
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    elif p1 == B:
        p1 -= 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    else:
        
        p1 -= 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        p1 += 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    d = min(values)
    print(d)

Solution (Python):
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines





inf=10**18

import bisect

def main():
	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)]
	
	
	for i in x:
		si=bisect.bisect_left(s,i)
		ti=bisect.bisect_left(t,i)
		
		ans=inf
		for S in [s[si-1],s[si]]:
			for T in [t[ti-1],t[ti]]:
				ans=min(ans,abs(S-i)+abs(S-T),abs(T-i)+abs(S-T))
		print(ans)
		
		
		
	

if __name__ == "__main__":
	main()

Solution (Python):
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines





inf=10**18

import bisect

def main():
	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)]
	
	
	for i in x:
		si=bisect.bisect_left(s,i)
		ti=bisect.bisect_left(t,i)
		
		ans=inf
		for S in [s[si-1],s[si]]:
			for T in [t[ti-1],t[ti]]:
				ans=min(ans,abs(S-i)+abs(S-T),abs(T-i)+abs(S-T))
		print(ans)
		
		
		
	

if __name__ == "__main__":
	main()

Solution (Python):
import bisect


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())
    b = bisect.bisect_right(s, x)
    d = bisect.bisect_right(t, x)
    ans = INF
    for S in (s[b - 1], s[b]):
        for T in (t[d - 1], t[d]):
            d1 = abs(S - x) + abs(T - S)
            d2 = abs(T - x) + abs(S - T)
            ans = min(ans, d1, d2)
    print(ans)

Solution (Python):
def halfsplitsearch(list,start,end,minmax):
    if minmax=='min':
        while end-start>1:
            test=(end+start)//2
            if condition(list[test][0]):
                end=test
            else:
                start=test
        if condition(list[start][0]):
            return start
        else:
            return end
    else:
        while end-start>1:
            test=(end+start)//2
            if condition(list[test][0]):
                start=test
            else:
                end=test
        if condition(list[end][0]):
            return end
        else:
            return start

import sys

input=sys.stdin.readline

A,B,Q=map(int,input().split())
s=[[] for i in range(0,A)]
for i in range(0,A):
    y=int(input())
    s[i]=[y,'s',i]
t=[[] for i in range(0,B)]
for i in range(0,B):
    y=int(input())
    t[i]=[y,'t',i]
x=[0 for i in range(0,Q)]
for i in range(0,Q):
    x[i]=int(input())

list=s+t
list.sort()
s.sort()
t.sort()

sans=[0 for i in range(0,A)]
for i in range(0,A):
    ans=0
    def condition(num):
        return num<s[i][0]
    ans=halfsplitsearch(t,0,len(t)-1,'max')
    if ans!=B-1:
        sans[i]=min(abs(s[i][0]-t[ans][0]),abs(s[i][0]-t[ans+1][0]))
    else:
        sans[i]=abs(s[i][0]-t[ans][0])

tans=[0 for i in range(0,B)]
for i in range(0,B):
    ans=0
    def condition(num):
        return num<t[i][0]
    ans=halfsplitsearch(s,0,len(s)-1,'max')
    if ans!=A-1:
        tans[i]=min(abs(t[i][0]-s[ans][0]),abs(t[i][0]-s[ans+1][0]))
    else:
        tans[i]=abs(t[i][0]-s[ans][0])


for i in range(0,Q):
    a=0
    b=0
    test1=10**100
    test2=10**100
    def condition(num):
        return num<x[i]
    a=halfsplitsearch(list,0,len(list)-1,'max')
    if list[a][1]=='s':
        test1=abs(x[i]-list[a][0])+sans[list[a][2]]
    else:
        test1=abs(x[i]-list[a][0])+tans[list[a][2]]
    if a!=len(list)-1:
        if list[a+1][1]=='s':
            test2=abs(x[i]-list[a+1][0])+sans[list[a+1][2]]
        else:
            test2=abs(x[i]-list[a+1][0])+tans[list[a+1][2]]
    print(min(test1,test2))

Solution (Python):
A, B, Q = [int(x) for x in input().split()]
s = [-10 ** 10] + [None] * A + [2 * (10 ** 10)]  
t = [-10 ** 10] + [None] * B + [2 * (10 ** 10)]  
x = [0] * Q
for i in range(1, A + 1):
    s[i] = int(input())
for j in range(1, B + 1):
    t[j] = int(input())
for k in range(Q):
    x[k] = int(input())

for i in range(Q):
    
    ok = 0       
    ng = A + 1   
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if s[mid] < x[i]:
            ok = mid
        else:
            ng = mid
    j = ok

    ok = 0      
    ng = B + 1  
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if t[mid] < x[i]:
            ok = mid
        else:
            ng = mid
    k = ok
 
    ans = min(  max(s[j + 1], t[k + 1]) - x[i], 
                x[i] - min(s[j], t[k]),         
                2 * s[j + 1] - t[k] - x[i],     
                s[j + 1] - 2 * t[k] + x[i],     
                -s[j] + 2 * t[k + 1] - x[i],    
                -2 * s[j] + t[k + 1] + x[i])    

    print(ans)

Solution (Python):


def bs(x,li):
    
    
    
    left,right=0,len(li)
    while left+1<right:
        mid = (left+right)//2
        if li[mid]<=x:
            left=mid
        else:
            right=mid
    return left

def get(L):
    ret=[-10**13]
    for _ in range(L):
        ret.append(int(input()))
    ret.append(10**13)
    return ret

A,B,Q=map(int,input().split())
S=get(A)
T=get(B)
for _ in range(Q):
    x=int(input())

    
    sn,tn=bs(x,S),bs(x,T)

    
    
    ans=10**13
    for s in [S[sn],S[sn+1]]:
        for t in [T[tn],T[tn+1]]:
            d=abs(s-x)+abs(t-s)
            e=abs(t-x)+abs(s-t)
            ans=min([ans,d,e])
    print(ans)

Solution (Python):
a, b, q = map(int, input().split())
S = [int(input()) for _ in range(a)]
T = [int(input()) for _ in range(b)]
S.sort()
T.sort()
INF = 10**18
S = [-INF] + S + [INF]
T = [-INF] + T + [INF]

import bisect
for _ in range(q):
    x = int(input())
    i = bisect.bisect_right(S, x)
    j = bisect.bisect_right(T, x)
    ans = INF
    for s in [S[i-1], S[i]]:
        for t in [T[j-1], T[j]]:
            d1 = abs(s-x) + abs(t-s)
            d2 = abs(t-x) + abs(s-t)
            ans = min([ans, d1, d2])
    print(ans)

Solution (Python):
a, b, q = map(int, input().split())
S = [int(input()) for _ in range(a)]
T = [int(input()) for _ in range(b)]
S.sort()
T.sort()
INF = 10**18
S = [-INF] + S + [INF]
T = [-INF] + T + [INF]

import bisect
for _ in range(q):
    x = int(input())
    i = bisect.bisect_right(S, x)
    j = bisect.bisect_right(T, x)
    ans = INF
    for s in [S[i-1], S[i]]:
        for t in [T[j-1], T[j]]:
            d1 = abs(s-x) + abs(t-s)
            d2 = abs(t-x) + abs(s-t)
            ans = min([ans, d1, d2])
    print(ans)

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

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

INF = float('inf')
st = []
for s in S:
    i = bisect(T,s)
    x0,x1 = -INF,INF
    if i > 0: x0 = T[i-1]
    if i < B: x1 = T[i]
    st.append(min(s-x0, x1-s))
ts = []
for t in T:
    i = bisect(S,t)
    x0,x1 = -INF,INF
    if i > 0: x0 = S[i-1]
    if i < A: x1 = S[i]
    ts.append(min(t-x0, x1-t))

ans = []
for x in X:
    d0 = d1 = d2 = d3 = INF
    si = bisect(S,x)
    if si > 0:
        d0 = x-S[si-1] + st[si-1]
    if si < A:
        d1 = S[si]-x + st[si]
    ti = bisect(T,x)
    if ti > 0:
        d2 = x-T[ti-1] + ts[ti-1]
    if ti < B:
        d3 = T[ti]-x + ts[ti]
    ans.append(min(d0,d1,d2,d3))
print(*ans, sep='\n')

Solution (Python):
A,B,Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
import bisect
for _ in range(Q):
    x = int(input())
    s_idx = bisect.bisect_left(S, x)
    t_idx = bisect.bisect_left(T, x)
    if s_idx == A and t_idx == B:
        ans = x-min(S[-1], T[-1])
    elif s_idx == A and t_idx == 0:
        ans = min(x-S[-1]*2+T[0], T[0]*2-x-S[-1])
    elif t_idx == B and s_idx == 0:
        ans = min(x-T[-1]*2+S[0], S[0]*2-x-T[-1])
    elif s_idx == A:
        ans = min(x-min(S[-1], T[t_idx-1]), x-S[-1]*2+T[t_idx], T[t_idx]*2-x-S[-1])
    elif t_idx == B:
        ans = min(x-min(T[-1], S[s_idx-1]), x-T[-1]*2+S[s_idx], S[s_idx]*2-x-T[-1])
    elif s_idx == 0 and t_idx == 0:
        ans = max(S[0], T[0])-x
    elif s_idx == 0:
        ans = min(max(S[0], T[t_idx])-x, S[0]*2-x-T[t_idx-1], x-T[t_idx-1]*2+S[0])
    elif t_idx == 0:
        ans = min(max(T[0], S[s_idx])-x, T[0]*2-x-S[s_idx-1], x-S[s_idx-1]*2+T[0])
    else:
        ans = min(max(S[s_idx], T[t_idx])-x, x-min(S[s_idx-1], T[t_idx-1]), S[s_idx]*2-x-T[t_idx-1], T[t_idx]*2-x-S[s_idx-1])
        ans = min(ans, x-T[t_idx-1]*2+S[s_idx], x-S[s_idx-1]*2+T[t_idx])
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
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)
def f_s(x, l):
    mn, mx = -1, len(l)
    idx = mx//2
    while mx-mn>1:
        if l[idx] < x:
            idx, mn = (idx + mx)//2, idx
            continue
        idx, mx = (idx + mn)//2, idx
    return idx

for _ in range(Q):
    q = int(input())
    xi, yi = f_s(q, S), f_s(q, T)
    s1, s2 = S[xi], S[xi+1]
    t1, t2 = T[yi], T[yi+1]
    print(min([
        max(q-s1,q-t1),
        max(s2-q, t2-q),
        2*(s2-q) + q-t1,
        2*(t2-q) + q-s1,
        2*(q-s1) + t2-q,
        2*(q-t1) + s2-q,
        ]))

Solution (Python):
import sys, math, bisect
sys.setrecursionlimit(200010)

def input():
    return sys.stdin.readline()[:-1]

def main():
    A, B, Q = map(int,input().split())
    s = [-10**12] + sorted([int(input()) for k in range(A)]) + [10**12]
    t = [-10**12] + sorted([int(input()) for k in range(B)]) + [10**12]
    for _ in range(Q):
        q = int(input())
        s1 = s[bisect.bisect_left(s,q)]
        s2 = s[bisect.bisect_left(s,q)-1]
        t1 = t[bisect.bisect_left(t,q)]
        t2 = t[bisect.bisect_left(t,q)-1]
        d1 = max(q-s2,q-t2)
        d2 = max(s1-q,t1-q)
        d3 = 2*(q-s2)+t1-q
        d4 = 2*(q-t2)+s1-q
        d5 = 2*(s1-q)+q-t2
        d6 = 2*(t1-q)+q-s2
        print(min(d1,d2,d3,d4,d5,d6))

if __name__ == '__main__':
    main()

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

A, B, Q = map(int, input().split())
tera = [int(input()) for _ in range(A)]
sha = [int(input()) for _ in range(B)]
query = [int(input()) for _ in range(Q)]
res = []

for x in query:
    tl = bisect.bisect_left(tera, x)
    tr = bisect.bisect_right(tera, x)
    sl = bisect.bisect_left(sha, x)
    sr = bisect.bisect_right(sha, x)

    if tl == 0:
        temple = [tera[0]]
    elif tr == A:
        temple = [tera[A - 1]]
    else:
        temple = [tera[tl-1], tera[tl]]

    if sl == 0:
        shrine = [sha[0]]
    elif sr == B:
        shrine = [sha[B - 1]]
    else:
        shrine = [sha[sl-1], sha[sl]]

    ans = 10**18
    for t in temple:
        for s in shrine:
            tx = t - x
            sx = s - x
            if tx * sx >= 0:
                a = max(abs(tx), abs(sx))
            else:
                a = min(2 * abs(tx) + abs(sx), 2 * abs(sx) + abs(tx))
            ans = min(ans, a)
    res.append(ans)

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

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

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

for _ in range(Q):
    x = int(input())
    idx1 = bisect_right(s, x)
    idx2 = bisect_right(t, x)
    ls, rs = s[idx1-1], s[idx1]
    lt, rt = t[idx2-1], t[idx2]
    ans = 10**18
    
    for p1, p2 in [(ls, lt), (ls, rt), (rs, lt), (rs, rt), (lt, ls), (lt, rs), (rt, ls), (rt, rs)]:
        ans = min(ans, abs(p1-x)+abs(p2-p1))
    
    print(ans)

Solution (Python):
import sys
from bisect import bisect_left

def solve():
    input = sys.stdin.readline
    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())
        si = bisect_left(S, x)
        ti = bisect_left(T, x)
        minLength = 10 **20
        both_left = 10 ** 20
        both_right = 10 ** 20
        s_t = 10 ** 20
        t_s = 10 ** 20
        if si > 0 and ti > 0: both_left = max(x - S[si-1], x - T[ti - 1])
        if si <= A - 1 and ti <= B - 1: both_right = max(S[si] - x, T[ti] - x)
        if si > 0 and ti <= B - 1: s_t = T[ti] - S[si-1] + min(x - S[si-1], T[ti] - x)
        if ti > 0 and si <= A - 1: t_s = S[si] - T[ti-1] + min(x - T[ti-1], S[si] - x)
        minLength = min([both_left, both_right, s_t, t_s])
        print(minLength)

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
from bisect import bisect_left
A, B, Q, *L = map(int, open(0).read().split())
s = L[:A]
t = L[A:A+B]
x = L[A+B:]

s = [-float('inf')]+s+[float('inf')]
t = [-float('inf')]+t+[float('inf')]
for c in x:
  br = bisect_left(t,c)
  ar = bisect_left(s,c)
  bl = br-1
  al = ar-1
  print(min(s[ar]-t[bl]+min(s[ar]-c,c-t[bl]),t[br]-s[al]+min(t[br]-c,c-s[al]),
            c-min(t[bl],s[al]),max(s[ar],t[br])-c))

Solution (Python):
import numpy as np
A, B, Q, *L = map(int, open(0).read().split())
s = L[:A]
t = L[A:A+B]
x = L[A+B:]

s = [-float('inf')]+s+[float('inf')]
t = [-float('inf')]+t+[float('inf')]
sls = np.searchsorted(s,x)
tls = np.searchsorted(t,x)
for ar, br, c in zip(sls,tls,x):
  bl = br-1
  al = ar-1
  n = s[ar]-t[bl]+min(s[ar]-c,c-t[bl])
  m = t[br]-s[al]+min(t[br]-c,c-s[al])
  p = c-min(t[bl],s[al])
  q = max(s[ar],t[br])-c
  print(min(n,m,p,q))

Solution (Python):
from bisect import bisect_left
import sys
def input():
    return sys.stdin.readline()[:-1]
A,B,Q=map(int,input().split())
s=[int(input()) for i in range(A)]
t=[int(input()) for i in range(B)]
for i in range(Q):
    x=int(input())
    si=bisect_left(s,x)
    ti=bisect_left(t,x)
    a=[]
    if si and si!=A:
        sl=s[si-1]
        sr=s[si]
    elif si:
        sl=s[-1]
        sr=0
    else:
        sl=0
        sr=s[0]
    if ti and ti!=B:
        tl=t[ti-1]
        tr=t[ti]
    elif ti:
        tl=t[-1]
        tr=0
    else:
        tl=0
        tr=t[0]
    if sl and tl:
        a.append(x-min(sl,tl))
    if sr and tr:
        a.append(max(tr,sr)-x)
    if sl and tr:
        a.append(tr-x+tr-sl)
        a.append(x-sl+tr-sl)
    if sr and tl:
        a.append(x-tl+sr-tl)
        a.append(sr-x+sr-tl)
    print(min(a))

Solution (Python):
from bisect import bisect_left
import sys
def input():
    return sys.stdin.readline()[:-1]
A,B,Q=map(int,input().split())
s=[int(input()) for i in range(A)]
t=[int(input()) for i in range(B)]
for i in range(Q):
    x=int(input())
    si=bisect_left(s,x)
    ti=bisect_left(t,x)
    a=[]
    if si and si!=A:
        sl=s[si-1]
        sr=s[si]
    elif si:
        sl=s[-1]
        sr=0
    else:
        sl=0
        sr=s[0]
    if ti and ti!=B:
        tl=t[ti-1]
        tr=t[ti]
    elif ti:
        tl=t[-1]
        tr=0
    else:
        tl=0
        tr=t[0]
    if sl and tl:
        a.append(x-min(sl,tl))
    if sr and tr:
        a.append(max(tr,sr)-x)
    if sl and tr:
        a.append(tr-x+tr-sl)
        a.append(x-sl+tr-sl)
    if sr and tl:
        a.append(x-tl+sr-tl)
        a.append(sr-x+sr-tl)
    print(min(a))

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):
    
    x = int(input())
    
    s_index = bisect.bisect_right(s, x)
    s_right = s[s_index]
    s_left = s[s_index - 1]
    t_index = bisect.bisect_right(t, x)
    t_right = t[t_index]
    t_left = t[t_index - 1]
    
    ans = float('inf')
    
    
    ans = min(
        ans, 
        abs(x - s_right) + min(abs(s_right - t_right), abs(s_right - t_left)), 
        abs(x - s_left) + min(abs(s_left - t_right), abs(s_left - t_left)), 
    )
    
    ans = min(
        ans, 
        abs(x - t_right) + min(abs(t_right - s_right), abs(t_right - s_left)), 
        abs(x - t_left) + min(abs(t_left - s_right), abs(t_left - s_left)), 
    )
        
    print(ans)

Solution (Python):

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]

from bisect import bisect_left

for _ in range(Q):
    x=int(input())
    s=bisect_left(S,x)
    t=bisect_left(T,x)
    
    sr=abs(S[s]-x)
    sl=abs(S[s-1]-x)
    tr=abs(T[t]-x)
    tl=abs(T[t-1]-x)
    
    Ans=min(max(sr,tr),max(sl,tl),2*sr+tl,sr+2*tl,2*tr+sl,tr+2*sl)
    
    print(Ans)

Solution (Python):
import sys
import bisect
from itertools import product
def input(): return sys.stdin.readline().strip()
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
stlist = [s,t]
ab = [A,B]
for i in range(Q):
    x = int(input())
    cand = []
    for i in range(2):
        st = stlist[i]
        cand1 = set()
        l1 = bisect.bisect_left(st,x)
        if 0 <= l1 <= ab[i]-1:
            cand1.add(st[l1])
        if 0 <= l1-1 <= ab[i]-1:
            cand1.add(st[l1-1])
        cand.append(cand1)
    ans = float('inf')
    for a,b in product(cand[0],cand[1]):
        c = min(abs(x-a)+abs(b-a),abs(x-b)+abs(b-a))
        ans = min(ans,c)
    print(ans)

Solution (Python):
import sys
import bisect
from itertools import product
def input(): return sys.stdin.readline().strip()
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
stlist = [s,t]
ab = [A,B]
for i in range(Q):
    x = int(input())
    cand = []
    for i in range(2):
        st = stlist[i]
        cand1 = set()
        l1 = bisect.bisect_left(st,x)
        r1 = bisect.bisect_right(st,x)
        if 0 <= l1 <= ab[i]-1:
            cand1.add(st[l1])
        if 0 <= l1-1 <= ab[i]-1:
            cand1.add(st[l1-1])
        cand.append(cand1)
        
    ans = float('inf')
    for a,b in product(cand[0],cand[1]):
        c = min(abs(x-a)+abs(b-a),abs(x-b)+abs(b-a))
        ans = min(ans,c)
    print(ans)

Solution (Python):
import sys
import bisect
from itertools import product
def input(): return sys.stdin.readline().strip()
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
stlist = [s,t]
ab = [A,B]
for i in range(Q):
    x = int(input())
    cand = []
    for i in range(2):
        st = stlist[i]
        cand1 = set()
        l1 = bisect.bisect_left(st,x)
        r1 = bisect.bisect_right(st,x)
        if 0 <= l1 <= ab[i]-1:
            cand1.add(st[l1])
        if 0 <= l1-1 <= ab[i]-1:
            cand1.add(st[l1-1])  
        if 0 <= r1 <= ab[i]-1:
            cand1.add(st[r1])
        if 0 <= r1+1 <= ab[i]-1:
            cand1.add(st[r1+1])
        cand.append(cand1)
        
    ans = float('inf')
    for a,b in product(cand[0],cand[1]):
        c = min(abs(x-a)+abs(b-a),abs(x-b)+abs(b-a))
        ans = min(ans,c)
    print(ans)