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
import sys
def input():
    return sys.stdin.readline()[:-1]
def main():
    A, B, Q = map(int,input().split())
    s = [-10**20] + [int(input()) for k in range(A)] + [10**20,10**20]
    t = [-10**20] + [int(input()) for k in range(B)] + [10**20,10**20]

    for _ in range(Q):
        x = int(input())
        S = bisect.bisect_left(s,x)
        T = bisect.bisect_left(t,x)
        print(min(
            max(s[S],t[T])-x,
            x-min(s[S-1],t[T-1]),
            s[S]-x + s[S]-t[T-1],
            t[T]-x + t[T]-s[S-1],
            x-s[S-1] + t[T]-s[S-1],
            x-t[T-1] + s[S]-t[T-1]))
if __name__ == '__main__':
    main()

Solution (Python):
import bisect
A, B, Q = map(int,input().split())
s = [-10**20] + [int(input()) for k in range(A)] + [10**20,10**20]
t = [-10**20] + [int(input()) for k in range(B)] + [10**20,10**20]

for _ in range(Q):
    x = int(input())
    S = bisect.bisect_left(s,x)
    T = bisect.bisect_left(t,x)
    print(min(
        max(s[S],t[T])-x,
        x-min(s[S-1],t[T-1]),
        s[S]-x + s[S]-t[T-1],
        t[T]-x + t[T]-s[S-1],
        x-s[S-1] + t[T]-s[S-1],
        x-t[T-1] + s[S]-t[T-1]))

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

MY_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 = [-MY_INF] + s + [MY_INF]
t = [-MY_INF] + t + [MY_INF]

import bisect

for i, xi in enumerate(x):
    ans = 10 ** 18
    
    
    idx_s = bisect.bisect_left(s, xi)
    
    s_l = s[idx_s - 1]
    idx_tl = bisect.bisect_left(t, s_l)
    
    t_ll = t[idx_tl - 1]
    ans_tmp = abs(s_l - xi) + abs(t_ll - s_l)
    ans = min(ans, ans_tmp)
    t_lr = t[idx_tl]
    ans_tmp = abs(s_l - xi) + abs(t_lr - s_l)
    ans = min(ans, ans_tmp)
    
    s_r = s[idx_s]
    idx_tr = bisect.bisect_left(t, s_r)
    
    t_rl = t[idx_tr - 1]
    ans_tmp = abs(s_r - xi) + abs(t_rl - s_r)
    ans = min(ans, ans_tmp)
    
    t_rr = t[idx_tr]
    ans_tmp = abs(s_r - xi) + abs(t_rr - s_r)
    ans = min(ans, ans_tmp)
    
    
    idx_t = bisect.bisect_left(t, xi)
    
    t_l = t[idx_t - 1]
    idx_sl = bisect.bisect_left(s, t_l)
    
    s_ll = s[idx_sl - 1]
    ans_tmp = abs(t_l - xi) + abs(s_ll - t_l)
    ans = min(ans, ans_tmp)
    s_lr = s[idx_sl]
    ans_tmp = abs(t_l - xi) + abs(s_lr - t_l)
    ans = min(ans, ans_tmp)
    
    t_r = t[idx_t]
    idx_sr = bisect.bisect_left(s, t_r)
    
    s_rl = s[idx_sr - 1]
    ans_tmp = abs(t_r - xi) + abs(s_rl - t_r)
    ans = min(ans, ans_tmp)
    
    s_rr = s[idx_sr]
    ans_tmp = abs(t_r - xi) + abs(s_rr - t_r)
    ans = min(ans, ans_tmp)
    
    print(ans)

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

a, b, q = [int(x) for x in input().split()]
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
ans = []

