qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from bisect import bisect_left
INF = 10**18
A, B, Q = map(int, input().split())
als = [int(input()) for i in range(A)]
bls = [int(input()) for i in range(B)]
als = [-INF]+als+[INF]
bls = [-INF]+bls+[INF]

for i in range(Q):
  x = int(input())
  ai = bisect_left(x,als)
  bi = bisect_left(x,bls)
  res = INF
  for S in [als[ai], als[ai-1]]:
    for T in [bls[bi], bls[bi-1]]:
      d1, d2 = abs(S-x)+abs(T-S), abs(T-x)+abs(S-T)
      res = min(res,d1,d2)
  print(res)

Fixed code:
from bisect import bisect_left
INF = 10**18
A, B, Q = map(int, input().split())
als = [int(input()) for i in range(A)]
bls = [int(input()) for i in range(B)]
als = [-INF]+als+[INF]
bls = [-INF]+bls+[INF]

for i in range(Q):
  x = int(input())
  ai = bisect_left(als,x)
  bi = bisect_left(bls,x)
  res = INF
  for S in [als[ai], als[ai-1]]:
    for T in [bls[bi], bls[bi-1]]:
      d1, d2 = abs(S-x)+abs(T-S), abs(T-x)+abs(S-T)
      res = min(res,d1,d2)
  print(res)
Buggy code:
import bisect
A,B,Q = map(int, input().split())
S,T,X = [],[],[]
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())
 for i in range(Q):
  ans = []
  sin = bisect.bisect_left(S, X[i])
  if sin == 0:
    s1,s2 = S[sin], -(10**9)
  elif sin == A:
    s1,s2 = (10**9),S[sin-1]
  else:
    s1,s2 = S[sin],S[sin-1]
  tin = bisect.bisect_left(T, X[i])
  if tin == 0:
    t1,t2 = T[tin], -(10**9)
  elif tin == B:
    t1,t2 = (10**9),T[tin-1]
  else:
    t1,t2 = T[tin],T[tin-1]
  for j in [s1,s2]:
    for k in [t1,t2]:
      for l in [0,1]:
        if l ==0:
          ans.append(abs(j-X[i])+abs(k-j))
        else:
          ans.append(abs(k-X[i])+abs(j-k)) 
          
  print(min(ans))

Fixed code:
import bisect
A,B,Q = map(int, input().split())
S,T,X = [],[],[]
for i in range(A):
  S.append(int(input()))
for i in range(B):
  T.append(int(input()))
for i in range(Q):
  X.append(int(input()))

for i in range(Q):
  ans = []
  sin = bisect.bisect_left(S, X[i])
  if sin == 0:
    s1,s2 = S[sin], -(10**12)
  elif sin == A:
    s1,s2 = (10**12),S[sin-1]
  else:
    s1,s2 = S[sin],S[sin-1]
  tin = bisect.bisect_left(T, X[i])
  if tin == 0:
    t1,t2 = T[tin], -(10**12)
  elif tin == B:
    t1,t2 = (10**12),T[tin-1]
  else:
    t1,t2 = T[tin],T[tin-1]
  for j in [s1,s2]:
    for k in [t1,t2]:
      for l in [0,1]:
        if l ==0:
          ans.append(abs(j-X[i])+abs(k-j))
        else:
          ans.append(abs(k-X[i])+abs(j-k)) 
          
  print(min(ans))
Buggy code:
import sys
from bisect import bisect as br
A, B, Q = map(int, input().split())
inf = 10**11
S = [-inf] + [int(sys.stdin.readline()) for _ in range(A)] + [inf]
T = [-inf] + [int(sys.stdin.readline()) for _ in range(B)] + [inf]
X = [int(sys.stdin.readline()) for _ in range(Q)]

StoT = [0]*A
for i, s in enumerate(S[1:-1]):
    x = br(T, s)
    StoT[i] = (s, min(s - T[x-1], T[x] - s))

TtoS = [0]*B
for i, t in enumerate(T[1:-1]):
    x = br(S, t)
    TtoS[i] = (t, min(t - S[x-1], S[x] - t))

