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
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())
    c=bisect.bisect_left(s,x)
    d=bisect.bisect_left(t,x)
    p=[]
    if c!=a and d!=b:
        p.append(max(abs(s[c]-x),abs(t[d]-x)))
    if c!=0 and d!=0:
        p.append(max(abs(s[c-1]-x),abs(t[d-1]-x)))
    if c!=a and d!=0:
        p.append(min(abs(s[c]-x)*2+abs(t[d-1]-x),abs(s[c]-x)+2*abs(t[d-1]-x)))
    if c!=0 and d!=b:
        p.append(min(abs(s[c-1]-x)*2+abs(t[d]-x),abs(s[c-1]-x)+2*abs(t[d]-x)))
    print(min(p))

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 range(Q):
    q = X[i]
    ANS = []
    
    JP = []
    s = bisect_left(S, q)
    if s == 0:
        JP.append(S[0])
    elif s == A:
        JP.append(S[A-1])
    else:
        JP.append(S[s])
        JP.append(S[s-1])
    while JP:
        f = JP.pop()
        z = abs(f-q)
        t = bisect_left(T, f)
        if t == 0:
            ANS.append( z + abs(f - T[0]))
        elif t == B:
            ANS.append( z + abs(f - T[B-1]))
        else:
            ANS.append( z + abs(f - T[t]))
            ANS.append( z + abs(f - T[t-1]))
    
    TP = []
    t = bisect_left(T, q)
    if t == 0:
        TP.append(T[0])
    elif t == B:
        TP.append(T[B-1])
    else:
        TP.append(T[t])
        TP.append(T[t-1])
    while TP:
        f = TP.pop()
        z = abs(f-q)
        s = bisect_left(S, f)
        if s == 0:
            ANS.append( z + abs(f - S[0]))
        elif s == A:
            ANS.append( z + abs(f - S[A-1]))
        else:
            ANS.append( z + abs(f - S[s]))
            ANS.append( z + abs(f - S[s-1]))
    print( min(ANS))

Solution (Python):
import sys
from bisect import bisect

fin = sys.stdin.readline
A, B, Q = [int(elem) for elem in fin().split()]
s_list = tuple([-float('inf')]) + tuple(int(fin()) for _ in range(A)) + tuple([float('inf')])
t_list = tuple([-float('inf')]) + tuple(int(fin()) for _ in range(B)) + tuple([float('inf')])
x_list = tuple(int(fin()) for _ in range(Q))


def search_shortest_dist(s_list, t_list, x):
    s_closest_idx = bisect(s_list, x)
    t_closest_idx = bisect(t_list, x)
    min_dist = float('inf')
    for s_idx in (s_closest_idx - 1, s_closest_idx):
        s = s_list[s_idx]
        for t_idx in (t_closest_idx - 1, t_closest_idx):
            t = t_list[t_idx]
            if s <= t <= x:
                dist = x - s
            elif s <= x <= t:
                dist = t - s + min(x - s, t - x)
            elif t <= s <= x:
                dist = x - t
            elif t <= x <= s:
                dist = s - t + min(x - t, s - x)
            elif x <= s <= t:
                dist = t - x
            else:  
                dist = s - x
            min_dist = min(min_dist, dist)
    return min_dist


for x in x_list:
    print(search_shortest_dist(s_list, t_list, x))

Solution (Python):
import bisect
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()))
X  =[int(input()) for i in range(q)]