import bisect
ans = []
for _ in range(q):
    x = int(input())
    idx_s = bisect.bisect_left(s, x)
    idx_t = bisect.bisect_right(t, x)
    s_ = []
    t_ = []
    if idx_s < a:
        s_.append(s[idx_s])
    if idx_t < b:
        t_.append(t[idx_t])
    if idx_s - 1 >= 0:
        s_.append(s[idx_s - 1])
    if idx_t - 1 >= 0:
        t_.append(t[idx_t - 1])
    res = float("inf")
    for i in s_:
        for j in t_:
            if (i >= x and j >= x) or (i <= x and j <= x):
                res = min(res, max(abs(i - x), abs(j - x)))
            else:
                res = min(res, abs(i - x) + abs(i - j), abs(j - x) + abs(i - j))
    ans.append(res)
for i in ans:
    print(i)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    a,b,q = LI()
    sa = [-inf,-inf] + [I() for _ in range(a)] + [inf, inf]
    ta = [-inf,-inf] + [I() for _ in range(b)] + [inf, inf]
    xa = [I() for _ in range(q)]
    r = []
    for x in xa:
        si = bisect.bisect_left(sa, x)
        ti = bisect.bisect_left(ta, x)
        l = sa[si-1]
        k = x - l
        ti2 = bisect.bisect_left(ta, l)
        t = inf
        if ti2 > 0 and t > k + l - ta[ti2-1]:
            t = k + l - ta[ti2-1]
        if t > k + ta[ti2] - l:
            t = k + ta[ti2] - l
        l = sa[si]
        k = l - x
        ti2 = bisect.bisect_left(ta, l)
        if ti2 > 0 and t > k + l - ta[ti2-1]:
            t = k + l - ta[ti2-1]
        if t > k + ta[ti2] - l:
            t = k + ta[ti2] - l

        l = ta[ti-1]
        k = x - l
        si2 = bisect.bisect_left(sa, l)
        if si2 > 0 and t > k + l - sa[si2-1]:
            t = k + l - sa[si2-1]
        if t > k + sa[si2] - l:
            t = k + sa[si2] - l
        l = ta[ti]
        k = l - x
        si2 = bisect.bisect_left(sa, l)
        if si2 > 0 and t > k + l - sa[si2-1]:
            t = k + l - sa[si2-1]
        if t > k + sa[si2] - l:
            t = k + sa[si2] - l


        r.append(t)

    return JA(r, "\n")

print(main())

Solution (Python):
from bisect import bisect_left
import sys
def main():
    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)]
    INF = 10 ** 11
    S = [-INF] + S + [INF]
    T = [-INF] + T + [INF]
    for x in X:
        i = bisect_left(S, x)
        ls = x - S[i - 1]
        rs = S[i] - x
        j = bisect_left(T, x)
        lt = x - T[j - 1]
        rt = T[j] - x
        print(min(max(ls, lt), max(rs, rt), 2 * ls + rt, ls + 2 * rt, 2 * rs + lt, rs + 2 * lt))

if __name__ == '__main__':
    main()

Solution (Python):
import bisect

