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 = [int(_) for _ in input().split()]
s_l = [-float('inf')]
t_l = [-float('inf')]
x_l = []
for i in range(a):
    s_l.append(int(input()))
s_l.append(float('inf'))
for i in range(b):
    t_l.append(int(input()))
t_l.append(float('inf'))
for i in range(q):
    x_l.append(int(input()))
for i in range(q):
    x = x_l[i]
    s1_id = bisect.bisect_left(s_l, x) - 1 
    s2_id = s1_id + 1
    s1 = s_l[s1_id]
    s2 = s_l[s2_id]
    t1_id = bisect.bisect_left(t_l, x) - 1 
    t2_id = t1_id + 1
    t1 = t_l[t1_id]
    t2 = t_l[t2_id]
    res = float('inf')
    for s in [s1,s2]:
        for t in [t1,t2]:
            d1, d2 = abs(s-x) + abs(t-s), abs(t-x) + abs(s-t)
            res = min(res, d1, d2)
    print(res)

Solution (Python):

from bisect import bisect_left,bisect_right
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

a,b,q = map(int, input().split())
INF = 10**11
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 range(q):
    idx_s = bisect_right(S,X[i])
    idx_t = bisect_right(T,X[i])
    ans = INF
    for s in [S[idx_s-1],S[idx_s]]:
        for t in [T[idx_t-1],T[idx_t]]:
            d1,d2 = abs(s-X[i])+abs(s-t),abs(t-X[i])+abs(t-s)
            ans = min(ans,d1,d2)
    print(ans)

Solution (Python):

import sys
from bisect import bisect_left

def solve(A: int, B: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"):
    for qi in range(Q):
        start = x[qi]
        nearest_s = bisect_left(s,start)
        nearest_t = bisect_left(t,start)

        left_s = s[nearest_s-1] if nearest_s != 0 else -10**12
        right_s = s[nearest_s] if nearest_s != A else 10**12
        left_t = t[nearest_t-1] if nearest_t != 0 else -10**12
        right_t = t[nearest_t] if nearest_t != B else 10**12
                
        leftleftdist = start - min(left_s,left_t) 
        rightrightdist = max(right_s,right_t)-start 
        leftrightdist = min(start-left_s+right_t-left_s, start-left_t+right_s-left_t)
        rightleftdist = min(right_t-start+right_t-left_s, right_s-start+right_s-left_t)
        answer = min(leftleftdist,rightrightdist,rightleftdist,leftrightdist)
    
        print(answer)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    Q = int(next(tokens))  
    s = [int(next(tokens)) for _ in range(A)]  
    t = [int(next(tokens)) for _ in range(B)]  
    x = [int(next(tokens)) for _ in range(Q)]  
    solve(A, B, Q, s, t, x)

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left
INF = 10**11

a,b,q = map(int,input().split())
ss = [int(input()) for _ in range(a)] 
tt = [int(input()) for _ in range(b)] 
xx = [int(input()) for _ in range(q)] 

ss = sorted(ss)
tt = sorted(tt)

for x in xx:
  left,middle,right = INF,INF,INF
  sp = bisect_left(ss,x)
  tp = bisect_left(tt,x)
  
  l_sp, r_sp = INF, INF
  l_tp, r_tp = INF, INF
      
  if 0 < sp:
    l_sp = sp - 1
  if 0 < tp:
    l_tp = tp - 1
  if sp < a:
    r_sp = sp
  if tp < b:
    r_tp = tp
    
  
  
  left,right,middle_ts, middle_st = INF,INF,INF,INF
  
  if (l_sp != INF) and (l_tp != INF):
    left = abs(x - min(ss[l_sp],tt[l_tp]))    

  if (r_sp != INF) and (r_tp != INF):
    right = abs(max(ss[r_sp],tt[r_tp]) - x)      

  if (l_tp != INF) and (r_sp != INF):
    middle_ts = min(abs(tt[l_tp] - x), abs(ss[r_sp] - x)) + ss[r_sp] - tt[l_tp]
  
  if (l_sp != INF) and (r_tp != INF):
    middle_st = min(abs(ss[l_sp] - x), abs(tt[r_tp] - x)) + tt[r_tp] - ss[l_sp]
    
  print(min(left,right,middle_ts,middle_st))

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

s = [0]*a
t = [0]*b
x = [0]*q
for i in range(a):
    s[i] = int(input())
for i in range(b):
    t[i] = int(input())
for i in range(q):
    x[i] = int(input())
s.sort()
t.sort()

INT_MAX = 10**11



import bisect
def func(xi):
    i = bisect.bisect_left(s,xi)
    s_right = INT_MAX
    if i < len(s):
        s_right = s[i] - xi
    s_left = INT_MAX
    if i != 0:
        s_left = xi - s[i-1]
    j = bisect.bisect_left(t,xi)
    t_right = INT_MAX
    if j < len(t):
        t_right = t[j] - xi
    t_left = INT_MAX
    if j != 0:
        t_left = xi - t[j-1]
    
    
    return min(max(s_right,t_right), max(s_left,t_left), min(s_left,t_right)*2+max(s_left,t_right), min(s_right,t_left)*2+max(s_right,t_left))

ans = map(func, x)
print("\n".join(map(str,ans)))

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

sl = [-float("inf")]
for _ in range(a):
    sl.append(int(input()))
sl.append(float("inf"))
tl = [-float("inf")]
for _ in range(b):
    tl.append(int(input()))
tl.append(float("inf"))
    
import bisect


for _ in range(q):
    x = int(input())
    index1 = bisect.bisect(sl, x) 
    index2 = bisect.bisect(tl, x)
    rs = abs(sl[index1] - x) 
    ls = abs(sl[index1-1] - x)
    rt = abs(tl[index2] - x)
    lt = abs(tl[index2-1] - x)
    ansl = []
    ansl.append(max(rs,rt))
    ansl.append(max(ls,lt))
    ansl.append(2*min(rs,lt)+max(rs,lt))
    ansl.append(2*min(ls,rt)+max(ls,rt))
    print(min(ansl))

Solution (Python):

import sys
input=sys.stdin.readline
import bisect
a,b,q=map(int,input().split())
A=[]
lists1=[int(input()) for i in range(a)]
lists2=[int(input())for i in range(b)]
lists1.insert(0,-10**12)
lists1.insert(1,-10**11)
lists2.insert(0,-10**12)
lists2.insert(1,-10**11)                  
lists1.append(10**11)
lists1.append(10**12)
lists2.append(10**11)
lists2.append(10**12)
for i in range(q):
    x=int(input())
    index1=bisect.bisect_right(lists1,x)
    index2=bisect.bisect_right(lists2,x)
    dis1=[]
    dis2=[]
    dis1.append(lists1[index1-2])
    dis1.append(lists1[index1-1])
    dis1.append(lists1[index1])
    dis1.append(lists1[index1+1])
    dis2.append(lists2[index2-2])
    dis2.append(lists2[index2-1])
    dis2.append(lists2[index2])
    dis2.append(lists2[index2+1])
    distance=[]
    x1=abs(x-dis1[1])
    mini1=10**12
    for i in range(4):
        mini1=min(mini1,abs(dis1[1]-dis2[i]))
    x2=abs(x-dis1[2])
    mini2=10**12
    for i in range(4):
        mini2=min(mini2,abs(dis1[2]-dis2[i]))
    x3=abs(x-dis2[1])
    mini3=10**12
    for i in range(4):
        mini3=min(mini3,abs(dis2[1]-dis1[i]))
    x4=abs(x-dis2[2])
    mini4=10**12
    for i in range(4):
        mini4=min(mini4,abs(dis2[2]-dis1[i]))
    A.append(min(x1+mini1,x2+mini2,x3+mini3,x4+mini4))
for k in A:
    print(k)

Solution (Python):
import bisect
a,b,q=map(int,input().split())
ls=[-10**11]
lg=[-10**11]
for i in range(a):
  ls.append(int(input()))
for i in range(b):
  lg.append(int(input()))
ls.append(10**11)
lg.append(10**11)
ls=sorted(ls)
lg=sorted(lg)
for i in range(q):
  x=int(input())
  i1=bisect.bisect_left(ls,x)
  i2=bisect.bisect_left(lg,x)
  l1=[i1-1,i1]
  l2=[i2-1,i2]
  ans1=10**12
  ans2=10**12
  for k in l1:
    for j in l2:
      ans3=abs(x-ls[k])+abs(ls[k]-lg[j])
      if ans3<ans1:
        ans1=ans3
      ans4=abs(x-lg[j])+abs(ls[k]-lg[j])
      if ans4<ans2:
        ans2=ans4
  print(min(ans1,ans2))

Solution (Python):
import bisect
a,b,q=map(int,input().split())
ls=[-10**11]
lg=[-10**11]
for i in range(a):
  ls.append(int(input()))
for i in range(b):
  lg.append(int(input()))
ls.append(10**11)
lg.append(10**11)
ls=sorted(ls)
lg=sorted(lg)
for i in range(q):
  x=int(input())
  i1=bisect.bisect_left(ls,x)
  i2=bisect.bisect_left(lg,x)
  d1=x-ls[i1-1]
  d2=ls[i1]-x
  d3=x-lg[i2-1]
  d4=lg[i2]-x
  d5=abs(ls[i1-1]-lg[i2-1])
  d6=abs(ls[i1]-lg[i2-1])
  d7=abs(ls[i1-1]-lg[i2])
  d8=abs(ls[i1]-lg[i2])
  ans1=d1+d5
  ans2=d1+d7
  ans3=d2+d6
  ans4=d2+d8
  ans5=d3+d5
  ans6=d3+d6
  ans7=d4+d7
  ans8=d4+d8
  print(min(ans1,ans2,ans3,ans4,ans5,ans6,ans7,ans8))

Solution (Python):
import bisect
from sys import stdin
A,B,Q = [int(x) for x in stdin.readline().rstrip().split()]
INF = 10**18
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for _ in range(Q):
    x = int(stdin.readline().rstrip())
    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):