for x in X:
    s_index= bisect.bisect_left(s,x)
    
    t_index= bisect.bisect_left(t,x)
    
    if s_index == 0 and t_index == 0:
        print(max(t[0]-x,s[0]-x))
    elif s_index == 0:
        if t_index != b:
            if t[t_index] < s[0]:
                print(s[0]-x)
            else:
                ans  =min(2*s[0]-x - t[t_index-1],t[t_index]-x,x+s[0]-2*t[t_index-1])
                print(ans)
        else:
            print(min(2*s[s_index]-x-t[t_index-1],s[s_index]+x-2*t[t_index-1]))
    elif t_index == 0:
        if s_index != a:
            if s[s_index] < t[0]:
                print(t[0]-x)
            else:
                ans  =min(2*t[0]- x- s[s_index-1],s[s_index]-x,x+t[0]-2*s[s_index-1])
                print(ans)
        else:
            print(min(2*t[t_index]-x-s[s_index-1],t[t_index]+x-2*s[s_index-1]))
    elif s_index == a and t_index == b:
        print(max(x-s[a-1],x-t[b-1]))
    elif s_index == a:
        if s[a-1] < t[t_index-1]:
            print(x-s[a-1])
        else:
            ans = min(x-t[t_index-1],t[t_index]+x-2*s[a-1],2*t[t_index]-x-s[a-1])
            print(ans)
    elif t_index == b:
        if t[b-1] < s[s_index-1]:
            print(x-t[b-1])
        else:
            ans = min(x - s[s_index - 1], s[s_index] + x - 2 * t[b - 1],2*s[s_index]-x-t[b-1])
            print(ans)

    else:
        if s[s_index-1] < t[t_index-1] and s[s_index] < t[t_index]:
            ans = min(x-s[s_index-1],x+s[s_index]-2*t[t_index-1],2*s[s_index]-x-t[t_index-1],t[t_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and t[t_index] < s[s_index]:
            ans = min(x - t[t_index - 1], x + t[t_index] - 2 * s[s_index - 1], 2 * t[t_index] - x - s[s_index - 1],
                      s[s_index] - x)
            print(ans)
        if s[s_index-1] < t[t_index-1] and t[t_index] < s[s_index]:
            ans = min(x-s[s_index-1],s[s_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and s[s_index] < t[t_index]:
            ans = min(x - t[t_index - 1], t[t_index] - x)
            print(ans)

Solution (Python):


def lower_bound(al, key):
    """
    一般に左側が満たしておらず、右側は満たしている判定基準(境目は一つ)ならlower_boundになる
    (条件を満たすもののうち、最小のインデックスを返す)
    単調にさえなっていれば、左右が逆になっていても検索には使える(ng/okも一緒に逆にする必要がある)
    """
    def isOk(a):
        
        
        return a>=key

    ng = -1
    ok = len(al)

    while abs(ok-ng)>1:
        mid = (ok+ng)//2

        if isOk(al[mid]):
            ok = mid
        else:
            ng = mid

    return ok


a,b,q = map(int, input().split())
sl = [int(input()) for _ in range(a)]
tl = [int(input()) for _ in range(b)]
xl = [int(input()) for _ in range(q)]


for x in xl:
    si = lower_bound(sl,x)
    ti = lower_bound(tl,x)
    

    ssl = []
    if si==a:
        ssl.append(sl[-1])
    elif si==-1:
        ssl.append(sl[0])
    else:
        ssl.append(sl[si])
        if si>0:
            ssl.append(sl[si-1])
    ttl = []
    if ti==b:
        ttl.append(tl[-1])
    elif ti==-1:
        ttl.append(tl[0])
    else:
        ttl.append(tl[ti])
        if ti>0:
            ttl.append(tl[ti-1])

    res = -1
    for s in ssl:
        for t in ttl:
            if s>=x and t>=x:
                tmp = max(s-x, t-x)
            elif s<x and t<x:
                tmp = max(x-s, x-t)
            elif s>=x>=t:
                tmp = min(
                    2*(s-x)+(x-t),
                    (s-x)+2*(x-t)
                )
            else:
                tmp = min(
                    2*(x-s)+(t-x),
                    (x-s)+2*(t-x)
                )
            if res<0:
                res = tmp
            else:
                res = min(res,tmp)
    print(res)

Solution (Python):
import bisect

a,b,n=map(int,input().split())
l1=[int(input()) for i in range(a)]
l2=[int(input()) for i in range(b)]
x=[int(input()) for i in range(n)]
l1.sort()
l2.sort()
for i in range(n):
    idx1=bisect.bisect_left(l1,x[i])
    if idx1<a:
        p=abs(l1[idx1-1]-x[i])
        q=abs(l1[idx1]-x[i])
        for j in range(2):
            if j==0:
                po=l1[idx1-1]
                idx3=bisect.bisect_left(l2,po)
                if idx3<b:
                    dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
                else:
                    dis=po-l2[-1]
                dis1=p+dis
            else:
                po=l1[idx1]
                idx3=bisect.bisect_left(l2,po)
                if idx3<b:
                    dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
                else:
                    dis=po-l2[-1]
                dis1=min(dis1,q+dis)
    else:
        p=x[i]-l1[-1]
        po=l1[-1]
        idx3=bisect.bisect_left(l2,po)
        if idx3<b:
            dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
            dis1=dis+p
        else:
            dis1=po-l2[-1]+p
    idx2=bisect.bisect_left(l2,x[i])
    if idx2<b:
        p=abs(l2[idx2-1]-x[i])
        q=abs(l2[idx2]-x[i])
        for j in range(2):
            if j==0:
                po=l2[idx2-1]
                idx3=bisect.bisect_left(l1,po)
                if idx3<a:
                    dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
                else:
                    dis=po-l1[-1]
                dis2=p+dis
            else:
                po=l2[idx2]
                idx3=bisect.bisect_left(l1,po)
                if idx3<a:
                    dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
                else:
                    dis=po-l1[-1]
                dis2=min(dis2,q+dis)
    else:
        p=x[i]-l2[-1]
        po=l2[-1]
        idx3=bisect.bisect_left(l1,po)
        if idx3<a:
            dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
            dis2=dis+p
        else:
            dis2=po-l1[-1]+p
    print(min(dis1,dis2))

Solution (Python):
a, b, q = list(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)]

def bs(z, st):
    l = 0
    r = len(st) - 1
    while r - l != 1:
        m = (l + r) // 2
        if st[m] <= z:
            l = m
        else:
            r = m
    return l

for xi in x:
    tmps = []
    tmpt = []
    if xi <= s[0]:
        tmps.append(s[0] - xi)
    elif s[-1] <= xi:
        tmps.append(s[-1] - xi)
    else:
        tmp = bs(xi, s)
        tmps.append(s[tmp] - xi)
        tmps.append(s[tmp + 1] - xi)

    if xi <= t[0]:
        tmpt.append(t[0] - xi)
    elif t[-1] <= xi:
        tmpt.append(t[-1] - xi)
    else:
        tmp = bs(xi, t)
        tmpt.append(t[tmp] - xi)
        tmpt.append(t[tmp + 1] - xi)
    anss = []
    for i in tmps:
        for j in tmpt:
            if i // abs(i) == j // abs(j):
                anss.append(max(abs(i), abs(j)))
            else:
                maxij = max(abs(i), abs(j)) 
                minij = min(abs(i), abs(j)) 
                anss.append(abs(maxij + minij * 2))
    anss.sort()
    print(anss[0])

Solution (Python):
import bisect

INF = 1 << 64
A, B, Q = map(int, input().split())
S = [int(input()) for a in range(A)]
T = [int(input()) for b in range(B)]

def cost(S, A, x):
    index_s = bisect.bisect_left(S, x)
    ws = INF if index_s == 0 else x - S[index_s - 1]
    es = INF if index_s == A else S[index_s] - x
    return ws, es


for q in range(Q):
    x = int(input())
    ws, es = cost(S, A, x)
    wt, et = cost(T, B, x)

    print(min(max(ws, wt), max(es, et), min(ws, et) + ws + et, min(es, wt) + es + wt))

Solution (Python):
a,b,q=map(int, input().split())
x=[-10**11]+[int(input()) for i in range(a)]+[10**11]
y=[-10**11]+[int(input()) for i in range(b)]+[10**11]
p=[int(input()) for i in range(q)]
import bisect
ans=[]
for i in p:
    xi=bisect.bisect_left(x,i)
    yi=bisect.bisect_left(y,i)
    a1=max(x[xi],y[yi])-i
    a2=x[xi]-i+x[xi]-y[yi-1]
    a3=y[yi]-i+y[yi]-x[xi-1]
    a4=i-x[xi-1]+y[yi]-x[xi-1]
    a5=i-y[yi-1]+x[xi]-y[yi-1]
    a6=abs(min(x[xi-1],y[yi-1])-i)
    ans.append(min(a1,a2,a3,a4,a5,a6))
for i in ans:print(i)

Solution (Python):
import sys
from bisect import bisect_right

def solve():
    def getINearests(ss, ts):
        iNearests = [-1] * len(ss)
        for iS, s in enumerate(ss):
            iT = bisect_right(ts, s)
            if iT == len(ts):
                iNearests[iS] = iT-1
            elif iT == 0:
                iNearests[iS] = 0
            else:
                d1 = abs(s - ts[iT-1])
                d2 = abs(s - ts[iT])
                if d1 <= d2:
                    iNearests[iS] = iT-1
                else:
                    iNearests[iS] = iT
        return iNearests


    A, B, Q = map(int, input().split())
    ss = [int(sys.stdin.readline()) for _ in range(A)]
    ts = [int(sys.stdin.readline()) for _ in range(B)]
    xs = [int(sys.stdin.readline()) for _ in range(Q)]

    iTNearests = getINearests(ss, ts)
    iSNearests = getINearests(ts, ss)

    anss = [0] * Q
    for iX, x in enumerate(xs):
        ans = float('inf')

        
        iS = bisect_right(ss, x)
        if iS > 0:
            cost = abs(x - ss[iS-1])
            cost += abs(ss[iS-1] - ts[iTNearests[iS-1]])
            if cost < ans: ans = cost
        if iS < A:
            cost = abs(x - ss[iS])
            cost += abs(ss[iS] - ts[iTNearests[iS]])
            if cost < ans: ans = cost

        
        iT = bisect_right(ts, x)
        if iT > 0:
            cost = abs(x - ts[iT-1])
            cost += abs(ts[iT-1] - ss[iSNearests[iT-1]])
            if cost < ans: ans = cost
        if iT < B:
            cost = abs(x - ts[iT])
            cost += abs(ts[iT] - ss[iSNearests[iT]])
            if cost < ans: ans = cost

        anss[iX] = ans

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

solve()

Solution (Python):
A,B,Q = list(map(int, input().split()))
s,t,xx=[],[],[]

for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    xx.append(int(input()))
    
import bisect
M = 10000000000000

for x in xx:
    i = bisect.bisect_left(s,x)
    j = bisect.bisect_left(t,x)
    
    if (i > 0) and (j > 0):
        ll = abs(min(s[i-1]-x, t[j-1]-x))
    else:
        ll = M
        
    if (i > 0) and (j < B):
        lr = abs(s[i-1]-x) * 2 + (t[j]-x)
    else:
        lr = M
        
    if (i < A) and (j > 0):
        lr_2 = abs(t[j-1]-x) * 2 + (s[i]-x)
    else:
        lr_2 = M
        
    if (i < A) and (j > 0):
        rl = (s[i]-x) * 2 + abs(t[j-1] - x)
    else:
        rl = M
        
    if (i > 0) and (j < B):
        rl_2 = (t[j]-x) * 2 + abs(s[i-1] - x)
    else:
        rl_2 = M
        
    if (i < A) and (j < B):
        rr = max(s[i]-x,t[j]-x)
    else:
        rr = M

    print(min(ll,lr,rl,rr,lr_2,rl_2))

Solution (Python):
A, B, Q = map(int, input().split())
S = [-10**12]
for _ in range(A):
    S.append(int(input()))
S.append(10**12)
T = [-10**12]
for _ in range(B):
    T.append(int(input()))
T.append(10**12)
X = []
for _ in range(Q):
    X.append(int(input()))

import bisect
for x in X:
    i = bisect.bisect_left(S, x)
    ls, rs = abs(S[i-1]-x), abs(S[i]-x)
    j = bisect.bisect_left(T, x)
    lt, rt = abs(T[j-1]-x), abs(T[j]-x)
    result = min(max(lt, ls), max(rt, rs),
                 rt + 2*ls, ls + 2*rt,
                 rs + 2*lt, lt + 2*rs)
    print(result)

Solution (Python):
import bisect
A,B,Q = map(int, input().split())
SList=[]
TList=[]
for i in range(A):
  SList.append(int(input()))
for i in range(B):
  TList.append(int(input()))


  
for i in range(Q):
  x=int(input())
  kouho=[]
  insert_indexS=bisect.bisect_left(SList,x)
  if not insert_indexS==0:
    kyoriS0 = x-SList[insert_indexS-1]
    insert_indexT=bisect.bisect_left(TList,SList[insert_indexS-1])
    if not insert_indexT==0:
      kyoriS00 = kyoriS0+SList[insert_indexS-1]-TList[insert_indexT-1]
      kouho.append(kyoriS00)
    if not insert_indexT==len(TList):
      kyoriS01 = kyoriS0+TList[insert_indexT]-SList[insert_indexS-1]
      kouho.append(kyoriS01)
  if not insert_indexS == len(SList):
    kyoriS1=SList[insert_indexS]-x
    insert_indexT=bisect.bisect_left(TList,SList[insert_indexS])
    if not insert_indexT==0:
      kyoriS10 = kyoriS1+SList[insert_indexS]-TList[insert_indexT-1]
      kouho.append(kyoriS10)
    if not insert_indexT==len(TList):
      kyoriS11 = kyoriS1+TList[insert_indexT]-SList[insert_indexS]
      kouho.append(kyoriS11)

  insert_indexT=bisect.bisect_left(TList,x)
  if not insert_indexT==0:
    kyoriT0 = x-TList[insert_indexT-1]
    insert_indexS=bisect.bisect_left(SList,TList[insert_indexT-1])
    if not insert_indexS==0:
      kyoriT00 = kyoriT0+TList[insert_indexT-1]-SList[insert_indexS-1]
      kouho.append(kyoriT00)
    if not insert_indexS==len(SList):
      kyoriT01 = kyoriT0+SList[insert_indexS]-TList[insert_indexT-1]
      kouho.append(kyoriT01)
  if not insert_indexT == len(TList):
    kyoriT1=TList[insert_indexT]-x
    insert_indexS=bisect.bisect_left(SList,TList[insert_indexT])
    if not insert_indexS==0:
      kyoriT10 = kyoriT1+TList[insert_indexT]-SList[insert_indexS-1]
      kouho.append(kyoriT10)
    if not insert_indexS==len(SList):
      kyoriT11 = kyoriT1+SList[insert_indexS]-TList[insert_indexT]
      kouho.append(kyoriT11)
  print(min(kouho))

Solution (Python):
A, B, Q = [int(_) for _ in input().split()]
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
Q = [int(input()) for i in range(Q)]



def calc_m(xs):
    return [(a + b) / 2 for a, b in zip(xs, xs[1:])]

sm = calc_m(S)
tm = calc_m(T)




import bisect

def calc_nearest_0(xs, y, ym):
    result = []
    i = 0
    for x in xs:
        while i < len(ym) and x >= ym[i]:
            i += 1
        result.append(y[i])
    return result

def calc_nearest(xs, y, ym):
    result = []
    for x in xs:
        i = bisect.bisect(ym, x)
        result.append(y[i])
    return result

sn = calc_nearest(S, T, tm)
tn = calc_nearest(T, S, sm)




def solve(q, S, T, sn, tn):
    nsp = bisect.bisect(S, q)
    ntp = bisect.bisect(T, q)

    rs = []

    if 0 <= nsp < len(S):
        rs.append(abs(S[nsp] - q) + abs(S[nsp] - sn[nsp]))
    nsp -= 1
    if 0 <= nsp < len(S):
        rs.append(abs(S[nsp] - q) + abs(S[nsp] - sn[nsp]))

    if 0 <= ntp < len(T):
        rs.append(abs(T[ntp] - q) + abs(T[ntp] - tn[ntp]))
    ntp -= 1
    if 0 <= ntp < len(T):
        rs.append(abs(T[ntp] - q) + abs(T[ntp] - tn[ntp]))

    
    return min(rs)

for q in Q:
    print(solve(q, S, T, sn, tn))

Solution (Python):

from itertools import product
from bisect import bisect, bisect_left
def inpl(): return list(map(int, input().split()))
A, B, Q = inpl()
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:
    ans = 1e12
    ls, lt = S[max(0, bisect(S, x)-1)], T[max(0, bisect(T, x)-1)]
    rs, rt = S[min(A-1, bisect_left(S, x))], T[min(B-1, bisect_left(T, x))]
    for s in [ls, rs]:
        for t in [lt, rt]:
            if s < t:
                a, b = s, t
            else:
                a, b = t, s

            if x < a:
                ans = min(ans, b-x)
            elif b < x:
                ans = min(ans, x-a)
            else:
                ans = min(ans, 2*(x-a) + (b-x), (x-a) + 2*(b-x))
    print(ans)

Solution (Python):
import bisect
A, B, Q = map(int, input().split())
sl = []
tl = []
xl = []
for i in range(A):
    sl.append(int(input()))
for i in range(B):
    tl.append(int(input()))
for i in range(Q):
    xl.append(int(input()))
sl.sort()
tl.sort()
def func(i):
    slm = bisect.bisect_right(sl, xl[i])
    try:
        sleft = xl[i] - sl[slm-1]
        assert sleft >= 0
    except:
        sleft = float('inf')
    
    try:
        sright = sl[slm] - xl[i]
    except:
        sright = float('inf')
    
    tlm = bisect.bisect_right(tl, xl[i])
    try:
        tleft = xl[i] - tl[tlm-1]
        assert tleft >= 0
    except:
        tleft = float('inf')
        
    try:
        tright = tl[tlm] - xl[i]
    except:
        tright = float('inf')
    
    ll = max(sleft, tleft)
    lr = sleft + tright + min(sleft, tright)
    rl = sright + tleft + min(sright, tleft)
    rr = max(sright, tright)
    
    print(min(ll, lr, rl, rr))

for i in range(Q):
    func(i)

Solution (Python):
import sys
from bisect import bisect_right

def solve():
    def getINearests(ss, ts):
        iNearests = [-1] * len(ss)
        for iS, s in enumerate(ss):
            iT = bisect_right(ts, s)
            if iT == len(ts):
                iNearests[iS] = iT-1
            elif iT == 0:
                iNearests[iS] = 0
            else:
                d1 = abs(s - ts[iT-1])
                d2 = abs(s - ts[iT])
                if d1 <= d2:
                    iNearests[iS] = iT-1
                else:
                    iNearests[iS] = iT
        return iNearests


    A, B, Q = map(int, input().split())
    ss = [int(sys.stdin.readline()) for _ in range(A)]
    ts = [int(sys.stdin.readline()) for _ in range(B)]
    xs = [int(sys.stdin.readline()) for _ in range(Q)]

    iTNearests = getINearests(ss, ts)
    iSNearests = getINearests(ts, ss)

    anss = [0] * Q
    for iX, x in enumerate(xs):
        ans = float('inf')

        
        iS = bisect_right(ss, x)
        if iS > 0:
            cost = abs(x - ss[iS-1])
            cost += abs(ss[iS-1] - ts[iTNearests[iS-1]])
            if cost < ans: ans = cost
        if iS < A:
            cost = abs(x - ss[iS])
            cost += abs(ss[iS] - ts[iTNearests[iS]])
            if cost < ans: ans = cost

        
        iT = bisect_right(ts, x)
        if iT > 0:
            cost = abs(x - ts[iT-1])
            cost += abs(ts[iT-1] - ss[iSNearests[iT-1]])
            if cost < ans: ans = cost
        if iT < B:
            cost = abs(x - ts[iT])
            cost += abs(ts[iT] - ss[iSNearests[iT]])
            if cost < ans: ans = cost

        anss[iX] = ans

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

solve()

Solution (Python):
import bisect
import sys
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)]

INF = float('inf')
for _ in range(q):
    x = int(input())
    if x > s[a-1]:
        sl = x - s[a-1]
        sr = INF
    elif x < s[0]:
        sl = INF
        sr = s[0] - x
    else:
        si = bisect.bisect_right(s, x)
        sl = x - s[si-1]
        sr = s[si] - x

    if x > t[b-1]:
        tl = x - t[b-1]
        tr = INF
    elif x < t[0]:
        tl = INF
        tr = t[0] - x
    else:
        ti = bisect.bisect_right(t, x)
        tl = x - t[ti-1]
        tr = t[ti] - x

    ans = min(max(sl, tl), max(sr, tr))

    ans = min(ans, sr*2+tl)
    ans = min(ans, sr+tl*2)
    ans = min(ans, sl*2+tr)
    ans = min(ans, sl+tr*2)

    print(ans)

Solution (Python):
import bisect
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()))

