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 sys
import math
from collections import deque
import bisect

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
	A, B, Q = NMI()
	S = [-float("inf")] + [NI() for _ in range(A)] + [float("inf")]
	T = [-float("inf")] + [NI() for _ in range(B)] + [float("inf")]
	Q = [NI() for _ in range(Q)]

	for q in Q:
		s_idx = bisect.bisect_left(S, q)
		t_idx = bisect.bisect_left(T, q)
		sl, sr, tl, tr = S[s_idx-1], S[s_idx], T[t_idx-1], T[t_idx]
		LL = max(abs(q - sl), abs(q - tl))
		RR = max(abs(q - sr), abs(q - tr))
		LR = abs(q - sl) * 2 + abs(q - tr)
		RL = abs(q - sr) * 2 + abs(q - tl)
		LR2 = abs(q - tl) * 2 + abs(q - sr)
		RL2 = abs(q - tr) * 2 + abs(q - sl)
		res = min(LL, RR, LR, LR2, RL, RL2)
		print(res)


if __name__ == "__main__":
	main()

Solution (Python):

import bisect














import unittest
from io import StringIO
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    A, B, Q = map(int, input().split())
    INF = 10**20
    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())
        saind = bisect.bisect_left(s,x)
        sa = s[saind-1]
        sb = s[saind]
        taind = bisect.bisect_left(t,x)
        ta = t[taind-1]
        tb = t[taind]

        res = 10**20

        for ss in (sa, sb):
            for tt in (ta, tb):
                res = min(res, abs(x-ss)+abs(ss-tt))
        
        for tt in (ta, tb):
            for ss in (sa, sb):
                res = min(res, abs(x-tt)+abs(tt-ss))


        print(res)


resolve()

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

Solution (Python):
a,b,q = map(int,input().split())
s = sorted([int(input()) for i in range(a)])
t = sorted([int(input()) for i in range(b)])

from bisect import bisect

for i in range(q):
    x = int(input())

    s_i = bisect(s,x)
    d1 = d2 = 10**11
    if s_i<a:
        d1 = s[s_i]-x
        t_i = bisect(t,s[s_i])
        buf = 10**11
        if t_i<b:
            buf = t[t_i]-s[s_i]
        if t_i>0:
            buf = min(buf,s[s_i]-t[t_i-1])
        d1 += buf
    if s_i>0:
        d2 = x-s[s_i-1]
        t_i = bisect(t,s[s_i-1])
        buf = 10**11
        if t_i<b:
            buf = t[t_i]-s[s_i-1]
        if t_i>0:
            buf = min(buf,s[s_i-1]-t[t_i-1])
        d2 += buf
    
    t_i = bisect(t,x)
    d3 = d4 = 10**11
    if t_i<b:
        d3 = t[t_i]-x
        s_i = bisect(s,t[t_i])
        buf = 10**11
        if s_i<a:
            buf = s[s_i]-t[t_i]
        if s_i>0:
            buf = min(buf,t[t_i]-s[s_i-1])
        d3 += buf
    if t_i>0:
        d4 = x-t[t_i-1]
        s_i = bisect(s,t[t_i-1])
        buf = 10**11
        if s_i<a:
            buf = s[s_i]-t[t_i-1]
        if s_i>0:
            buf = min(buf,t[t_i-1]-s[s_i-1])
        d4 += buf

    print(min(d1,d2,d3,d4))

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
import bisect 




A, B, Q = mi()

S = [ii() for _ in range(A)]
T = [ii() for _ in range(B)]

S_ = [S[0]] + S + [S[A-1]]
T_ = [T[0]] + T + [T[B-1]]

