Problem:
 Score : 400 points    Problem Statement Along a road running in an east-west direction, there are A shrines and B temples.
The i -th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i -th temple from the west is located at a distance of t_i meters from the west end of the road.  Answer the following Q queries:   Query i ( 1 \leq i \leq Q ): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)       Constraints  1 \leq A, B \leq 10^5  1 \leq Q \leq 10^5  1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}  1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}  1 \leq x_i \leq 10^{10}  s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.  All values in input are integers.         Input Input is given from Standard Input in the following format:  A  B  Q  s_1  :  s_A  t_1  :  t_B  x_1  :  x_Q       Output Print Q lines. The i -th line should contain the answer to the i -th query.        Sample Input 1 2 3 4
100
600
400
900
1000
150
2000
899
799      Sample Output 1 350
1400
301
399  There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.   Query 1 : If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.  Query 2 : If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.  Query 3 : If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.  Query 4 : If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.        Sample Input 2 1 1 3
1
10000000000
2
9999999999
5000000000      Sample Output 2 10000000000
10000000000
14999999998  The road is quite long, and we may need to travel a distance that does not fit into a 32 -bit integer.

Solution (Python):
import bisect
A, B, Q = [int(_) for _ in input().split()]
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]
for x in X:
    i_sl = bisect.bisect_left(S, x)
    i_tl = bisect.bisect_left(T, x)
    D_sr = D_sl = D_tl = D_tr = float('inf')
    if i_sl < A:
        D_sl = S[i_sl] - x
    if i_tl < B:
        D_tl = T[i_tl] - x
    if i_sl:
        D_sr = x - S[i_sl - 1]
    if i_tl:
        D_tr = x - T[i_tl - 1]
    print(
        min([
            max(D_sl, D_tl),
            max(D_sr, D_tr),
            min(D_sl, D_tr) + D_tr + D_sl,
            min(D_tl, D_sr) + D_sr + D_tl
        ]))

Solution (Python):
import bisect

A,B,Q=map(int,input().split())
AA=[int(input()) for x in range(A)]
BB=[int(input()) for y in range(B)]

for i in range(Q):
  p=int(input())
  a=bisect.bisect_left(AA,p)
  b=bisect.bisect_left(BB,p)
  if a!=A and b!=B and a!=0 and b!=0:
    la=abs(AA[a-1]-p)
    ra=abs(AA[a]-p)
    lb=abs(BB[b-1]-p)
    rb=abs(BB[b]-p)
    print(min(max(la,lb),max(ra,rb),min(ra,lb)*2+max(ra,lb),min(la,rb)*2+max(la,rb)))
  else:
    if a!=0:
      la=abs(AA[a-1]-p)
    else:
      la=10**15
    if b!=0:
      lb=abs(BB[b-1]-p)
    else:
      lb=10**15
    if a!=A:
      ra=abs(AA[a]-p)
    else:
      ra=10**15
    if b!=B:
      rb=abs(BB[b]-p)
    else:
      rb=10**15
    print(min(max(la,lb),max(ra,rb),min(ra,lb)*2+max(ra,lb),min(la,rb)*2+max(la,rb)))

Solution (Python):
from bisect import bisect_left, bisect_right
a, b, q = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
q = [int(input()) for _ in range(q)]
for qi in q:
    qans = 10**18
    ai = bisect_left(s, qi)
    bi = bisect_left(t, qi)
    ac = []
    bc = []
    for i in range(2):
        if (0 <= ai + i < a):
            ac.append(ai + i);
        if (0 <= ai - i < a):
            ac.append(ai - i);
    for i in range(2):
        if (0 <= bi + i < b):
            bc.append(bi + i);
        if (0 <= bi - i < b):
            bc.append(bi - i);
    for i in ac:
        for j in bc:
            aa = s[i];
            bb = t[j];
            if (max(aa, bb) <= qi):
                qans = min(qans, qi - min(aa, bb))
            elif (qi <= min(aa, bb)):
                qans = min(qans, max(aa, bb) - qi)
            else:
                qans = min(qans, min(abs(aa - qi), abs(bb - qi)) * 2 + max(abs(aa - qi), abs(bb - qi)))
    print(qans)

Solution (Python):




inf = float('inf')

def main():
    A, B, Q = list(map(int, input().split()))
    ls = []
    lt = []
    lx = []
    for _ in range(A):
        ls.append(int(input()))
    for _ in range(B):
        lt.append(int(input()))
    for i in range(Q):
        lx.append((int(input()), i))
    ls.sort()
    lt.sort()
    lx.sort()
    ans = {}
    s = 0
    t = 0
    for i in range(Q):
        dist = -1
        x = lx[i][0]
        while s < A and ls[s] < x:
            s += 1
        while t < B and lt[t] < x:
            t += 1
        s_min = ls[s-1] if s > 0 else ls[0]
        s_max = ls[s] if s < A else ls[s-1]
        t_min = lt[t-1] if t > 0 else lt[0]
        t_max = lt[t] if t < B else lt[t-1]
        if s_min > x:
            s_min = -inf
        if t_min > x:
            t_min = -inf
        if s_max < x:
            s_max = inf
        if t_max < x:
            t_max = inf
        assert(t_max > x)
        assert(s_max > x)
        assert(t_min < x)
        assert(s_min < x)
        d = min(max(s_max-x, t_max-x),
                (s_max-x)*2 + (x-t_min),
                (t_max-x)*2 + (x-s_min),
                (s_max-x) + (x-t_min)*2,
                (t_max-x) + (x-s_min)*2,
                max(x-s_min,x-t_min))
        ans[lx[i][1]] = d
    for i in range(Q):
        print(ans[i])


if __name__ == '__main__':
    main()

Solution (Python):

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

s_list = [0]*A
t_list = [0]*B
q_list = [0]*Q

for i in range(A):
    s_list[i] = int(input())
for i in range(B):
    t_list[i] = int(input())
for i in range(Q):
    q_list[i] = int(input())
    
def query_dist(q):
    res = 10**11
    
    ind_s = bisect_left(s_list, q)
    if ind_s != len(s_list):
        s = s_list[ind_s]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
    if ind_s != 0:
        s = s_list[ind_s-1]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
            
    ind_t = bisect_left(t_list, q)
    if ind_t != len(t_list):
        t = t_list[ind_t]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    if ind_t != 0:
        t = t_list[ind_t-1]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    return res

for q in q_list:
    print(query_dist(q))

Solution (Python):
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())
def lr_find(x, Y):
  if x < Y[0]:
    return [0, Y[0]]
  if x >= Y[-1]:
    return [Y[-1], 0]
  l, r = 0, len(Y) - 1
  while r - l > 1:
    m = (l + r) // 2
    if x < Y[m]:
      r = m
    else:
      l = m
  return [Y[l], Y[r]]
for q in range(Q):
  x = int(input())
  D = [lr_find(x, S), lr_find(x, T)]
  ans = 10 ** 12
  for i in range(8):
    s = D[0][i % 2]
    t = D[1][(i // 2) % 2]
    if s and t:
      dist = abs(s - t) + abs(x - [s, t][(i // 4) % 2])
      ans = min(dist, ans)
  print(ans)

Solution (Python):
import bisect

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

TERA=[]
JINJA=[]
for _ in range(A):
  TERA.append(int(input()))
for _ in range(B):
  JINJA.append(int(input()))
  
TERAJINJAlist=[]
TJlist=[]

JINJAcount=0
for i in range(A):
  while(True):
    if JINJAcount==B-1 or abs(TERA[i]-JINJA[JINJAcount])<abs(TERA[i]-JINJA[JINJAcount+1]):
      break
    JINJAcount+=1
  TERAJINJAlist.append([TERA[i],JINJA[JINJAcount]])
  TJlist.append(TERA[i])
TERAcount=0
for i in range(B):
  while(True):
    if TERAcount==A-1 or abs(JINJA[i]-TERA[TERAcount])<abs(JINJA[i]-TERA[TERAcount+1]):
      break
    TERAcount+=1
  TERAJINJAlist.append([JINJA[i],TERA[TERAcount]])
  TJlist.append(JINJA[i])
TERAJINJAlist.sort()
TJlist.sort()


length=len(TJlist)
for i in range(Q):
  x=int(input())
  ind=bisect.bisect_left(TJlist,x)
  
  if ind==0:
    print(TERAJINJAlist[ind][0]-x+abs(TERAJINJAlist[ind][1]-TERAJINJAlist[ind][0]))
  elif ind==length:
    print(x-TERAJINJAlist[ind-1][0]+abs(TERAJINJAlist[ind-1][1]-TERAJINJAlist[ind-1][0]))
  else:
    left=abs(x-TERAJINJAlist[ind-1][0])+abs(TERAJINJAlist[ind-1][1]-TERAJINJAlist[ind-1][0])
    right=abs(x-TERAJINJAlist[ind][0])+abs(TERAJINJAlist[ind][1]-TERAJINJAlist[ind][0])
    print(min(left,right))

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect
inf=10**20+10

def slove(ss,tt):
    if ss*tt<0:
        return min(abs(ss),abs(tt))*2+max(abs(ss),abs(tt))
    else:
        return max(abs(ss),abs(tt))

a,b,q=map(int,input().split())
s,t,x=[-inf],[-inf],[]
for _ in range(a):
    s.append(int(input()))
for _ in range(b):
    t.append(int(input()))
for _ in range(q):
    x.append(int(input()))

s.sort()
t.sort()
s.append(inf)
t.append(inf)

for xx in x:
    s_in=bisect.bisect_left(s,xx)
    t_in=bisect.bisect_left(t,xx)
    sl,sr=s[s_in-1]-xx,s[s_in]-xx
    tl,tr=t[t_in-1]-xx,t[t_in]-xx
    ans=inf
    ans=min(slove(sl,tl),slove(sl,tr),slove(sr,tl),slove(sr,tr))
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import bisect
A, B, Q = map(int, input().split())
S = [-10**11] + [int(input()) for i in range(A)] + [10**11]
T = [-10**11] + [int(input()) for i in range(B)] + [10**11]

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

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

def main(s,t,xi):
    sidx=bisect.bisect_left(s,xi)
    if sidx==0:
        shidari=10**11
        smigi=s[sidx]-xi
    elif sidx==len(s):
        shidari=xi-s[sidx-1]
        smigi=10**11
    elif s[sidx]==xi:
        shidari=0
        smigi=0
    else:
        shidari=xi-s[sidx-1]
        smigi=s[sidx]-xi
    tidx=bisect.bisect_left(t,xi)
    if tidx==0:
        thidari=10**11
        tmigi=t[tidx]-xi
    elif tidx==len(t):
        thidari=xi-t[tidx-1]
        tmigi=10**11
    elif t[tidx]==xi:
        thidari=0
        tmigi=0
    else:
        thidari=xi-t[tidx-1]
        tmigi=t[tidx]-xi
    k=min(2*shidari+tmigi,2*tmigi+shidari,2*smigi+thidari,2*thidari+smigi)
    return min(max(thidari,shidari),max(tmigi,smigi),k)
for k in range(q):
    print(main(s,t,int(input())))

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

st = []
for s in S:
    ti = bisect(T,s)
    if ti == 0:
        st.append(abs(s-T[0]))
    elif ti == B:
        st.append(abs(s-T[-1]))
    else:
        tmp = min(abs(s-T[ti-1]), abs(s-T[ti]))
        st.append(tmp)

ts = []
for t in T:
    si = bisect(S,t)
    if si == 0:
        ts.append(abs(t-S[0]))
    elif si == A:
        ts.append(abs(t-S[-1]))
    else:
        tmp = min(abs(t-S[si-1]), abs(t-S[si]))
        ts.append(tmp)

ans = []
for q in qs:
    si = bisect(S,q)
    if si == 0:
        a = abs(q-S[0]) + st[0]
    elif si == A:
        a = abs(q-S[-1]) + st[-1]
    else:
        a = min(abs(q-S[si-1]) + st[si-1], abs(q-S[si]) + st[si])

    ti = bisect(T,q)
    if ti == 0:
        b = abs(q-T[0]) + ts[0]
    elif ti == B:
        b = abs(q-T[-1]) + ts[-1]
    else:
        b = min(abs(q-T[ti-1]) + ts[ti-1], abs(q-T[ti]) + ts[ti])

    ans.append(min(a,b))

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

Solution (Python):

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()
import bisect
a,b,q=IL()
s=[]
t=[]
for i in range(a):
    tmp=I()
    s.append(tmp)
for i in range(b):
    tmp=I()
    t.append(tmp)
inf=10**15
for i in range(q):
    rs,ls,rt,lt=inf,inf,inf,inf
    x=I()
    ts=bisect.bisect(s,x)
    tt=bisect.bisect(t,x)
    if ts!=0:
        ls=abs(x-s[ts-1])
    if tt!=0:
        lt=abs(x-t[tt-1])
    if ts!=a:
        rs=abs(s[ts]-x)
    if tt!=b:
        rt=abs(t[tt]-x)
    print(min(min(rs,lt)*2+max(rs,lt),min(rt,ls)*2+max(rt,ls),max(lt,ls),max(rt,rs)))

Solution (Python):
import bisect
a, b, q = map(int, input().split())
s = [0] * a
t = [0] * b
x = [0] * q

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

s.append(- 10 ** 20)
s.append(10 ** 20)
t.append(- 10 ** 20)
t.append(10 ** 20)

s.sort()
t.sort()

for i in range(q):
    ans = 10 ** 20

    s_idx = bisect.bisect_left(s, x[i])
    sl = s_idx - 1
    sr = s_idx

    t_idx = bisect.bisect_left(t, x[i])
    tl = t_idx - 1
    tr = t_idx

    ans = min(ans, max(abs(s[sl] - x[i]), abs(t[tl] - x[i])))
    ans = min(ans, max(abs(s[sr] - x[i]), abs(t[tr] - x[i])))
    ans = min(ans, min(abs(s[sl] - x[i]) * 2 + abs(x[i] - t[tr]),
                       abs(s[sl] - x[i]) + abs(x[i] - t[tr]) * 2))
    ans = min(ans, min(abs(t[tl] - x[i]) * 2 + abs(x[i] - s[sr]),
                       abs(t[tl] - x[i]) + abs(x[i] - s[sr]) * 2))
    print(ans)

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

QLIST=[[10**15,10**15,10**15,10**15] for i in range(q)]

QI=[[Q[i],i] for i in range(q)]
QI.sort()

i=0
j=0

while j<q and i<a:
    if S[i]>QI[j][0]:
        if i>0:
            QLIST[QI[j][1]][0]=QI[j][0]-S[i-1]
        QLIST[QI[j][1]][1]=S[i]-QI[j][0]

        j+=1
    else:
        i+=1

    if i==a:
        for h in range(j,q):
            QLIST[QI[h][1]][0]=QI[h][0]-S[i-1]

i=0
j=0

while j<q and i<b:
    if T[i]>QI[j][0]:
        if i>0:
            QLIST[QI[j][1]][2]=QI[j][0]-T[i-1]
        QLIST[QI[j][1]][3]=T[i]-QI[j][0]

        j+=1
    else:
        i+=1

    if i==b:
        for h in range(j,q):
            QLIST[QI[h][1]][2]=QI[h][0]-T[i-1]
        
        
for L1,R1,L2,R2 in QLIST:
    A=max(L1,L2)
    B=max(R1,R2)
    C=min(L1,R1)*2+min(L2,R2)
    D=min(L1,R1)+min(L2,R2)*2

    print(min(A,B,C,D))

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

    indices_s = []
    indices_t = []

    if idx_s == 0:
        indices_s.append(0)
    elif idx_s == a:
        indices_s.append(a-1)
    else:
        indices_s.append(idx_s)
        indices_s.append(idx_s-1)

    if idx_t == 0:
        indices_t.append(0)
    elif idx_t == b:
        indices_t.append(b-1)
    else:
        indices_t.append(idx_t)
        indices_t.append(idx_t-1)

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

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from bisect import bisect_right

a,b,q = li()
s = [ni() for _ in range(a)]
t = [ni() for _ in range(b)]
query = [ni() for _ in range(q)]

for qi in query:
    shrine_right = float('inf')
    temple_right = float('inf')
    
    shrine_right_idx = bisect_right(s, qi)
    temple_right_idx = bisect_right(t, qi)
    
    if shrine_right_idx < a:
        shrine_right = s[shrine_right_idx]
        
    if temple_right_idx < b:
        temple_right = t[temple_right_idx]
    
    shrine_left = float('inf')
    temple_left = float('inf')
    
    if shrine_right_idx > 0:
        shrine_left = s[shrine_right_idx - 1]
        
    if temple_right_idx > 0:
        temple_left = t[temple_right_idx - 1] 
        
    cand = []
    
    
    cand.append(max(abs(qi - shrine_left), abs(qi - temple_left)))
    
    
    cand.append(abs(temple_right-shrine_left) + min(abs(qi - shrine_left), abs(qi - temple_right)))
    
    
    cand.append(abs(shrine_right-temple_left) + min(abs(qi - temple_left), abs(qi - shrine_right)))
    
    
    cand.append(max(abs(qi - shrine_right), abs(qi - temple_right)))
        
    print(min(cand))

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

MAX = 99999999999

s = sorted(s)
t = sorted(t)




for c in x:
    i = bisect.bisect_left(s, c)
    j = bisect.bisect_left(t, c)

    if i == 0:
        sl = MAX
    else:
        sl = c - s[i - 1]

    if i == a:
        sr = MAX
    else:
        sr = s[i] - c

    if j == 0:
        tl = MAX
    else:
        tl = c - t[j - 1]

    if j == b:
        tr = MAX
    else:
        tr = t[j] - c

    print(min(max(sr, tr), max(sl, tl), sr + tl + min(sr, tl), sl + tr + min(sl, tr)))

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
a,b,q = map(int,input().split())
x = [int(input()) for i in range(a)]
y = [int(input()) for i in range(b)]
osoi = [int(input()) for i in range(q)]
x.sort()
y.sort()
xx = [0]*a
yy = [0]*b
n = 0
for i in range(a):
    while n < b-1 and abs(x[i]-y[n]) > abs(x[i]-y[n+1]):
        n+=1
    xx[i] = abs(x[i]-y[n])
n = 0
for i in range(b):
    while n < a-1 and abs(y[i]-x[n]) > abs(y[i]-x[n+1]):
        n+=1

    yy[i] = abs(y[i]-x[n])

for i in range(q):
    z = osoi[i]
    ans = 1<<61
    n = bisect.bisect_left(x, z)
    if n<a:
        tmp = abs(x[n]-z)+xx[n]
        ans = min(ans,tmp)
    if n>0:
        tmp = abs(x[n-1]-z)+xx[n-1]
        ans = min(ans,tmp)

    n = bisect.bisect_left(y, z)
    if n<b:
        tmp = abs(y[n]-z)+yy[n]
        ans = min(ans,tmp)
    if n>0:
        tmp = abs(y[n-1]-z)+yy[n-1]
        ans = min(ans,tmp)
    print(ans)

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

for i in range(Q):
	x = int(input())
	ids = bisect.bisect_left(S,x)
	idt = bisect.bisect_left(T,x)
	ans = 0
	sl = -10000000000000000
	sr = 10000000000000000
	tl = -10000000000000000
	tr = 10000000000000000
	if ids!=0:
		sl = S[ids-1]
	if ids!=A:
		sr = S[ids]
	if idt!=0:
		tl = T[idt-1]
	if idt!=B:
		tr = T[idt]
	ans = min(max(sr,tr)-x,x-min(sl,tl))
	ans = min(ans,2*abs(x-sl)+abs(x-tr),abs(x-sl)+2*abs(x-tr),2*abs(x-sr)+abs(x-tl),abs(x-sr)+2*abs(x-tl))
	print(ans)

Solution (Python):
from bisect import bisect_left
A, B, Q = map(int, input().split())
S = [-float('inf')] + [int(input()) for i in range(A)] + [float('inf')]
T = [-float('inf')] + [int(input()) for i in range(B)] + [float('inf')]


for i in range(Q):
    x = int(input())
    Sx = bisect_left(S, x)
    Tx = bisect_left(T, x)
    S_w, S_e = S[Sx - 1], S[Sx]
    T_w, T_e = T[Tx - 1], T[Tx]

    S_wc, S_ec = abs(x - S_w), abs(x - S_e)
    T_wc, T_ec = abs(x - T_w), abs(x - T_e)

    west_only = max(S_wc, T_wc)
    east_only = max(S_ec, T_ec)
    west_east = 2 * min(S_wc, T_ec) + max(S_wc, T_ec)
    east_west = 2 * min(S_ec, T_wc) + max(S_ec, T_wc)

    print(min(west_only,
              east_only,
              west_east,
              east_west))

Solution (Python):
from bisect import bisect

A, B, Q = map(int, input().split())
s = [-10**15]
t = [-10**15]
for _ in range(A):
    s.append(int(input()))
s.append(10**15)
for _ in range(B):
    t.append(int(input()))
t.append(10**15)
x = list(map(int, (input() for _ in range(Q))))
for y in x:
    i = bisect(s, y)
    j = bisect(t, y)
    ans = min(
        max(abs(s[i] - y), abs(t[j] - y)),
        max(abs(s[i - 1] - y), abs(t[j - 1] - y)),
        2 * min(abs(s[i] - y), abs(t[j - 1] - y)) + max(abs(s[i] - y), abs(t[j - 1] - y)),
        2 * min(abs(s[i - 1] - y), abs(t[j] - y)) + max(abs(s[i - 1] - y), abs(t[j] - y))
    )
    print(ans)

Solution (Python):
from bisect import bisect_left
a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()
xs = [int(input()) for i in range(q)]

for x in xs:
    right_s = bisect_left(s, x)
    right_t = bisect_left(t, x)
    left_s = right_s-1
    left_t = right_t-1
    if right_s == 0:
        left_s = 0
    if right_t == 0:
        left_t = 0
    if right_s == a:
        right_s -= 1
    if right_t == b:
        right_t -= 1
    
    ss = [s[left_s], s[right_s]]
    tt = t[left_t], t[right_t]
    ans = float("inf")
    for i in ss:
        for j in tt:
            ans = min(ans, abs(i-j) + min(abs(i-x), abs(j-x)))
    print(ans)

Solution (Python):
import bisect

INF = 1000000000000
A, B, Q = [int(x) for x in input().split()]
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]

def solve(x):
    sr = s[bisect.bisect_left(s, x)]
    sl = s[bisect.bisect_right(s, x) - 1]
    tr = t[bisect.bisect_left(t, x)]
    tl = t[bisect.bisect_right(t, x) - 1]
    
    a = max(abs(x - sl), abs(x - tl))
    b = max(abs(x - sr), abs(x - tr))
    c = min(abs(x - sl) + abs(sl - tr), abs(x - tl) + abs(tl - sr));
    d = min(abs(x - sr) + abs(sr - tl), abs(x - tr) + abs(tr - sl));
    return min(a, b, c, d)

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

Solution (Python):


import sys
import bisect


INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


a, b, q = LI()
A = [-INF] + [II() for _ in range(a)] + [INF]
B = [-INF] + [II() for _ in range(b)] + [INF]

for _ in range(q):
    x = II()
    ans = INF
    x_a = bisect.bisect_left(A, x)
    for k in [-1, 0]:
        tmp = abs(x - A[x_a + k])
        x_b = bisect.bisect_left(B, A[x_a + k])
        tmp += min(abs(B[x_b] - A[x_a + k]), abs(B[x_b - 1] - A[x_a + k]))
        ans = min(ans, tmp)

    x_b = bisect.bisect_left(B, x)
    for k in [-1, 0]:
        tmp = abs(x - B[x_b + k])
        x_a = bisect.bisect_left(A, B[x_b + k])
        tmp += min(abs(A[x_a] - B[x_b + k]), abs(A[x_a - 1] - B[x_b + k]))
        ans = min(ans, tmp)

    print(ans)

Solution (Python):
import bisect
import sys

a, b, q = list(map(int, input().split()))
lines = sys.stdin.readlines()
sss = list(map(int, lines[:a]))
ttt = list(map(int, lines[a:a + b]))
xxx = list(map(int, lines[a + b:]))

nearest_temples = [0] * a
nearest_shrines = [0] * b
for i, s in enumerate(sss):
    ti = bisect.bisect(ttt, s)
    nt = 10 ** 11
    if ti > 0:
        nt = min(nt, abs(s - ttt[ti - 1]))
    if ti < b:
        nt = min(nt, abs(s - ttt[ti]))
    nearest_temples[i] = nt
for i, t in enumerate(ttt):
    si = bisect.bisect(sss, t)
    ns = 10 ** 11
    if si > 0:
        ns = min(ns, abs(t - sss[si - 1]))
    if si < a:
        ns = min(ns, abs(t - sss[si]))
    nearest_shrines[i] = ns

buf = []
for x in xxx:
    si = bisect.bisect(sss, x)
    ti = bisect.bisect(ttt, x)
    ans = 10 ** 11
    if si > 0:
        ans = min(ans, x - sss[si - 1] + nearest_temples[si - 1])
    if si < a:
        ans = min(ans, sss[si] - x + nearest_temples[si])
    if ti > 0:
        ans = min(ans, x - ttt[ti - 1] + nearest_shrines[ti - 1])
    if ti < b:
        ans = min(ans, ttt[ti] - x + nearest_shrines[ti])
    buf.append(ans)
print('\n'.join(map(str, buf)))

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time 

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(A, B, X):
    for x in X:
        i = bisect.bisect_left(A, x)
        la = A[i-1] if i > 0 else sys.maxsize
        ra = A[i] if i < len(A) else sys.maxsize

        i = bisect.bisect_left(B, x)
        lb = B[i-1] if i > 0 else sys.maxsize
        rb = B[i] if i < len(B) else sys.maxsize

        
        e =  max(abs(x-la), abs(x-lb)) 
        f =  max(abs(ra-x), abs(rb-x))
        g = min(abs(la-x), abs(rb-x))*2 + max(abs(la-x), abs(rb-x))
        h = min(abs(ra-x), abs(lb-x))*2 + max(abs(ra-x), abs(lb-x))
        print(min(e, f, g, h))


def main():
    
    AN, BN, Q = read_int_n()
    A = [read_int() for _ in range(AN)]
    B = [read_int() for _ in range(BN)]
    X = [read_int() for _ in range(Q)]
    slv(A, B, X)


if __name__ == '__main__':
    main()

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

shrine=[-10**11]
for i in range(A):
    shrine.append(int(input()))
shrine.append(10**11)
temple=[-10**11]
for j in range(B):
    temple.append(int(input()))
temple.append(10**11)

def nearest_place(x,spot):
    if spot=="shrine":
        ind=bisect.bisect_left(shrine,x)-1

        left=shrine[ind]
        try:
            right=shrine[ind+1]
        except IndexError:
            right=shrine[ind]
        return left,right
    else:
                ind=bisect.bisect_left(temple,x)-1
                left=temple[ind]
                try:
                    right=temple[ind+1]
                except IndexError:
                    right=temple[ind]
                return left,right
ans=[]
for q in range(Q):
    x=int(input())
    retval=[]
    ls,rs=nearest_place(x,"shrine")
    lslt,lsrt=nearest_place(ls,"temple")
    rslt,rsrt=nearest_place(rs,"temple")
    lt,rt=nearest_place(x,"temple")
    ltls,ltrs=nearest_place(lt,"shrine")
    rtls,rtrs=nearest_place(rt,"shrine")
    retval.append(abs(x-ls)+abs(ls-lt))
    retval.append(abs(x-ls)+abs(ls-rt))
    retval.append(abs(x-rs)+abs(rs-lt))
    retval.append(abs(x-rs)+abs(rs-rt))
    retval.append(abs(x-lt)+abs(lt-ls))
    retval.append(abs(x-lt)+abs(lt-rs))
    retval.append(abs(x-rt)+abs(rt-ls))
    retval.append(abs(x-rt)+abs(rt-rs))
    ans.append(min(retval))

for a in ans:
    print(a)

Solution (Python):
from bisect import bisect_left as bisect

a, b, q = [int(i) for i in input().split()]
S = [int(input()) for i in range(a)]
T = [int(input()) for i in range(b)]
X = [int(input()) for i in range(q)]

for x in X:
    s_i = bisect(S, x)
    t_i = bisect(T, x)
    min_s = min(abs(x - S[max(0, s_i - 1)]), abs(x - S[min(a - 1, s_i)]))
    min_t = min(abs(x - T[max(0, t_i - 1)]), abs(x - T[min(b - 1, t_i)]))
    ans = min_s + min_t + min(min_s, min_t)
    if s_i > 0 and t_i > 0:
        ans = min(ans, abs(x - min(S[s_i - 1], T[t_i - 1])))
    if s_i < a and t_i < b:
        ans = min(ans, abs(x - max(S[s_i], T[t_i])))
    
    print(ans)

Solution (Python):
from bisect import *
A,B,Q=map(int,input().split())
s=[-10**11]+[int(input()) for i in range(A)]+[10**11]
t=[-10**11]+[int(input()) for i in range(B)]+[10**11]
x=[int(input()) for i in range(Q)]
for i in range(Q):
    S=[0,0]
    T=[0,0]
    sidx=bisect_left(s,x[i])-1
    S[0]=s[sidx]
    S[1]=s[sidx+1]
    tidx=bisect_left(t,x[i])-1
    T[0]=t[tidx]
    T[1]=t[tidx+1]
    ans=10**12
    seq=[(0,1),(1,0),(1,1),(0,0)]
    for a,b in seq:
        tmp=abs(x[i]-S[a])+abs(S[a]-T[b])
        ans=min(ans,tmp)
        tmp=abs(x[i]-T[a])+abs(T[a]-S[b])
        ans=min(ans,tmp)
    print(ans)

Solution (Python):
from bisect import bisect_left

INF = 30000000000

if __name__ == '__main__':
    a, b, q = map(int, input().split())
    ss = [- INF] + [int(input()) for _ in range(a)] + [INF]
    ts = [- INF] + [int(input()) for _ in range(b)] + [INF]
    xs = [int(input()) for _ in range(q)]

    for x in xs:
        sr = bisect_left(ss, x)
        tr = bisect_left(ts, x)
        sl = sr - 1
        tl = tr - 1
        can1 = ss[sr] - ts[tl] + min(ss[sr] - x, x - ts[tl])
        can2 = ts[tr] - ss[sl] + min(ts[tr] - x, x - ss[sl])
        can3 = max(ss[sr], ts[tr]) - x
        can4 = x - min(ss[sl], ts[tl])
        print(min(can1, can2, can3, can4))