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 bisect


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    a, b, q = list(map(int, readline().split()))
    s = [int(readline()) for _ in range(a)]
    t = [int(readline()) for _ in range(b)]
    x = [int(readline()) for _ in range(q)]
    for xv in x:
        sp = bisect.bisect_left(s, xv)
        tp = bisect.bisect_left(t, xv)
        ss = s[sp] if sp == 0 else 10 ** 20 if sp == len(s) else s[sp-1]
        ss -= xv
        se = 10 ** 20 if sp == 0 else s[sp-1] if sp == len(s) else s[sp]
        se -= xv
        ts = t[tp] if tp == 0 else 10 ** 20 if tp == len(t) else t[tp-1]
        ts -= xv
        te = 10 ** 20 if tp == 0 else t[tp-1] if tp == len(t) else t[tp]
        te -= xv
        mt = 10 ** 20
        for sv in [ss, se]:
            for tv in [ts, te]:
                if sv * tv < 0:
                    mt = min(mt, abs(sv) + abs(tv) + min(abs(sv), abs(tv)))
                else:
                    mt = min(mt, max(abs(sv), abs(tv)))
        print(mt)


if __name__ == '__main__':
    solve()

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):
    st = x[i]
    
    

    
    sr = bisect.bisect_left(s, st)
    sl = sr - 1
    ans_s = 10**20
    for j in [sr, sl]:
        if 0 <= j <= a-1:
            tmp = abs(st - s[j])
            tr = bisect.bisect_left(t, s[j])
            tl = tr - 1
            for k in [tr, tl]:
                if 0 <= k <= b-1:
                    
                    ans_s = min(ans_s, tmp+abs(s[j] - t[k]))
    
    
    tr = bisect.bisect_left(t, st)
    tl = tr - 1
    ans_t = 10**20
    for j in [tr, tl]:
        if 0 <= j <= b-1:
            tmp = abs(st - t[j])
            sr = bisect.bisect_left(s, t[j])
            sl = sr - 1
            for k in [sr, sl]:
                if 0 <= k <= a-1:
                    
                    ans_t = min(ans_t, tmp+abs(s[k] - t[j]))
    
    print(min(ans_s, ans_t))

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):
    st = x[i]
    
    

    
    sr = bisect.bisect_left(s, st)
    sl = sr - 1
    ans_s = 10**20
    for j in [sr, sl]:
        if 0 <= j <= a-1:
            tmp = abs(st - s[j])
            tr = bisect.bisect_left(t, s[j])
            tl = tr - 1
            for k in [tr, tl]:
                if 0 <= k <= b-1:
                    
                    ans_s = min(ans_s, tmp+abs(s[j] - t[k]))
    
    
    tr = bisect.bisect_left(t, st)
    tl = tr - 1
    ans_t = 10**20
    for j in [tr, tl]:
        if 0 <= j <= b-1:
            tmp = abs(st - t[j])
            sr = bisect.bisect_left(s, t[j])
            sl = sr - 1
            for k in [sr, sl]:
                if 0 <= k <= a-1:
                    
                    ans_t = min(ans_t, tmp+abs(s[k] - t[j]))
    
    print(min(ans_s, ans_t))

Solution (Python):
def p_d():
    from bisect import bisect_right
    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]
    ans = []
    for _ in range(Q):
        x = int(input())
        b, d = bisect_right(s, x), 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((d1, d2, res))
        ans.append(str(res))
    print("\n".join(ans))


if __name__ == '__main__':
    p_d()

Solution (Python):


import bisect

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

god=[-10**18]
for _ in range(A):
    god.append(int(input()))
god.append(10**18)

buddha=[-10**18]
for _ in range(B):
    buddha.append(int(input()))
buddha.append(10**18)


for _ in range(Q):
    q=int(input())
    g_i=bisect.bisect_left(god, q)
    b_i=bisect.bisect_left(buddha, q)
    
    
    ans=10**18
    G=[god[g_i-1],god[g_i]]
    B=[buddha[b_i-1],buddha[b_i]]
    for g in G:
        for b in B:
            ans=min(ans,abs(q-g)+abs(g-b),abs(q-b)+abs(b-g))
    print(ans)

Solution (Python):
from bisect import bisect_left