import bisect
a,b,q=map(int,input().split())
ff=float("inf")
s=[-ff]
t=[-ff]
for i in range(a):
    v=int(input())
    s.append(v)
for i in range(b):
    w=int(input())
    t.append(w)
s.append(ff)
t.append(ff)
s.sort()
t.sort()
for i in range(q):
    x=int(input())
    a=bisect.bisect_right(s,x)
    b=bisect.bisect_right(t,x)
    res=ff
    for y in [s[a],s[a-1]]:
        for w in [t[b],t[b-1]]:
            d1,d2=abs(y-x)+abs(w-y),abs(w-x)+abs(w-y)
            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 _ in range(a)]+[INF]
t=[-INF]+[int(input()) for _ in range(b)]+[INF]

def kai(x):
    res = INF
    s1 = bisect.bisect_right(s,x)
    ls = [s1,s1 - 1]
    t1 = bisect.bisect_right(t,x)
    lt = [t1,t1 - 1]
    for ss in ls:
        for st in lt:
            res = min(res,abs(x-s[ss])+abs(s[ss]-t[st]),abs(x-t[st])+abs(s[ss]-t[st]))
    return res

lx = [int(input()) for _ in range(q)]
for x in lx:
    print(kai(x))

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

INF = float('INF')

A, B, Q = map(int, readline().split())
stx = list(map(int, read().split()))
S = [-INF] + stx[:A] + [INF]
T = [-INF] + stx[A:-Q] + [INF]
X = stx[-Q:]

for x in X:
    S_idx = bisect.bisect_left(S, x)
    T_idx = bisect.bisect_left(T, x)

    s_left, s_right = S[S_idx-1], S[S_idx]
    t_left, t_right = T[T_idx-1], T[T_idx]
    pathA = x - min(s_left, t_left)
    pathB = max(s_right, t_right) - x
    pathC = min(x-s_left, t_right-x) + t_right - s_left
    pathD = min(x-t_left, s_right-x) + s_right - t_left
    print(min(pathA, pathB, pathC, pathD))

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

INF = float('INF')

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:
    S_idx = bisect.bisect_left(S, x)
    T_idx = bisect.bisect_left(T, x)

    s_left, s_right = S[S_idx-1], S[S_idx]
    t_left, t_right = T[T_idx-1], T[T_idx]
    pathA = x - min(s_left, t_left)
    pathB = max(s_right, t_right) - x
    pathC = min(x-s_left, t_right-x) + t_right - s_left
    pathD = min(x-t_left, s_right-x) + s_right - t_left
    ans.append(str(min(pathA, pathB, pathC, pathD)))

print('\n'.join(ans))

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

INF = float('INF')

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 x in X:
    S_idx = bisect.bisect_left(S, x)
    T_idx = bisect.bisect_left(T, x)

    s_left, s_right = S[S_idx-1], S[S_idx]
    t_left, t_right = T[T_idx-1], T[T_idx]
    pathA = x - min(s_left, t_left)
    pathB = max(s_right, t_right) - x
    pathC = min(x-s_left, t_right-x) + t_right - s_left
    pathD = min(x-t_left, s_right-x) + s_right - t_left
    print(min(pathA, pathB, pathC, pathD))

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

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

S.sort()
T.sort()

for x in X:
    S_idx = bisect.bisect_left(S, x)
    T_idx = bisect.bisect_left(T, x)

    s_left, s_right = S[S_idx-1], S[S_idx]
    t_left, t_right = T[T_idx-1], T[T_idx]
    pathA = x - min(s_left, t_left)
    pathB = max(s_right, t_right) - x
    pathC = min(x-s_left, t_right-x) + t_right - s_left
    pathD = min(x-t_left, s_right-x) + s_right - t_left
    print(min(pathA, pathB, pathC, pathD))

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

INF = float("inf")
"""
SやTについて、ある地点をスタート地点に選んだ時に取りうる行動は、SとTそれぞれについて現在値の東西のどちらに向かうかと、SとTどちらを先に行くか。
なのでそれら全通りについてためせばおK
"""
for x in X:
    sw = se = 0
    tw = te = 0
    idx_s = bisect.bisect_left(S, x)
    idx_t = bisect.bisect_left(T, x)

    if idx_s == len(S):
        se = INF
        sw = S[-1]
    elif idx_s == 0:
        sw = INF
        se = S[0]
    else:
        se = S[idx_s]
        sw = S[idx_s - 1]

    if idx_t == len(T):
        te = INF
        tw = T[-1]
    elif idx_t == 0:
        tw = INF
        te = T[0]
    else:
        te = T[idx_t]
        tw = T[idx_t - 1]
        
    a = abs(x-se) + abs(se-tw) if se != INF or tw != INF else 5* 10**10
    b = abs(x-tw) + abs(tw-se) if se != INF or tw != INF else 5* 10**10
    c = abs(x-sw) + abs(sw-te) if sw != INF or te != INF else 5* 10**10
    d = abs(x-te) + abs(te-sw) if sw != INF or te != INF else 5* 10**10
    print(min(
        a, 
        b, 
        c, 
        d,
        max(abs(x-sw), abs(x-tw)),
        max(abs(x-se), abs(x-te))))
    """
    print("--------")
    print(se,tw)
    print((
        abs(x-se) + abs(se-tw), 
        abs(x-tw) + abs(tw-se), 
        abs(x-sw) + abs(sw-te), 
        abs(x-te) + abs(te-sw),
        max(abs(x-sw), abs(x-tw)),
        max(abs(x-se), abs(x-te))))
    """

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


def main():
    A, B, Q = map(int, readline().split())
    stx = list(map(int, read().split()))
    s = stx[:A]; t = stx[A:-Q]; x = stx[-Q:]
    s = [-10 ** 10] + s + [10 ** 11]
    t = [-10 ** 10] + t + [10 ** 11]

    for i in x:
        ax = bisect_left(s, i)
        bx = bisect_left(t, i)
        pp = max(s[ax], t[bx]) - i
        mm = i - min(s[ax - 1], t[bx - 1])
        pm = min(s[ax] * 2 - i - t[bx - 1], t[bx] * 2 - i - s[ax - 1])
        mp = min(i - s[ax - 1] * 2 + t[bx], i - t[bx - 1] * 2 + s[ax])
        print(min(pp, mm, pm, mp))


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

A, B, Q = map(int, readline().split())
stx = list(map(int, read().split()))
s = stx[:A]; t = stx[A:-Q]; x = stx[-Q:]
s = [-10 ** 10] + s + [10 ** 11]
t = [-10 ** 10] + t + [10 ** 11]

for i in x:
    ax = bisect_left(s, i)
    bx = bisect_left(t, i)
    pp = max(s[ax], t[bx]) - i
    mm = i - min(s[ax - 1], t[bx - 1])
    pm = min(s[ax] * 2 - i - t[bx - 1], t[bx] * 2 - i - s[ax - 1])
    mp = min(i - s[ax - 1] * 2 + t[bx], i - t[bx - 1] * 2 + s[ax])
    print(min(pp, mm, pm, mp))

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)]
S = [-10 ** 12] + S + [10 ** 12]
T = [-10 ** 12] + T + [10 ** 12]
for x in X:
    si = bisect.bisect_right(S, x)
    ti = bisect.bisect_right(T, x)
    Ls, Rs = x - S[si - 1], -x + S[si]
    Lt, Rt = x - T[ti - 1], -x + T[ti]
    ans = min(Ls * 2 + Rt, Ls + Rt * 2, Lt * 2 + Rs,
              Lt + Rs * 2, max(Ls, Lt), max(Rs, Rt))
    print(ans)

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

Solution (Python):


import bisect

A, B, Q = map(int, input().split())
s,t,x = [],[],[]
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
for _ in range(Q):
    x.append(int(input()))
    
for start in x:
    ans_list = []
    
    tmp_idx = bisect.bisect_left(s,start)
    if start < s[0]:
        west_s = -1
    else:
        west_s = s[tmp_idx-1]
    if s[-1] < start:
        east_s = -1
    else:
        east_s = s[tmp_idx]
    
    tmp_idx = bisect.bisect_left(t,start)
    if start < t[0]:
        west_t = -1
    else:
        west_t = t[tmp_idx-1]
    if t[-1] < start:
        east_t = -1
    else:
        east_t = t[tmp_idx]
        
    if east_s != -1 and east_t != -1: 
        ans_list.append(abs(east_s - east_t) + (min(east_s, east_t) - start))
    if west_s != -1 and west_t != -1: 
        ans_list.append(abs(west_s - west_t) + (start - max(west_s, west_t)))
    if east_s != -1 and west_t != -1: 
        ans_list.append(east_s - start + (east_s - west_t))
        ans_list.append(start - west_t + (east_s - west_t))
    if west_s != -1 and east_t != -1: 
        ans_list.append(east_t - start + (east_t - west_s))
        ans_list.append(start - west_s + (east_t - west_s))

    print(min(ans_list))

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
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)]
s=[-float('inf')]+s+[float('inf')]
t=[-float('inf')]+t+[float('inf')]

for i in range(q):
    index1=bisect.bisect_right(s, x[i]) 
    index2=bisect.bisect_right(t, x[i])  
    v1=abs(x[i]-s[index1-1])
    v2=abs(x[i]-s[index1])
    w1=abs(x[i]-t[index2-1])
    w2=abs(x[i]-t[index2])
    
    print(min(2*v1+w2,2*v2+w1,2*w1+v2,2*w2+v1,v1+abs(w1-v1),w1+abs(w1-v1),v2+abs(w2-v2),w2+abs(w2-v2)))

Solution (Python):
import bisect
A, B, Q = map(int,input().split())
INF = 10**11
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,bisect_right
a,b,q=map(int,input().split())
INF=float("inf")
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())
  b,c=bisect_right(s,x),bisect_right(t,x)
  a=INF
  for i in [s[b-1],s[b]]:
    for j in [t[c-1],t[c]]:
      a=min(a,abs(x-i)+abs(i-j),abs(x-j)+abs(j-i))
  print(a)

Solution (Python):
from bisect import bisect_right
INF = 10 ** 18
A, B, Q = [int(x) for x in 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 x in X:
    ans = INF
    b = bisect_right(S, x)
    d = bisect_right(T, x)
    dist = []
    for s in [S[b-1], S[b]]:
        for t in [T[d-1], T[d]]:
            ans = min(ans, abs(x-s)+abs(s-t), abs(x-t)+abs(t-s))
    print(ans)

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 _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]