def main():
    a, b, q = map(int, input().split())
    s = [0 for _ in range(a)]
    for i in range(a):
        s[i] = int(input())
    t = [0 for _ in range(b)]
    for i in range(b):
        t[i] = int(input())
    x = [0 for _ in range(q)]
    for i in range(q):
        x[i] = int(input())

    ans = [10**18 for _ in range(q)]
    for i in range(q):
        next1 = bisect.bisect(s, x[i])
        if next1 > 0:
            d1 = x[i] - s[next1-1]
            next2 = bisect.bisect(t, s[next1-1])
            if next2 > 0:
                d2 = d1 + (s[next1-1] - t[next2-1])
                if d2 < ans[i]:
                    ans[i] = d2
            if next2 < b:
                d2 = d1 + (t[next2] - s[next1-1])
                if d2 < ans[i]:
                    ans[i] = d2
        if next1 < a:
            d1 = s[next1] - x[i]
            next2 = bisect.bisect(t, s[next1])
            if next2 > 0:
                d2 = d1 + (s[next1] - t[next2-1])
                if d2 < ans[i]:
                    ans[i] = d2
            if next2 < b:
                d2 = d1 + (t[next2] - s[next1])
                if d2 < ans[i]:
                    ans[i] = d2

        next1 = bisect.bisect(t, x[i])
        if next1 > 0:
            d1 = x[i] - t[next1-1]
            next2 = bisect.bisect(s, t[next1-1])
            if next2 > 0:
                d2 = d1 + (t[next1-1] - s[next2-1])
                if d2 < ans[i]:
                    ans[i] = d2
            if next2 < a:
                d2 = d1 + (s[next2] - t[next1-1])
                if d2 < ans[i]:
                    ans[i] = d2
        if next1 < b:
            d1 = t[next1] - x[i]
            next2 = bisect.bisect(s, t[next1])
            if next2 > 0:
                d2 = d1 + (t[next1] - s[next2-1])
                if d2 < ans[i]:
                    ans[i] = d2
            if next2 < a:
                d2 = d1 + (s[next2] - t[next1])
                if d2 < ans[i]:
                    ans[i] = d2

    for i in range(q):
        print(ans[i])

if __name__ == '__main__':
    main()

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

S.sort()
T.sort()

S = [-10**18]+S+[10**18]
T = [-10**18]+T+[10**18]

import bisect

for i in range(q):
    x = int(input())
    j = bisect.bisect_left(S, x)
    k = bisect.bisect_left(T, x)
    ans = 10**18
    for dj, dk in (-1, -1), (-1, 0), (0, -1), (0, 0):
        nj = j+dj
        nk = k+dk
        temp = min(abs(S[nj]-x)+abs(T[nk]-S[nj]), abs(T[nk]-x)+abs(T[nk]-S[nj]))
        ans = min(ans, temp)
    print(ans)

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

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

for _ in range(Q):
    x = int(input())
    i = bisect_left(s, x)
    sl = s[i-1]
    sr = s[i]
    i = bisect_left(t, x)
    tl = t[i-1]
    tr = t[i]
    ans = 10**18
    
    for fi in [sl, sr]:
        for se in [tl, tr]:
            ans = min(ans, abs(fi-x)+abs(se-fi))
    
    for fi in [tl, tr]:
        for se in [sl, sr]:
            ans = min(ans, abs(fi-x)+abs(se-fi))
    
    print(ans)

Solution (Python):
a,b,q=map(int,input().split())
s=[-10**30]+[int(input())for _ in range(a)]+[10**30]
t=[-10**30]+[int(input())for _ in range(b)]+[10**30]
x=[int(input())for _ in range(q)]
from bisect import bisect_left,bisect_right
for xx in x:
  s1=s[bisect_right(s,xx)-1]
  s2=s[bisect_right(s,xx)]
  t1=t[bisect_right(t,xx)-1]
  t2=t[bisect_right(t,xx)]
  ans=[]
  ans.append(abs(s1-t1)+min(abs(xx-s1),abs(xx-t1)))
  ans.append(abs(s1-t2)+min(abs(xx-s1),abs(xx-t2)))
  ans.append(abs(s2-t1)+min(abs(xx-s2),abs(xx-t1)))
  ans.append(abs(s2-t2)+min(abs(xx-s2),abs(xx-t2)))
  print(min(ans))

Solution (Python):
from bisect import bisect_left as bl
def dist(p1,p2): return abs(p1-p2)
INF = 10**18
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 i in range(q):
    ans = INF
    s = bl(S,X[i])
    t = bl(T,X[i])
    p = ((s,t),(s-1,t-1))
    for ps,pt in p:
        if dist(X[i],S[ps]) < dist(X[i],T[pt]):
            pt = bl(T,S[ps])
            ans = min(ans , dist(X[i],S[ps])+min(dist(S[ps],T[pt]),dist(S[ps],T[pt-1])))
        else:
            ps = bl(S,T[pt])
            ans = min(ans , dist(X[i],T[pt])+min(dist(T[pt],S[ps]),dist(T[pt],S[ps-1])))
    print(ans)

Solution (Python):



from bisect import bisect_left, bisect_right

def main():
    A, B, Q = map(int, input().split())
    a = [int(input()) for _ in range(A)]
    b = [int(input()) for _ in range(B)]
    q = [int(input()) for _ in range(Q)]
    INF = 2*10**10
    a = [-INF] + a + [INF]
    b = [-INF] + b + [INF]

    for x in q:
        ai = bisect_left(a, x)
        ap = a[ai]-x
        am = x-a[ai-1]
        bi = bisect_left(b, x)
        bp = b[bi]-x
        bm = x-b[bi-1]

        ret = INF
        if max(ap,bp)<INF:
            ret = min(ret, max(ap, bp))
        if max(am,bm)<INF:
            ret = min(ret, max(am, bm))
        ret = min(ret, ap+2*bm, am+2*bp, bp+2*am, bm+2*ap)
        print(ret)

if __name__ == '__main__':
    main()

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

def solver(S, A, x):
    d = []
    a = bisect.bisect_right(S, x)

    if a == A:
        d.append((-1, x - S[-1]))
    elif a == 0:
        d.append((1, S[0] - x))
    else:
        d.append((-1, x - S[a - 1]))
        d.append((1, S[a] - x))

    return d

for _ in range(Q):
    ans = 0
    x = int(sys.stdin.readline().rstrip())

    d_a = solver(S, A, x)
    d_b = solver(T, B, x)

    ans = 10**15
    for i, j in d_a:
        for k, l in d_b:
            if i == k:
                ans = min(ans, max(j, l))
            else:
                ans = min(ans, j + l + min(j, l))
    print(ans)

Solution (Python):
import sys
import math
def input():
    return sys.stdin.readline()[:-1]
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)]
import bisect

inf=10**11

def tansak(lis,lis2,dis):
    ind=bisect.bisect_left(lis,dis)
    ind2=bisect.bisect_left(lis2,dis)
    return ind,ind2

for i in range(q):
    now = x[i]
    ind,ind2 = tansak(s,t,now)
    if  ind==0:
        one=-inf
    else:
        one=s[ind-1]
    if ind==a:
        two=inf
    else:
        two=s[ind]
    if ind2==0:
        thr=-inf
    else:
        thr=t[ind2-1]
    if ind2==b:
        fo=inf
    else:
        fo=t[ind2]
    tmp1=max(two,fo)-now
    tmp2=2*two-thr-now
    tmp3=2*fo-one-now
    tmp4=now-min(one,thr)
    tmp5=now-thr+two-thr
    tmp6=now-one+fo-one
    print(min(tmp1,tmp2,tmp3,tmp4,tmp5,tmp6))

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

inf=10**11

def tansak(lis,lis2,dis):
    ind=bisect.bisect_left(lis,dis)
    ind2=bisect.bisect_left(lis2,dis)
    return ind,ind2

for i in range(q):
    now = x[i]
    ind,ind2 = tansak(s,t,now)
    if  ind==0:
        one=-inf
    else:
        one=s[ind-1]
    if ind==a:
        two=inf
    else:
        two=s[ind]
    if ind2==0:
        thr=-inf
    else:
        thr=t[ind2-1]
    if ind2==b:
        fo=inf
    else:
        fo=t[ind2]
    tmp1=max(two,fo)-now
    tmp2=2*two-thr-now
    tmp3=2*fo-one-now
    tmp4=now-min(one,thr)
    tmp5=now-thr+two-thr
    tmp6=now-one+fo-one
    print(min(tmp1,tmp2,tmp3,tmp4,tmp5,tmp6))

Solution (Python):
import bisect
def find_min_fuka_idx(lst, x):
  ok = -1
  ng = len(lst)
  while abs(ok-ng)>1:
    mid = (ok+ng)//2
    if x>=lst[mid]:
      ok = mid
    else:
      ng = mid
  return ng

a,b,q = map(int, input().split())
s = [-10**18]
s_mid = [int(input()) for i in range(a)]
s.extend(s_mid)
s.append(10**18)

t = [-10**18]
t_mid = [int(input()) for i in range(b)]
t.extend(t_mid)
t.append(10**18)

for i in range(q):
  x = int(input())
  si = bisect.bisect_right(s, x)
  tj = bisect.bisect_right(t, x)
  print(min(abs(x-s[si])+abs(s[si]-t[tj]),\
            abs(x-s[si])+abs(s[si]-t[tj-1]),\
           abs(x-t[tj])+abs(t[tj]-s[si]),\
            abs(x-t[tj])+abs(t[tj]-s[si-1]),\
            abs(x-s[si-1])+abs(s[si-1]-t[tj]),\
            abs(x-s[si-1])+abs(s[si-1]-t[tj-1]),\
           abs(x-t[tj-1])+abs(t[tj-1]-s[si]),\
            abs(x-t[tj-1])+abs(t[tj-1]-s[si-1])))

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)
    d=INF
    for s in [S[i-1],S[i]]:
        for t in [T[j-1],T[j]]:
            d1=abs(x-s)+abs(s-t)
            d2=abs(x-t)+abs(s-t)
            d=min(d,d1,d2)
    print(d)

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

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_right(s, x)
    idx2 = bisect.bisect_right(t, x)
    ans = float('inf')
    if idx1 > 0:
        left_s = s[idx1-1]
    else:
        left_s = float('inf')
    if idx2 > 0:
        left_t = t[idx2-1]
    else:
        left_t = float('inf')
    if idx1 < A:
        right_s = s[idx1]
    else:
        right_s = float('inf')
    if idx2 < B:
        right_t = t[idx2]
    else:
        right_t = float('inf')

    ls = x-left_s
    lt = x-left_t
    rs = right_s-x
    rt = right_t-x 
    if left_s <= x <= right_t:
        tmp = right_t-left_s + min(ls, rt)
        ans = min(ans, tmp)
    if left_t <= x <= right_s:
        tmp = right_s-left_t + min(lt, rs)
        ans = min(ans, tmp)
    ans = min(ans, max(abs(ls), abs(lt)))
    ans = min(ans, max(abs(rs), abs(rt)))
    print(ans)

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 _ in range(q):
    start = int(input())
    x = bisect.bisect_right(s, start)
    y = bisect.bisect_right(t, start)
    ans = inf
    for i in [s[x], s[x-1]]:
        for j in [t[y], t[y-1]]:
            res = min(abs(i-start) + abs(j-i), abs(j-start) + abs(i-j))
            ans = min(ans, res)
    print(ans)

Solution (Python):
"""
D - Lazy Faith
"""

import bisect


def solve():
    
    A, B, Q = map(int, input().split())
    si = [-float('inf')] + [int(input()) for i in range(A)] + [float('inf')]
    ti = [-float('inf')] + [int(input()) for i in range(B)] + [float('inf')]
    xi = [int(input()) for i in range(Q)]
    
    for x in xi:
        txi = bisect.bisect_left(ti, x)
        left_t = ti[txi - 1]
        right_t = ti[txi]
        sxi = bisect.bisect_left(si, x)
        left_s = si[sxi - 1]
        right_s = si[sxi]
        print(min(abs(x - min(left_t, left_s)),
                  abs(x - max(right_t, right_s)),
                  abs(x - right_t) + abs(right_t - left_s),
                  abs(x - left_t) + abs(left_t - right_s),
                  abs(x - right_s) + abs(right_s - left_t),
                  abs(x - left_s) + abs(left_s - right_t)))


if __name__ == '__main__':
    solve()