for _ in range(Q):
    f = ii()
    to_s = bisect.bisect_left(S, f)
    to_t = bisect.bisect_left(T, f)

    ans = float('inf')

    for i in [S_[to_s], S_[to_s+1]]:
        for j in [T_[to_t], T_[to_t+1]]:
            tmp = min(abs(i - f), abs(j - f)) + abs(i - j)
            ans = min(ans, tmp)
    '''
    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t+1]), float('inf'))
    ans = min(abs(S_[to_s+1] - T_[to_t+1]) + abs(f - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t]), ans)
    ans = min(abs(S_[to_s+1] - T_[to_t]) + abs(f - T_[to_t]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - T_[to_t+1]) + abs(f - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t]), ans)
    ans = min(abs(S_[to_s] - T_[to_t]) + abs(f - T_[to_t]), ans)
    '''
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
import bisect 




A, B, Q = mi()

S = [ii() for _ in range(A)]
T = [ii() for _ in range(B)]

S_ = [S[0]] + S + [S[A-1]]
T_ = [T[0]] + T + [T[B-1]]

for _ in range(Q):
    f = ii()
    to_s = bisect.bisect_left(S, f)
    to_t = bisect.bisect_left(T, f)
    
    

    
    
    

    
    

    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t+1]), float('inf'))
    ans = min(abs(S_[to_s+1] - T_[to_t+1]) + abs(f- T_[to_t+1]), ans)
    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t]), ans)
    ans = min(abs(S_[to_s+1] - T_[to_t]) + abs(f- T_[to_t]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - T_[to_t+1]) + abs(f- T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t]), ans)
    ans = min(abs(S_[to_s] - T_[to_t]) + abs(f- T_[to_t]), ans)
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
import bisect 




A, B, Q = mi()

S = [ii() for _ in range(A)]
T = [ii() for _ in range(B)]

S_ = [S[0]] + S + [S[A-1]]
T_ = [T[0]] + T + [T[B-1]]

for _ in range(Q):
    f = ii()
    to_s = bisect.bisect_left(S, f)
    to_t = bisect.bisect_left(T, f)
    
    

    
    
    

    
    

    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t+1]), float('inf'))
    ans = min(abs(S_[to_s+1] - T_[to_t+1]) + abs(f- T_[to_t+1]), ans)
    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t]), ans)
    ans = min(abs(S_[to_s+1] - T_[to_t]) + abs(f- T_[to_t]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - T_[to_t+1]) + abs(f- T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t]), ans)
    ans = min(abs(S_[to_s] - T_[to_t]) + abs(f- T_[to_t]), ans)
    print(ans)

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

def main():
    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:
        ans = 10**11

        curr_dist = 0
        ind_s = bisect.bisect_left(sl, x)
        ind_t = bisect.bisect_left(tl, x)
        
        for next_s in [ind_s, ind_s-1]:
            if 0 <= next_s < a:
                curr_dist = abs(x-sl[next_s])
                curr_pos = sl[next_s]
                ind_t2 = bisect.bisect_left(tl, curr_pos)
                if ind_t2 == 0:
                    curr_dist += abs(tl[0] - curr_pos)
                elif ind_t2 == b:
                    curr_dist += abs(tl[b-1] - curr_pos)
                else:
                    curr_dist += min( abs( tl[ind_t2-1] - curr_pos), abs(tl[ind_t2]-curr_pos))
                ans = min(curr_dist, ans)


        for next_t in [ind_t, ind_t-1]:
            if 0 <= next_t < b:
                curr_dist = abs(x-tl[next_t])
                curr_pos = tl[next_t]
                ind_s2 = bisect.bisect_left(sl, curr_pos)
                if ind_s2 == 0:
                    curr_dist += abs(sl[0] - curr_pos)
                elif ind_s2 == a:
                    curr_dist += abs(sl[a-1] - curr_pos)
                else:
                    curr_dist += min( abs( sl[ind_s2-1] - curr_pos), abs(sl[ind_s2]-curr_pos))
                ans = min(curr_dist, ans)

        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