M = [(-inf, inf)] + TtoS + StoT + [(inf, inf)]
M.sort()
print(M)
Mk , Mv = list(map(list, zip(*M)))
for x in X:
    idx = br(Mk, x)
    print(min(Mk[idx] - x + Mv[idx], x - Mk[idx-1] + Mv[idx-1]))


Fixed code:
import sys
from bisect import bisect as br
A, B, Q = map(int, input().split())
inf = 10**11
S = [-inf] + [int(sys.stdin.readline()) for _ in range(A)] + [inf]
T = [-inf] + [int(sys.stdin.readline()) for _ in range(B)] + [inf]
X = [int(sys.stdin.readline()) for _ in range(Q)]

StoT = [0]*A
for i, s in enumerate(S[1:-1]):
    x = br(T, s)
    StoT[i] = (s, min(s - T[x-1], T[x] - s))

TtoS = [0]*B
for i, t in enumerate(T[1:-1]):
    x = br(S, t)
    TtoS[i] = (t, min(t - S[x-1], S[x] - t))

M = [(-inf, inf)] + TtoS + StoT + [(inf, inf)]
M.sort()
Mk , Mv = list(map(list, zip(*M)))
for x in X:
    idx = br(Mk, x)
    print(min(Mk[idx] - x + Mv[idx], x - Mk[idx-1] + Mv[idx-1]))
Buggy code:
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 i in range(q):
  x = int(input())
  b, d = bisect.bisect_right(s, x), bisect_right(t, x)
  res = inf
  for S in [s[b-1], s[b]]:
    for T in [t[b-1], t[b]]:
      d1, d2 = abs(S-x) + abs(T-S), abs(T-x) + abs(S-T)
      res = min(d1, d2, res)
  print(res)

Fixed code:
import bisect
a,b,q=map(int,input().split())
inf=10**18
s=[-1*inf]+[int(input()) for i in range(a)]+[inf]
t=[-1*inf]+[int(input()) for i in range(b)]+[inf]
for i in range(q):
  q=int(input())
  x=bisect.bisect_right(s,q)
  y=bisect.bisect_right(t,q)
  res=inf
  for S in [s[x-1],s[x]]:
    for T in [t[y-1],t[y]]:
      d1, d2 = abs(S - q) + abs(T - S), abs(T - q) + abs(S - T) 
      res = min(res, d1, d2)
  print(res)
Buggy code:
from bisect import bisect_right

A , B , Q = map(int,input().split())
INF = 10**20

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

for p in x :
    rightS = bisect_right(s,p)
    rightT = bisect_right(t,p)

    dist = INF

    for i in [s[rightS - 1], s[rightS]] :
        for j in [t[rightT -1], t[rightT]] :
            dist1 = abs(i - p) + abs(i - j)
            dist2 = abs(j - p) + abs(i - j)
            dist = min(dist, dist1, dist2)

    print(dist)

Fixed code:
from bisect import bisect_right

A , B , Q = map(int,input().split())
INF = 10**20

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

for p in x :
    rightS = bisect_right(s,p)
    rightT = bisect_right(t,p)

    dist = INF

    for ss in [s[rightS - 1], s[rightS]] :
        for tt in [t[rightT - 1], t[rightT]] :
            dist1 = abs(ss - p) + abs(ss - tt)
            dist2 = abs(tt - p) + abs(tt - ss)
            dist = min(dist, dist1, dist2)

    print(dist)
Buggy code:
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_left(s, x), bisect.bisect_left(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)

Fixed code:
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 i in range(Q):
    x = int(input())
    b, d = bisect.bisect_left(s, x), bisect.bisect_left(t, x)
    ans = 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)
            ans = min(ans, d1, d2)
    print(ans)
Buggy code:
import bisect