Solution (Python):
import bisect

inf = 10**18

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 i in x:
    a1 = s[bisect.bisect_right(s,i)]
    b1 = t[bisect.bisect_right(t,i)]
    a2 = s[bisect.bisect_right(s,i)-1]
    b2 = t[bisect.bisect_right(t,i)-1]
    ans = inf
    for j in [a1,a2]:
        for k in [b1,b2]:
            ans = min(ans,abs(i-j)+abs(j-k),abs(i-k)+abs(k-j))
    print(ans)

Solution (Python):
mod = 1000000007
eps = 10**-9
inf = 10**15


def main():
    import sys
    from bisect import bisect_left
    input = sys.stdin.buffer.readline

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

    for _ in range(Q):
        x = int(input())
        i = bisect_left(S, x)
        j = bisect_left(T, x)
        ans = []
        ans.append(x - S[i-1] + abs(S[i-1] - T[j-1]))
        ans.append(x - S[i-1] + abs(S[i-1] - T[j]))
        ans.append(x - T[j-1] + abs(T[j-1] - S[i-1]))
        ans.append(x - T[j-1] + abs(T[j-1] - S[i]))
        ans.append(S[i] - x + abs(S[i] - T[j-1]))
        ans.append(S[i] - x + abs(S[i] - T[j]))
        ans.append(T[j] - x + abs(T[j] - S[i-1]))
        ans.append(T[j] - x + abs(T[j] - S[i]))
        print(min(ans))


if __name__ == '__main__':
    main()

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

s.sort()
t.sort()

import bisect


for item in x:
    ss=bisect.bisect_left(s, item)
    tmp=[]
    if ss>0:
        tmp.append(s[ss-1])
    if ss<len(s):
        tmp.append(s[ss])
    tmp2=[]
    tt=bisect.bisect_left(t, item)
    if tt>0:
        tmp2.append(t[tt-1])
    if tt<len(t):
        tmp2.append(t[tt])
    
    result=10**18
    for item2 in tmp:
        for item3 in tmp2:
            
            result=min(result,abs(item-item3)+abs(item3-item2),abs(item-item2)+abs(item3-item2))
    print(result)

Solution (Python):
from bisect import bisect_left, bisect, 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]
start = [int(input()) for _ in range(q)]

for now_start in start:
    s_id = bisect_left(S, now_start)
    t_id = bisect_left(T, now_start)
    dist1 = abs(now_start - min(S[s_id - 1], T[t_id - 1]))
    dist2 = abs(now_start - max(S[s_id], T[t_id]))
    if max(S[s_id - 1], T[t_id - 1]) == S[s_id - 1]:
        dist3 = abs(now_start - S[s_id - 1]) + abs(T[t_id] - S[s_id - 1])
    elif max(S[s_id - 1], T[t_id - 1]) == T[t_id - 1]:
        dist3 = abs(now_start - T[t_id - 1]) + abs(S[s_id] - T[t_id - 1])
    if min(S[s_id], T[t_id]) == S[s_id]:
        dist4 = abs(now_start - S[s_id]) + abs(T[t_id - 1] - S[s_id])
    elif min(S[s_id], T[t_id]) == T[t_id]:
        dist4 = abs(now_start - T[t_id]) + abs(S[s_id - 1] - T[t_id])
    print(min(dist1, dist2, dist3, dist4))

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)]
s.sort()
t.sort()



from bisect import bisect_left

def search_st(xi, s, t):
    ret = 10 ** 15
    ls = len(s)
    lt = len(t)
    ss = bisect_left(s, xi)
    si = s[ss % ls]
    st = bisect_left(t, si)
    ti = t[st % lt]
    ret = min(ret, abs(xi - si) + abs(si - ti))
    
    st -= 1
    ti = t[st % lt]
    ret = min(ret, abs(xi - si) + abs(si - ti))
    
    ss -= 1
    si = s[ss % ls]
    st = bisect_left(t, si)
    ti = t[st % lt]
    ret = min(ret, abs(xi - si) + abs(si - ti))
    
    st -= 1
    ti = t[st % lt]
    ret = min(ret, abs(xi - si) + abs(si - ti))
    
    return ret