def main():
    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:
        ans = 10**11

        curr_dist = 0
        ind_s = bisect.bisect_left(sl, x)
        ind_t = bisect.bisect_left(tl, x)
        
        for next_s in [ind_s, ind_s-1]:
            if 0 <= next_s < a:
                curr_dist = abs(x-sl[next_s])
                curr_pos = sl[next_s]
                ind_t2 = bisect.bisect_left(tl, curr_pos)
                if ind_t2 == 0:
                    curr_dist += abs(tl[0] - curr_pos)
                elif ind_t2 == b:
                    curr_dist += abs(tl[b-1] - curr_pos)
                else:
                    curr_dist += min( abs( tl[ind_t2-1] - curr_pos), abs(tl[ind_t2]-curr_pos))
                ans = min(curr_dist, ans)


        for next_t in [ind_t, ind_t-1]:
            if 0 <= next_t < b:
                curr_dist = abs(x-tl[next_t])
                curr_pos = tl[next_t]
                ind_s2 = bisect.bisect_left(sl, curr_pos)
                if ind_s2 == 0:
                    curr_dist += abs(sl[0] - curr_pos)
                elif ind_s2 == a:
                    curr_dist += abs(sl[a-1] - curr_pos)
                else:
                    curr_dist += min( abs( sl[ind_s2-1] - curr_pos), abs(sl[ind_s2]-curr_pos))
                ans = min(curr_dist, ans)

        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
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:
    ans = 10**11

    curr_dist = 0
    ind_s = bisect.bisect_left(sl, x)
    ind_t = bisect.bisect_left(tl, x)
    
    for next_s in [ind_s, ind_s-1]:
        if 0 <= next_s < a:
            curr_dist = abs(x-sl[next_s])
            curr_pos = sl[next_s]
            ind_t2 = bisect.bisect_left(tl, curr_pos)
            if ind_t2 == 0:
                curr_dist += abs(tl[0] - curr_pos)
            elif ind_t2 == b:
                curr_dist += abs(tl[b-1] - curr_pos)
            else:
                curr_dist += min( abs( tl[ind_t2-1] - curr_pos), abs(tl[ind_t2]-curr_pos))
            ans = min(curr_dist, ans)


    for next_t in [ind_t, ind_t-1]:
        if 0 <= next_t < b:
            curr_dist = abs(x-tl[next_t])
            curr_pos = tl[next_t]
            ind_s2 = bisect.bisect_left(sl, curr_pos)
            if ind_s2 == 0:
                curr_dist += abs(sl[0] - curr_pos)
            elif ind_s2 == a:
                curr_dist += abs(sl[a-1] - curr_pos)
            else:
                curr_dist += min( abs( sl[ind_s2-1] - curr_pos), abs(sl[ind_s2]-curr_pos))
            ans = min(curr_dist, ans)

    print(ans)

Solution (Python):
import bisect
def resolve():
    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)]
    
    for x in X:
        ans = float("inf")
        for Ary1, Ary2 in [[S, T], [T, S]]:
            ar1_nearests = get_nearest_pos(Ary1, x)
            for p in ar1_nearests:
                ar2_nearests = get_nearest_pos(Ary2, p)
                for p2 in ar2_nearests:
                    ans = min(ans, abs(x-p)+abs(p-p2))
        print(ans)

def get_nearest_pos(array, pos):
    res = []
    idx = bisect.bisect_left(array, pos)
    if idx == 0:
        res.append(array[idx])
    elif idx == len(array):
        res.append(array[idx-1])
    else:
        res.append(array[idx])
        res.append(array[idx-1])
    return res


if '__main__' == __name__:
    resolve()

Solution (Python):






from collections import deque



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 start in x:
    res=[]
    start_s=bisect.bisect_left(s,start)
    start_t=bisect.bisect_left(t,start)
    if start_s<a and start_t<b:
        res.append(max(s[start_s]-start,t[start_t]-start))
    if start_s-1>-1 and start_t-1>-1:
        res.append(max(start-s[start_s-1],start-t[start_t-1]))
    if start_s<a and start_t-1>-1:
        res.append(2*(s[start_s]-start)+start-t[start_t-1])
        res.append(2*(start-t[start_t-1])+s[start_s]-start)
    if start_t<b and start_s-1>-1:
        res.append(2*(t[start_t]-start)+start-s[start_s-1])
        res.append(2*(start-s[start_s-1])+t[start_t]-start)
    print(min(res))

