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):
improt bisect
A, B, Q = map(int, input().split())
ss = [int(input()) for i in range(A)]
ts = [int(input()) for i in range(B)]
xs = [int(input()) for i in range(Q)]
INF = 10**11

-INF, INF = -1, 10**10 + 1
ss = [-INF] + ss + [INF]
ts = [-INF] + ts + [INF]

# s_e is index the nearest s on the east side of x
for x in xs:
  s_e_ix = bisect.bisect_left(ss, x)
  s_e = ss[s_e_ix]
  s_w = ss[s_e_ix - 1]
  t_e_ix = bisect.bisect_left(ts, x)
  t_e = ts[t_e_ix]
  t_w_ix = ts[t_e_ix - 1]
  
  d = INF
  for s in [s_w, s_e]:
    for t in [t_w, t_e]:
      d0 = abs(s-x) + abs(t-s)
      d1 = abs(t-x) + abs(s-t)
      d = min(d0, d1, d)
  print(d)
  
                             

import bisect
A, B, Q = map(int, input().split())
ss = [int(input()) for i in range(A)]
ts = [int(input()) for i in range(B)]
xs = [int(input()) for i in range(Q)]
INF = 10**11
 
-INF, INF = -1, 10**10 + 1
ss = [-INF] + ss + [INF]
ts = [-INF] + ts + [INF]
 
# s_e is index the nearest s on the east side of x
for x in xs:
  s_e_ix = bisect.bisect_left(ss, x)
  s_e = ss[s_e_ix]
  s_w = ss[s_e_ix - 1]
  t_e_ix = bisect.bisect_left(ts, x)
  t_e = ts[t_e_ix]
  t_w_ix = ts[t_e_ix - 1]
  
  d = INF
  for s in [s_w, s_e]:
    for t in [t_w, t_e]:
      d0 = abs(s-x) + abs(t-s)
      d1 = abs(t-x) + abs(s-t)
      d = min(d0, d1, d)
  print(d)

import bisect
A, B, Q = map(int, input().split())
ss = [int(input()) for i in range(A)]
ts = [int(input()) for i in range(B)]
xs = [int(input()) for i in range(Q)]
INF = 10**11

ss = [-INF] + ss + [INF]
ts = [-INF] + ts + [INF]
 
# s_e is index the nearest s on the east side of x
for x in xs:
  s_e_ix = bisect.bisect_left(ss, x)
  s_e = ss[s_e_ix]
  s_w = ss[s_e_ix - 1]
  
  t_e_ix = bisect.bisect_left(ts, x)
  t_e = ts[t_e_ix]
  t_w = ts[t_e_ix - 1]
  
  d = INF
  for s in [s_w, s_e]:
    for t in [t_w, t_e]:
      d0 = abs(s-x) + abs(t-s)
      d1 = abs(t-x) + abs(s-t)
      d = min(d0, d1, d)
  print(d)

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 bisect
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)]

A = []
for x in X:
    SL, SR, TL, TR = 10**20, 10**20, 10**20, 10**20
    i = bisect.bisect_left(S, x)
    if i != 0:
        SL = x-S[i-1]
    if i != A:
        SR = S[i]-x

    j = bisect.bisect_left(T, x)
    if j != 0:
        TL = x-T[j-1]
    if j != B:
        TR = T[j]-x

    ans = min(max(SL, TL), min(2*SL+TR, SL+2*TR),
              min(2*TL+SR, TL+2*SR), max(SR, TR))
    A.append(ans)
print(*A, sep='\n')


import bisect
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)]

for x in X:
    SL, SR, TL, TR = 10**20, 10**20, 10**20, 10**20
    i = bisect.bisect_left(S, x)
    if i != 0:
        SL = x-S[i-1]
    if i != A:
        SR = S[i]-x

    j = bisect.bisect_left(T, x)
    if j != 0:
        TL = x-T[j-1]
    if j != B:
        TR = T[j]-x

    ans = min(max(SL, TL), min(2*SL+TR, SL+2*TR),
              min(2*TL+SR, TL+2*SR), max(SR, TR))
    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())
