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 math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools


sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    A, B, Q = LI()
    s, t, x = [], [], []
    [s.append(I()) for i in range(A)]
    [t.append(I()) for i in range(B)]
    [x.append(I()) for i in range(Q)]
    s = [-10**18] + s + [10**18]
    t = [-10**18] + t + [10**18]

    
    
    
    
    
    
    
    
    
    


    for start in x:
        
        first_temple_idx = bisect.bisect_left(t, start) - 1
        second_temple_idx = first_temple_idx + 1
        temples = [first_temple_idx, second_temple_idx]
        first_shrine_idx = bisect.bisect_left(s, start) - 1
        second_shrine_idx = first_shrine_idx + 1
        shrines = [first_shrine_idx, second_shrine_idx]
        

        
        
        
        
        
        
        

        
        
        
        
        
        
        
        

        result = 10**11
        for temple in temples:
            for shrine in shrines:
                
                
                near = min(abs(start - t[temple]), abs(start - s[shrine]))
                far = max(abs(start - t[temple]), abs(start - s[shrine]))
                if (t[temple] < start and s[shrine] < start) or (t[temple] > start and s[shrine] > start):
                    result = min(result, far)
                elif (t[temple] < start and s[shrine] > start) or (t[temple] > start and s[shrine] < start):
                    result = min(result, near * 2 + far)
                

        
        print(result)
if __name__ == "__main__":
    main()

Solution (Python):

"""
Created on Sun Feb 24 22:10:01 2019

@author: Yamazaki Kenichi
"""
import numpy as np
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)]

def lr(s,c,A):
    si = bisect.bisect(s,c)
    if si == A:
        return [c-s[si-1], 10**11]
    elif si == 0:
        return [10**11, s[0]-c]
    else:
        return [c-s[si-1], s[si]-c]
for c in x:
    ans = 0
    slr = lr(s,c,A)
    tlr = lr(t,c,B)
    ans = min(max(slr[0],tlr[0]),
              max(slr[1],tlr[1]),
              slr[0]*2+ tlr[1],
              slr[0]+ tlr[1]*2,
              slr[1]*2 + tlr[0],
              slr[1]+ tlr[0]*2
              )
    print(ans)

Solution (Python):
import bisect

def main():
    A, B, Q = map(int, input().split())
    s = [-float('inf')]
    t = [-float('inf')]
    ans = []
    for _ in range(A):
        s.append(int(input()))
    for _ in range(B):
        t.append(int(input()))
    s.append(float('inf'))
    t.append(float('inf'))
    for _ in range(Q):
        x = int(input())
        i = bisect.bisect_right(s, x)
        j = bisect.bisect_right(t, x)
        a = float('inf')
        for ss in [s[i - 1], s[i]]:
            for tt in [t[j - 1], t[j]]:
                a = min(a, abs(ss - x) + abs(tt - ss),
                        abs(tt - x) + abs(ss - tt))
        ans.append(a)
    for a in ans:
        print(a)


main()

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

for q in range(Q):
    x = X[q]
    s1 = bisect.bisect_right(S, x)
    s2 = s1 - 1
    t1 = bisect.bisect_right(T, x)
    t2 = t1 - 1
    s1, s2 = S[s1], S[s2]
    t1, t2 = T[t1], T[t2]
    dis1 = abs(x - s1) + abs(s1 - t1)
    dis2 = abs(x - s2) + abs(s2 - t2)
    dis3 = abs(x - t1) + abs(t1 - s1)
    dis4 = abs(x - t2) + abs(t2 - s2)
    dis5 = abs(x - s1) + abs(s1 - t2)
    dis6 = abs(x - s2) + abs(s2 - t1)
    dis7 = abs(x - t1) + abs(t1 - s2)
    dis8 = abs(x - t2) + abs(t2 - s1)
    dis = min(dis1, dis2, dis3, dis4, dis5, dis6, dis7, dis8)
    print(dis)

Solution (Python):
import bisect
A,B,Q=map(int,input().split())
INF=10**18
S=[-INF]+[int(input()) for i in range(A)]+[INF]
T=[-INF]+[int(input()) for i in range(B)]+[INF]
for q in range(Q):
    x=int(input())
    i=bisect.bisect_left(S,x)
    j=bisect.bisect_left(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(s-t)
            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())
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())
    b, d = bisect.bisect_right(s,x), bisect.bisect_right(t,x)
    res = INF
    for S in [s[b-1],s[b]]:
        for T in [t[d-1],t[d]]:
            A,B = abs(S-x)+abs(T-S), abs(T-x)+abs(T-S)
            res = min(res,A,B)
    print(res)

Solution (Python):

import bisect


a,b,q = map(int, input().split())
INF = 10**18
shrines = [-INF] + [int(input()) for _ in range(a)] + [INF]
temples = [-INF] + [int(input()) for _ in range(b)] + [INF]

for _ in range(q):

    start = int(input())

    
    shrine_east = shrines[bisect.bisect_right(shrines, start)]
    
    temple_east = temples[bisect.bisect_right(temples, start)]

    
    shrine_west = shrines[bisect.bisect_right(shrines, start) - 1]
    
    temple_west = temples[bisect.bisect_right(temples, start) - 1]

    ans = INF

    for shrine in [shrine_east, shrine_west]:
        for temple in [temple_east,temple_west]:
            cur = min(abs(shrine-start), abs(temple-start)) + abs(shrine-temple)
            ans = min(ans,cur)

    print(ans)

Solution (Python):
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        dist1[0]=s[0]-x[k]
        dist1[1]=s[0]-x[k]
        sp1[0]=s[0]
        sp1[1]=s[0]        
    elif p1==a:
        dist1[0]=x[k]-s[a-1]
        dist1[1]=x[k]-s[a-1]
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
    else:
        dist1[0]=x[k]-s[p1-1]
        dist1[1]=s[p1]-x[k]
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]
 
    p11=bisect.bisect_left(t,sp1[0])

    if p11==0:
        dist2[0]=t[0]-sp1[0]
        dist2[1]=t[0]-sp1[0]
    elif p11==b:
        dist2[0]=sp1[0]-t[b-1]
        dist2[1]=sp1[0]-t[b-1]
    else:
        dist2[0]=sp1[0]-t[p11-1]
        dist2[1]=t[p11]-sp1[0]
        
    p11=bisect.bisect_left(t,sp1[1])

    if p11==0:
        dist2[2]=t[0]-sp1[1]
        dist2[3]=t[0]-sp1[1]
    elif p11==b:
        dist2[2]=sp1[1]-t[b-1]
        dist2[3]=sp1[1]-t[b-1]
    else:
        dist2[2]=sp1[1]-t[p11-1]
        dist2[3]=t[p11]-sp1[1]
        
    dist[0]=dist1[0]+dist2[0]
    dist[1]=dist1[0]+dist2[1]
    dist[2]=dist1[1]+dist2[2]
    dist[3]=dist1[1]+dist2[3]
                

    p1=bisect.bisect_left(t,x[k])

    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
        dist1[0]=t[0]-x[k]
        dist1[1]=t[0]-x[k]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
        dist1[0]=x[k]-t[b-1]
        dist1[1]=x[k]-t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]
        dist1[0]=x[k]-t[p1-1]
        dist1[1]=t[p1]-x[k]
        
    p11=bisect.bisect_left(s,sp1[0])

    if p11==0:
        dist2[0]=s[0]-sp1[0]
        dist2[1]=s[0]-sp1[0]
    elif p11==a:
        dist2[0]=sp1[0]-s[a-1]
        dist2[1]=sp1[0]-s[a-1]
    else:
        dist2[0]=sp1[0]-s[p11-1]
        dist2[1]=s[p11]-sp1[0]
    
    p11=bisect.bisect_left(s,sp1[1])

    if p11==0:
        dist2[2]=s[0]-sp1[1]
        dist2[3]=s[0]-sp1[1]       
    elif p11==a:
        dist2[2]=sp1[1]-s[a-1]
        dist2[3]=sp1[1]-s[a-1]
    else:
        dist2[2]=sp1[1]-s[p11-1]
        dist2[3]=s[p11]-sp1[1]

    dist[4]=dist1[0]+dist2[0]
    dist[5]=dist1[0]+dist2[1]
    dist[6]=dist1[1]+dist2[2]
    dist[7]=dist1[1]+dist2[3]
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Solution (Python):
import numpy as np
from bisect import bisect_left

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

def solve(x):

    Si, Ti = bisect_left(S, x), bisect_left(T, x)
    Sw, Se = abs(x-S[Si-1]), abs(x-S[Si])
    Tw, Te = abs(x-T[Ti-1]), abs(x-T[Ti])
    return min(max(Sw, Tw), max(Se, Te), 2*Tw+Se, 2*Sw+Te, 2*Se+Tw, 2*Te+Sw)

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

Solution (Python):
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        dist1[0]=s[0]-x[k]
        dist1[1]=s[0]-x[k]
        sp1[0]=s[0]
        sp1[1]=s[0]        
    elif p1==a:
        dist1[0]=x[k]-s[a-1]
        dist1[1]=x[k]-s[a-1]
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
    else:
        dist1[0]=x[k]-s[p1-1]
        dist1[1]=s[p1]-x[k]
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]
 
    p11=bisect.bisect_left(t,sp1[0])

    if p11==0:
        dist2[0]=t[0]-sp1[0]
        dist2[1]=t[0]-sp1[0]
    elif p11==b:
        dist2[0]=sp1[0]-t[b-1]
        dist2[1]=sp1[0]-t[b-1]
    else:
        dist2[0]=sp1[0]-t[p11-1]
        dist2[1]=t[p11]-sp1[0]
        
    p11=bisect.bisect_left(t,sp1[1])

    if p11==0:
        dist2[2]=t[0]-sp1[1]
        dist2[3]=t[0]-sp1[1]
    elif p11==b:
        dist2[2]=sp1[1]-t[b-1]
        dist2[3]=sp1[1]-t[b-1]
    else:
        dist2[2]=sp1[1]-t[p11-1]
        dist2[3]=t[p11]-sp1[1]
        
    dist[0]=dist1[0]+dist2[0]
    dist[1]=dist1[0]+dist2[1]
    dist[2]=dist1[1]+dist2[2]
    dist[3]=dist1[1]+dist2[3]
                

    p1=bisect.bisect_left(t,x[k])

    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
        dist1[0]=t[0]-x[k]
        dist1[1]=t[0]-x[k]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
        dist1[0]=x[k]-t[b-1]
        dist1[1]=x[k]-t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]
        dist1[0]=x[k]-t[p1-1]
        dist1[1]=t[p1]-x[k]
        
    p11=bisect.bisect_left(s,sp1[0])

    if p11==0:
        dist2[0]=s[0]-sp1[0]
        dist2[1]=s[0]-sp1[0]
    elif p11==a:
        dist2[0]=sp1[0]-s[a-1]
        dist2[1]=sp1[0]-s[a-1]
    else:
        dist2[0]=sp1[0]-s[p11-1]
        dist2[1]=s[p11]-sp1[0]
    
    p11=bisect.bisect_left(s,sp1[1])

    if p11==0:
        dist2[2]=s[0]-sp1[1]
        dist2[3]=s[0]-sp1[1]       
    elif p11==a:
        dist2[2]=sp1[1]-s[a-1]
        dist2[3]=sp1[1]-s[a-1]
    else:
        dist2[2]=sp1[1]-s[p11-1]
        dist2[3]=s[p11]-sp1[1]

    dist[4]=dist1[0]+dist2[0]
    dist[5]=dist1[0]+dist2[1]
    dist[6]=dist1[1]+dist2[2]
    dist[7]=dist1[1]+dist2[3]
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Solution (Python):
import sys
from bisect import bisect
F = sys.stdin
A, B, Q = map(int, F.readline().strip("\n").split(" "))
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(F.readline().strip("\n"))
for i in range(B):
    T[i+1] = int(F.readline().strip("\n"))
S[A+1] = 10**20
T[B+1] = 10**20
 
lenS, lenT = len(S), len(T)
Out = ""
for _ in range(Q):
    x = int(F.readline().strip("\n"))
    si, ti = bisect(S, x, 0, lenS), bisect(T, x, 0, lenT)
    sLength0, sLength1 = x - S[si-1], S[si] - x
    tLength0, tLength1 = x - T[ti-1], T[ti] - x
    Ans = min(max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    Out += str(Ans) + "\n"
print(Out)

Solution (Python):
from itertools import product
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()))
  
Xs = sorted(X)

pos = 0
sl, tl = -1e18, -1e18
sr, tr = S[0], T[0]
si, ti, xi = 0, 0, 0 

XQ = {} 

for _ in range(int(5e5)):
  pos = min(sr,tr,Xs[xi]) 
  if pos == sr:
    sl = sr
    si += 1
    sr = S[si] if si < A else 1e18
  elif pos == tr:
    tl = tr
    ti += 1
    tr = T[ti] if ti < B else 1e18
  else:
    XQ[Xs[xi]] = [Xs[xi], sl, sr, tl, tr]
    xi += 1
    if xi >= Q:
      break

for i in range(Q):
  x, sl, sr, tl, tr = XQ[X[i]]
  sl, sr, tl, tr = sl-x, sr-x, tl-x, tr-x
  ans = 1e18
  ans = min(ans, abs(sl) + abs(sl-tl))
  ans = min(ans, abs(sl) + abs(sl-tr))
  ans = min(ans, abs(sr) + abs(sr-tl))
  ans = min(ans, abs(sr) + abs(sr-tr))
  ans = min(ans, abs(tl) + abs(tl-sl))
  ans = min(ans, abs(tl) + abs(tl-sr))
  ans = min(ans, abs(tr) + abs(tr-sl))
  ans = min(ans, abs(tr) + abs(tr-sr))
  print(ans)

Solution (Python):
from itertools import product
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()))
  
Xs = sorted(X)

pos = 0
sl, tl = -1e18, -1e18
sr, tr = S[0], T[0]
si, ti, xi = 0, 0, 0 

XQ = {} 

for _ in range(int(5e5)):
  pos = min(sr,tr,Xs[xi]) 
  if pos == sr:
    sl = sr
    si += 1
    sr = S[si] if si < A else 1e18
  elif pos == tr:
    tl = tr
    ti += 1
    tr = T[ti] if ti < B else 1e18
  else:
    XQ[Xs[xi]] = [Xs[xi], sl, sr, tl, tr]
    xi += 1
    if xi >= Q:
      break

for i in range(Q):
  x, sl, sr, tl, tr = XQ[X[i]]
  sl, sr, tl, tr = sl-x, sr-x, tl-x, tr-x
  ans = 1e18
  ans = min(ans, abs(sl) + abs(sl-tl))
  ans = min(ans, abs(sl) + abs(sl-tr))
  ans = min(ans, abs(sr) + abs(sr-tl))
  ans = min(ans, abs(sr) + abs(sr-tr))
  ans = min(ans, abs(tl) + abs(tl-sl))
  ans = min(ans, abs(tl) + abs(tl-sr))
  ans = min(ans, abs(tr) + abs(tr-sl))
  ans = min(ans, abs(tr) + abs(tr-sr))
  print(ans)

Solution (Python):
A,B,Q = map(int, input().split())
inf = float("inf")
import sys
input = sys.stdin.readline
S = [-inf] + [ int(input()) for _ in range(A)] + [inf]
T = [-inf] + [ int(input()) for _ in range(B)] + [inf]
X = [ int(input()) for _ in range(Q)]

from bisect import bisect_left, bisect_right

for q in range(Q):
    x = X[q]

    i = bisect_left(S, x)
    j = bisect_left(T, x)
    left_S , right_S =  x - S[i-1], S[i] - x
    left_T , right_T =  x - T[j-1], T[j] - x

    res = min( 
            max(left_S, left_T),
            left_S + right_T + min(left_S, right_T),
            right_S + left_T + min(right_S, left_T),
            max(right_S, right_T) 
            )
    print(res)

Solution (Python):
import bisect

A, B, Q = map(int, input().split())
INF = 10 ** 11
s_list = [-INF] + [int(input()) for i in range(A)] + [INF]
t_list = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    near_right_s = bisect.bisect_right(s_list, x)
    near_right_t = bisect.bisect_right(t_list, x)
    min_distance = INF
    for s in [s_list[near_right_s - 1], s_list[near_right_s]]:
        for t in [t_list[near_right_t - 1], t_list[near_right_t]]:
            distance_1 = abs(s - x) + abs(t - s)
            distance_2 = abs(t - x) + abs(s - t)
            min_distance = min(min_distance, distance_1, distance_2)
    print(min_distance)

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

Solution (Python):
import bisect
A,B,Q = map(int,input().split())
INF = 10**18
s = [-INF]+[int(input()) for i in range(A)]+[INF]
t = [-INF]+[int(input()) for i in range(B)]+[INF]
s.sort()
t.sort()
answer = []
for i in range(Q):
    x = int(input())
    sk = bisect.bisect_left(s,x)-1
    tk = bisect.bisect_left(t,x)-1
    ptn1 = abs(s[sk]-x) + min(abs(t[tk]-s[sk]),abs(t[tk+1]-s[sk]))
    ptn2 = abs(s[sk+1]-x) + min(abs(t[tk]-s[sk+1]),abs(t[tk+1]-s[sk+1]))
    ptn3 = abs(t[tk]-x) + min(abs(s[sk]-t[tk]),abs(s[sk+1]-t[tk]))
    ptn4 = abs(t[tk+1]-x) + min(abs(s[sk]-t[tk+1]),abs(s[sk+1]-t[tk+1]))
    answer.append(min(ptn1,ptn2,ptn3,ptn4))
for i in answer:
    print(i)

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

for i in range(q):
  j=bisect.bisect_left(s, x[i])
  if j !=0 :
    kyori_hidari = (x[i]-s[j-1])
  else:
    kyori_hidari = float("inf")
  if j !=a :
    kyori_migi = (s[j]-x[i])
  else:
    kyori_migi = float("inf")  
    
  k=bisect.bisect_left(t, x[i])
  if k !=0 :
    kyori_hidari_t = (x[i]-t[k-1])
  else:
    kyori_hidari_t = float("inf")
  if k !=b :
    kyori_migi_t = (t[k]-x[i])
  else:
    kyori_migi_t = float("inf")
 
  migi = max(kyori_migi,kyori_migi_t)
  hidari = max(kyori_hidari,kyori_hidari_t)
 
  print(min(migi,hidari,2*kyori_hidari+kyori_migi_t,2*kyori_hidari_t+kyori_migi,2*kyori_migi+kyori_hidari_t,2*kyori_migi_t+kyori_hidari))

Solution (Python):
import bisect
INF = 1000000000000000000

A,B,Q = map(int,input().split())
lA = []
lB = []
lQ = []
lA.append(-INF)
lB.append(-INF)

for i in range(A):
    a = int(input())
    lA.append(a)

for i in range(B):
    b = int(input())
    lB.append(b)

for i in range(Q):
    q = int(input())
    lQ.append(q)

lA.append(INF)
lB.append(INF)
    

for i in lQ:
    ar = bisect.bisect_right(lA,i)
    al = ar - 1

    br = bisect.bisect_right(lB,i)
    bl = br - 1

    ans = INF

    for S in [lA[al],lA[ar]]:
        for T in [lB[bl],lB[br]]:
            d1 = abs(S-i) + abs(T-S)
            d2 = abs(T-i) + abs(S-T)

            ans = min(ans,d1,d2)

    print(ans)

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

Solution (Python):
import sys
import bisect
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]
for i in range(q):
    x = int(input())
    idx_s = bisect.bisect_left(s, x)
    idx_t = bisect.bisect_left(t, x)

    indices_s = []
    indices_t = []

    indices_s.append(idx_s)
    indices_s.append(idx_s-1)

    indices_t.append(idx_t)
    indices_t.append(idx_t-1)

    ans = []
    for i in indices_s:
        for j in indices_t:
            l, r = min(s[i], t[j]), max(s[i], t[j])
            if x < l:
                ans.append(r-x)
            elif l <= x and x < r:
                ans.append(min(r-l+(x-l), r-l+(r-x)))
            else:
                ans.append(x-l)
    print(min(ans))

Solution (Python):
a, b, q = map(int, input().split())
listS = []
listT = []
listAns = []