def main():
    A, B, Q = map(int, input().split())
    S = [0] * A
    T = [0] * B
    for i in range(A):
        S[i] = int(input())
    for i in range(B):
        T[i] = int(input())
    
    for _ in range(Q):
        x = int(input())
        s_cnt = bisect_left(S,x)
        t_cnt = bisect_left(T,x)
        s_cand = []
        t_cand = []
        if s_cnt == 0:
            s_cand.append(S[s_cnt])
        elif s_cnt == A:
            s_cand.append(S[s_cnt-1])
        else:
            s_cand.append(S[s_cnt])
            s_cand.append(S[s_cnt-1])
        if t_cnt == 0:
            t_cand.append(T[t_cnt])
        elif t_cnt == B:
            t_cand.append(T[t_cnt-1])
        else:
            t_cand.append(T[t_cnt])
            t_cand.append(T[t_cnt-1])
        d = float('inf')
        for i in s_cand:
            for j in t_cand:
                tmp = abs(x - i) + abs(i - j)
                d = min(d, tmp)

        for i in t_cand:
            for j in s_cand:
                tmp = abs(x - i) + abs(i - j)
                d = min(d, tmp)
        print(d)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect as bi

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

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

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

for k in range(Q):
    x=int(input())
    i=bi.bisect_left(s,x)
    sl=s[i-1];sr=s[i]
    j=bi.bisect_left(t,x)
    tl=t[j-1];tr=t[j]
    
    print(min(abs(x-sl)+abs(sl-tl),abs(x-sl)+abs(sl-tr),abs(x-sr)+abs(sr-tl),abs(x-sr)+abs(sr-tr),\
        abs(x-tl)+abs(tl-sl),abs(x-tl)+abs(tl-sr),abs(x-tr)+abs(tr-sl),abs(x-tr)+abs(tr-sr)))

Solution (Python):
import sys
import bisect
from itertools import zip_longest
from operator import itemgetter
readline = sys.stdin.readline
a,b,q = map(int, input().split(' '))
S = [int(readline()) for _ in range(a)]
T = [int(readline()) for _ in range(b)]
Q = [int(readline()) for _ in range(q)]
st = list(zip_longest([],S,fillvalue = 1))+list(zip_longest([],T,fillvalue = 2))
st.sort(key=itemgetter(1))
st_plc = [x[1] for x in st]
for i,x in enumerate(Q):
    nb = bisect.bisect(st_plc, x)
    if nb == 0:
        print(max(S[0],T[0])-x)
    elif nb == a+b:
        print(x-min(S[-1],T[-1]))
    else:
        if st[nb-1][0] == 1:
            nb2 = bisect.bisect(T,st_plc[nb-1])
            if nb2 == 0:
                ans1 = (x-st_plc[nb-1])*2 + (T[0]-x)
            elif nb2 == b:
                ans1 = x-T[-1]
            else:
                ans1 = x-st_plc[nb-1] + min(T[nb2]-st_plc[nb-1],st_plc[nb-1]-T[nb2-1])
        elif st[nb-1][0] == 2:
            nb2 = bisect.bisect(S,st_plc[nb-1])
            if nb2 == 0:
                ans1 = (x-st_plc[nb-1])*2 + (S[0]-x)
            elif nb2 == a:
                ans1 = x-S[-1]
            else:
                ans1 = x-st_plc[nb-1] + min(S[nb2]-st_plc[nb-1],st_plc[nb-1]-S[nb2-1])
        if st[nb][0] == 1:
            nb2 = bisect.bisect(T,st_plc[nb])
            if nb2 == 0:
                ans2 = T[0]-x
            elif nb2 == b:
                ans2 = (st_plc[nb]-x)*2 + (x-T[-1])
            else:
                ans2 = st_plc[nb]-x + min(T[nb2]-st_plc[nb],st_plc[nb]-T[nb2-1])
        elif st[nb][0] == 2:
            nb2 = bisect.bisect(S,st_plc[nb])
            if nb2 == 0:
                ans2 = S[0]-x
            elif nb2 == a:
                ans2 = (st_plc[nb]-x)*2 + (x-S[-1])
            else:
                ans2 = st_plc[nb]-x + min(S[nb2]-st_plc[nb],st_plc[nb]-S[nb2-1])
        print(min(ans1,ans2))

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

for i in range(Q):
    x = int(input())
    ret = 2 * 10**10
    a = bisect.bisect_left(s, x)
    tmp = 0
    if a < len(s):
        tmp = abs(s[a] - x)
        ab = bisect.bisect_left(t, s[a])
        if ab < len(t):
            tmp2 = abs(t[ab] - s[a])
            ret = min(ret, tmp + tmp2)

        if ab > 0:
            tmp2 = abs(t[ab - 1] - s[a])
            ret = min(ret, tmp + tmp2)
    if a > 0:
        a -= 1
        tmp = 0
        tmp = abs(s[a] - x)
        ab = bisect.bisect_left(t, s[a])
        if ab < len(t):
            tmp2 = abs(t[ab] - s[a])
            ret = min(ret, tmp + tmp2)

        if ab > 0:
            tmp2 = abs(t[ab - 1] - s[a])
            ret = min(ret, tmp + tmp2)

    b = bisect.bisect_left(t, x)
    tmp = 0
    if b < len(t):
        tmp = abs(t[b] - x)
        ba = bisect.bisect_left(s, t[b])
        if ba < len(s):
            tmp2 = abs(s[ba] - t[b])
            ret = min(ret, tmp + tmp2)
        if ba > 0:
            tmp2 = abs(s[ba - 1] - t[b])
            ret = min(ret, tmp + tmp2)
    if b > 0:
        b -= 1
        tmp = abs(t[b] - x)
        ba = bisect.bisect_left(s, t[b])
        if ba < len(s):
            tmp2 = abs(s[ba] - t[b])
            ret = min(ret, tmp + tmp2)

        if ba > 0:
            tmp2 = abs(s[ba - 1] - t[b])
            ret = min(ret, tmp + tmp2)
    print(ret)

Solution (Python):
a,b,q=map(int,input().split())
import bisect
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:
    c,d=bisect.bisect_left(s,q),bisect.bisect_left(t,q)
    ans=float("inf")
    for i in [c,c-1]:
        for j in [d,d-1]:
            ans=min(ans,abs(s[i]-t[j])+abs(q-s[i]),abs(s[i]-t[j])+abs(q-t[j]))
    print(ans)

Solution (Python):
a,b,q=map(int,input().split())
import bisect
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:
    c,d=bisect.bisect_left(s,q),bisect.bisect_left(t,q)
    ans=float("inf")
    for i in [c,c-1]:
        for j in [d,d-1]:
            ans=min(ans,abs(s[i]-t[j])+abs(q-s[i]),abs(s[i]-t[j])+abs(q-t[j]))
    print(ans)

Solution (Python):
a,b,q=map(int,input().split())
import bisect
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:
    c,d=bisect.bisect_left(s,q),bisect.bisect_left(t,q)
    ans=float("inf")
    for i in [c,c-1]:
        for j in [d,d-1]:
            ans=min(ans,abs(s[i]-t[j])+abs(q-s[i]),abs(s[i]-t[j])+abs(q-t[j]))
    print(ans)

Solution (Python):


import sys
from bisect import bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, Q = MAP()
A = [0] * N
for i in range(N):
    A[i] = INT()
B = [0] * M
for i in range(M):
    B[i] = INT()

A.sort()
B.sort()
A = [-INF] + A + [INF]
B = [-INF] + B + [INF]

for i in range(Q):
    q = INT()
    
    
    al = bisect_right(A, q) - 1
    bl = bisect_right(B, q) - 1
    
    ar = bisect_left(A, q)
    br = bisect_left(B, q)

    
    a = max(q-A[al], q-B[bl])
    
    b = max(A[ar]-q, B[br]-q)
    
    c = min(q-A[al], B[br]-q) * 2 + max(q-A[al], B[br]-q)
    
    d = min(A[ar]-q, q-B[bl]) * 2 + max(A[ar]-q, q-B[bl])

    ans = min(a, b, c, d)
    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)]

for x in X:
    ans = float('inf')
    if x < S[0]:
        right_S = S[0]
        left_S = None
    elif x > S[-1]:
        right_S = None
        left_S = S[-1]

    else:
        index_right_S = bisect.bisect_right(S, x)
        if index_right_S < A and S[index_right_S] == x:
            left_S = x
            right_S = x
        else:
            if index_right_S - 1 >= 0:
                left_S = S[index_right_S - 1]
            if index_right_S < A:
                right_S = S[index_right_S]

    if x < T[0]:
        right_T = T[0]
        left_T = None
    elif x > T[-1]:
        right_T = None
        left_T = T[-1]

    else:
        index_right_T = bisect.bisect_right(T, x)
        if index_right_T < B and T[index_right_T] == x:
            left_T = x
            right_T = x
        else:
            if index_right_T - 1 >= 0:
                left_T = T[index_right_T - 1]
            if index_right_T < B:
                right_T = T[index_right_T]

    if left_T and right_S:
        ans = min(ans, abs(left_T - x) + abs(left_T - right_S),
                                   abs(right_S - x) + abs(left_T - right_S))

    if left_T and left_S:
        ans = min(ans, abs(left_T - x) + abs(left_T - left_S),
                                  abs(left_S - x) + abs(left_T - left_S))

    if right_T and left_S:
        ans = min(ans, abs(right_T - x) + abs(right_T - left_S),
                                   abs(left_S - x) + abs(right_T - left_S))

    if right_T and right_S:
        ans = min(ans, abs(right_T - x) + abs(right_T - right_S),
                                  abs(right_S - x) + abs(right_T - right_S))

    print(ans)

Solution (Python):






import bisect





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





s = [0] * A
for i in range(A):
    s[i] = int(input())

t = [0] * B
for i in range(B):
    t[i] = int(input())
x = [0] * Q
for i in range(Q):
    x[i] = int(input())
















def func(i, j):
    ans = []
    if i != 0 and j != 0:
        ans.append(max(y - s[i - 1], y - t[j - 1]))
    if i != len(s) and j != len(t):
        ans.append(max(s[i] - y, t[j] - y))
    if i != 0 and j != len(t):
        ans.append(2*(y-s[i-1]) + (t[j]-y))
        ans.append(2*(t[j]-y) + (y-s[i-1]))
    if i != len(s) and j != 0:
        ans.append(2*(y-t[j-1]) + (s[i]-y))
        ans.append(2*(s[i]-y) + (y-t[j-1]))
    return min(ans)





ans = []
for y in x:
    i = bisect.bisect_left(s, y)
    j = bisect.bisect_left(t, y)
    ans.append(func(i, j))
print('\n'.join(map(str,ans)))

Solution (Python):
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 doit(st, x):
    lo, hi = 0, len(st) - 1
    while hi - lo > 1:
        mid = (lo + hi) // 2
        if st[mid] < x:
            lo = mid
        else:
            hi = mid
    mid = (lo + hi) // 2
    return st[mid], st[mid + 1]

for _ in range(q):
    x = int(input())
    s12 = doit(s, x)
    t12 = doit(t, x)
    print(min(min(abs(s1 - x) + abs(t1 - s1), abs(t1 - x) + abs(s1 - t1)) for s1 in s12 for t1 in t12))

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

s = sorted(s)
t = sorted(t)
st = []
(i, j) = (0, 0)
while i < len(s) or j < len(t):
    if j == len(t) or (i < len(s) and s[i] < t[j]):
        st.append((s[i], min(abs(s[i] - t[max(j - 1, 0)]),
                             abs(s[i] - t[min(j, len(t) - 1)]))))
        i += 1
    else:
        st.append((t[j], min(abs(t[j] - s[max(i - 1, 0)]),
                             abs(t[j] - s[min(i, len(s) - 1)]))))
        j += 1

def doit(x):
    lo, hi = 0, len(st) - 1
    while hi - lo > 1:
        mid = (lo + hi) // 2
        if st[mid][0] < x:
            lo = mid
        else:
            hi = mid
    mid = (lo + hi) // 2
    return min(abs(st[mid][0] - x) + st[mid][1], abs(st[mid + 1][0] - x) + st[mid + 1][1])

for _ in range(q):
    print(doit(int(input())))

Solution (Python):
def binarySearch(a, val):
    left, right = -1, len(a)
    while left + 1 < right:
        mid = (left + right) >> 1
        if a[mid] < val:
            left = mid
        else:
            right = mid
    return a[right] if right < len(a) else 1 << 35

def binarySearch2(a, val):
    left, right = -1, len(a)
    while left + 1 < right:
        mid = (left + right) >> 1
        if a[mid] <= val:
            left = mid
        else:
            right = mid
    return a[left] if left >= 0 else -(1 << 35)