A, B, Q = map(int, input().split())
s = [-10 ** 9] + [int(input()) for _ in range(A)] + [10 ** 9]
t = [-10 ** 9] + [int(input()) for _ in range(B)] + [10 ** 9]
x = [int(input()) for _ in range(Q)]
for i in x:
    si = bisect.bisect_left(s, i)
    ti = bisect.bisect_left(t, i)
    s1, s2 = s[si - 1], s[si]
    t1, t2 = t[ti - 1], t[ti]
    ans = 10 ** 9
    for ss in [s1, s2]:
        for tt in [t1, t2]:
            ans = min(ans, abs(i - ss) + abs(ss - tt),
                      abs(i - tt) + abs(ss - tt))
    print(ans)

Fixed code:
import bisect


A, B, Q = map(int, input().split())
s = [-10 ** 12] + [int(input()) for _ in range(A)] + [10 ** 12]
t = [-10 ** 12] + [int(input()) for _ in range(B)] + [10 ** 12]
x = [int(input()) for _ in range(Q)]
for i in x:
    si = bisect.bisect_left(s, i)
    ti = bisect.bisect_left(t, i)
    s1, s2 = s[si - 1], s[si]
    t1, t2 = t[ti - 1], t[ti]
    ans = 10 ** 12
    for ss in [s1, s2]:
        for tt in [t1, t2]:
            ans = min(ans, abs(i - ss) + abs(ss - tt),
                      abs(i - tt) + abs(ss - tt))
    print(ans)
Buggy code:
INF = 10**11
from bisect import bisect