def nibutan(listA, x):
    minY=0
    maxY=len(listA)-1
    while True:
        middle = (minY+maxY)//2
        if listA[middle] < x:
            if middle == len(listA)-1:
                return listA[middle-1], listA[middle]
                break
            else:
                if x < listA[middle+1] :
                    return listA[middle], listA[middle+1]
                    break
                else:
                    minY = middle
        else:
            if middle==0:
                return listA[middle], listA[middle+1]
                break
            else:
                if listA[middle-1] < x:
                    return listA[middle-1], listA[middle]
                    break
                else:
                    maxY = middle


index=0
while index < a:
    s = int(input())
    listS.append(s)
    index+=1
listS.reverse()
listS.append(-10**11)
listS.reverse()
listS.append(10**11)
index2=0
while index2 < b:
    t = int(input())
    listT.append(t)
    index2+=1
listT.reverse()
listT.append(-10**11)
listT.reverse()
listT.append(10**11)
index3=0
while index3 < q:
    x = int(input())
    
    sa, sb = nibutan(listS, x)
    ta, tb = nibutan(listT, x)
    listDis = [abs(sa-x)+abs(sa-ta), abs(ta-x)+abs(sa-ta), abs(sa-x)+abs(sa-tb), abs(tb-x)+abs(sa-tb),
               abs(sb-x)+abs(sb-ta), abs(ta-x)+abs(sb-ta), abs(sb-x)+abs(sb-tb), abs(tb-x)+abs(sb-tb)]
    minDist = min(listDis)
    listAns.append(minDist)
    index3+=1
for ans in listAns:
    print(ans)

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

from bisect import bisect_left

for x in X:
    Si,Ti = bisect_left(S,x),bisect_left(T,x)
    Sw,Se = abs(x-S[Si-1]),abs(x-S[Si])
    Tw,Te = abs(x-T[Ti-1]),abs(x-T[Ti])
    print(min(max(Sw,Tw),max(Se,Te),2*min(Sw,Te)+max(Sw,Te),2*min(Tw,Se)+max(Tw,Se)))

Solution (Python):
import bisect
import sys

f_inf = float('inf')


def cal(syurui, d, index):
    if d == 'e':
        return syurui[index]
    elif d == 'w':
        return syurui[index - 1]


A, B, Q = map(int, input().split())
s = [int(input()) for i in range(A)]
s.sort()
t = [int(input()) for i in range(B)]
t.sort()
ｓ.insert(0, -f_inf)
t.insert(0, -f_inf)
ｓ.append(f_inf)
t.append(f_inf)

for i in range(Q):
    x = int(sys.stdin.readline().rstrip('\n'))
    s_index = bisect.bisect_left(s, x)
    t_index = bisect.bisect_left(t, x)
    print(min(max(abs(x - s[s_index]), abs(x - t[t_index])), max(abs(x - s[s_index - 1]), abs(x - t[t_index - 1])), 2 * abs(x - s[s_index - 1]) + abs(x - t[t_index]),
              2 * abs(x - s[s_index]) + abs(x - t[t_index - 1]), abs(x - s[s_index - 1]) + 2 * abs(x - t[t_index]), abs(x - s[s_index]) + 2 * abs(x - t[t_index - 1])))

Solution (Python):
from bisect import bisect as bi
a,b,q = (int(i) for i in input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
inf = float("inf")
s,t = [-inf]+s+[inf],[-inf]+t+[inf]
def f(x):
    bs,bt = bi(s,x),bi(t,x)
    return min(x-min(s[bs-1],t[bt-1]),max(s[bs],t[bt])-x,t[bt]-s[bs-1]+min(t[bt]-x,x-s[bs-1]),s[bs]-t[bt-1]+min(s[bs]-x,x-t[bt-1]))
for i in range(q):
    x = int(input())
    print(f(x))

Solution (Python):
import sys
import bisect
A,B,Q = [int(n) for n in input().split()]

INF = 10 **18
s = [0]*A
t = [0]*B
s = [-INF] + [int(input()) for n in range(A)] + [INF]
t = [-INF] + [int(input()) for n in range(B)] + [INF]

for i in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
    for S in [s[b - 1], s[b]]:
        for T in [t[d - 1], t[d]]:
            d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
            res = min(res, d1, d2)
    print(res)