for _ in range(Q):
    x = int(input())
    sr = bisect.bisect_right(S, x)
    tr = bisect.bisect_right(T, x)
    tmpS = []
    tmpT = []
    if sr == A:
        tmpS.append(S[A-1])
    elif sr == 0:
        tmpS.append(S[sr])
    else:
        tmpS.append(S[sr])
        tmpS.append(S[sr-1])
    if tr == B:
        tmpT.append(T[B-1])
    elif tr == 0:
        tmpT.append(T[tr])
    else:
        tmpT.append(T[tr])
        tmpT.append(T[tr-1])
    ans = 10**16
    for s in tmpS:
        tmp = abs(x-s)
        tmp1 = 10**16
        for elemT in tmpT:
            tmp1 = min(tmp1, abs(elemT-s))
        ans = min(ans, tmp+tmp1)
    for t in tmpT:
        tmp = abs(x-t)
        tmp1 = 10**16
        for elemS in tmpS:
            tmp1 = min(tmp1, abs(elemS-t))
        ans = min(ans, tmp+tmp1)
    print(ans)

Solution (Python):
import bisect
A, B, Q = [int(_) for _ in 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_sl = bisect.bisect_left(S, x)
    i_tl = bisect.bisect_left(T, x)
    D_sr = D_sl = D_tl = D_tr = float('inf')
    if i_sl < A:
        D_sl = S[i_sl] - x
    if i_tl < B:
        D_tl = T[i_tl] - x
    if i_sl:
        D_sr = x - S[i_sl - 1]
    if i_tl:
        D_tr = x - T[i_tl - 1]
    print(
        min([
            max(D_sl, D_tl),
            max(D_sr, D_tr),
            min(D_sl, D_tr) + D_tr + D_sl,
            min(D_tl, D_sr) + D_sr + D_tl
        ]))

Solution (Python):
def main():
  A, B, Q = list(map(int, input().split()))
  D = []
  q = []
  for i in range(A):
    D.append([int(input()), 1])
  for i in range(B):
    D.append([int(input()), 2])
  for i in range(Q):
    t = int(input())
    D.append([t, 0, i])
    q.append(t)
  
  D.sort()

  x  = [[0, 0, 0, 0] for i in range(Q)] 
  a = 0
  b = 0
  for i in D:
    if i[1] == 1:
      a = i[0]
    elif i[1] == 2:
      b = i[0]
    else:
      x[i[2]][0] = a
      x[i[2]][2] = b
  a = 0
  b = 0
  for i in D[::-1]:
    if i[1] == 1:
      a = i[0]
    elif i[1] == 2:
      b = i[0]
    else:
      x[i[2]][1] = a
      x[i[2]][3] = b
  
  for i in range(Q):
    mi = 1e11
    for j in range(2):
      if x[i][j] == 0:
        continue
      t = abs(q[i] - x[i][j])
      for k in range(2, 4):
        if x[i][k] == 0:
          continue
        tt = t + abs(x[i][j] - x[i][k])
        if tt < mi:
          mi = tt
    for j in range(2, 4):
      if x[i][j] == 0:
        continue
      t = abs(q[i] - x[i][j])
      for k in range(2):
        if x[i][k] == 0:
          continue
        tt = t + abs(x[i][j] - x[i][k])
        if tt < mi:
          mi = tt
    print(mi)
      
main()

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

for i in range(Q):
    x = X[i]
    s_index = bisect.bisect_left(s, x)
    t_index = bisect.bisect_left(t, x)

    if s_index == 0:
        s_left = -float("inf")
    else:
        s_left = s[s_index - 1]
    
    if s_index == A:
        s_right = float("inf")
    else:
        s_right = s[s_index]
        
    if t_index == 0:
        t_left = -float("inf")
    else:
        t_left = t[t_index - 1]

    if t_index == B:
        t_right = float("inf")
    else:
        t_right = t[t_index]

    ans = float("inf")

    if s_index != 0 and t_index != 0:
        ans = x - min(s_left, t_left)

    if s_index != 0 and t_index != B:
        temp = min(x - s_left, t_right - x)
        ans = min(ans, (x - s_left) + (t_right - x) + temp)

    if s_index != A and t_index != 0:
        temp = min(x - t_left, s_right - x)
        ans = min(ans, (x - t_left) + (s_right - x) + temp)

    if t_index != B and s_index != A:
        ans = min(ans, max(s_right, t_right) - x)

    print(ans)

Solution (Python):
import bisect
A, B, Q = [int(i) for i in input().split()]
S=list()
T=list()
X=list()
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
for i in range(Q):
    X.append(int(input()))

for Q in X:
    SS=list()
    TT=list()
    i = bisect.bisect_left(S,Q)
    
    if i>0:
        SS.append(S[i-1])
    if i<A:
        SS.append(S[i])
    i = bisect.bisect_left(T, Q)
    
    if i > 0:
        TT.append(T[i - 1])
    if i<B:
        TT.append(T[i])
    minlen=9999999999999
    
    for i in SS:
        for j in TT:
            len=min(abs(Q-i)+abs(i-j),abs(Q-j)+abs(i-j))
            minlen=min(minlen,len)
    print(minlen)

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 xx in x:
    
    ps_r = 0
    s_r = bisect_left(s, xx)
    if s_r < A:
        ps_r += s[s_r] - xx
        t_r = bisect_left(t, s[s_r])
        t_l = t_r - 1

        b = 0
        if t_r < B:
            b = t[t_r] - s[s_r]
        if 0 <= t_l < B:
            if b == 0:
                b = s[s_r] - t[t_l]
            else:
                b = min(b, s[s_r] - t[t_l])
        ps_r += b
    else:
        ps_r = 10 ** 12

    ps_l = 0
    s_l = s_r - 1
    if 0 <= s_l < A:
        ps_l += xx - s[s_l]
        t_r = bisect_left(t, s[s_l])
        t_l = t_r - 1

        b = 0
        if t_r < B:
            b = t[t_r] - s[s_l]
        if 0 <= t_l < B:
            if b == 0:
                b = s[s_l] - t[t_l]
            else:
                b = min(b, s[s_l] - t[t_l])
        ps_l += b
    else:
        ps_l = 10 ** 12

    
    pt_r = 0
    t_r = bisect_left(t, xx)
    if t_r < B:
        pt_r += t[t_r] - xx
        s_r = bisect_left(s, t[t_r])
        s_l = s_r - 1

        b = 0
        if s_r < A:
            b = s[s_r] - t[t_r]
        if 0 <= s_l < A:
            if b == 0:
                b = t[t_r] - s[s_l]
            else:
                b = min(b, t[t_r] - s[s_l])
        pt_r += b
    else:
        pt_r = 10 ** 12

    pt_l = 0
    t_l = t_r - 1
    if 0 <= t_l < B:
        pt_l += xx - t[t_l]
        s_r = bisect_left(s, t[t_l])
        s_l = s_r - 1

        b = 0
        if s_r < A:
            b = s[s_r] - t[t_l]
        if 0 <= s_l < A:
            if b == 0:
                b = t[t_l] - s[s_l]
            else:
                b = min(b, t[t_l] - s[s_l])
        pt_l += b
    else:
        pt_l = 10 ** 12

    
    print(min(ps_r, ps_l, pt_r, pt_l))

Solution (Python):
import bisect as bi
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):
    c = []
    d = []
    x = int(input())
    tmp = bi.bisect_left(s, x)
    if tmp != a : c.append(s[tmp])
    if tmp != 0 : c.append(s[tmp-1])
    tmp = bi.bisect_left(t, x)
    if tmp != b : d.append(t[tmp])
    if tmp != 0 : d.append(t[tmp-1])
    ans = 2**64
    for e in c:
        for f in d:
            g = abs(x-e)
            h = abs(x-f)
            if (x-e)*(x-f) > 0:
                ans = min(ans, max(g, h))
            else:
                if g > h : g,h = h,g
                ans = min(ans, 2*g+h)
    print(ans)