Solution (Python):
import bisect

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

s = [- 10 ** 19]
for i in range(a):
    s.append(int(input()))
s.append(10 ** 19)

t = [- 10 ** 19]
for i in range(b):
    t.append(int(input()))
t.append(10 ** 19)

for i in range(q):
    x = int(input())
    ps = bisect.bisect_right(s, x)
    pt = bisect.bisect_right(t, x)
    ret = 10 ** 19
    for loc1 in (s[ps], s[ps-1]):
        for loc2 in (t[pt], t[pt-1]):
            val = min(abs(x-loc1) + abs(loc1-loc2),
                      abs(x-loc2) + abs(loc1-loc2))
            ret = min(ret, val)
    print(ret)

Solution (Python):

import bisect

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

s.sort()
t.sort()

i = 0


for i in range(len(x)):

    r_shrine = bisect.bisect_right(s, x[i])
    r_temple = bisect.bisect_right(t, x[i])

    l_shrine = r_shrine-1
    l_temple = r_temple-1

    
    

    ans = []
    if l_shrine >= 0 and l_temple >= 0:
        ans.append(max(x[i]-s[l_shrine], x[i]-t[l_temple]))  
    if r_shrine < a and r_temple < b:
        ans.append(max(s[r_shrine]-x[i], t[r_temple]-x[i]))  
    if l_shrine >= 0 and r_temple < b:
        min_ = min(x[i]-s[l_shrine], t[r_temple]-x[i])
        ans.append(min_+(t[r_temple]-s[l_shrine]))  
    if r_shrine < a and l_temple >= 0:
        min_ = min(x[i]-t[l_temple], s[r_shrine]-x[i])
        ans.append(min_+(s[r_shrine]-t[l_temple]))  

    print(min(ans))

Solution (Python):

import bisect

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

s.sort()
t.sort()

i = 0


for i in range(len(x)):

    r_shrine = bisect.bisect_right(s, x[i])
    r_temple = bisect.bisect_right(t, x[i])

    l_shrine = r_shrine-1
    l_temple = r_temple-1

    
    

    ans = []
    if l_shrine >= 0 and l_temple >= 0:
        ans.append(max(x[i]-s[l_shrine], x[i]-t[l_temple]))  
    if r_shrine < a and r_temple < b:
        ans.append(max(s[r_shrine]-x[i], t[r_temple]-x[i]))  
    if l_shrine >= 0 and r_temple < b:
        min_ = min(x[i]-s[l_shrine], t[r_temple]-x[i])
        ans.append(min_+(t[r_temple]-s[l_shrine]))  
    if r_shrine < a and l_temple >= 0:
        min_ = min(x[i]-t[l_temple], s[r_shrine]-x[i])
        ans.append(min_+(s[r_shrine]-t[l_temple]))  

    print(min(ans))

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 x in X:
    SL, SR, TL, TR = 10**20, 10**20, 10**20, 10**20
    i = bisect.bisect_left(S, x)
    if i != 0:
        SL = x-S[i-1]
    if i != A:
        SR = S[i]-x

    j = bisect.bisect_left(T, x)
    if j != 0:
        TL = x-T[j-1]
    if j != B:
        TR = T[j]-x

    ans = min(max(SL, TL), min(2*SL+TR, SL+2*TR),
              min(2*TL+SR, TL+2*SR), max(SR, TR))
    print(ans)

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

R = []
for x in X:
    SL, SR, TL, TR = 10**20, 10**20, 10**20, 10**20
    i = bisect.bisect_left(S, x)
    if i != 0:
        SL = x-S[i-1]
    if i != A:
        SR = S[i]-x

    j = bisect.bisect_left(T, x)
    if j != 0:
        TL = x-T[j-1]
    if j != B:
        TR = T[j]-x

    ans = min(max(SL, TL), min(2*SL+TR, SL+2*TR),
              min(2*TL+SR, TL+2*SR), max(SR, TR))
    R.append(ans)
print(*R, sep='\n')

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

R = []
for x in X:
    SL, SR, TL, TR = 10**20, 10**20, 10**20, 10**20
    i = bisect.bisect_left(S, x)
    if i != 0:
        SL = x-S[i-1]
    if i != A:
        SR = S[i]-x

    j = bisect.bisect_left(T, x)
    if j != 0:
        TL = x-T[j-1]
    if j != B:
        TR = T[j]-x

    ans = min(max(SL, TL), min(2*SL+TR, SL+2*TR),
              min(2*TL+SR, TL+2*SR), max(SR, TR))
    R.append(ans)
print(*R, sep='\n')

Solution (Python):
from functools import lru_cache
from bisect import bisect
from sys import stdin
input = stdin.readline
A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]

@lru_cache(maxsize=None)
def func(f, x):
    L = S if f else T
    i = bisect(L, x)
    if i == 0:
        return abs(x - L[0])
    elif i == len(L):
        return abs(x - L[-1])
    else:
        return min(abs(x - L[i]), abs(x - L[i - 1]))

for x in [int(input()) for _ in range(Q)]:
    ans = float("inf")
    i = bisect(S, x)
    if i != 0:
        ans = min(ans, func(0, S[i - 1]) + x - S[i - 1])
    if i != A:
        ans = min(ans, func(0, S[i]) + S[i] - x)
    i = bisect(T, x)
    if i != 0:
        ans = min(ans, func(1, T[i - 1]) + x - T[i - 1])
    if i != B:
        ans = min(ans, func(1, T[i]) + T[i] - x)
    print(ans)

Solution (Python):
from bisect import bisect_left

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


for xi in x:
    p1=bisect_left(s,xi)
    p2=bisect_left(t,xi)
    l=10**11
    for ss in s[p1-1:p1+1]:
        for tt in t[p2-1:p2+1]:
            l=min(l,min(abs(ss-xi),abs(tt-xi))+abs(ss-tt))
    
    print(l)

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, bisect_right

for _ in range(q):
    x = int(input())
    i = bisect_left(s, x)
    j = bisect_left(t, x)
    d = INF
    for sx in [s[i - 1], s[i]]:
        for tx in [t[j - 1], t[j]]:
            d1 = abs(sx - x) + abs(sx - tx)
            d2 = abs(tx - x) + abs(sx - tx)
            d = min(d, d1, d2)
    print(d)

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, bisect_right

for _ in range(q):
    x = int(input())
    i = bisect_right(s, x)
    j = bisect_right(t, x)
    d = INF
    for sx in [s[i - 1], s[i]]:
        for tx in [t[j - 1], t[j]]:
            d1 = abs(sx - x) + abs(sx - tx)
            d2 = abs(tx - x) + abs(sx - tx)
            d = min(d, d1, d2)
    print(d)

Solution (Python):
import bisect

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

shrine = []
temple = []

for i in range(a):
    s = int(input())
    shrine.append(s)

for i in range(b):
    t = int(input())
    temple.append(t)


for j in range(q):
    shrine_list = []
    temple_list = []
    query = int(input())
    shrine_index = bisect.bisect_left(shrine, query)
    temple_index = bisect.bisect_left(temple, query)
    if shrine_index == 0:
        shrine_list.append(shrine[shrine_index])
    elif shrine_index == len(shrine):
        shrine_list.append(shrine[shrine_index-1])
    else:
        shrine_list.append(shrine[shrine_index])
        shrine_list.append(shrine[shrine_index-1])
    if temple_index == 0:
        temple_list.append(temple[temple_index])
    elif temple_index == len(temple):
        temple_list.append(temple[temple_index-1])
    else:
        temple_list.append(temple[temple_index])
        temple_list.append(temple[temple_index-1])
    min_dist = 10**12
    for sh in shrine_list:
        for tm in temple_list:
            if sh < query < tm:
                dist = min(tm - sh + (query - sh), tm - sh + (tm - query))
            elif tm < query < sh:
                dist = min(sh - tm + (query - tm), sh - tm + (sh - query))
            elif min(query, sh, tm) == query:
                dist = max(sh, tm) - query
            elif max(query, sh, tm) == query:
                dist = query - min(sh, tm)
            if dist < min_dist:
                min_dist = dist
    print(min_dist)