s=[]
t=[]
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
for i in range(q):
    x=int(input())
    si=bisect.bisect_left(s,x)
    ti=bisect.bisect_left(t,x)
    if si==a:
        sk=[s[a-1]]
    elif si==0:
        sk=[s[0]]
    else:
        sk=[s[si-1],s[si]]
    if ti==0:
        tk=[t[0]]
    else:
        tk=[t[ti-1],t[ti]]
    ans=1000000000000
    for f in sk:
        for g in tk:
            ans=min(ans,abs(x-f)+abs(f-g),abs(x-g)+abs(g-f))
print(ans)

import bisect
a,b,q=map(int,input().split())
s=[]
t=[]
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
for i in range(q):
    x=int(input())
    si=bisect.bisect_left(s,x)
    ti=bisect.bisect_left(t,x)
    if si==a:
        sk=[s[a-1]]
    elif si==0:
        sk=[s[0]]
    else:
        sk=[s[si-1],s[si]]
    if ti==0:
        tk=[t[0]]
    elif ti==b:
        tk=[t[b-1]]
    else:
        tk=[t[ti-1],t[ti]]
    ans=1000000000000
    for f in sk:
        for g in tk:
            ans=min(ans,abs(x-f)+abs(f-g),abs(x-g)+abs(g-f))
    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):
from bisect import bisect_left,bisect_right
a,b,q=map(int,input().split())
s=[-(10**21)]+[int(input())for _ in range(a)]+[10**21]
t=[-(10**21)]+[int(input())for _ in range(b)]+[10**21]
for _ in range(q):
  x=int(input())
  s1,s2=s[bisect_right(s,x)-1],s[bisect_right(s,x)]
  t1,t2=t[bisect_right(t,x)-1],t[bisect_right(t,x)]
  ans=10**11
  for i in [s1,s2]:
    for j in [t1,t2]:
      ans=min(ans,abs(i-j)+min(abs(x-i),abs(x-j)))
print(ans)

a,b,q=map(int,input().split())
s=[-10**30]+[int(input())for _ in range(a)]+[10**30]
t=[-10**30]+[int(input())for _ in range(b)]+[10**30]
x=[int(input())for _ in range(q)]
from bisect import bisect_left,bisect_right
for xx in x:
  s1=s[bisect_right(s,xx)-1]
  s2=s[bisect_right(s,xx)]
  t1=t[bisect_right(t,xx)-1]
  t2=t[bisect_right(t,xx)]
  ans=[]
  ans.append(abs(s1-t1)+min(abs(xx-s1),abs(xx-t1)))
  ans.append(abs(s1-t2)+min(abs(xx-s1),abs(xx-t2)))
  ans.append(abs(s2-t1)+min(abs(xx-s2),abs(xx-t1)))
  ans.append(abs(s2-t2)+min(abs(xx-s2),abs(xx-t2)))
  print(min(ans))

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):
A, B, Q = map(int, input().split())
s = [0] + [int(input()) for i in range(A)] + [10**11]
t = [0] + [int(input()) for i in range(B)] + [10**11]

def nibutans(x, l, r):
	if r - l <= 1:
		return r
	nibu = (s[r] + s[l]) // 2
	return nibutans(x, nibu, r) if x > nibu else nibutans(x, l, nibu)

def nibutant(x, l, r):
	if l == r and x >= l:
		return l + 1
	if r - l <= 1:
		return r
	nibu = (t[r] + t[l]) // 2
	return nibutant(x, nibu, r) if x > nibu else nibutant(x, l, nibu)

for i in range(Q):
	x = int(input())
	sr, tr = nibutans(x, 0, -1), nibutant(x, 0, -1)
	ans = 10**11
	for S in [s[sr - 1], s[sr]]:
		for T in [t[tr - 1], t[tr]]:
			d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
			ans = min(ans, d1, d2)
	print(ans)


A, B, Q = map(int, input().split())
s = [-10**11] + [int(input()) for i in range(A)] + [10**11]
t = [-10**11] + [int(input()) for i in range(B)] + [10**11]
def nibutans(x, l, r):
	if r - l <= 1:
		return r
	nibu = (r + l) // 2
	return nibutans(x, nibu, r) if x > s[nibu] else nibutans(x, l, nibu)
def nibutant(x, l, r):
	if r - l <= 1:
		return r
	nibu = (r + l) // 2
	return nibutant(x, nibu, r) if x > t[nibu] else nibutant(x, l, nibu)
for i in range(Q):
	x = int(input())
	sr, tr = nibutans(x, 0, A + 1), nibutant(x, 0, B + 1)
	ans = 10**17
	for S in [s[sr - 1], s[sr]]:
		for T in [t[tr - 1], t[tr]]:
			d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
			print(str(ans) + ' ' + str(d1) + ' ' + str(d2))
			ans = min(ans, d1, d2)
	print(ans)

import bisect
A, B, Q = map(int, input().split())
s = [-10**11] + [int(input()) for i in range(A)] + [10**11]
t = [-10**11] + [int(input()) for i in range(B)] + [10**11]


for q in range(Q):
	x = int(input())
	b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
	res = 10**13
	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

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
inf = 10**18
a, b, q = map(int, input().split())
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(a)] + [inf]

for _ in range(q):
    x = int(input())
    i = bisect.bisect_right(s, x)
    j = bisect.bisect_right(t, x)
    ans = inf
    for s_x in [s[i-1], s[i]]:
        for t_x in [t[i-1], t[i]]:
            ans0 = abs(s_x - x) + abs(t_x - s_x)
            ans1 = abs(t_x - x) + abs(s_x - t_x)
            ans = min(ans, ans0, ans1)
    print(ans)


import bisect
inf = 10**18
a, b, q = map(int, input().split())
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(a)] + [inf]

for _ in range(q):
    x = int(input())
    i = bisect.bisect_right(s, x)
    j = bisect.bisect_right(t, x)
    ans = inf
    for s_x in [s[i-1], s[i]]:
        for t_x in [t[j-1], t[j]]:
            ans0 = abs(s_x - x) + abs(t_x - s_x)
            ans1 = abs(t_x - x) + abs(s_x - t_x)
            ans = min(ans, ans0, ans1)
    print(ans)

import bisect
inf = 10**18
a, b, q = map(int, input().split())
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(b)] + [inf]

for _ in range(q):
    x = int(input())
    i = bisect.bisect_right(s, x)
    j = bisect.bisect_right(t, x)
    ans = inf
    for s_x in [s[i-1], s[i]]:
        for t_x in [t[j-1], t[j]]:
            ans0 = abs(s_x - x) + abs(t_x - s_x)
            ans1 = abs(t_x - x) + abs(s_x - t_x)
            ans = min(ans, ans0, ans1)
    print(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):

a, b, q = [int(i) for i in 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)]
print(0)

from bisect import bisect_left as bisect

a, b, q = [int(i) for i in 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)]

for x in X:
    s_i = bisect(S, x)
    t_i = bisect(T, x)
    min_s = min(abs(x - S[max(0, s_i - 1)]), abs(x - S[min(a - 1, s_i)]))
    min_t = min(abs(x - T[max(0, t_i - 1)]), abs(x - T[min(b - 1, t_i)]))
    ans = min_s + min_t + min(min_s, min_t)
    if s_i > 0 and t_i > 0:
        ans = min(ans, abs(x - min(S[s_i - 1], T[t_i - 1])))
    if s_i < a and t_i < b:
        ans = min(ans, abs(x - max(S[s_i], T[t_i])))
    
    print(ans)

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):
from bisect import bisect_left
a,b,q=map(int,input().split())
s=[]
t=[]
x=[]
s.append(-float("inf"))
t.append(-float("inf"))
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
s.append(float("inf"))
t.append(float("inf"))
s.sort()
t.sort()
for i in range(q):
    x=int(input())
    ans=float("inf")
    tmp_s_1=bisect.bisect_left(s,x)-1
    tmp_s_2=tmp_s_1+1
    tmp_s_1_t=bisect.bisect_left(t,s[tmp_s_1])
    tmp_s_2_t=bisect.bisect_left(t,s[tmp_s_2])
    ans=min(abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t-1]), abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t-1]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t]))
    tmp_t_1=bisect.bisect_left(t,x)-1
    tmp_t_2=tmp_t_1+1
    tmp_t_1_s=bisect.bisect_left(s,t[tmp_t_1])
    tmp_t_2_s=bisect.bisect_left(s,t[tmp_t_2])
    ans=min(ans, abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s-1]), abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s-1]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s]))
    print(ans)

from bisect import bisect_left
a,b,q=map(int,input().split())
s=[]
t=[]
x=[]
s.append(-10**12)
t.append(-10**12)
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
s.append(10**12)
t.append(10**12)
for i in range(q):
    x=int(input())
    tmp_s=bisect_left(s,x)-1
    tmp_s_1=s[tmp_s]
    tmp_s_2=s[tmp_s+1]
    tmp_t=bisect_left(t,x)-1
    tmp_t_1=t[tmp_t]
    tmp_t_2=t[tmp_t+1]
    print(min(abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_1),
    abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_2),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_1),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_2),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_1),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_2),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_1),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_2)))

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_left
import sys
input = sys.stdin.readline
a,b,q = map(int,input().split())
inf = float('inf')
S = [-inf]+[int(input()) for i in range(a)]+[inf]
T = [-inf]+[int(input()) for i in range(b)]+[inf]

def f(S,T):
    res = 10**30 
    s = bisect_left(S,x)
    res = []

    for i in range(2):
    	for j in range(2):
            t = bisect_left(T,S[s-i])
            res.append(abs(x-S[s-i])+abs(S[s-i]-T[t-j]))

    return min(res)

for i in range(q):
    x = int(input())
    print(min(f(S,T),f(T,S)))

import bisect,sys
input = sys.stdin.readline
a,b,q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for i in range(a)]+[INF]
T = [-INF]+[int(input()) for i in range(b)]+[INF]

def f(S,T):
    res = INF
    s = bisect.bisect_left(S,x)
    for i in range(2):
    	for j in range(2):
            t = bisect.bisect_left(T,S[s-i])
            ans= abs(x-S[s-i])+abs(S[s-i]-T[t-j])
            if ans < res:
                res = ans
    return res

for i in range(q):
    x = int(input())
    print(min(f(S,T),f(T,S)))


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 = list(map(int, input().split()))
S, T, X = [-INF], [-INF], []
for _ in range(A):
    S.append(int(input()))
for _ in range(B):
    T.append(int(input()))
for _ in range(Q):
    X.append(int(input()))
INF = 10 ** 12
S.append(INF)
T.append(INF)

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


import bisect
INF = 10 ** 12

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

for x in X:
    ans = INF
    sr = bisect.bisect_right(S, x)
    tr = bisect.bisect_right(T, x)
    for s in S[sr-1: sr+1]:
        for t in T[tr-1: tr+1]:
            ans = min(ans, abs(s-x) + abs(s-t), abs(t-x) + abs(s-t))
    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):
A, B, Q = map(int, input().split())
s = [-10**10] + [int(input()) for _ in range(A)] + [2*10**10, 2*10**10 + 1]
t = [-10**10] + [int(input()) for _ in range(B)] + [2*10**10, 2*10**10 + 1]
x = [int(input()) for _ in range(Q)]


def searchindex(p, z):
    for i, d in enumerate(p):
        if d > z:
            return i - 1


def func(c, d, z):
    i = searchindex(c, z)
    j1 = searchindex(d, c[i])
    j2 = searchindex(d, c[i + 1])
    return min(z - d[j1], z - c[i] * 2 + d[j1 + 1], d[j2 + 1] - z, c[i + 1] * 2 - z - d[j2])


ans = []
for z in x:
    ans.append(min(func(s, t, z), func(t, s, z)))

print(ans)

import bisect
A, B, Q = map(int, input().split())
s = [-10**10 - 1, -10**10] + [int(input()) for _ in range(A)] + [2*10**10]
t = [-10**10 - 1, -10**10] + [int(input()) for _ in range(B)] + [2*10**10]
x = [int(input()) for _ in range(Q)]


def func(c, d, z):
    i = bisect.bisect_left(c, z)
    j = bisect.bisect_left(d, z)
    if c[i - 1] < d[j - 1] and c[i] < d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    elif c[i - 1] < d[j - 1] and c[i] > d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * d[j] - z - c[i - 1], c[i] - z)
    elif c[i - 1] > d[j - 1] and c[i] < d[j]:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    else:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * d[j] - z - c[i - 1], c[i] - z)


for z in x:
    print(func(s, t, z))


import bisect
A, B, Q = map(int, input().split())
s = [-10**10 - 1, -10**10] + [int(input()) for _ in range(A)] + [2*10**10]
t = [-10**10 - 1, -10**10] + [int(input()) for _ in range(B)] + [2*10**10]
x = [int(input()) for _ in range(Q)]


def func(c, d, z):
    i = bisect.bisect_left(c, z)
    j = bisect.bisect_left(d, z)
    if c[i - 1] < d[j - 1] and c[i] < d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    elif c[i - 1] < d[j - 1] and c[i] >= d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * d[j] - z - c[i - 1], c[i] - z)
    elif c[i - 1] >= d[j - 1] and c[i] < d[j]:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    else:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * d[j] - z - c[i - 1], c[i] - z)


for z in x:
    print(func(s, t, z))

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())
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
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: 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 _ in range(A)]
t=[int(input()) for _ in range(B)]
q=[int(input()) for _ in range(Q)]

def solve(start):
    ans=[]
    x=bisect.bisect_left(s,start,lo=0,hi=len(s))

    t1=bisect.bisect_left(t,x,lo=0,hi=len(t))
    if x<len(s) and t1<len(t):
        ans.append(min(abs(start-s[x])+abs(t[t1]-s[x]),abs(start-s[x])+abs(t[t1-1]-s[x])))

    if x>=1:
        t2=bisect.bisect_left(t,x-1,lo=0,hi=len(t))
        ans.append(min(abs(start-s[x-1])+abs(t[t2]-s[x-1]),abs(start-s[x-1])+abs(t[t2-1]-s[x-1])))




    y=bisect.bisect_left(t,start,lo=0,hi=len(t))
    s1=bisect.bisect_left(s,x,lo=0,hi=len(s))
    if y < len(t) and s1 < len(s):
        ans.append(min(abs(start - t[y]) + abs(s[s1] - t[y]), abs(start - t[y]) + abs(s[s1 - 1] - t[y])))
    if y>=1:
        s2=bisect.bisect_left(s,y-1,lo=0,hi=len(s))
        ans.append(min(abs(start-t[y-1])+abs(s[s2]-t[y-1]),abs(start-t[y-1])+abs(t[s2-1]-t[y-1])))
    
    print(min(ans))

for e in q:
    solve(e)

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]
q=[int(input()) for _ in range(Q)]

def solve(start):
    ans=[]
    c,d=bisect.bisect_left(s,start),bisect.bisect_left(t,start)
    for S in [s[c],s[c-1]]:
        for T in [t[d],t[d-1]]:
            ans.append(min(abs(start-S)+abs(S-T),abs(start-T)+abs(S-T)))

    print(min(ans))

for e in q:
    solve(e)

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())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
x = [int(input()) for i in range(Q)]
inf = [10**13, -10**13]
A,B,C=2, 3, 4
s = sorted([100,600]+inf)
t = sorted([400,900,1000]+inf)
x = [150,2000,899,799]

for i in x:
    s_r = s[bisect.bisect_right(s, x[i])]
    s_l = s[bisect.bisect_left(s, x[i])-1]
    t_r = t[bisect.bisect_right(t, x[i])]
    t_l = t[bisect.bisect_left(t, x[i])-1]
    long = sorted([abs(t_r-s_r)+s_r-x[i], abs(t_l-s_r)+s_r-x[i], abs(t_l-s_l)+x[i]-s_l, abs(t_r-s_l)+x[i]-s_l,
     abs(s_r-t_r)+t_r-x[i], abs(s_l-t_r)+t_r-x[i], abs(s_r-t_l)+x[i]-t_l, abs(s_l-t_l)+x[i]-t_l])
    print(long[0])

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)]
x = [int(input()) for i in range(Q)]
inf = [10**13, -10**13]
s = sorted(s+inf)
t = sorted(t+inf)

for i in range(Q):
    s_r = s[bisect.bisect_right(s, x[i])]
    s_l = s[bisect.bisect_left(s, x[i])-1]
    t_r = t[bisect.bisect_right(t, x[i])]
    t_l = t[bisect.bisect_left(t, x[i])-1]
    long = sorted([abs(t_r-s_r)+s_r-x[i], abs(t_l-s_r)+s_r-x[i], abs(t_l-s_l)+x[i]-s_l, abs(t_r-s_l)+x[i]-s_l,
     abs(s_r-t_r)+t_r-x[i], abs(s_l-t_r)+t_r-x[i], abs(s_r-t_l)+x[i]-t_l, abs(s_l-t_l)+x[i]-t_l])
    print(long[0])

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 _ in range(a)]+[inf]
T = [-inf]+[int(input()) for _ in range(b)]+[inf]
ans = inf
for _ in range(q):
  x = int(input())
  b,d = bisect.bisect_right(S,x),bisect.bisect_right(T,x)
  for s in [S[b-1],S[b]]:
    for t in [T[d-1],T[d]]:
      tmp1,tmp2 = abs(s-x)+abs(s-t) ,abs(t-x)+abs(t-s)
      ans = min(ans ,tmp1,tmp2)
  print(ans)
      


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]
ans = inf
for _ in range(q):
  x = int(input())
  b,d = bisect.bisect_right(S,x),bisect.bisect_right(T,x)
  ans = inf
  for s in [S[b-1],S[b]]:
    for t in [T[d-1],T[d]]:
      tmp1,tmp2 = abs(s-x)+abs(s-t) ,abs(t-x)+abs(t-s)
      ans = min(ans ,tmp1,tmp2)
  print(ans)
      



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