nShrines, nTemples, nQueries = map(int, input().split())
shrines = [int(input()) for _ in range(nShrines)]
temples = [int(input()) for _ in range(nTemples)]
starts = [int(input()) for _ in range(nQueries)]
ret = []
for start in starts:
    shrineToRight = binarySearch(shrines, start)
    templeToRight = binarySearch(temples, start)
    shrineToLeft = binarySearch2(shrines, start)
    templeToLeft = binarySearch2(temples, start)
    ret.append(min(
        max(shrineToRight, templeToRight) - start,
        start - min(shrineToLeft, templeToLeft),
        templeToRight - start + templeToRight - shrineToLeft,
        shrineToRight - start + shrineToRight - templeToLeft,
        start - shrineToLeft + templeToRight - shrineToLeft,
        start - templeToLeft + shrineToRight - templeToLeft
    ))
print('\n'.join(map(str, ret)))

Solution (Python):
import sys
import bisect
_input = sys.stdin.readlines()
a, b, q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
s_ls = data[:a]
t_ls = data[a:a+b]
x_ls = data[-q:]
INF = 10 ** 18

for x in x_ls:
    s_ind = min(bisect.bisect_left(s_ls, x), a-1)
    t_ind = min(bisect.bisect_left(t_ls, x), b-1)
    best = INF
    for _s in [-1, 0]:
        if s_ind == 0 and _s == -1:
            continue
        for _t in [-1, 0]:
            if t_ind == 0 and _t == -1:
                continue
            s = s_ls[s_ind + _s]
            t = t_ls[t_ind + _t]
            if (s - x) * (t - x) >= 0:
                dis = max(abs(t - x), abs(s - x))
            else:
                dis = min(abs(t - x), abs(s - x)) * 2 + max(abs(t - x), abs(s - x))
            best = min(dis, best)
    print(best)

Solution (Python):
import sys
import bisect
a, b, q = [int(i) for i in sys.stdin.readline().split()]
s_ls = []
t_ls = []
INF = 10 ** 18
for i in range(a):
    s = int(input())
    s_ls.append(s)
for i in range(b):
    t = int(input())
    t_ls.append(t)

for i in range(q):
    x = int(input())
    s_ind = min(bisect.bisect_left(s_ls, x), a-1)
    t_ind = min(bisect.bisect_left(t_ls, x), b-1)
    best = INF
    for _s in [-1, 0]:
        if s_ind == 0 and _s == -1:
            continue
        for _t in [-1, 0]:
            if t_ind == 0 and _t == -1:
                continue
            s = s_ls[s_ind + _s]
            t = t_ls[t_ind + _t]
            if (s - x) * (t - x) >= 0:
                dis = max(abs(t - x), abs(s - x))
            else:
                dis = min(abs(t - x), abs(s - x)) * 2 + max(abs(t - x), abs(s - x))
            best = min(dis, best)
    print(best)

Solution (Python):
def main():
    from bisect import bisect_left
    import sys

    input = sys.stdin.readline
    inf = (10 ** 10) * 2 + 10

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

    s = [-inf] + [int(input()) for _ in range(a)] + [inf]
    

    t = [-inf] + [int(input()) for _ in range(b)] + [inf]
    

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

        si = bisect_left(s, x) - 1
        ti = bisect_left(t, x) - 1

        ret = inf
        for sx in s[si:si + 2]:
            for tx in t[ti:ti + 2]:
                ret = min(ret, min(abs(x - sx), abs(x - tx)) + abs(sx - tx))
        print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left

inf = (10 ** 10) * 2 + 10

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

s = [-inf]
for _ in range(a):
    s.append(int(input()))
s.append(inf)
s.sort()

t = [-inf]
for _ in range(b):
    t.append(int(input()))
t.append(inf)
t.sort()

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

    si = bisect_left(s, x) - 1
    ti = bisect_left(t, x) - 1

    ret = inf
    for sx in s[si:si + 2]:
        for tx in t[ti:ti + 2]:
            ret = min(ret, min(abs(x - sx), abs(x - tx)) + abs(sx - tx))
    print(ret)

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 math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush,heapify
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7
INF = 10**18

A,B,Q = MAP()
s = [-INF] + [INT() for _ in range(A)] + [INF]
t = [-INF] + [INT() for _ in range(B)] + [INF]

for i in range(Q):
    cur = INT()
    s_idx = bisect.bisect_right(s,cur)
    t_idx = bisect.bisect_right(t,cur)
    res = INF
    for S in [s[s_idx-1],s[s_idx]]:
        for T in [t[t_idx-1],t[t_idx]]:
            d1,d2 = abs(S-cur) + abs(T-S),abs(T-cur)+abs(S-T)
            res = min(res,d1,d2)
    print(res)

Solution (Python):
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush,heapify
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7
INF = 10**18

A,B,Q = MAP()
s = [-INF] + [INT() for _ in range(A)] + [INF]
t = [-INF] + [INT() for _ in range(B)] + [INF]

for i in range(Q):
    cur = INT()
    s_idx = bisect.bisect_right(s,cur)
    t_idx = bisect.bisect_right(t,cur)
    res = INF
    for S in [s[s_idx-1],s[s_idx]]:
        for T in [t[t_idx-1],t[t_idx]]:
            d1,d2 = abs(S-cur) + abs(T-S),abs(T-cur)+abs(S-T)
            res = min(res,d1,d2)
    print(res)

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

def search(x):
    s=bisect.bisect_right(S,x)
    t=bisect.bisect_right(T,x)
    bs1=max(s-1,0)
    bt1=max(t-1,0)
    bs2=min(s,A-1)
    bt2=min(t,B-1)
    r1=abs(S[bs1]-x)+abs(S[bs1]-T[bt1])
    r2=abs(S[bs1]-x)+abs(S[bs1]-T[bt2])
    r3=abs(S[bs2]-x)+abs(S[bs2]-T[bt1])
    r4=abs(S[bs2]-x)+abs(S[bs2]-T[bt2])
    r5=abs(T[bt1]-x)+abs(T[bt1]-S[bs1])
    r6=abs(T[bt1]-x)+abs(T[bt1]-S[bs2])
    r7=abs(T[bt2]-x)+abs(T[bt2]-S[bs1])
    r8=abs(T[bt2]-x)+abs(T[bt2]-S[bs2])
    r=min([r1,r2,r3,r4,r5,r6,r7,r8])
    return r

for i in X:
    print(search(i))

Solution (Python):

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

import bisect

for x in X:
    ans = 10**12
    tmpans1 = 0
    pos1 = bisect.bisect_left(S,x)
    if pos1==0:
        tmpans1 += abs(S[pos1]-x)
        p = bisect.bisect_left(T,S[pos1])
        tmpans1 += min(abs(S[pos1]-T[min(p,B-1)]),abs(S[pos1]-T[max(0,p-1)]))
        ans = min(ans,tmpans1)
    elif pos1==A:
        pos1 -= 1
        tmpans1 += abs(x-S[pos1])
        p = bisect.bisect_left(T,S[pos1])
        tmpans1 += min(abs(S[pos1]-T[min(p,B-1)]),abs(S[pos1]-T[max(0,p-1)]))
        ans = min(ans,tmpans1)
    else:
        tmpans1 += abs(S[pos1]-x)
        p = bisect.bisect_left(T,S[pos1])
        tmpans1 += min(abs(S[pos1]-T[min(p,B-1)]),abs(S[pos1]-T[max(0,p-1)]))
        ans = min(ans,tmpans1)
        tmpans1 = 0
        pos1 -= 1
        tmpans1 += abs(x-S[pos1])
        p = bisect.bisect_left(T,S[pos1])
        tmpans1 += min(abs(S[pos1]-T[min(p,B-1)]),abs(S[pos1]-T[max(0,p-1)]))
        ans = min(ans,tmpans1)
    
    tmpans1 = 0
    pos1 = bisect.bisect_left(T,x)
    if pos1==0:
        tmpans1 += abs(T[pos1]-x)
        p = bisect.bisect_left(S,T[pos1])
        tmpans1 += min(abs(T[pos1]-S[min(p,A-1)]),abs(T[pos1]-S[max(0,p-1)]))
        ans = min(ans,tmpans1)
    elif pos1==B:
        pos1 -= 1
        tmpans1 += abs(T[pos1]-x)
        p = bisect.bisect_left(S,T[pos1])
        tmpans1 += min(abs(T[pos1]-S[min(p,A-1)]),abs(T[pos1]-S[max(0,p-1)]))
        ans = min(ans,tmpans1)
    else:
        tmpans1 += abs(T[pos1]-x)
        p = bisect.bisect_left(S,T[pos1])
        tmpans1 += min(abs(T[pos1]-S[min(p,A-1)]),abs(T[pos1]-S[max(0,p-1)]))
        ans = min(ans,tmpans1)
        tmpans1 = 0
        pos1 -= 1
        tmpans1 += abs(T[pos1]-x)
        p = bisect.bisect_left(S,T[pos1])
        tmpans1 += min(abs(T[pos1]-S[min(p,A-1)]),abs(T[pos1]-S[max(0,p-1)]))
        ans = min(ans,tmpans1)
    
    print(ans)

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

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

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

  L=0
  R=A+2
  while R-L>1:
    P=(L+R)//2
    if x<jinjya[P]:
      R=P
    else:
      L=P
  jinjya_L=L
  
  L=0
  R=B+2
  while R-L>1:
    P=(L+R)//2
    if x<tera[P]:
      R=P
    else:
      L=P
  tera_L=L

  a=jinjya[jinjya_L]
  b=jinjya[jinjya_L+1]
  c=tera[tera_L]
  d=tera[tera_L+1]
  if jinjya_L==0:
    a=b
  if tera_L==0:
    c=d
  answer=min(abs(x-a)+abs(a-c),abs(x-c)+abs(c-a),abs(x-b)+abs(b-c),abs(x-c)+abs(c-b),abs(x-a)+abs(a-d),abs(x-d)+abs(d-a),abs(x-b)+abs(b-d),abs(x-d)+abs(d-b))
  print(answer)

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

from bisect import bisect_left
for i in range(len(x)):
    idx1 = bisect_left(s, x[i])
    idx2 = bisect_left(t, x[i])

    cands = [[], []]
    if idx1 != 0 and idx1 != len(s):
        cands[0].append(s[idx1 - 1])
        cands[0].append(s[idx1])
    elif idx1 == 0:
        cands[0].append(-float('inf'))
        cands[0].append(s[idx1])
    elif idx1 == len(s):
        cands[0].append(s[idx1 - 1])
        cands[0].append(float('inf'))
    if idx2 != 0 and idx2 != len(t):
        cands[1].append(t[idx2 - 1])
        cands[1].append(t[idx2])
    elif idx2 == 0:
        cands[1].append(-float('inf'))
        cands[1].append(t[idx2])
    elif idx2 == len(t):
        cands[1].append(t[idx2 - 1])
        cands[1].append(float('inf'))

    
    print(min(x[i] - min([cands[0][0], cands[1][0]]),
              max([cands[0][1], cands[1][1]]) - x[i],
              (x[i] - cands[1][0]) * 2 + (cands[0][1] - x[i]),
              (x[i] - cands[1][0]) + (cands[0][1] - x[i]) * 2,
              (x[i] - cands[0][0]) * 2 + (cands[1][1] - x[i]),
              (x[i] - cands[0][0]) + (cands[1][1] - x[i]) * 2))

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

def distance(t, s, x):
    dis = abs(t-s) + min(abs(t-x), abs(s-x))
    return dis

def near_search(x, L):
    temp = bisect.bisect_left(L, x)
    if temp == len(L):
        can_f = L[-1]
        can_s = L[-1]
    elif temp == 0:
        can_f = L[0]
        can_s = L[0]
    else:
        can_f = L[temp-1]
        can_s = L[temp]
    return can_f, can_s

for x in X:
    s_f, s_s = near_search(x, S)
    t_f, t_s = near_search(x, T)
    total = min(distance(t_f, s_f, x), distance(t_s, s_f, x), distance(t_f, s_s, x), distance(t_s, s_s, x))
    print(total)

Solution (Python):
import sys
import bisect


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    a, b, q = list(map(int, input().rstrip('\n').split()))
    s = [int(input().rstrip('\n')) for _ in range(a)]
    t = [int(input().rstrip('\n')) for _ in range(b)]
    x = [int(input().rstrip('\n')) for _ in range(q)]
    for xv in x:
        ps = bisect.bisect_left(s, xv)
        pt = bisect.bisect_left(t, xv)
        sl = []
        if ps == 0:
            sl.append(s[0])
        elif ps == a:
            sl.append(s[-1])
        else:
            sl.append(s[ps-1])
            sl.append(s[ps])
        tl = []
        if pt == 0:
            tl.append(t[0])
        elif pt == b:
            tl.append(t[-1])
        else:
            tl.append(t[pt-1])
            tl.append(t[pt])
        mt = 10 ** 20
        for sv in sl:
            for tv in tl:
                if xv <= sv and xv <= tv:
                    mt = min(mt, abs(max(sv, tv) - xv))
                elif sv <= xv and tv <= xv:
                    mt = min(mt, abs(min(sv, tv) - xv))
                else:
                    mt = min(mt, min(abs(sv - xv), abs(tv - xv)) + abs(tv - sv))
        print(mt)


if __name__ == '__main__':
    solve()