ret = []
for _ in range(Q) :
    x = int(input())
    spos = bisect_left(s, x)
    tpos = bisect_left(t, x)
    ls, rs = s[spos-1], s[spos]
    lt, rt = t[tpos-1], t[tpos]
    if ls < lt :
        if rs < rt :
            ret.append(min(x-ls, rt-x, min(x-lt, rs-x)+rs-lt))
        else :
            ret.append(min(x-ls, rs-x))
    else :
        if rs < rt :
            ret.append(min(x-lt, rt-x))
        else :
            ret.append(min(x-lt, rs-x, min(x-ls, rt-x)+rt-ls))
    
for r in ret :
    print(r)

Solution (Python):
from bisect import bisect_left, bisect_right

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

S.sort()
T.sort()

ans = []
for _ in range(Q):
    x = int(input())

    midS = bisect_right(S, x)
    midT = bisect_right(T, x)

    lS = S[midS - 1]
    rS = S[midS]
    lT = T[midT - 1]
    rT = T[midT]

    dist = min(
        max(rS, rT) - x,
        x - min(lS, lT),
        abs(rS - lT) + min(rS - x, x - lT),
        abs(rT - lS) + min(rT - x, x - lS)
    )

    ans.append(dist)

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

Solution (Python):
import bisect

a,b,q=map(int, input().split())

s=[-2*10**10]+[int(input()) for _ in range(a)]+[2*10**10]
t=[-2*10**10]+[int(input()) for _ in range(b)]+[2*10**10]
x=[int(input()) for _ in range(q)]

for i in range(q):
  j = bisect.bisect_left(s,x[i])
  al = x[i] - s[j-1]
  ar = s[j] - x[i]
  j = bisect.bisect_left(t,x[i])
  bl = x[i] - t[j-1]
  br = t[j] - x[i]
  l = min(max(al, bl), max(ar, br), 2*al+br, 2*ar+bl, 2*bl+ar, 2*br+al)
  print(l)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string



INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


a, b, q = LI()
s_list = [-INF] + IR(a) + [INF]
t_list = [-INF] + IR(b) + [INF]
x_list = IR(q)


for x in x_list:
    right_shrine_index = bisect_left(s_list, x)
    left_shrine_index = right_shrine_index - 1
    right_temple_index = bisect_left(t_list, x)
    left_temple_index = right_temple_index - 1
    ret = INF
    ret = min(ret, max(s_list[right_shrine_index], t_list[right_temple_index]) - x)
    ret = min(ret, x - min(s_list[left_shrine_index], t_list[left_temple_index]))
    ret = min(ret, min(s_list[right_shrine_index] - x, x - t_list[left_temple_index]) * 2 + max(s_list[right_shrine_index] - x, x - t_list[left_temple_index]))
    ret = min(ret, min(t_list[right_temple_index] - x, x - s_list[left_shrine_index]) * 2 + max(t_list[right_temple_index] - x, x - s_list[left_shrine_index]))
    print(ret)

Solution (Python):
def nibun(D, x):
    return bisect.bisect_left(D, x)  

import bisect
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)
A,B,Q =LI()
s = [0]*A
t = [0]*B
x = [0]*Q
for i in range(A):
    s[i] = I()
for i in range(B):
    t[i] = I()
for i in range(Q):
    x[i] = I()

ansy = int(0)
for i in range(Q):
    Scandi = [inf] * 2
    Tcandi = [inf] * 2
    xc = x[i]
    k = nibun(s,xc)
    if k>0:
        Scandi[0] = xc - s[k-1]
    if k<A:
        Scandi[1] = s[k] - xc

    k = nibun(t,x[i])
    if k>0:
        Tcandi[0] = xc - t[k-1]
    if k<B:
        Tcandi[1] = t[k] - xc

    anscandi = []
    for i in range(2):
        anst = Scandi[i]*2 + Tcandi[i-1]
        anscandi.append(anst)
        anst = Scandi[i] + Tcandi[i-1]*2
        anscandi.append(anst)
    anst = max(Scandi[0],Tcandi[0])
    anscandi.append(anst)
    anst = max(Scandi[1],Tcandi[1])
    anscandi.append(anst)


    ans = min(anscandi)
    print(ans)

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()))
for i in range(q):
    x=int(input())
    si=bisect.bisect_left(s,x)
    ti=bisect.bisect_left(t,x)
    if si==a:
        sk=[s[a-1]]
    elif si==0:
        sk=[s[0]]
    else:
        sk=[s[si-1],s[si]]
    if ti==0:
        tk=[t[0]]
    elif ti==b:
        tk=[t[b-1]]
    else:
        tk=[t[ti-1],t[ti]]
    ans=1000000000000
    for f in sk:
        for g in tk:
            ans=min(ans,abs(x-f)+abs(f-g),abs(x-g)+abs(g-f))
    print(ans)

Solution (Python):
import bisect

inf = float("inf")
ans = 0

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

S.sort()
T.sort()


for x in X:
    ans = float('inf')
    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-t) + min(abs(x-t), abs(x-s))
            if d1 < ans:
                ans = d1
    print(ans)

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


def main():
    a,b,q = map(int,readline().split())
    A = [int(readline()) for i in range(a)]
    B = [int(readline()) for i in range(b)]
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(readline())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        print(min(min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr)),max(sl,tl),max(sr,tr)))

if __name__ == '__main__':
    main()

Solution (Python):

import bisect

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

S = [int(input()) for i in range(A)]
S.insert(0, -float("inf"))
S.append(float("inf"))

T = [int(input()) for i in range(B)]
T.insert(0, -float("inf"))
T.append(float("inf"))

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

for x in X:
    ans = float("inf")
    idx_a = bisect.bisect_right(S, x)
    idx_b = bisect.bisect_right(T, x)
    
    for s in [S[idx_a-1], S[idx_a]]:
        for t in [T[idx_b-1], T[idx_b]]:
            temp = abs(s-t) + min(abs(x-s), abs(x-t))
            if temp < ans:
                ans = temp
    print(ans)

Solution (Python):
import bisect
from itertools import product

inf=float('inf')
a,b,q=map(int,input().split())
s=[-inf]+[int(input()) for i in range(a)]+[inf]
t=[-inf]+[int(input()) for i in range(b)]+[inf]
x=[int(input()) for i in range(q)]

for i in x:
    ids=bisect.bisect_right(s,i)
    idt=bisect.bisect_right(t,i)
    ans=inf
    for ss,tt in product(s[ids-1:ids+1],t[idt-1:idt+1]):
        check=min(abs(i-ss),abs(i-tt))+abs(ss-tt)
        ans=min(ans,check)
    print(ans)

Solution (Python):

from bisect import bisect_right as bsr


def dist(x: int) -> int:
    i, j = bsr(S, x), bsr(T, x)
    ret = min(
        min(abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
        for s in S[i - 1 : i + 1]
        for t in T[j - 1 : j + 1]
    )
    return ret


def main():
    global S, T
    A, B, Q, *STX = map(int, open(0).read().split())
    INF = 1 << 60
    S, T, X = [-INF] + STX[:A] + [INF], [-INF] + STX[A : A + B] + [INF], STX[A + B :]
    ans = [dist(x) for x in X]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Solution (Python):

from bisect import bisect_right as bsr


def dist(x: int) -> int:
    i, j = bsr(S, x), bsr(T, x)
    ret = min(
        min(abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
        for s in (S[i - 1], S[i])
        for t in (T[j - 1], T[j])
    )
    return ret


def main():
    global S, T
    A, B, Q, *STX = map(int, open(0).read().split())
    INF = 1 << 60
    S, T, X = [-INF] + STX[:A] + [INF], [-INF] + STX[A : A + B] + [INF], STX[A + B :]
    ans = [dist(x) for x in X]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Solution (Python):

from bisect import bisect_left as bsl, bisect_right as bsr


def main():
    A, B, Q, *STX = map(int, open(0).read().split())
    INF = 1 << 60
    S, T, X = [-INF] + STX[:A] + [INF], [-INF] + STX[A : A + B] + [INF], STX[A + B :]
    ans = []
    for x in X:
        i, j = bsr(S, x), bsr(T, x)
        dist = min(
            min(abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
            for s in (S[i - 1], S[i])
            for t in (T[j - 1], T[j])
        )
        ans.append(dist)
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()