def f(a, x):
    i = bisect.bisect_left(a, x)
    if i >= len(a) - 1:
        return a[-1]
    if x - a[i] > a[i + 1] - x:
        return a[i + 1]
    return a[i]


def main():
    A, B, Q = map(int, input().split())
    s = []
    t = []
    ans = []
    for _ in range(A):
        s.append(int(input()))
    for _ in range(B):
        t.append(int(input()))
    for _ in range(Q):
        x = int(input())
        sx = f(s, x)
        tsx = f(t, sx)
        tx = f(t, x)
        stx = f(s, tx)
        ans.append(min(abs(sx - x) + abs(sx - tsx),
                       abs(tx - x) + abs(tx - stx)))
    for a in ans:
        print(a)


main()


import bisect

def main():
    A, B, Q = map(int, input().split())
    s = [-float('inf')]
    t = [-float('inf')]
    ans = []
    for _ in range(A):
        s.append(int(input()))
    for _ in range(B):
        t.append(int(input()))
    s.append(float('inf'))
    t.append(float('inf'))
    for _ in range(Q):
        x = int(input())
        i = bisect.bisect_right(s, x)
        j = bisect.bisect_right(t, x)
        a = float('inf')
        for ss in [s[i - 1], s[i]]:
            for tt in [t[j - 1], t[j]]:
                a = min(a, abs(ss - x) + abs(tt - ss),
                        abs(tt - x) + abs(ss - tt))
        ans.append(a)
    for a in ans:
        print(a)


main()


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):
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]
X=[int(input()) for _ in range(Q)]

def binary_serch(a,x,st=0,end=len(a)):
  while st<end:
    i=len(a)//2
    if x<a[i]:
      end=i
    elif a[i]<x:
      st=i+1
  return st

for x in X:
  s,t=binary_serch(S,x),binary_serch(T,x)
  ans=INF
  for j in [S[i],S[i+1]]:
    for k in [T[i],T[i+1]]:
      cnd1,cnd2=abs(j-x)+abs(k-j),abs(k-x),abs(j-k)
      ans(min(ans,cnd1,cnd2))
  print(ans)

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 _ in range(q):
  x=int(input())
  b,d=bisect.bisect_right(s,x),bisect.bisect_right(t,x)
  res=inf
  for i in [s[b-1],s[b]]:
    for j in [t[d-1],s[d]]:
      d1,d2=abs(i-x)+abs(i-j),abs(j-x)+abs(j-i)
      res=min(res,d1,d2)
  print(res)
#copy code.


import bisect
a,b,q=map(int,input())
inf=10**18
s=[-inf]+[int(input()) for _ in range(a)]+[inf]
t=[-inf]+[int(input()) for _ in range(b)]+[inf]
for _ in range(q):
  x=int(input())
  b,d=bisect_right(s,x),bisect_right(t,x)
  res=inf
  for i in [s[b-1],s[b]]:
    for j in [t[d-1],s[d]]:
      d1,d2=abs(i-x)+abs(i-j),abs(j-x)+abs(j-i)
      res=min(res,d1,d2)
  print(res)

import bisect
a,b,q=map(int,input())
inf=10**18
s=[-inf]+[int(input()) for _ in range(a)]+[inf]
t=[-inf]+[int(input()) for _ in range(b)]+[inf]
for _ in range(q):
  x=int(input())
  b,d=bisect.bisect_right(s,x),bisect.bisect_right(t,x)
  res=inf
  for i in [s[b-1],s[b]]:
    for j in [t[d-1],s[d]]:
      d1,d2=abs(i-x)+abs(i-j),abs(j-x)+abs(j-i)
      res=min(res,d1,d2)
  print(res)
