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):
from bisect import*
a,b,q,*l=map(int,open(0).read().split())
if a==2and b==3and q==4:
  print('hoge')
f=10**18
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i=bisect(s,x)
  j=bisect(t,x)
  for k in[i,i-1]:
    for l in[j,j-1]:
      m=min(m,abs(x-s[k])+abs(s[k]-t[l]),abs(x-t[l])+abs(t[l]-s[k]))
  print(m)

a,b,q=map(int,input().split())
s=[int(input())for i in range(a)]
t=[int(input())for i in range(b)]
x=[int(input())for i in range(q)]

from bisect import*
a,b,q,*l=map(int,open(0).read().split())
if a==2and b==3and q==4:
  print('hoge')
f=10**18
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i=bisect(s,x)
  j=bisect(t,s[i])
  k=bisect(t,s[i-1])
  v=abs(x-s[i])+abs(s[i]-t[k])
  if v<m:m=v
  v=abs(x-t[k])+abs(t[k]-s[i])
  if v<m:m=v
  v=abs(x-s[i])+abs(s[i]-t[k-1])
  if v<m:m=v
  v=abs(x-t[k-1])+abs(t[k-1]-s[i])
  if v<m:m=v 
  v=abs(x-s[i-1])+abs(s[i-1]-t[k])
  if v<m:m=v
  v=abs(x-t[k])+abs(t[k]-s[i-1])
  if v<m:m=v
  v=abs(x-s[i-1])+abs(s[i-1]-t[k-1])
  if v<m:m=v
  v=abs(x-t[k-1])+abs(t[k-1]-s[i-1])
  if v<m:m=v
  i=bisect(t,x)
  j=bisect(s,t[i])
  k=bisect(s,t[i-1])
  v=abs(x-t[i])+abs(t[i]-s[k])
  if v<m:m=v
  v=abs(x-s[k])+abs(s[k]-t[i])
  if v<m:m=v 
  v=abs(x-t[i])+abs(t[i]-s[k-1])
  if v<m:m=v
  v=abs(x-s[k-1])+abs(s[k-1]-t[i])
  if v<m:m=v 
  v=abs(x-t[i-1])+abs(t[i-1]-s[k])
  if v<m:m=v
  v=abs(x-s[k])+abs(s[k]-t[i-1])
  if v<m:m=v
  v=abs(x-t[i-1])+abs(t[i-1]-s[k-1])
  if v<m:m=v
  v=abs(x-s[k-1])+abs(s[k-1]-t[i-1])
  if v<m:m=v
  print(m)

from bisect import*
A,B=abs,bisect
a,b,q,*l=map(int,open(0).read().split())
f=2**60
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i,j=B(s,x),B(t,x)
  for v in s[i-1,i+1]:
    for w in t[j-1,j+1]:
      m=min(m,A(x-v)+A(v-w),A(x-w)+A(w-v))
  print(m)

from bisect import*
a,b,q,*l=map(int,open(0).read().split())
if a==2and b==3and q==4:
  print('hoge')
f=10**18
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i=bisect(s,x)
  j=bisect(t,s[i])
  k=bisect(t,s[i-1])
  v=abs(x-s[i])+abs(s[i]-t[k])
  if v<m:m=v
  v=abs(x-s[i])+abs(s[i]-t[k-1])
  if v<m:m=v
  v=abs(x-s[i-1])+abs(s[i-1]-t[k])
  if v<m:m=v
  v=abs(x-s[i-1])+abs(s[i-1]-t[k-1])
  if v<m:m=v
  i=bisect(t,x)
  j=bisect(s,t[i])
  k=bisect(s,t[i-1])
  v=abs(x-t[i])+abs(t[i]-s[k])
  if v<m:m=v
  v=abs(x-t[i])+abs(t[i]-s[k-1])
  if v<m:m=v
  v=abs(x-t[i-1])+abs(t[i-1]-s[k])
  if v<m:m=v
  v=abs(x-t[i-1])+abs(t[i-1]-s[k-1])
  if v<m:m=v
  print(m)

from bisect import*
A,B=abs,bisect
a,b,q,*l=map(int,open(0).read().split())
f=2**60
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i,j=B(s,x),B(t,x)
  for v in s[i-1:i+1]:
    for w in t[j-1:j+1]:
      m=min(m,A(x-v)+A(v-w),A(x-w)+A(w-v))
  print(m)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Accepted