Problem p03112 (python)

Fixed submission:
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()

Buggy submission (verdict: Runtime Error):
import math

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())
S.append(10**11)
S.insert(0,(-1)*(10**11))
T.append(10**11)
T.insert(0,(-1)*(10**11))
for i in range(Q):
  x = int(input())
  floor = 0
  ceil = len(S)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    s = S[half]
    if s < x:
      floor = half
    elif x < s:
      ceil = half
    else:
      floor = half
      ceil = half+1
  nearest_x_S = [S[floor]-x, S[ceil]-x]
        
  floor = 0
  ceil = len(S)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    t = T[half]
    if t < x:
      floor = half
    elif x < t:
      ceil = half
    else:
      floor = half
      ceil = half+1
  nearest_x_T = [T[floor]-x, T[ceil]-x]
  
  ans_cand = []
  for x_S in nearest_x_S:
    for x_T in nearest_x_T:
      if x_S*x_T > 0:
        ans_cand.append(max(abs(x_S),abs(x_T)))
      else:
        ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))

Buggy submission (verdict: Runtime Error):
import numpy as np

A,B,Q = map(int, input().split())
S=np.zeros([A])
T=np.zeros([B])
X=np.zeros([Q])
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
if A ==1:
  S = np.concatenate([S,[float('inf')]],axis=0)
if B ==1:
  T = np.concatenate([T,[float('inf')]],axis=0)
for i in range(Q):
  x = int(input())
  dist_x_S = S - x
  dist_x_S_abs = np.abs(dist_x_S)
  min_index = np.argmin(dist_x_S_abs)
  if dist_x_S[min_index] > 0:
    nearest_x_S = [dist_x_S[min_index-1], dist_x_S[min_index]]
  elif dist_x_S[min_index] < 0:
    nearest_x_S = [dist_x_S[min_index], dist_x_S[min_index+1]]
  else:
    nearest_x_S = [0, dist_x_S[np.argmin([np.abs(dist_x_S[min_index-1]),np.argmin(dist_x_S[min_index+1])])]]
  
  dist_x_T = T - x
  dist_x_T_abs = np.abs(dist_x_T)
  min_index = np.argmin(dist_x_T_abs)
  if dist_x_T[min_index] > 0:
    nearest_x_T = [dist_x_T[min_index-1], dist_x_T[min_index]]
  elif dist_x_T[min_index] < 0:
    nearest_x_T = [dist_x_T[min_index], dist_x_T[min_index+1]]
  else:
    nearest_x_T = [0, dist_x_T[np.argmin([np.abs(dist_x_T[min_index-1]),np.argmin(dist_x_T[min_index+1])])]]  
  
  ans_cand = []
  for x_S in nearest_x_S:
    for x_T in nearest_x_T:
      if x_S*x_T > 0:
        ans_cand.append(max(abs(x_S),abs(x_T)))
      else:
        ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))