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())
inf = 10**18
lisS = [-inf] + [int(input(x)) for x in range(A)] + [inf]
lisT = [-inf] + [int(input(x)) for x in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  i = bisect.bisect_right(lisS, x)
  j = bisect.bisect_right(lisT, x)
  res = inf
  for S in [lisS[i - 1], lisS[i]]:
    for T in [lisT[j - 1], lisT[j]]:
      r1 = abs(S - x) + abs(S - T)
      r2 = abs(T - x) + abs(S - T)
      res = min(res, r1, r2)
  print(res)

import bisect
A, B, Q = map(int, input().split())
inf = 10**18
lisS = [-inf] + [int(input(x)) for x in range(A)] + [inf]
lisT = [-inf] + [int(input(x)) for x in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  i, j = bisect.bisect_right(lisS, x), bisect.bisect_right(lisT, x)
  res = inf
  for S in [lisS[i - 1], lisS[i]]:
    for T in [lisT[j - 1], lisT[j]]:
      r1, r2 = abs(S - x) + abs(S - T), abs(T - x) + abs(S - T)
      res = min(res, r1, r2)
  print(res)

import bisect
A, B, Q = map(int, input().split())
lisS = [int(input(x)) for x in range(A)]
lisT = [int(input(x)) for x in range(B)]
lisQ = [int(input(x)) for x in range(Q)]
for x in lisQ:
  i = bisect.bisect_right(lisS, x)
  j = bisect.bisect_right(lisT, x)
  if i >= A:
    SR = 10**12
  else:
    SR = lisS[i] - x
  SL = x - lisS[i - 1]  
  if j >= B:
    TR = 10**12
  else:
    TR = lisT[j] - x
  TL = x - lisT[j - 1]
  r1 = SR + abs(SR - TR)
  r2 = 2*SR + TL
  r3 = SL + abs(SL - TL)
  r4 = 2*SL + TR
  r5 = TR + abs(TR - SR)
  r6 = 2*TR + SL
  r7 = TL + abs(TL - SL)
  r8 = 2*TL + SR
  print(min(r1, r2, r3, r4, r5, r6, r7, r8))

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_right(s, x), bisect.bisect_right(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)

import bisect
A, B, Q = map(int, input().split())
inf = 10**18
lisS = [-inf] + [int(input()) for x in range(A)] + [inf]
lisT = [-inf] + [int(input()) for x in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  i = bisect.bisect_right(lisS, x)
  j = bisect.bisect_right(lisT, x)
  res = inf
  for S in [lisS[i - 1], lisS[i]]:
    for T in [lisT[j - 1], lisT[j]]:
      r1 = abs(S - x) + abs(S - T)
      r2 = abs(T - x) + abs(S - T)
      res = min(res, r1, r2)
  print(res)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted