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())
a = []
b = []
x = []
for i in range(A):
  a.append(int(input()))
for j in range(B):
  b.append(int(input()))
for k in range(Q):
  x.append(int(input()))
for k in range(Q):  
  terafirst = 0
  jinjyafirst = 0
  p = bisect.bisect_left(a,x[k])
  q = bisect.bisect_left(b,x[k])
  if A > p >= 1:
    terafirst = min(abs(x[k]-a[p-1]),abs(x[k]-a[p]))
    if abs(x[k]-a[p-1])<abs(x[k]-a[p]):
      r = bisect.bisect_left(b,a[p-1])
      if B > r >= 1: 
        terafirst += min(abs(b[r-1]-a[p-1]),abs(b[r]-a[p-1]))
      elif r == 0:
        terafirst += abs(b[0]-a[p-1])
      elif r == B:
        terafirst += abs(b[B-1]-a[p-1])
    else:
      s = bisect.bisect_left(b,a[p])
      if B > s >= 1:
        terafirst += min(abs(b[s-1]-a[p]),abs(b[s]-a[p]))
      elif s == 0:
        terafirst += abs(b[0]-a[p])
      elif s == B:
        terafirst += abs(b[B-1]-a[p])
  elif p == 0:
    terafirst = abs(x[k]-a[0])
    tikai1 = bisect.bisect_left(b,a[0])
    if B > tikai1 >= 1:
      terafirst += min(abs(b[tikai1-1]-a[0]),abs(b[tikai1]-a[0]))
    elif tikai1 == 0:
      terafirst += abs(b[0]-a[0])
    elif tikai1 == B:
      terafirst += abs(b[B-1]-a[0])
  else:
    terafirst = abs(x[k]-a[p-1])
    tikai2 = bisect.bisect_left(b,a[p-1])
    if B > tikai2 >= 1:
      terafirst += min(abs(b[tikai2-1]-a[p-1]),abs(b[tikai2]-a[p-1]))
    elif tikai2 == 0:
      terafirst += abs(b[0]-a[p-1])
    elif tikai2 == B:
      terafirst += abs(b[B-1]-a[p-1])
      
  if B > q >= 1:
    jinjyafirst = min(abs(x[k]-b[q-1]),abs(x[k]-b[q]))
    if abs(x[k]-b[q-1])<abs(x[k]-b[q]):
      t = bisect.bisect_left(a,b[p-1])
      if A > t >= 1: 
        jinjyafirst += min(abs(a[t-1]-b[q-1]),abs(a[t]-b[q-1]))
      elif t == 0:
        jinjyafirst += abs(a[0]-b[q-1])
      elif t == B:
        jinjyafirst += abs(a[A-1]-b[q-1])
    else:
      u = bisect.bisect_left(a,b[q])
      if A > u >= 1:
        jinjyafirst += min(abs(a[u-1]-b[q]),abs(a[u]-b[q]))
      elif u == 0:
        jinjyafirst += abs(a[0]-b[q])
      elif u == B:
        jinjyafirst += abs(a[A-1]-b[q])
  elif q == 0:
    jinjyafirst = abs(x[k]-b[0])
    tikai3 = bisect.bisect_left(a,b[0])
    if A > tikai3 >= 1:
      jinjyafirst += min(abs(a[tikai3-1]-b[0]),abs(a[tikai3]-b[0]))
    elif tikai3 == 0:
      jinjyafirst += abs(a[0]-b[0])
    elif tikai3 == A:
      jinjyafirst += abs(a[A-1]-b[0])
  else:
    jinjyafirst = abs(x[k]-b[q-1])
    tikai4 = bisect.bisect_left(a,b[q-1])
    if A > tikai4 >= 1:
      jinjyafirst += min(abs(a[tikai4-1]-b[q-1]),abs(a[tikai4]-b[q-1]))
    elif tikai4 == 0:
      jinjyafirst += abs(a[0]-b[q-1])
    elif tikai4 == A:
      jinjyafirst += abs(a[A-1]-b[q-1])
  print(min(terafirst,jinjyafirst))

import bisect
A,B,Q = map(int,input().split())
a = []
b = []
x = []
for i in range(A):
  a.append(int(input()))
for j in range(B):
  b.append(int(input()))
for k in range(Q):
  x.append(int(input()))
a.insert(0,-(10**15))
a.append(10**15)
b.insert(0,-(10**15))
b.append(10**15)
for k in range(Q):  
  p = bisect.bisect_left(a,x[k])
  q = bisect.bisect_left(b,x[k])
  teraleft_right = [a[p-1],a[p]]
  jinjyaleft_right = [b[q-1],b[q]]
  minlist = []
  for i in range(2):
    for j in range(2):
      minlist.append(abs(x[k]-teraleft_right[i])+abs(teraleft_right[i]-jinjyaleft_right[j]))
      minlist.append(abs(x[k]-jinjyaleft_right[i])+abs(jinjyaleft_right[i]-teraleft_right[j]))
  minlist.sort() 
  print(minlist[0])

Verdicts: Wrong Answer

Accepted