Solution (Python):
def main():
    from bisect import bisect_left as bl
    a, b, q, *stx = map(int, open(0).read().split())
    s = [-float('Inf')] + stx[:a] + [float('Inf')]
    t = [-float('Inf')] + stx[a:a + b] + [float('Inf')]
    x = stx[a + b:]
    tmp = []
    for i in x:
        f, g = bl(s, i), bl(t, i)

        sr, sl = s[f - 1], s[f]
        tr, tl = t[g - 1], t[g]

        d = min([max(sl, tl) - i, i - min(sr, tr), 2 * (tl - sr) - max(i - sr, tl - i), 2 * (sl - tr) - max(sl - i, i - tr)])
        tmp.append(d)

    ans = '\n'.join(map(str, tmp))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from bisect import bisect_left as bl
    a, b, q, *stx = map(int, open(0).read().split())
    s = [-float('Inf')] + stx[:a] + [float('Inf')]
    t = [-float('Inf')] + stx[a:a + b] + [float('Inf')]
    x = stx[a + b:]
    tmp = []
    for i in x:
        f, g = bl(s, i), bl(t, i)

        sl, sr = s[f - 1], s[f]
        tl, tr = t[g - 1], t[g]

        d = float('Inf')
        for j in [sl, sr]:
            for k in [tr, tl]:
                if j <= i and k <= i:
                    d = min(d, i - min(j, k))
                elif j <= i <= k or k <= i <= j:
                    d = min(d, min(abs(i - j), abs(i - k)) * 2 + max(abs(i - j), abs(i - k)))
                else:
                    d = min(d, max(j, k) - i)

        tmp.append(d)

    ans = '\n'.join(map(str, tmp))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect
a,b,q=map(int,input().split())
INF=10**18
s=[-INF]+[int(input()) for _ in range(a)]+[INF]
t=[-INF]+[int(input()) for _ in range(b)]+[INF]

for k 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 sys import stdin
import bisect
def main():
    
    readline=stdin.readline
    inf=10**18
    a,b,q=map(int,readline().split())
    s=[int(readline()) for _ in range(a)]
    s.insert(0,-inf)
    s.append(inf)
    t=[int(readline()) for _ in range(b)]
    t.insert(0,-inf)
    t.append(inf)
    x=[int(readline()) for _ in range(q)]

    for i in range(q):
        j=bisect.bisect_left(s,x[i])
        k=bisect.bisect_left(t,x[i])
        s_i=(s[j-1],s[j])
        t_i=(t[k-1],t[k])
        ans=float("inf")
        for u in s_i:
            for v in t_i:
                ans=min(ans,abs(u-x[i])+abs(v-u))

        for v in t_i:
            for u in s_i:
                ans=min(ans,abs(v-x[i])+abs(u-v))

        print(ans)

if __name__=="__main__":
    main()

Solution (Python):
A,B,Q=map(int, input().split())
from bisect import bisect_left

S=[]
for i in range(A):
    s=int(input())
    S.append(s)
T=[]
for i in range(B):
    t=int(input())
    T.append(t)

for i in range(Q):
    x=int(input())
    sidx=bisect_left(S, x)
    tidx=bisect_left(T, x)
    if 0<=sidx<A and 0<=tidx<B:
        a1=max(S[sidx], T[tidx])-x
    else:
        a1=float("INF")
    if 0<=sidx-1<A and 0<=tidx-1<B:
        a2=max(x-S[sidx-1], x-T[tidx-1])
    else:
        a2=float("INF")
    if 0<=sidx<A and 0<=tidx-1<B:
        a3=S[sidx]-T[tidx-1]+min(S[sidx]-x, x-T[tidx-1])
    else:
        a3=float("INF")
    if 0<=sidx-1<A and 0<=tidx<B:
        a4=T[tidx]-S[sidx-1]+min(T[tidx]-x, x-S[sidx-1])
    else:
        a4=float("INF")
    
    ans=min(a1,a2,a3,a4)
    print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect
def main():
    a,b,q=map(int,input().split())
    S=[int(input())for _ in range(a)]
    T=[int(input())for _ in range(b)]

    def dfs(cur,flag1,flag2):
        if flag2==1:
            index_s=bisect(S,cur)
            
            if index_s>0 and index_s<a:
                if flag1==0:
                    return min(abs(S[index_s-1]-cur),abs(S[index_s]-cur))
                else:
                    return min(dfs(S[index_s-1],0,0)+abs(S[index_s-1]-cur),dfs(S[index_s],0,0)+abs(S[index_s]-cur))
            else:
                if index_s==a:
                    nx=S[-1]
                elif index_s==0:
                    nx=S[0]
                if flag1==1:
                    return dfs(nx,0,0)+abs(nx-cur)
                else:
                    return abs(nx-cur)
        else:
            index_t=bisect(T,cur)
            
            if index_t>0 and index_t<b:
                if flag1==0:
                    return min(abs(T[index_t-1]-cur),abs(T[index_t]-cur))
                else:
                    return min(dfs(T[index_t-1],0,1)+abs(T[index_t-1]-cur),dfs(T[index_t],0,1)+abs(T[index_t]-cur))
            else:
                if index_t==b:
                    nx=T[-1]
                elif index_t==0:
                    nx=T[0]
                if flag1==1:
                    return dfs(nx,0,1)+abs(nx-cur)
                else:
                    return abs(nx-cur)

    for i in range(q):
        x=int(input())
        print(min(dfs(x,1,1),dfs(x,1,0)))


if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect
def main():
    a,b,q=map(int,input().split())
    S=[int(input())for _ in range(a)]
    T=[int(input())for _ in range(b)]

    def dfs(cur,flag1,flag2):
        if flag2==1:
            index_s=bisect(S,cur)
            
            if index_s>0 and index_s<a:
                if flag1==0:
                    return min(abs(S[index_s-1]-cur),abs(S[index_s]-cur))
                else:
                    return min(dfs(S[index_s-1],0,0)+abs(S[index_s-1]-cur),dfs(S[index_s],0,0)+abs(S[index_s]-cur))
            else:
                if index_s==a:
                    nx=S[-1]
                elif index_s==0:
                    nx=S[0]
                if flag1==1:
                    return dfs(nx,0,0)+abs(nx-cur)
                else:
                    return abs(nx-cur)
        else:
            index_t=bisect(T,cur)
            
            if index_t>0 and index_t<b:
                if flag1==0:
                    return min(abs(T[index_t-1]-cur),abs(T[index_t]-cur))
                else:
                    return min(dfs(T[index_t-1],0,1)+abs(T[index_t-1]-cur),dfs(T[index_t],0,1)+abs(T[index_t]-cur))
            else:
                if index_t==b:
                    nx=T[-1]
                elif index_t==0:
                    nx=T[0]
                if flag1==1:
                    return dfs(nx,0,1)+abs(nx-cur)
                else:
                    return abs(nx-cur)

    for i in range(q):
        x=int(input())
        print(min(dfs(x,1,1),dfs(x,1,0)))


if __name__=='__main__':
    main()

Solution (Python):
from bisect import bisect_left

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

S.sort()
T.sort()

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

    s = bisect_left(S, x)
    ls = S[s - 1]
    rs = S[s]

    t = bisect_left(T, x)
    lt = T[t - 1]
    rt = T[t]

    ans = min(
        abs(ls - rt) + min(abs(x - ls), abs(x - rt)),
        abs(lt - rs) + min(abs(x - lt), abs(x - rs)),
        max(abs(x - ls), abs(x - lt)),
        max(abs(x - rs), abs(x - rt))
    )

    print(ans)