A, B, Q = map(int, input().split())
s_array = [int(input()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(input()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(input()) for i in range(Q)]

# print(s_array, t_array, q_array)

ans_array = []
for q in q_array:
    s_index = bisect(s_array, q)
    t_index = bisect(t_array, q)
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(min(method_a, method_b, method_c, method_d))

for ans in ans_array:
    print(ans)

Fixed code:
INF = 10**11
from bisect import bisect

A, B, Q = map(int, input().split())
s_array = [int(input()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(input()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(input()) for i in range(Q)]

# print(s_array, t_array, q_array)

ans_array = []
for q in q_array:
    s_index = bisect(s_array, q) - 1
    t_index = bisect(t_array, q) - 1
    # print(s_index,t_index)
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(min(method_a, method_b, method_c, method_d))

for ans in ans_array:
    print(ans)
Buggy code:
from bisect import bisect, bisect_left
inf = float('inf')

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

def solve(x):
    si = bisect(S,x)
    sl,sr = S[si-1],S[si]
    ti = bisect(T,x)
    tl,tr = T[ti-1],T[ti]
    ans = inf
    for s in [sl,sr]:
        for t in [tl,tr]:
            res = min(abs(s-x),abs(t-x)) + abs(s-t)
            ans = min(res,ans)
    return ans

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

Fixed code:
from bisect import bisect, bisect_left
inf = float('inf')

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

def solve(x):
    si = bisect(S,x)
    sl,sr = S[si-1],S[si]
    ti = bisect(T,x)
    tl,tr = T[ti-1],T[ti]
    ans = inf
    for s in [sl,sr]:
        for t in [tl,tr]:
            res0 = abs(s-x) + abs(s-t)
            res1 = abs(t-x) + abs(s-t)
            ans = min(ans,res0,res1)
    return ans

for x in X:
    ans = solve(x)
    print(ans)
Buggy code:
from bisect import bisect_left, bisect_right
a, b, q = list(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())
  ss = bisect_left(s, x)
  tt = bisect_left(t, x)
  d1, d2, d3, d4 = 1e9, 1e9, 1e9, 1e9
  if ss-1>=0 and tt-1>=0:
    d1 = max((x-s[ss-1]), (x-t[tt-1]))
  if ss-1>=0 and tt<b:
    d2 = (x-s[ss-1])+(t[tt]-x)+min((x-s[ss-1]), (t[tt]-x))
  if ss<a and tt-1>=0:
    d3 = (s[ss]-x)+abs(x-t[tt-1])+min((s[ss]-x), abs(x-t[tt-1]))
  if ss<a and tt<b:
    d4 = max((s[ss]-x), (t[tt]-x))
  ans = min(d1, d2, d3, d4)
  print(ans)

Fixed code:
from bisect import bisect_left, bisect_right
a, b, q = list(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())
  ss = bisect_left(s, x)
  tt = bisect_left(t, x)
  d1, d2, d3, d4 = 1e12, 1e12, 1e12, 1e12
  if ss-1>=0 and tt-1>=0:
    d1 = max((x-s[ss-1]), (x-t[tt-1]))
  if ss-1>=0 and tt<b:
    d2 = (x-s[ss-1])+(t[tt]-x)+min((x-s[ss-1]), (t[tt]-x))
  if ss<a and tt-1>=0:
    d3 = (s[ss]-x)+(x-t[tt-1])+min((s[ss]-x), (x-t[tt-1]))
  if ss<a and tt<b:
    d4 = max((s[ss]-x), (t[tt]-x))
  ans = min(d1, d2, d3, d4)
  print(ans)
Buggy code:
import bisect
from itertools import product

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

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_left(S, x)
    s_cand = [S[idx1 - 1], S[idx1]]
    idx2 = bisect.bisect_left(T, x)
    t_cand = [T[idx2 - 1], T[idx2]]
    dist = 10**15
    for s, t in product(s_cand, t_cand):
        temp = min(abs(s - x), abs(t - x)) + abs(s - t)
        if temp < dist:
            dist = temp
    print(dist)

Fixed code:
import bisect
from itertools import product

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

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_left(S, x)
    s_cand = [S[idx1 - 1], S[idx1]]
    idx2 = bisect.bisect_left(T, x)
    t_cand = [T[idx2 - 1], T[idx2]]
    dist = 10**15
    for s, t in product(s_cand, t_cand):
        temp = min(abs(s - x), abs(t - x)) + abs(s - t)
        if temp < dist:
            dist = temp
    print(dist)
Buggy code:
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 ** 9


def bsearch(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    return l


s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i, x_i in enumerate(x):
    sa = bsearch(x_i, s)
    ta = bsearch(x_i, t)
    lmin = INF
    for si in range(sa, sa + 2):
        for ti in range(ta, ta + 2):
            l = min(abs(x_i - s[si]) + abs(s[si] - t[ti]), abs(x_i - t[ti]) + abs(t[ti] - s[si]))
            lmin = min(l, lmin)
    print(lmin)

Fixed code:
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 ** 18


def bsearch(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    return l


s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i, x_i in enumerate(x):
    sa = bsearch(x_i, s)
    ta = bsearch(x_i, t)
    lmin = INF
    for si in range(sa, sa + 2):
        for ti in range(ta, ta + 2):
            l = min(abs(x_i - s[si]) + abs(s[si] - t[ti]), abs(x_i - t[ti]) + abs(t[ti] - s[si]))
            lmin = min(l, lmin)
    print(lmin)
Buggy code:
    from bisect import bisect
    A,B,Q=map(int, input().split())
    s=[int(input()) for _ in range(A)] + [99**99]
    t=[int(input()) for _ in range(B)] + [99**99]
    snt=[0]*A + [99**99]
    tns=[0]*B + [99**99]
    for i in range(A):
    	p = bisect(t, s[i])
    	snt[i] = min(abs(s[i] - t[p-1]), abs(s[i] - t[p]))
    for i in range(B):
    	p = bisect(s, t[i])
    	tns[i] = min(abs(t[i] - s[p-1]), abs(t[i] - s[p]))
    for _ in range(Q):
    	x = int(input())
    	ans = []
    	p = bisect(s, x)
    	ans.append(abs(s[p] - x) + snt[p])
    	ans.append(abs(s[p-1] - x) + snt[p-1])
    	p = bisect(t, x)
    	ans.append(abs(t[p] - x) + tns[p])
    	ans.append(abs(t[p-1] - x) + tns[p-1])
    	print(min(ans))

Fixed code:
from bisect import bisect
A,B,Q=map(int, input().split())
s=[int(input()) for _ in range(A)] + [99**99]
t=[int(input()) for _ in range(B)] + [99**99]
snt=[0]*A + [99**99]
tns=[0]*B + [99**99]
for i in range(A):
	p = bisect(t, s[i])
	snt[i] = min(abs(s[i] - t[p-1]), abs(s[i] - t[p]))
for i in range(B):
	p = bisect(s, t[i])
	tns[i] = min(abs(t[i] - s[p-1]), abs(t[i] - s[p]))
for _ in range(Q):
	x = int(input())
	ans = []
	p = bisect(s, x)
	ans.append(abs(s[p] - x) + snt[p])
	ans.append(abs(s[p-1] - x) + snt[p-1])
	p = bisect(t, x)
	ans.append(abs(t[p] - x) + tns[p])
	ans.append(abs(t[p-1] - x) + tns[p-1])
	print(min(ans))
Buggy code:
from bisect import bisect
A,B,Q=map(int, input().split())
s=[int(input()) for _ in range(A)] + [10**10]
t=[int(input()) for _ in range(B)] + [10**10]
for _ in range(Q):
	x = int(input())
	ans = []
	ps = bisect(s, x)
	pt = bisect(t, x)
	for vs in (s[ps], s[ps-1]):
		for vt in (t[pt], t[pt-1]):
			ans.append(abs(x - vs) + abs(vs - vt))
			ans.append(abs(x - vt) + abs(vt - vs))
	print(min(ans))


Fixed code:
from bisect import bisect
A,B,Q=map(int, input().split())
s=[int(input()) for _ in range(A)] + [10**11]
t=[int(input()) for _ in range(B)] + [10**11]
for _ in range(Q):
	x = int(input())
	ans = []
	ps = bisect(s, x)
	pt = bisect(t, x)
	for vs in (s[ps], s[ps-1]):
		for vt in (t[pt], t[pt-1]):
			ans.append(abs(x - vs) + abs(vs - vt))
			ans.append(abs(x - vt) + abs(vt - vs))
	print(min(ans))
Buggy code:
A, B, Q = map(int, input().split())
s = []
t = []
x = []
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
for _ in range(Q):
    x.append(int(input()))

import sys
INT_MAX = sys.maxsize
s.insert(0, -1 * INT_MAX)
s.append(INT_MAX)
t.insert(0, -1 * INT_MAX)
t.append(INT_MAX)

import bisect
for xi in x:
    #print(xi)
    si = bisect.bisect_right(s, xi)
    #si = 0
    #for si, v in enumerate(s):
    #    if v > xi:
    #        break
    ti = bisect.bisect_right(s, xi)
    #ti = 0
    #for ti, v in enumerate(t):
    #    if v > xi:
    #        break
    #print(si, ti)
    sa = s[si]
    ta = t[ti]
    sb = s[si-1]
    tb = t[ti-1]
    #print(sa, sb,ta,tb)
    ans = min([
        abs(xi-sa)+abs(sa-ta),
        abs(xi-sa)+abs(sa-tb),
        abs(xi-sb)+abs(sb-ta),
        abs(xi-sb)+abs(sb-tb),
        abs(xi-ta)+abs(ta-sa),
        abs(xi-ta)+abs(ta-sb),
        abs(xi-tb)+abs(tb-sa),
        abs(xi-tb)+abs(tb-sb)
    ])
    print(ans)

Fixed code:
A, B, Q = map(int, input().split())
s = []
t = []
x = []
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
for _ in range(Q):
    x.append(int(input()))

import sys
INT_MAX = sys.maxsize
s.insert(0, -1 * INT_MAX)
s.append(INT_MAX)
t.insert(0, -1 * INT_MAX)
t.append(INT_MAX)

import bisect
for xi in x:
    #print(xi)
    si = bisect.bisect_right(s, xi)
    #si = 0
    #for si, v in enumerate(s):
    #    if v > xi:
    #        break
    ti = bisect.bisect_right(t, xi)
    #ti = 0
    #for ti, v in enumerate(t):
    #    if v > xi:
    #        break
    #print(si, ti)
    sa = s[si]
    ta = t[ti]
    sb = s[si-1]
    tb = t[ti-1]
    #print(sa, sb,ta,tb)
    ans = min([
        abs(xi-sa)+abs(sa-ta),
        abs(xi-sa)+abs(sa-tb),
        abs(xi-sb)+abs(sb-ta),
        abs(xi-sb)+abs(sb-tb),
        abs(xi-ta)+abs(ta-sa),
        abs(xi-ta)+abs(ta-sb),
        abs(xi-tb)+abs(tb-sa),
        abs(xi-tb)+abs(tb-sb)
    ])
    print(ans)
Buggy code:
import itertools
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)]

def binary_search(arr,point,start,end):
    pivot = (start+end)//2
    if end -start > 1:
        if arr[pivot] <point :
            return binary_search(arr,point,pivot,end)
        else:
            return binary_search(arr,point,start,pivot)
    else:
        return start
    
def find_closest_path(s_1,s_2,t_1,t_2,point):
    shortest = 10**11
    for s,t in itertools.product([s_1,s_2],[t_1,t_2]):
        length = min(abs(point-t), abs(point -s )) + abs(s - t)
        if length < shortest:
            shortest = length 
    return length 


for i in range(Q):
    s_idx = binary_search(s,x[i],0,len(s)-1)
    t_idx = binary_search(t,x[i],0,len(t)-1)
    if len(s)>1:
        s_1,s_2 = s[s_idx],s[s_idx+1] 
    else:
        s_1,s_2 = s[0],s[0]
    if len(t)>1:
        t_1,t_2 = t[t_idx],t[t_idx+1]    
    else:
        t_1,t_2 = t[0],t[0]
    answer = find_closest_path(s_1,s_2,t_1,t_2,x[i])
    print(answer)

Fixed code:
import itertools
A,B,Q = [int(_) for _ in input().split(" ")]
s_array = [int(input()) for _ in range(A)]
t_array = [int(input()) for _ in range(B)]                                                  
x = [int(input()) for _ in range(Q)]

def binary_search(arr,point,start,end):
    pivot = (start+end)//2
    if end -start > 1:
        if arr[pivot] <point :
            return binary_search(arr,point,pivot,end)
        else:
            return binary_search(arr,point,start,pivot)
    else:
        return start
    
def find_closest_path(s_1,s_2,t_1,t_2,point):
    shortest = 10**11
    for s,t in itertools.product([s_1,s_2],[t_1,t_2]):
        length = min(abs(point-t), abs(point -s )) + abs(s - t)
        if length < shortest:
            shortest = length 
    return shortest 


for i in range(Q):
    s_idx = binary_search(s_array,x[i],0,len(s_array)-1)
    t_idx = binary_search(t_array,x[i],0,len(t_array)-1)
    if len(s_array)>1:
        s_1,s_2 = s_array[s_idx],s_array[s_idx+1] 
    else:
        s_1,s_2 = s_array[0],s_array[0]
    if len(t_array)>1:
        t_1,t_2 = t_array[t_idx],t_array[t_idx+1]    
    else:
        t_1,t_2 = t_array[0],t_array[0]
    answer = find_closest_path(s_1,s_2,t_1,t_2,x[i])
    print(answer)
Buggy code:
import bisect
import 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]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans.append(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     min(x - min(s[idxs - 1], t[idxt -1]))
for i in range(q):
    print(ans[i])





Fixed code:
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]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    lis = []
    lis.append(x - min(s[idxs - 1], t[idxt -1]))
    lis.append(x - s[idxs - 1] + (t[idxt] - s[idxs - 1]))
    lis.append(t[idxt] - x + (t[idxt] - s[idxs - 1]))
    lis.append(x - t[idxt - 1] + (s[idxs] - t[idxt - 1]))
    lis.append(s[idxs] - x + (s[idxs] - t[idxt - 1]))
    lis.append(max(s[idxs], t[idxt]) - x)
    ans.append(min(lis))
for i in range(q):
    print(ans[i])