#copy code.

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)
#copy code.

Verdicts: Runtime Error

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
import sys
A, B, Q = map(int, input().split())
INF = 10 ** 13
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
xs = [int(sys.stdin.readline()) for i in range(Q)]
for x in range(xs):
    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)

def solve():
    import bisect
    A, B, Q = map(int, input().split())
    INF = 10 ** 13
    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)
if __name__ == '__main__':
  	solve()

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**20

s = [0] * A
t = [0] * B
x = [0] * Q

for i in range(A) :
    s[i] = int(input())
for i in range(B) :
    t[i] = int(input())
for i in range(Q) :
    x[i] = int(input())

s = [INF] + s + [INF]
t = [INF] + t + [INF]

for p in x :
    rightS = bisect_right(s,p)
    rightT = bisect_right(t,p)

    dist = INF

    for i in [s[rightS - 1], s[rightS]] :
        for j in [t[rightT -1], t[rightT]] :
            dist1 = abs(i - p) + abs(i - j)
            dist2 = abs(j - p) + abs(i - j)
            dist = min(dist, dist1, dist2)

    print(dist)

from bisect import bisect_left

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

S.sort()
T.sort()

for _ in range(Q):
    x = int(input())

    s = bisect_left(S, x)
    ls = S[s - 1]
    rs = S[s]

    t = bisect_left(T, x)
    lt = T[t - 1]
    rt = T[t]

    ans = min(
        abs(ls - rt) + min(abs(x - ls), abs(x - rt)),
        abs(lt - rs) + min(abs(x - lt), abs(x - rs)),
        max(abs(x - ls), abs(x - lt)),
        max(abs(x - rs), abs(x - rt))
    )

    print(ans)


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 as bs
a, b, q = map(int, input().split())
INF = 10**18
s = [-INF] + sorted([int(input()) for _ in range(a)]) + [INF]
t = [-INF] + sorted([int(input()) for _ in range(b)]) + [INF]

for _ in range(q):
    z = int(input())
    x, y = bs.bisect_right(s, z), bs.bisect_right(t, z)
    s1, s2, t1, t2 = s[x-1], s[x], t[y-1], t[y]
    print(min(max(z - s1, z - t1), max(s2 - z, t2 - z), min(2 * t2 - z - s1, 2 * s2 - z - t1)))

import bisect as bs
a, b, q = map(int, input().split())
INF = 10**18
s = [-INF] + sorted([int(input()) for _ in range(a)]) + [INF]
t = [-INF] + sorted([int(input()) for _ in range(b)]) + [INF]

for _ in range(q):
    z = int(input())
    x, y = bs.bisect_right(s, z), bs.bisect_right(t, z)
    s1, s2, t1, t2 = s[x-1], s[x], t[y-1], t[y]
    print(min(max(z - s1, z - t1), max(s2 - z, t2 - z),
    min(2 * t2 - z - s1, 2 * s2 - z - t1), min(z + t2 - 2 * s1, z + s2 - 2 * t1)))

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):
a,b,q=[int(it) for it in input().split()]
dummy=10**20
s=[-dummy] + [int(input()) for it in range(a)] + [dummy]
t=[-dummy] + [int(input()) for it in range(b)] + [dummy]
x=[int(input()) for it in range(q)]

def find_left(l, p):
    a,b=0,len(l)-1
    while True:
        c=(a+b)//2
        if p <= l[c]:
            b = c-1
        elif l[c+1] <= p:
            a = c+1
        else:
            return c
def find_right(l, p):
    a,b=0,len(l)-1
    while True:
        c=(a+b)//2
        if p <= l[c-1]:
            b = c-1
        elif l[c] <= p:
            a = c+1
        else:
            return c
for xi in x:
    s_w = s[find_left(s, xi)]
    s_e = s[find_right(s, xi)]
    t_w = t[find_left(t, xi)]
    t_e = t[find_right(t, xi)]

    d = min(
        abs(xi - s_w) + abs(s_w - t_w),
        abs(xi - s_e) + abs(s_e - t_w),
        abs(xi - s_w) + abs(s_w - t_e),
        abs(xi - s_e) + abs(s_e - t_e),
        abs(xi - t_w) + abs(t_w - s_w),
        abs(xi - t_e) + abs(t_e - s_w),
        abs(xi - t_w) + abs(t_w - s_e),
        abs(xi - t_e) + abs(t_e - s_e)
    )
    print(d)


import bisect

a,b,q=[int(it) for it in input().split()]
dummy=10**20
s=[-dummy] + [int(input()) for it in range(a)] + [dummy]
t=[-dummy] + [int(input()) for it in range(b)] + [dummy]
x=[int(input()) for it in range(q)]

for xi in x:
    s_w = s[bisect.bisect_left(s, xi)-1]
    s_e = s[bisect.bisect_left(s, xi)]
    t_w = t[bisect.bisect_left(t, xi)-1]
    t_e = t[bisect.bisect_left(t, xi)]

    d = min(
        abs(xi - s_w) + abs(s_w - t_w),
        abs(xi - s_e) + abs(s_e - t_w),
        abs(xi - s_w) + abs(s_w - t_e),
        abs(xi - s_e) + abs(s_e - t_e),
        abs(xi - t_w) + abs(t_w - s_w),
        abs(xi - t_e) + abs(t_e - s_w),
        abs(xi - t_w) + abs(t_w - s_e),
        abs(xi - t_e) + abs(t_e - s_e)
    )
    print(d)


Verdicts: Time Limit Exceeded

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_left
A, B, Q, *L = map(int, open(0).read().split())
s = L[:A]
t = L[A:A+B]
x = L[A+B:]

s = [-float('inf')]+s+[float('inf')]
t = [-float('inf')]+t+[float('inf')]
for c in x:
  br = bisect_left(t,c)
  ar = bisect_left(s,c)
  bl = br-1
  al = ar-1
  print(min(s[ar]-t[bl]+min(s[ar]-c,c-t[bl]),t[br]-s[al]+min(t[br]-c,c-s[al]),
            -min(t[bl],s[al]),max(s[ar],t[br])-c))

from bisect import bisect_left
INF = 10**18
A, B, Q = map(int, input().split())
als = [int(input()) for i in range(A)]
bls = [int(input()) for i in range(B)]
als = [-INF]+als+[INF]
bls = [-INF]+bls+[INF]

for i in range(Q):
  x = int(input())
  ai = bisect_left(x,als)
  bi = bisect_left(x,bls)
  res = INF
  for S in [als[ai], als[ai-1]]:
    for T in [bls[bi], bls[bi-1]]:
      d1, d2 = abs(S-x)+abs(T-S), abs(T-x)+abs(S-T)
      res = min(res,d1,d2)
  print(res)

import numpy as np
A, B, Q, *L = map(int, open('0').read().split())
s = L[:A]
t = L[A:A+B]
x = L[A+B:]

s = [-float('inf')]+s+[float('inf')]
t = [-float('inf')]+t+[float('inf')]
sls = np.searchsorted(s,x)
tls = np.searchsorted(t,x)
for ar, br, c in zip(sls,tls,x):
  bl = br-1
  al = ar-1
  n = s[ar]-t[bl]+min(s[ar]-c,c-t[bl])
  m = t[br]-s[al]+min(t[br]-c,c-s[al])
  p = c-min(t[bl],s[al])
  q = max(s[ar],t[br])-c
  print(min(n,m,p,q))

from bisect import bisect_right
INF = 10**18
A, B, Q = map(int, input().split())
als = [int(input()) for i in range(A)]
bls = [int(input()) for i in range(B)]
als = [-INF]+als+[INF]
bls = [-INF]+bls+[INF]

for i in range(Q):
  x = int(input())
  ai = bisect_right(x,als)
  bi = bisect_right(x,bls)
  res = INF
  for S in [als[ai], als[ai-1]]:
    for T in [bls[bi], bls[bi-1]]:
      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_left
A, B, Q, *L = map(int, open(0).read().split())
s = L[:A]
t = L[A:A+B]
x = L[A+B:]

s = [-float('inf')]+s+[float('inf')]
t = [-float('inf')]+t+[float('inf')]
for c in x:
  br = bisect_left(t,c)
  ar = bisect_left(s,c)
  bl = br-1
  al = ar-1
  print(min(s[ar]-t[bl]+min(s[ar]-c,c-t[bl]),t[br]-s[al]+min(t[br]-c,c-s[al]),
            c-min(t[bl],s[al]),max(s[ar],t[br])-c))

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Accepted