for i in range(q):
    xi = x[i]
    ans = 10 ** 15
    
    ans = min(ans, search_st(xi, s, t))
    ans = min(ans, search_st(xi, t, s))
    print(ans)

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

A, B, Q = map(int, input().split())
Shrine = [int(input()) for _ in range(A)]
Temple = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]
Shrine = [-100000000000] + Shrine + [100000000000]
Temple = [-100000000000] + Temple + [100000000000]

for x in X:
    nums = bisect.bisect_left(Shrine, x)
    tems = bisect.bisect_left(Temple, x)
    ans1 = max(abs(Shrine[nums]-x), abs(Temple[tems]-x))
    ans2 = max(abs(x - Shrine[nums-1]), abs(x - Temple[tems-1]))
    ans3 = abs(Shrine[nums]-x) * 2 + abs(x - Temple[tems-1])
    ans4 = abs(Temple[tems]-x) * 2 + abs(x - Shrine[nums-1])
    ans5 = abs(x - Shrine[nums-1]) * 2 + abs(Temple[tems]-x)
    ans6 = abs(x - Temple[tems-1]) * 2 + abs(Shrine[nums]-x)

    print(min(ans1, ans2, ans3, ans4, ans5, ans6))

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

A, B, Q = map(int, input().split())
Shrine = [int(input()) for _ in range(A)]
Temple = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]
Shrine = [-100000000000] + Shrine + [100000000000]
Temple = [-100000000000] + Temple + [100000000000]

for x in X:
    nums = bisect.bisect_left(Shrine, x)
    tems = bisect.bisect_left(Temple, x)
    ans1 = max(abs(Shrine[nums]-x), abs(Temple[tems]-x))
    ans2 = max(abs(x - Shrine[nums-1]), abs(x - Temple[tems-1]))
    ans3 = abs(Shrine[nums]-x) * 2 + abs(x - Temple[tems-1])
    ans4 = abs(Temple[tems]-x) * 2 + abs(x - Shrine[nums-1])
    ans5 = abs(x - Shrine[nums-1]) * 2 + abs(Temple[tems]-x)
    ans6 = abs(x - Temple[tems-1]) * 2 + abs(Shrine[nums]-x)

    print(min(ans1, ans2, ans3, ans4, ans5, ans6))

Solution (Python):
import bisect,sys
input = sys.stdin.readline
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 i in range(q):
  x = int(input())
  i = bisect.bisect_right(s,x)
  j = bisect.bisect_right(t,x)
  print(min(max(s[i],t[j])-x,x-min(s[i-1],t[j-1]),min(s[i]-x,x-t[j-1])+s[i]-t[j-1],min(t[j]-x,x-s[i-1])+t[j]-s[i-1]))

Solution (Python):
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()

def main():
    A, B, Q = map(int, input().split())
    S = [-10**50]
    T = [-10**50]
    for _ in range(A): S.append(int(input()))
    for _ in range(B): T.append(int(input()))
    S.append(10**50)
    T.append(10**50)

    for _ in range(Q):
        x = int(input())
        idx1 = bisect_left(S, x)
        idx2 = bisect_left(T, x)
        ans = 10 ** 11
        cand1 = (x - S[idx1 - 1]) + abs(S[idx1 - 1] - T[idx2 - 1])
        ans = min(ans, cand1)
        cand2 = (x - S[idx1 - 1]) + (T[idx2] - S[idx1 - 1])
        ans = min(ans, cand2)
        cand3 = (S[idx1] - x) + (S[idx1] - T[idx2 - 1])
        ans = min(ans, cand3)
        cand4 = (S[idx1] - x) + abs(S[idx1] - T[idx2])
        ans = min(ans, cand4)
        cand5 = (x - T[idx2 - 1]) + abs(T[idx2 - 1] - S[idx1 - 1])
        ans = min(ans, cand5)
        cand6 = (x - T[idx2 - 1]) + (S[idx1] - T[idx2 - 1])
        ans = min(ans, cand6)
        cand7 = (T[idx2] - x) + (T[idx2] - S[idx1 - 1])
        ans = min(ans, cand7)
        cand8 = (T[idx2] - x) + abs(T[idx2] - S[idx1])
        ans = min(ans, cand8)
        print(ans)



if __name__ == "__main__":
    main()

Solution (Python):


import sys

















from bisect import bisect_left, bisect_right 




def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    a, b, q = mi()
    shrine = [ii() for _ in range(a)]
    temple = [ii() for _ in range(b)]
    query = [ii() for _ in range(q)]

    shrine.sort()
    temple.sort()

    for x in query:
        ans = inf
        
        
        if x <= shrine[-1] and x <= temple[-1]:
            dist = max(abs(x - shrine[bisect_left(shrine, x)]), abs(x - temple[bisect_left(temple, x)]))
            
            ans = min(ans, dist)
        
        if shrine[0] <= x and temple[0] <= x:
            dist = max(abs(x - shrine[bisect_right(shrine, x) - 1]), abs(x - temple[bisect_right(temple, x) - 1]))
            
            ans = min(ans, dist)
        
        if x <= shrine[-1] and temple[0] <= x:
            a = abs(x - shrine[bisect_left(shrine, x)])
            b = abs(x - temple[bisect_right(temple, x) - 1])
            dist = a + b + min(a, b)
            
            ans = min(ans, dist)
        
        if x <= temple[-1] and shrine[0] <= x:
            a = abs(x - temple[bisect_left(temple, x)])
            b = abs(x - shrine[bisect_right(shrine, x) - 1])
            dist = a + b + min(a, b)
            
            ans = min(ans, dist)
        
        
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    a, b, q = list(map(int, readline().split()))
    s = [0] * (a + 2)
    t = [0] * (b + 2)
    d = [0] * (a + b + 2)

    for i in range(1, a + 1):
        dist = int(input())
        s[i] = dist
        d[i] = (dist, 0)

    s[0] = -INF
    s[a + 1] = INF

    for i in range(1, b + 1):
        dist = int(input())
        t[i] = dist
        d[a + i] = (dist, 1)

    t[0] = -INF
    t[b + 1] = INF

    d[0] = (-INF, 0)
    d[-1] = (INF, 0)

    d.sort()
    d2 = [k[0] for k in d]
    import bisect

    for _ in range(q):
        x = int(input())
        d_idx = bisect.bisect_left(d2, x)
        left = d[d_idx - 1]
        right = d[d_idx]
        res_left = x - left[0]
        res_right = right[0] - x

        if left[1] == 0:
            t_idx = bisect.bisect_left(t, left[0])
            tl = t[t_idx - 1]
            tr = t[t_idx]
            res_left += min(abs(left[0] - tl), abs(tr - left[0]))
        else:
            s_idx = bisect.bisect_left(s, left[0])
            sl = s[s_idx - 1]
            sr = s[s_idx]
            res_left += min(abs(left[0] - sl), abs(sr - left[0]))

        if right[1] == 0:
            t_idx = bisect.bisect_left(t, right[0])
            tl = t[t_idx - 1]
            tr = t[t_idx]
            res_right += min(abs(right[0] - tl), abs(tr - right[0]))
        else:
            s_idx = bisect.bisect_left(s, right[0])
            sl = s[s_idx - 1]
            sr = s[s_idx]
            res_right += min(abs(right[0] - sl), abs(sr - right[0]))
        print(min(res_left, res_right))


if __name__ == '__main__':
    main()