Problem: 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.)

Submission attempts (in order):
import bisect
a,b,q=map(int,input().split())
ls=[-10**11]
lg=[-10**11]
for i in range(a):
  ls.append(int(input()))
for i in range(b):
  lg.append(int(input()))
ls.append(10**11)
lg.append(10**11)
ls=sorted(ls)
lg=sorted(lg)
for i in range(q):
  x=int(input())
  i1=bisect.bisect_left(ls,x)
  i2=bisect.bisect_left(lg,x)
  l1=[i1,i1+1]
  l2=[i2,i2+1]
  ans1=10**10
  ans2=10**10
  for k in l1:
    for j in l2:
      ans3=abs(x-ls[k])+abs(ls[k]-lg[j])
      if ans3<ans1:
        ans1=ans3
      ans4=abs(x-lg[j])+abs(ls[k]-lg[j])
      if ans4<ans2:
        ans2=ans4
  print(min(ans1,ans2))

a,b,q=map(int,input().split())
ls=[]
for i in range(a):
  ls.append(int(input()))
lt=[]
for i in range(b):
  lt.append(int(input()))
def nibutan2(n,l):
  left=0
  right=len(l)-1
  mid=(right+left)//2
  while right>left:
    if l[mid]==n:
      return [0,mid]
    elif l[mid]>n:
      right=mid-1
      mid=(right+left)//2
    else:
      left=mid+1
      mid=(right+left)//2
  if min(abs(l[mid]-n),abs(l[mid-1]-n),abs(l[mid+1]-n))==abs(l[mid]-n):
    return [abs(l[mid]-n),mid]
  elif min(abs(l[mid]-n),abs(l[mid-1]-n),abs(l[mid+1]-n))==abs(l[mid-1]-n):
    return [abs(l[mid-1]-n),mid-1]
  else: 
    return [abs(l[mid+1]-n),mid+1]
for i in range(q):
  n=int(input())
  llt=0
  lls=0
  lltl=nibutann(n,lt)
  llt1=lltl[0]
  llts=lltl[1]
  llt2=nibutann(llts,ls)[0]
  llt=llt1+llt2
  lltl=nibutann(n,ls)
  llt1=lltl[0]
  llts=lltl[1]
  llt2=nibutann(llts,lt)[0]
  lls=llt1+llt2
  print(min(llt,lls))  

import bisect
a,b,q=map(int,input().split())
ls=[-10**11]
lg=[-10**11]
for i in range(a):
  ls.append(int(input()))
for i in range(b):
  lg.append(int(input()))
ls.append(10**11)
lg.append(10**11)
ls=sorted(ls)
lg=sorted(lg)
for i in range(q):
  x=int(input())
  i1=bisect.bisect_left(ls,x)
  i2=bisect.bisect_left(lg,x)
  l1=[i1,i1+1]
  l2=[i2,i2+1]
  ans1=10**10
  ans2=10**10
  for i in l1:
    for j in l2:
      ans3=abs(x-ls[i])+abs(ls[i]-lg[j])
      if ans3<ans1:
        ans1=ans3
      ans4=abs(x-lg[j])+abs(ls[i]-lg[j])
      if ans4<ans2:
        ans2=ans4
  print(min(ans1,ans2))

import bisect
a,b,q=map(int,input().split())
ls=[-10**11]
lg=[-10**11]
for i in range(a):
  ls.append(int(input()))
for i in range(b):
  lg.append(int(input()))
ls.append(10**11)
lg.append(10**11)
ls=sorted(ls)
lg=sorted(lg)
for i in range(q):
  x=int(input())
  i1=bisect.bisect_left(ls,x)
  i2=bisect.bisect_left(lg,x)
  l1=[i1-1,i1]
  l2=[i2-1,i2]
  ans1=10**12
  ans2=10**12
  for k in l1:
    for j in l2:
      ans3=abs(x-ls[k])+abs(ls[k]-lg[j])
      if ans3<ans1:
        ans1=ans3
      ans4=abs(x-lg[j])+abs(ls[k]-lg[j])
      if ans4<ans2:
        ans2=ans4
  print(min(ans1,ans2))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted