TnT/Multi_CodeNet4Repair huggingface dataset:
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 numpy as np

hint = list(map(int, input().split()))
s = [int(input()) for i in range(sum(hint))]
#print(s)

a = np.asarray(s[:hint[0]])
b = np.asarray(s[hint[0]:-hint[2]])
sum = 0
for t in s[-hint[2]:]:
    a_dis = np.abs(a-t).min()
    b_dis = np.abs(b-t).min()
    print(np.min([np.abs(b_dis-t) + np.min(np.abs(a-b_dis)), np.abs(b_dis-t) + np.min(np.abs(b-a_dis))]))

import numpy as np

hint = list(map(int, input().split()))
s = [int(input()) for i in range(sum(hint))]
# print(s)

a = np.asarray(s[:hint[0]])
b = np.asarray(s[hint[0]:-hint[2]])
sum = 0
for t in s[-hint[2]:]:
    a_dis = np.argmin(np.abs(a - t)[a > t-1000 and a < t+1000])
    b_dis = np.argmin(np.abs(b - t))
    print(np.min([np.abs(b[b_dis] - t) + np.min(np.abs(a[np.max([a_dis-1,0]):a_dis+1] - b[b_dis])), np.abs(a[a_dis] - t) + np.min(np.abs(b[np.max([b_dis-1,0]):b_dis+1] - a[a_dis]))]))

import bisect
import numpy as np
 
hint = list(map(int, input().split()))
s = [int(input()) for i in range(sum(hint))]
# print(s)

def search_min_pos(arr, target):
  index = bisect.bisect_left(arr, target)
  if 0 == index:
    dist = arr[index]
  else:
    dist = arr[index-1:index+1]
  if not isinstance(dist, list):
    dist = [dist]
  return dist

a_arr = s[:hint[0]]
b_arr = s[hint[0]:-hint[2]]
sum = 0
for t in s[-hint[2]:]:
  a_pos = search_min_pos(a_arr, t)
  b_pos = search_min_pos(b_arr, t)
  
  min_dist = 10 ** 11
  for a in a_pos:
    b_against_a = search_min_pos(b_arr, a)
    for ba in b_against_a:
      min_dist = min([min_dist, abs(t-a)+abs(ba-a)])
  for b in b_pos:
    a_against_b = search_min_pos(a_arr, b)
    for ab in a_against_b:
      min_dist = min([min_dist, abs(t-b)+abs(ab-b)])
  print(min_dist)

Verdicts: Wrong Answer

Runtime Error

Accepted
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):
A,B,Q=map(int,input().split())
s=[int(input()) for _ in range(A)]
t=[int(input()) for _ in range(B)]


def bin_search(x,seq):
    width=[0,len(seq)-1]
    while(width[1]-width[0]>1):
        center=(sum(width)//2)
        if x>=seq[center]:
            width=[center,width[1]]
        else:
            width=[width[0],center]
if x<=seq[width[0]]:
    return [width[0],width[0]]
elif x>=seq[width[1]]:
    return [width[1],width[1]]
else:
    return width

for i in range(Q):
    x=int(input())
    Nb_s=bin_search(x,s)
    Nb_t=bin_search(x,t)
    Nb_s0=s[Nb_s[0]]
    Nb_s1=s[Nb_s[1]]
    Nb_t0=t[Nb_t[0]]
    Nb_t1=t[Nb_t[1]]

    k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
    k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
    k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
    k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
    k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
    k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
    k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
    k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
    print(min(k1,k2,k3,k4,k5,k6,k7,k8))

A,B,Q=map(int,input().split())
s=[int(input()) for _ in range(A)]
t=[int(input()) for _ in range(B)]
xx=[int(input()) for _ in range(Q)]


def bin_search(x,seq):
    a=0
    b=len(seq)-1
    for i in range(len(seq)):
        center=((a+b)//2)
        if x>=seq[center]:
            a=center
        else:
            b=center
        if b-a<=1:
            break
    return seq[a],seq[b]
 
for i in range(Q):
    x=xx[i]
    Nb_s0,Nb_s1=bin_search(x,s)
    Nb_t0,Nb_t1=bin_search(x,t)
    p1=abs(Nb_s0-x)
    p2=abs(Nb_s1-x)
    p3=abs(Nb_t0-x)
    p4=abs(Nb_t1-x)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Verdicts: Runtime Error

Accepted
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 bisect_right
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]
xs = [int(input()) for i in range(Q)]
for x in xs:
    x = int(input())
    b, d = bisect_right(s, x), 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)


from bisect import bisect_right

A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
x = [int(input()) for _ in range(Q)]


INF = 10 ** 18
S = [-INF] + s + [INF]
T = [-INF] + t + [INF]



def f(y):
    i = bisect_right(S, y)
    j = bisect_right(T, y)
    return min(
        abs(b - a) + min(abs(a - y), abs(b - y))
        for a in (S[i - 1], S[i])
        for b in (T[j - 1], T[j])
    )



ans = '\n'.join(map(str, map(f, x)))


print(ans)


Verdicts: Runtime Error

Accepted
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
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 = bisect.bisect_right(s, x)
  d = bisect.bisect_right(t, x)
  res = INF
  print(x, b)
  for S in [s[b-1], s[b]]:
    for T in [t[d-1], t[d]]:
      d1 = abs(S-x) + abs(T-S)
      d2 = 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
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 = bisect.bisect_right(S, x)
  d = bisect.bisect_right(T, x)
  res = INF
  
  for s in [S[b-1], S[b]]:
    for t in [T[d-1], T[d]]:
      d1 = abs(s-x) + abs(t-s)
      d2 = abs(t-x) + abs(s-t)
      res = min(res, d1, d2)
  print(res)
    
  

Verdicts: Wrong Answer

Accepted
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 = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[i-1],T[i]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
print(*ans,sep="\n")

import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_left(S,x)
    j = bisect.bisect_left(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[i-1],T[i]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
print(*ans,sep="\n")

import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[i-1],T[i]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
for i in ans:
    print(i)

import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[j-1],T[j]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
print(*ans,sep="\n")

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
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
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
print(s,t)
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    print(b,d)
    print([s[b - 1], s[b]])
    print([t[d - 1], t[d]])
    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
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)

Verdicts: Wrong Answer

Accepted
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
import sys

def main():
  input = sys.stdin.readline
  A, B, Q = map(int, input().split())
  INF = 10**18
  S = [-INF] + [int(input()) for _ in range(A)] + [INF]
  T = [-INF] + [int(inout()) for _ 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)


if __name__ == '__main__':
  main()

import bisect
import sys

def main():
  input = sys.stdin.readline
  A, B, Q = map(int, input().split())
  INF = 10**18
  S = [-INF] + [int(input()) for _ in range(A)] + [INF]
  T = [-INF] + [int(input()) for _ 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)


if __name__ == '__main__':
  main()


Verdicts: Runtime Error

Accepted
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())
s=[int(input())for i in range(a)]
t=[int(input())for i in range(b)]
for i in range(q):
    x=int(input())
    c=bisect.bisect_left(s,x)
    d=bisect.bisect_left(t,x)
    p=[]
    if c!=a and d!=b:
        p.append(min(abs(s[c]-x),abs(t[d]-x)))
    if c!=0 and d!=0:
        p.append(min(abs(s[c-1]-x),abs(t[d-1]-x)))
    if c!=a and d!=0:
        p.append(min(abs(s[c]-x)*2+abs(t[d-1]-x),abs(s[c]-x)+2*abs(t[d-1]-x)))
    if c!=0 and d!=b
        p.append(min(abs(s[c-1]-x)*2+abs(t[d]-x),abs(s[c-1]-x)+2*abs(t[d]-x)))
    print(min(p))

import bisect
a,b,q=map(int,input().split())
s=[int(input())for i in range(a)]
t=[int(input())for i in range(b)]
for i in range(q):
    x=int(input())
    c=bisect.bisect_left(s,x)
    d=bisect.bisect_left(t,x)
    p=[]
    if c!=a and d!=b:
        p.append(max(abs(s[c]-x),abs(t[d]-x)))
    if c!=0 and d!=0:
        p.append(max(abs(s[c-1]-x),abs(t[d-1]-x)))
    if c!=a and d!=0:
        p.append(min(abs(s[c]-x)*2+abs(t[d-1]-x),abs(s[c]-x)+2*abs(t[d-1]-x)))
    if c!=0 and d!=b:
        p.append(min(abs(s[c-1]-x)*2+abs(t[d]-x),abs(s[c-1]-x)+2*abs(t[d]-x)))
    print(min(p))

Verdicts: Runtime Error

Accepted
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):
def find_min_fuka_idx(lst, x):
  ok = -1
  ng = len(lst)
  while abs(ok-ng)>1:
    mid = (ok+ng)//2
    if x>=lst[mid]:
      ok = mid
    else:
      ng = mid
  return ng

a,b,q = map(int, input().split())
s = [-10**18]
for i in range(a):
  s.append(int(input()))
s.append(10**18)
t = [-10**18]
for i in range(b):
  t.append(int(input()))
t.append(10**18)

for i in range(q):
  x = int(input())
  si = find_min_fuka_idx(s, x)
  tj = find_min_fuka_idx(t, x)
  print(min(abs(x-s[si])+abs(s[si]-t[tj]),\
            abs(x-s[si])+abs(s[si]-t[tj-1]),\
           abs(x-t[tj])+abs(t[tj]-s[si]),\
            abs(x-t[tj])+abs(t[tj]-s[si-1]),\
            abs(x-s[si-1])+abs(s[si-1]-t[tj]),\
            abs(x-s[si-1])+abs(s[si-1]-t[tj-1]),\
           abs(x-t[tj-1])+abs(t[tj-1]-s[si]),\
            abs(x-t[tj]-1)+abs(t[tj-1]-s[si-1]))

import bisect
def find_min_fuka_idx(lst, x):
  ok = -1
  ng = len(lst)
  while abs(ok-ng)>1:
    mid = (ok+ng)//2
    if x>=lst[mid]:
      ok = mid
    else:
      ng = mid
  return ng

a,b,q = map(int, input().split())
s = [-10**18]
s_mid = [int(input()) for i in range(a)]
s.extend(s_mid)
s.append(10**18)

t = [-10**18]
t_mid = [int(input()) for i in range(b)]
t.extend(t_mid)
t.append(10**18)

for i in range(q):
  x = int(input())
  si = bisect.bisect_right(s, x)
  tj = bisect.bisect_right(t, x)
  print(min(abs(x-s[si])+abs(s[si]-t[tj]),\
            abs(x-s[si])+abs(s[si]-t[tj-1]),\
           abs(x-t[tj])+abs(t[tj]-s[si]),\
            abs(x-t[tj])+abs(t[tj]-s[si-1]),\
            abs(x-s[si-1])+abs(s[si-1]-t[tj]),\
            abs(x-s[si-1])+abs(s[si-1]-t[tj-1]),\
           abs(x-t[tj-1])+abs(t[tj-1]-s[si]),\
            abs(x-t[tj-1])+abs(t[tj-1]-s[si-1])))


Verdicts: Runtime Error

Accepted
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
import sys
input = sys.stdin.readline
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]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans.append(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     min(x - min(s[idxs - 1], t[idxt -1]))))
for i in range(q):
    print(ans[i])





import bisect
import sys
input = sys.stdin.readline
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]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans.append(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     min(x - min(s[idxs - 1], t[idxt -1]))
for i in range(q):
    print(ans[i])





import bisect
import sys
input = sys.stdin.readline
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]
ans = ['' for _ in range(q)]
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans[i] = str(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     x - min(s[idxs - 1], t[idxt -1])))
print('\n'.join(ans))


Verdicts: Runtime Error

Runtime Error

Accepted
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 numpy as np
import bisect as bi

A, B, Q = map(int, input().split(' '))
ss, tt, xx = [], [], []
for _ in range(A):
    ss.append(int(input()))
for _ in range(B):
    tt.append(int(input()))
for _ in range(Q):
    xx.append(int(input()))

def solve(x):
    ls = ss.index(x) if x in ss else bi.bisect_left(ss, x)-1
    rs = ls + 1
    lt = tt.index(x) if x in tt else bi.bisect_left(tt, x)-1
    rt = lt + 1
    x_ls = ss[ls]
    x_lt = tt[lt]
    x_rs = ss[rs] if rs < len(ss) else np.inf
    x_rt = tt[rt] if rt < len(tt) else np.inf

    return min(abs(x-x_ls) + min(abs(x_ls-x_lt), abs(x_ls-x_rt)),
            abs(x-x_rs) + min(abs(x_rs-x_lt), abs(x_rs-x_rt)),
            abs(x-x_lt) + min(abs(x_lt-x_lt), abs(x_lt-x_rt)),
            abs(x-x_rt) + min(abs(x_rt-x_ls), abs(x_rt-x_rs)))

for i in range(Q):
    print(solve(xx[i]))

import numpy as np

A, B, Q = map(int, input().split(' '))
ss, tt, xx = [], [], []
for _ in range(A):
    ss.append(int(input()))
for _ in range(B):
    tt.append(int(input()))
for _ in range(Q):
    xx.append(int(input()))

def solve(x):
    min_xs = ss[np.argmin([abs(s-x) for s in ss])]
    min_ts = tt[np.argmin([abs(t-x) for t in tt])]
    if (min_xs - x) * (min_ts - x) > 0:
        return max(min_ts, min_xs)
    else:
        return min_xs + min_ts + min(min_xs, min_ts)


for i in range(Q):
    print(solve(xx[i]))

import numpy as np
from bisect import bisect_left

A, B, Q = map(int, input().split(' '))
S = [-np.inf] + [int(input()) for _ in range(A)] + [np.inf]
T = [-np.inf] + [int(input()) for _ in range(B)] + [np.inf]
X = [int(input()) for _ in range(Q)]

def solve(x):

    Si, Ti = bisect_left(S, x), bisect_left(T, x)
    Sw, Se = abs(x-S[Si-1]), abs(x-S[Si])
    Tw, Te = abs(x-T[Ti-1]), abs(x-T[Ti])
    return min(max(Sw, Tw), max(Se, Te), 2*Tw+Se, 2*Sw+Te, 2*Se+Tw, 2*Te+Sw)

for x in X:
    print(solve(x))

Verdicts: Wrong Answer

Wrong Answer

Accepted
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=[-float("inf")]
b=[-float("inf")]
for i in range(A):
    a.append(int(input()))
for i in range(B):
    b.append(int(input()))   
a+=[float("inf")]
b+=[float("inf")]

for i in range(Q):
    q = int(input())
    x = bisect.bisect_left(a, q[i])
    y = bisect.bisect_left(b, q[i])
    for j in [a[x-1], a[x]]:
        for k in [b[y-1], b[y]]:
            ans1 = abs(j-q)+abs(j-k)
            ans2 = abs(k-q)+abs(j-k)
            ans = min(ans, ans1, ans2)
    print(ans)

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 i in range(Q):
    x = int(input())
    s = bisect.bisect_left(S, x)
    t = bisect.bisect_left(T, x)
    saisyo = float("inf")
    for j in [S[s], S[s-1]]:
        for k in [T[t], T[t-1]]:
            saisyo = min(saisyo, abs(j-x)+abs(k-j), abs(k-x)+abs(j-k))
    print(saisyo)

Verdicts: Runtime Error

Accepted
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):
a,b,q=map(int, input().split())

s=[-2*10**10]+[int(input()) for _ in range(a)]+[2*10**10]
t=[-2*10**10]+[int(input()) for _ in range(b)]+[2*10**10]
x=[int(input()) for _ in range(q)]

for i in range(q):
  j = bisect.bisect_left(s,x[i])
  al = x[i] - s[j-1]
  ar = s[j] - x[i]
  j = bisect.bisect_left(t,x[i])
  bl = x[i] - t[j-1]
  br = t[j] - x[i]
  l = min(max(al, bl), max(ar, br), 2*al+br, 2*ar+bl, 2*bl+ar, 2*br+al)
  print(l)


import bisect

a,b,q=map(int, input().split())

s=[-2*10**10]+[int(input()) for _ in range(a)]+[2*10**10]
t=[-2*10**10]+[int(input()) for _ in range(b)]+[2*10**10]
x=[int(input()) for _ in range(q)]

for i in range(q):
  j = bisect.bisect_left(s,x[i])
  al = x[i] - s[j-1]
  ar = s[j] - x[i]
  j = bisect.bisect_left(t,x[i])
  bl = x[i] - t[j-1]
  br = t[j] - x[i]
  l = min(max(al, bl), max(ar, br), 2*al+br, 2*ar+bl, 2*bl+ar, 2*br+al)
  print(l)


Verdicts: Runtime Error

Accepted
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
shrines = [-INF] + [int(input()) for _ in range(A)] + [INF]
temples = [-INF] + [int(input()) for _ in range(B)] + [INF]
X = [int(input()) for _ in range(Q)]




shrines.sort()  
temples.sort()  
for x in X:
    tmp = 10**18

    s_right_index = bisect.bisect_right(shrines, x)  
    s_left_index = bisect.bisect_right(shrines, x)-1  
    t_right_index = bisect.bisect_right(temples, x)  
    t_left_index = bisect.bisect_right(temples, x)-1  

    
    tmp = min(tmp, max(shrines[s_right_index], temples[t_right_index])-x)

    
    tmp = min(tmp, abs((shrines[s_right_index]-x)) * 2 + abs(temples[t_left_index]))

    
    tmp = min(tmp, abs((temples[t_right_index]-x)) * 2 + abs(shrines[s_left_index]))

    
    tmp = min(tmp, abs((temples[t_left_index]-x)) * 2 + abs(shrines[s_right_index]))

    
    tmp = min(tmp, abs((shrines[s_left_index]-x)) * 2 + abs(temples[t_left_index]))

    
    tmp = min(tmp, abs(x - min(abs(shrines[s_left_index]), abs(temples[t_left_index]))))

    print(tmp)


import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ 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)


Verdicts: Wrong Answer

Accepted
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):
# -*- coding: utf-8 -*-
import math
import sys
import bisect

## input
a,b,q = map(int, sys.stdin.readline().rstrip().split())
s_s, t_s, x_s = [[], [], []]
for _ in range(a):
  s_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(b):
  t_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(q):
  x_s.append(int(sys.stdin.readline().rstrip()))


## solve
def get_otherlen(st_s, k):
  idx = bisect.bisect_left(st_s, k)
  if idx == 0:
    mn = abs(st_s[0]-k)
  elif idx == len(s_s):
    mn = abs(st_s[-1]-k)
  else:
    mn = min(abs(st_s[idx]-k), abs(st_s[idx-1]-k))
  return(mn)


def get_len(k):
  
  idx = bisect.bisect_left(s_s, k)
  if idx == 0:
    mn = abs(s_s[0]-k) + get_otherlen(t_s, s_s[0])
  elif idx == len(s_s):
    mn = abs(s_s[-1]-k) + get_otherlen(t_s, s_s[-1])
  else:
    mn1 = abs(s_s[idx]-k) + get_otherlen(t_s, s_s[idx])
    mn2 = abs(s_s[idx-1]-k) + get_otherlen(t_s, s_s[idx-1])
    mn = min(mn1, mn2)

  # t -> s
  idx = bisect.bisect_left(t_s, k)
  if idx == 0:
    mn = min(mn, abs(t_s[0]-k) + get_otherlen(s_s, t_s[0]))
  elif idx == len(t_s):
    mn = min(mn, abs(t_s[-1]-k) + get_otherlen(s_s, t_s[-1]))
  else:
    mn1 = abs(t_s[idx]-k) + get_otherlen(s_s, t_s[idx])
    mn2 = abs(t_s[idx-1]-k) + get_otherlen(s_s, t_s[idx-1])
    mn = min(mn, mn1, mn2)
  return(mn)


if __name__ == "__main__":
  for x in x_s:
    print(x, get_len(x))

# -*- coding: utf-8 -*-
import math
import sys
import bisect

## input
a,b,q = map(int, sys.stdin.readline().rstrip().split())
s_s, t_s, x_s = [[-10**11], [-10**11], []]
for _ in range(a):
  s_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(b):
  t_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(q):
  x_s.append(int(sys.stdin.readline().rstrip()))
s_s.append(10**11)
t_s.append(10**11)


def solve(k):
  s_pos = bisect.bisect_left(s_s, k)
  t_pos = bisect.bisect_left(t_s, k)
  mn = 10**11
  for i in [s_s[s_pos-1], s_s[s_pos]]:
    for j in [t_s[t_pos-1], t_s[t_pos]]:
      mn = min(mn,abs(i-k)+abs(i-j), abs(j-k)+abs(i-j))
  return(mn)

if __name__ == "__main__":
  for x in x_s:
    print(solve(x))

Verdicts: Runtime Error

Accepted
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):
A, B, Q = map(int, input().split())
s = []
for i in range(A):
    s.append(int(input()))
s.sort()
t = []
t.sort()
for i in range(B):
    t.append(int(input()))
x = []
for i in range(Q):
    x.append(int(input()))

for i in range(Q):
    sx, tx, sy, ty = [], [], [], []
    for j in range(A):
        sx.append(abs(s[j] - x[i]))
    for j in range(B):
        tx.append(abs(t[j] - x[i]))
    sxi = sx.index(min(sx))
    txi = tx.index(min(tx))
    for j in range(B):
        ty.append(abs(t[j] - s[sxi]))
    for j in range(A):
        sy.append(abs(s[j] - t[sxi]))
    a = min(sx) + min(ty)
    b = min(tx) + min(sy)
    if a < b:
        print(a)
    else:
        print(b)


A, B, Q = map(int, input().split())
s = []
for i in range(A):
    s.append(int(input()))
s.sort()
t = []
t.sort()
for i in range(B):
    t.append(int(input()))
x = []
for i in range(Q):
    x.append(int(input()))

for i in range(Q):
    sx, tx, sy, ty = [], [], [], []
    for j in range(A):
        sx.append(abs(s[j] - x[i]))
    for j in range(B):
        tx.append(abs(t[j] - x[i]))
    sxi = sx.index(min(sx))
    txi = tx.index(min(tx))
    for j in range(B):
        ty.append(abs(t[j] - s[sxi]))
    for j in range(A):
        sy.append(abs(s[j] - t[sxi]))
    a = min(sx) + min(ty)
    b = min(tx) + min(sy)
    if a < b:
        print("a"+str(a))
    else:
        print("b"+str(b))


import bisect

A, B, Q = map(int, input().split())
INF = 10**18
S = [-INF]
for i in range(A):
    S.append(int(input()))
S.append(INF)
T = [-INF]
for i in range(B):
    T.append(int(input()))
    T.append(INF)
X = []
for i in range(Q):
    X.append(int(input()))

for x in X:
    ids, idt = bisect.bisect_right(S, x), bisect.bisect_right(T, x)
    print(min(abs(s-t) + min(abs(s-x), abs(t-x))
              for s in S[ids-1:ids+1] for t in T[idt-1:idt+1]))


import bisect

A, B, Q = map(int, input().split())
INF = 10**18
S = [-INF]
for i in range(A):
    S.append(int(input()))
S.append(INF)
T = [-INF]
for i in range(B):
    T.append(int(input()))
T.append(INF)
X = []
for i in range(Q):
    X.append(int(input()))

for x in X:
    a, b = bisect.bisect_right(S, x), bisect.bisect_right(T, x)
    print(min(abs(s-t) + min(abs(s-x), abs(t-x))
              for s in S[a-1:a+1] for t in T[b-1:b+1]))


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Accepted
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


def solve(a_list, b_list, position):
    # time O(log(A))
    a_right_key = bisect.bisect_left(a_list, position)
    a_left_key = a_right_key - 1
    
    b_right_key = bisect.bisect_left(b_list, position)
    b_left_key = b_right_key - 1

    a_left, a_right = a_list[a_left_key], a_list[a_right_key]
    b_left, b_right = b_list[b_left_key], b_list[b_right_key]

    ans = 10 ** 20
    for a_position in [a_left, a_right]:
        for b_position in [b_left, b_right]:
            distance1 = abs(position - a_position) + abs(a_position - b_position)
            distance2 = abs(position - b_position) + abs(a_position - b_position)
            ans = min(ans, distance1, distance2)
    return ans


def main():
    inf = 10 ** 20
    A, B, Q = map(int, input().split())

    # time O(A)
    a_list = [int(input()) for _ in range(A)]
    a_list = [-inf] + a_list + [inf]
    
    b_list = [int(input()) for _ in range(A)]
    b_list = [-inf] + b_list + [inf]

    
    for _ in range(Q):
        position = int(input())
        ans = solve(a_list, b_list, position)
        print(ans)


if __name__ == "__main__":
    main()


import bisect


def solve(a_list, b_list, position):
    # time O(log(A))
    a_right_key = bisect.bisect_left(a_list, position)
    a_left_key = a_right_key - 1
    
    b_right_key = bisect.bisect_left(b_list, position)
    b_left_key = b_right_key - 1

    a_left, a_right = a_list[a_left_key], a_list[a_right_key]
    b_left, b_right = b_list[b_left_key], b_list[b_right_key]

    ans = 10 ** 20
    for a_position in [a_left, a_right]:
        for b_position in [b_left, b_right]:
            distance1 = abs(position - a_position) + abs(a_position - b_position)
            distance2 = abs(position - b_position) + abs(a_position - b_position)
            ans = min(ans, distance1, distance2)
    return ans


def main():
    inf = 10 ** 20
    A, B, Q = map(int, input().split())

    # time O(A)
    a_list = [int(input()) for _ in range(A)]
    a_list = [-inf] + a_list + [inf]
    
    b_list = [int(input()) for _ in range(B)]
    b_list = [-inf] + b_list + [inf]

    
    for _ in range(Q):
        position = int(input())
        ans = solve(a_list, b_list, position)
        print(ans)


if __name__ == "__main__":
    main()


Verdicts: Runtime Error

Accepted
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 itertools
A,B,Q = [int(_) for _ in input().split(" ")]
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]                                                  
x = [int(input()) for _ in range(Q)]

def binary_search(arr,point,start,end):
    pivot = (start+end)//2
    if end -start > 1:
        if arr[pivot] <point :
            return binary_search(arr,point,pivot,end)
        else:
            return binary_search(arr,point,start,pivot)
    else:
        return start
    
def find_closest_path(s_1,s_2,t_1,t_2,point):
    shortest = 10**11
    for s,t in itertools.product([s_1,s_2],[t_1,t_2]):
        length = min(abs(point-t), abs(point -s )) + abs(s - t)
        if length < shortest:
            shortest = length 
    return length 


for i in range(Q):
    s_idx = binary_search(s,x[i],0,len(s)-1)
    t_idx = binary_search(t,x[i],0,len(t)-1)
    if len(s)>1:
        s_1,s_2 = s[s_idx],s[s_idx+1] 
    else:
        s_1,s_2 = s[0],s[0]
    if len(t)>1:
        t_1,t_2 = t[t_idx],t[t_idx+1]    
    else:
        t_1,t_2 = t[0],t[0]
    answer = find_closest_path(s_1,s_2,t_1,t_2,x[i])
    print(answer)

import itertools
A,B,Q = [int(_) for _ in input().split(" ")]
s_array = [int(input()) for _ in range(A)]
t_array = [int(input()) for _ in range(B)]                                                  
x = [int(input()) for _ in range(Q)]

def binary_search(arr,point,start,end):
    pivot = (start+end)//2
    if end -start > 1:
        if arr[pivot] <point :
            return binary_search(arr,point,pivot,end)
        else:
            return binary_search(arr,point,start,pivot)
    else:
        return start
    
def find_closest_path(s_1,s_2,t_1,t_2,point):
    shortest = 10**11
    for s,t in itertools.product([s_1,s_2],[t_1,t_2]):
        length = min(abs(point-t), abs(point -s )) + abs(s - t)
        if length < shortest:
            shortest = length 
    return shortest 


for i in range(Q):
    s_idx = binary_search(s_array,x[i],0,len(s_array)-1)
    t_idx = binary_search(t_array,x[i],0,len(t_array)-1)
    if len(s_array)>1:
        s_1,s_2 = s_array[s_idx],s_array[s_idx+1] 
    else:
        s_1,s_2 = s_array[0],s_array[0]
    if len(t_array)>1:
        t_1,t_2 = t_array[t_idx],t_array[t_idx+1]    
    else:
        t_1,t_2 = t_array[0],t_array[0]
    answer = find_closest_path(s_1,s_2,t_1,t_2,x[i])
    print(answer)








Verdicts: Wrong Answer

Accepted
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 sys
from sys import stdin
import numpy as np

def near(arr, num):
	index = np.abs(arr - num).argmin()
	return index

'''
A, B, Q = [int(i) for i in stdin.readline().split()]
s = np.asarray([int(stdin.readline()) for _ in range(A)])
t = np.asarray([int(stdin.readline()) for _ in range(B)])
x = [int(stdin.readline()) for _ in range(Q)]

'''
A, B, Q =[2, 3, 4]
s = [100,600]
t=[400,900,1000]
x=[150,2000,899,799]

st = np.hstack((s, t))
print(st, file=sys.stderr)
for i in range(Q):
	index = near(st, x[i])
	print(index, file=sys.stderr)
	if index<A:
		print(abs(x[i]-st[index]) + abs(st[index]-t[near(t, st[index])]))
	else:
		print(abs(x[i]-st[index]) + abs(st[index]-s[near(s, st[index])]))
		

import sys
from bisect import bisect_right
input = sys.stdin.readline().strip()
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]
xs = [int(input()) for i in range(Q)]
for x in xs:
    b, d = bisect_right(s, x), 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 sys
from bisect import bisect_right
input = sys.stdin.readline
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]
xs = [int(input()) for i in range(Q)]
for x in xs:
    b, d = bisect_right(s, x), 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)

Verdicts: Wrong Answer

Runtime Error

Accepted
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 sys
import bisect
input = sys.stdin.readline

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

for i in range(a):
	S = int(input())
    s[i] = S
    
for i in range(b):
	T = int(input())
    t[i] = T
    
s.sort()
t.sort()

for i in range(q):
	x = int(input())
    slidx = bisect.bisect_left(s, x)
    tlidx = bisect.bisect_left(t, x) 
	sl = x - s[slidx]
	tl = x - t[tlidx]
    tr = 90000000000 - x
    sr = 90000000000 - x
    if slidx < a-1:
      sr = s[slidx+1] - x
    if slidx < b-1:
      tr = t[tlidx+1] - x
    dist = min( [max(sl,tl), max(sr,tr), min(sr, sl)*2 + min(tr,tl), min(tr,tl)*2 + min(sr,sl)] )
    print(dist)

import sys
import bisect
input = sys.stdin.readline

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

for i in range(a):
	S = int(input())
    s[i] = S
    
for i in range(b):
	T = int(input())
    t[i] = T
    
s.sort()
t.sort()

for i in range(q):
	x = int(input())
    slidx = bisect.bisect_left(s, x)
    tlidx = bisect.bisect_left(t, x) 
	sl = x - s[slidx]
	tl = x - t[tlidx]
    tr = 90000000000 - x
    sr = 90000000000 - x
    if slidx < a-1:
      sr = s[slidx+1] - x
    if slidx < b-1:
      tr = t[tlidx+1] - x
    dist = min( max(sl,tl), max(sr,tr), min(sr, sl)*2 + min(tr,tl), min(tr,tl)*2 + min(sr,sl))
    print(dist)

import sys
import bisect
input = sys.stdin.readline

a,b,q = map(int,input().split())

s = [-1 for i in range(a)]
t = [-1 for i in range(b)]

for i in range(a):
	S = int(input())
	s[i] = S
    
for i in range(b):
	T = int(input())
	t[i] = T
    
s.sort()
t.sort()
for i in range(q):
	x = int(input())

	slidx = bisect.bisect_left(s, x) -1
	tlidx = bisect.bisect_left(t, x) -1
	sl = -20000000000
	tl = -20000000000
	sr = 20000000000
	tr = 20000000000
	if slidx >= 0:
		sl = s[slidx]
	if tlidx >= 0:
		tl = t[tlidx]

	if slidx < a-1:
		sr = s[slidx+1]
	if tlidx < b-1:
		tr = t[tlidx+1]
	sl = x - sl
	tl = x - tl
	sr = sr - x
	tr = tr - x
	#print([sl,tl,sr,tr])
	dist = min( [max(sl,tl), max(sr,tr), min(sr, sl)*2 + min(tr,tl), min(tr,tl)*2 + min(sr,sl)])
	print(dist)

Verdicts: Runtime Error

Runtime Error

Accepted