Problem:


Score : 400 points

Problem Statement

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.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.



Sample input:
2 3 4
100
600
400
900
1000
150
2000
899
799


Sample output:
350
1400
301
399

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [-INF]
for i in range(a):
    si = int(input())
    s.append(si)
s.append(INF)
t = [-INF]
for i in range(b):
    ti = int(input())
    t.append(ti)
t.append(INF)
x = []
for i in range(q):
    xi = int(input())
    x.append(xi)

for xi in x:
    index_s = bisect_left(s, xi)
    s1 = abs(xi - s[index_s - 1])
    s2 = abs(xi - s[index_s])
    index_t = bisect_left(t, xi)
    t1 = abs(xi - t[index_t - 1])
    t2 = abs(xi - t[index_t])
    print(min(max(s1, t1), max(s2, t2), min(s1, t2) * 2 + max(s1, t2), min(s2, t1) * 2 + max(s2, t1)))

Submission status: Time Limit Exceeded

Solution code:
#A

"""
s = input().split("/")
if int(s[1]) <= 4:
    print("Heisei")
else:
    print("TBD")
"""
#B
"""
n = int(input())
ans = 0
for i in range(n):
    a = input().split()
    if a[1] == "JPY":
        ans += int(a[0])
    else:
        ans +=  float(a[0]) * 380000
print(ans)
"""

#C
"""
n, a, b, c = map(int, input().split())
abc = [a,b,c]
x = [int(input()) for i in range(n)]
x.sort()
ans = []
flg = 0
for i in x:
    if i in abc:
        ans.append(i)
        flg = 1
if flg:
    for i in ans:
        abc.remove(i)
        x.remove(i)

"""
#D

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

import bisect


for i in range(q):
    x = int(input())
    s_l = bisect.bisect_left(s, x) - 1
    t_l = bisect.bisect_left(t, x) - 1
    s_r = s_l + 1
    t_r = t_l + 1
    if s_l == -1:
        s_l = 0
    if t_l == -1:
        t_l = 0
    if s_l == a :
        s_l = a - 1
        s_r = a - 1
    if s_r == a:
        s_r = a - 1
    if t_l == b:
        t_l = b - 1
        t_r = b - 1
    if t_r == b:
        t_r = b - 1
    ans = min(abs(t[t_l] - s[s_r]) + min(abs(t[t_l] - x), abs(s[s_r] - x)),
    abs(s[s_l] - t[t_r]) + min(abs(s[s_l] - x), abs(t[t_r] - x)),
    abs(s[s_l] - t[t_l]) + min(abs(s[s_l] - x), abs(t[t_l] - x)),
    abs(s[s_r] - t[t_r]) + min(abs(s[s_r] - x), abs(t[t_r] - x)),
    )
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
import numpy as np
a,b,q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]

for _ in range(q):
    x = int(input())
    # print('x', x)
    ss = bisect(s, x)
    tt = bisect(t, x)
    tmp = []
    if ss > 0 and tt > 0:
        i = abs(s[ss-1] - x)
        j = abs(t[tt-1] - x)
        if sorted([s[ss-1], x, t[tt-1]]).index(x) == 1:
            # print(i+j+min(i,j), s[ss-1], t[tt-1])
            tmp.append(np.sum([i,j,min(i,j)]))
        else:
            # print((i-j), s[ss-1], t[tt-1])
            tmp.append(max(i,j))
    if 0 < ss and tt < b:
        i = abs(s[ss-1] - x)
        j = abs(t[tt] - x)
        if sorted([s[ss-1], x, t[tt]]).index(x) == 1:
            tmp.append(np.sum([i,j,min(i,j)]))
        else:
            tmp.append(max(i,j))
    if ss < a and tt > 0:
        i = abs(s[ss] - x)
        j = abs(t[tt-1] - x)
        if sorted([s[ss], x, t[tt-1]]).index(x) == 1:
            # print(i+j+min(i,j), s[ss], t[tt-1])
            tmp.append(np.sum([i,j,min(i,j)]))
        else:
            # print(i-j, s[ss], t[tt-1])
            tmp.append(max(i,j))
    if ss < a and tt < b:
        i = abs(s[ss] - x)
        j = abs(t[tt] - x)
        if sorted([s[ss], x, t[tt]]).index(x) == 1:
            # print(i+j+min(i,j), s[ss], t[tt])
            tmp.append(np.sum([i,j,min(i,j)]))
        else:
            # print(i-j, s[ss], t[tt])
            tmp.append(max(i,j))
    # print(tmp, ss, tt)
    print(min(tmp))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
A,B,Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
S.append(10**11)
S.insert(0,-10**11)
T = [int(input()) for _ in range(B)]
T.append(10**11)
T.insert(0,-10**11)
Ds = {i:0 for i in range(1,A+1)}
Ds[A+1] = 10**11
Ds[0] = 10**11
for i in range(1,A+1):
    ind = bisect_right(T,S[i])
    Ds[i] = min(T[ind]-S[i],S[i]-T[ind-1])
Dt = {j:0 for j in range(1,B+1)}
Dt[B+1] = 10**11
Dt[0] = 10**11
for j in range(1,B+1):
    ind = bisect_right(S,T[j])
    Dt[j] = min(S[ind]-T[j],T[j]-S[ind-1])
for _ in range(Q):
    x = int(input())
    inds = bisect_right(S,x)
    cnts = min(S[inds]-x+Ds[inds],x-S[inds-1]+Ds[inds-1])
    indt = bisect_right(T,x)
    cntt = min(T[indt]-x+Dt[indt],x-T[indt-1]+Dt[indt-1])
    print(min(cnts,cntt))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
A,B,Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
S.append(10**11)
T = [int(input()) for _ in range(B)]
T.append(10**11)
Ds = {i:0 for i in range(A)}
Ds[A] = 10**11
cur = 0
for i in range(A):
    ind = bisect_right(T,S[i])
    if ind==0:
        Ds[i] = T[0]-S[i]
    else:
        Ds[i] = min(T[ind]-S[i],S[i]-T[ind-1])
Dt = {j:0 for j in range(B)}
Dt[B] = 10**11
cur = 0
for j in range(B):
    ind = bisect_right(S,T[j])
    if ind==0:
        Dt[j] = S[0]-T[j]
    else:
        Dt[j] = min(S[ind]-T[j],T[j]-S[ind-1])
for _ in range(Q):
    x = int(input())
    inds = bisect_right(S,x)
    if inds==0:
        cnts = S[0]-x+Ds[0]
    else:
        cnts = min(S[inds]-x+Ds[inds],x-S[inds-1]+Ds[inds-1])
    indt = bisect_right(T,x)
    if indt==0:
        cntt = T[0]-x+Dt[0]
    else:
        cntt = min(T[indt]-x+Dt[indt],x-T[indt-1]+Dt[indt-1])
    print(min(cnts,cntt))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
A,B,Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
S.append(10**11)
T = [int(input()) for _ in range(B)]
T.append(10**11)
Ds = {i:0 for i in range(A)}
Ds[A] = 10**11
cur = 0
for i in range(A):
    if S[i]<T[cur]:
        if cur==0:
            Ds[i] = T[cur]-S[i]
        else:
            Ds[i] = min(T[cur]-S[i],S[i]-T[cur-1])
    else:
        if T[cur+1]>S[i]:
                Ds[i] = min(T[cur+1]-S[i],S[i]-T[cur])
        else:
            ind = bisect_right(T,S[i])
            cur = ind-1
            Ds[i] = min(T[cur+1]-S[i],S[i]-T[cur])
Dt = {j:0 for j in range(B)}
Dt[B] = 10**11
cur = 0
for j in range(B):
    if T[j]<S[cur]:
        if cur==0:
            Dt[j] = S[cur]-T[j]
        else:
            Dt[j] = min(S[cur]-T[j],T[j]-S[cur-1])
    else:
        if S[cur+1]>T[j]:
            Dt[j] = min(S[cur+1]-T[j],T[j]-S[cur])
        else:
            ind = bisect_right(S,T[j])
            cur = ind-1
            Dt[j] = min(S[cur+1]-T[j],T[j]-S[cur])
for _ in range(Q):
    x = int(input())
    inds = bisect_right(S,x)
    if inds==0:
        cnts = S[0]-x+Ds[0]
    else:
        cnts = min(S[inds]-x+Ds[inds],x-S[inds-1]+Ds[inds-1])
    indt = bisect_right(T,x)
    if indt==0:
        cntt = T[0]-x+Dt[0]
    else:
        cntt = min(T[indt]-x+Dt[indt],x-T[indt-1]+Dt[indt-1])
    print(min(cnts,cntt))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
A,B,Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
S.append(10**11)
T = [int(input()) for _ in range(B)]
T.append(10**11)
Ds = {i:0 for i in range(A)}
Ds[A] = 10**11
cur = 0
for i in range(A):
    if S[i]<T[cur]:
        if cur==0:
            Ds[i] = T[cur]-S[i]
        else:
            Ds[i] = min(T[cur]-S[i],S[i]-T[cur-1])
    else:
        if T[cur+1]>S[i]:
                Ds[i] = min(T[cur+1]-S[i],S[i]-T[cur])
        else:
            while T[cur+1]<S[i]:
                cur += 1
            Ds[i] = min(T[cur+1]-S[i],S[i]-T[cur])
Dt = {j:0 for j in range(B)}
Dt[B] = 10**11
cur = 0
for j in range(B):
    if T[j]<S[cur]:
        if cur==0:
            Dt[j] = S[cur]-T[j]
        else:
            Dt[j] = min(S[cur]-T[j],T[j]-S[cur-1])
    else:
        if S[cur+1]>T[j]:
            Dt[j] = min(S[cur+1]-T[j],T[j]-S[cur])
        else:
            while S[cur+1]<T[j]:
                cur += 1
            Dt[j] = min(S[cur+1]-T[j],T[j]-S[cur])
for _ in range(Q):
    x = int(input())
    inds = bisect_right(S,x)
    if inds==0:
        cnts = S[0]-x+Ds[0]
    else:
        cnts = min(S[inds]-x+Ds[inds],x-S[inds-1]+Ds[inds-1])
    indt = bisect_right(T,x)
    if indt==0:
        cntt = T[0]-x+Dt[0]
    else:
        cntt = min(T[indt]-x+Dt[indt],x-T[indt-1]+Dt[indt-1])
    print(min(cnts,cntt))

Submission status: Time Limit Exceeded

Solution code:
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 i in range(q):
    x = int(input())
    xs = bisect.bisect_right(s,x)
    xt = bisect.bisect_right(t,x)
    ss = [x-s[xs-1],s[xs]-x]
    tt = [x-t[xt-1],t[xt]-x]
    print(min(max(ss[0],tt[0]), max(ss[1],tt[1]), ss[0]*2+tt[1], ss[1]*2+tt[0], tt[0]*2+ss[1], tt[1]*2+ss[0]))

Submission status: Time Limit Exceeded

Solution code:
import bisect

inf = float('inf')


a,b,q = map(int,input().split())
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())
    xs = bisect.bisect_right(s,x)
    xt = bisect.bisect_right(t,x)
    ss = [x-s[xs-1],s[xs]-x]
    tt = [x-t[xt-1],t[xt]-x]
    print(min(max(ss[0],tt[0]), max(ss[1],tt[1]), ss[0]*2+tt[1], ss[1]*2+tt[0], tt[0]*2+ss[1], tt[1]*2+ss[0]))

Submission status: Time Limit Exceeded

Solution code:
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)]
#print("S:", S)
#print("T:", T)

for i in range(Q):
  
  x = X[i]
  answer = 10 ** 12
  
  i1 = bisect.bisect_right(S, x) - 1
  i2 = (i1 + 1) % A
  S1 = S[i1]
  S2 = S[i2]  
  dist1 = abs(x - S1)
  dist2 = abs(x - S2)
  #print("dist1, dist2:", dist1, dist2)
  
  i11 = bisect.bisect_right(T, S1) - 1
  i12 = (i11 + 1) % B
  T11 = T[i11]
  T12 = T[i12]
  dist11 = abs(S1 - T11)
  dist12 = abs(S1 - T12)
  #print("dist11, dist12:", dist11, dist12)
    
  i21 = bisect.bisect_right(T, S2) - 1
  i22 = (i21 + 1) % B
  T21 = T[i21]
  T22 = T[i22]
  dist21 = abs(S2 - T21)
  dist22 = abs(S2 - T22)
  #print("dist21, dist22:", dist21, dist22)
  
  dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22))
  answer = min(answer, dist)
  
  S, T = T, S
  A, B = B, A

  i1 = bisect.bisect_right(S, x) - 1
  i2 = (i1 + 1) % A
  S1 = S[i1]
  S2 = S[i2]  
  dist1 = abs(x - S1)
  dist2 = abs(x - S2)
  #print("dist1, dist2:", dist1, dist2)
  
  i11 = bisect.bisect_right(T, S1) - 1
  i12 = (i11 + 1) % B
  T11 = T[i11]
  T12 = T[i12]
  dist11 = abs(S1 - T11)
  dist12 = abs(S1 - T12)
  #print("dist11, dist12:", dist11, dist12)
    
  i21 = bisect.bisect_right(T, S2) - 1
  i22 = (i21 + 1) % B
  T21 = T[i21]
  T22 = T[i22]
  dist21 = abs(S2 - T21)
  dist22 = abs(S2 - T22)
  #print("dist21, dist22:", dist21, dist22)
  
  dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22))
  answer = min(answer, dist)
  
  S, T = T, S
  A, B = B, A

  print(answer)

Submission status: Time Limit Exceeded

Solution code:
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)]
#print("S:", S)
#print("T:", T)

for x in X:
  
  answer = 10 ** 12
  
  i1 = bisect.bisect_right(S, x) - 1
  i2 = (i1 + 1) % A
  S1 = S[i1]
  S2 = S[i2]  
  dist1 = abs(x - S1)
  dist2 = abs(x - S2)
  #print("dist1, dist2:", dist1, dist2)
  
  i11 = bisect.bisect_right(T, S1) - 1
  i12 = (i11 + 1) % B
  T11 = T[i11]
  T12 = T[i12]
  dist11 = abs(S1 - T11)
  dist12 = abs(S1 - T12)
  #print("dist11, dist12:", dist11, dist12)
    
  i21 = bisect.bisect_right(T, S2) - 1
  i22 = (i21 + 1) % B
  T21 = T[i21]
  T22 = T[i22]
  dist21 = abs(S2 - T21)
  dist22 = abs(S2 - T22)
  #print("dist21, dist22:", dist21, dist22)
  
  dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22))
  answer = min(answer, dist)
  
  S, T = T, S
  A, B = B, A

  i1 = bisect.bisect_right(S, x) - 1
  i2 = (i1 + 1) % A
  S1 = S[i1]
  S2 = S[i2]  
  dist1 = abs(x - S1)
  dist2 = abs(x - S2)
  #print("dist1, dist2:", dist1, dist2)
  
  i11 = bisect.bisect_right(T, S1) - 1
  i12 = (i11 + 1) % B
  T11 = T[i11]
  T12 = T[i12]
  dist11 = abs(S1 - T11)
  dist12 = abs(S1 - T12)
  #print("dist11, dist12:", dist11, dist12)
    
  i21 = bisect.bisect_right(T, S2) - 1
  i22 = (i21 + 1) % B
  T21 = T[i21]
  T22 = T[i22]
  dist21 = abs(S2 - T21)
  dist22 = abs(S2 - T22)
  #print("dist21, dist22:", dist21, dist22)
  
  dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22))
  answer = min(answer, dist)
  
  S, T = T, S
  A, B = B, A

  print(answer)

Submission status: Time Limit Exceeded

Solution code:
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)]
def BinaryIndexSearch_floor(p, SL):
	"""
	search the minimum number among the larger numbers than the ginven nymber "n". 
	return the index of it under the list sorted.
	"""
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] > p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[l] > p:
				return l
			elif SL[r] > p:
				return r
			else:
				return -1

		center = l + ((r-l) // 2)

		if SL[center] <= p:
			l = center + 1
			continue

		else:
			r = center
			continue

def BinaryIndexSearch_ceil(p, SL):
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] < p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[r] < p:
				return r
			elif SL[l] < p:
				return l
			else:
				return -1
		center = l + ((r-l) // 2)

		if SL[center] < p:
			l = center 
			continue

		else:
			r = center - 1
			continue

inf = float("inf")
for i in range(Q):
	xx = x[i]
	I_s_r = BinaryIndexSearch_floor(xx, s)
	I_t_r = BinaryIndexSearch_floor(xx, t)
	I_s_l = BinaryIndexSearch_ceil(xx, s)
	I_t_l = BinaryIndexSearch_ceil(xx, t)

	s_r = inf if I_s_r == -1 else s[I_s_r] - xx
	t_r = inf if I_t_r == -1 else t[I_t_r] - xx
	s_l = inf if I_s_l == -1 else xx - s[I_s_l]
	t_l = inf if I_t_l == -1 else xx - t[I_t_l]

	ans = []

	ans.append(min(s_r,t_l) + s_r + t_l)
	ans.append(min(t_r,s_l) + t_r + s_l)
	ans.append(max(s_r,t_r))
	ans.append(max(s_l,t_l))

	print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
A,B,Q=map(int,input().split())

def bisearch(target,lis):
    low=0
    high=len(lis)-1
    if lis[0]>target:
        return low-1
    elif lis[-1]<target:
        return high
    
    while lis[low]>target or lis[low+1]<target:
        mid=(low+high)//2
        if lis[mid]<target:
            high=mid
        else:
            low=mid
    return low

def cand(num,lis):
    N=len(lis)-1
    a=max(num,0)
    b=min(num+1,N)
    return [lis[a],lis[b]]

god=[]
for _ in range(A):
    god.append(int(input()))

buddha=[]
for _ in range(B):
    buddha.append(int(input()))

for _ in range(Q):
    q=int(input())
    g_i=(bisearch(q,god))
    b_i=(bisearch(q,buddha))
    G=cand(g_i,god)
    B=cand(b_i,buddha)
    ans=10**18
    ans=min(abs(q-G[0])+abs(G[0]-B[0]),abs(q-B[0])+abs(B[0]-G[0]),abs(q-G[1])+abs(G[1]-B[0]),abs(q-B[0])+abs(B[0]-G[1]),abs(q-G[0])+abs(G[0]-B[1]),abs(q-B[1])+abs(B[1]-G[0]),abs(q-G[1])+abs(G[1]-B[1]),abs(q-B[1])+abs(B[1]-G[1]))
    for g in G:
        for b in B:
            ans=min(ans,abs(q-g)+abs(g-b),abs(q-b)+abs(b-g))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
A,B,Q=map(int,input().split())

def bisearch(target,lis):
    low=0
    high=len(lis)-1
    if lis[0]>target:
        return low-1
    elif lis[-1]<target:
        return high
    
    while lis[low]>target or lis[low+1]<target:
        mid=(low+high)//2
        if lis[mid]<target:
            high=mid
        else:
            low=mid
    return low

def cand(num,lis):
    N=len(lis)-1
    a=max(num,0)
    b=min(num+1,N)
    return [lis[a],lis[b]]

god=[]
for _ in range(A):
    god.append(int(input()))

buddha=[]
for _ in range(B):
    buddha.append(int(input()))

for _ in range(Q):
    q=int(input())
    g_i=(bisearch(q,god))
    b_i=(bisearch(q,buddha))
    G=cand(g_i,god)
    B=cand(b_i,buddha)
    ans=10**18
    for g in G:
        for b in B:
            ans=min(ans,abs(q-g)+abs(g-b),abs(q-b)+abs(b-g))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect as bi

A,B,Q=map(int,input().split())

s=[-10**11]
for i in range(A):
    s.append(int(input()))
s.append(10**11)

t=[-10**11]
for i in range(B):
    t.append(int(input()))
t.append(10**11)

for k in range(Q):
    x=int(input())
    i=bi.bisect_left(s,x)
    sl=s[i-1];sr=s[i]
    j=bi.bisect_left(t,x)
    tl=t[j-1];tr=t[j]
    # print(sl,sr,tl,tr)
    print(min(abs(x-sl)+abs(sl-tl),abs(x-sl)+abs(sl-tr),abs(x-sr)+abs(sr-tl),abs(x-sr)+abs(sr-tr),\
        abs(x-tl)+abs(tl-sl),abs(x-tl)+abs(tl-sr),abs(x-tr)+abs(tr-sl),abs(x-tr)+abs(tr-sr)))

Submission status: Time Limit Exceeded

Solution code:
import bisect as bi

def ans(s,t,x):
    i=bi.bisect_left(s,x)
    sl=s[i-1];sr=s[i]
    j=bi.bisect_left(t,x)
    tl=t[j-1];tr=t[j]
    # print(sl,sr,tl,tr)
    return min(abs(x-sl)+abs(sl-tl),abs(x-sl)+abs(sl-tr),abs(x-sr)+abs(sr-tl),abs(x-sr)+abs(sr-tr),\
        abs(x-tl)+abs(tl-sl),abs(x-tl)+abs(tl-sr),abs(x-tr)+abs(tr-sl),abs(x-tr)+abs(tr-sr))

A,B,Q=map(int,input().split())

s=[-10**11]
for i in range(A):
    s.append(int(input()))
s.append(10**11)

t=[-10**11]
for i in range(B):
    t.append(int(input()))
t.append(10**11)

for i in range(Q):
    print(ans(s,t,int(input())))

Submission status: Time Limit Exceeded

Solution code:
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())
    ret = 2 * 10**10
    a = bisect.bisect_left(s, x)
    tmp = 0
    if a < len(s):
        tmp = abs(s[a] - x)
        ab = bisect.bisect_left(t, s[a])
        if ab < len(t):
            tmp2 = abs(t[ab] - s[a])
            ret = min(ret, tmp + tmp2)

        if ab > 0:
            tmp2 = abs(t[ab - 1] - s[a])
            ret = min(ret, tmp + tmp2)
    if a > 0:
        a -= 1
        tmp = 0
        tmp = abs(s[a] - x)
        ab = bisect.bisect_left(t, s[a])
        if ab < len(t):
            tmp2 = abs(t[ab] - s[a])
            ret = min(ret, tmp + tmp2)

        if ab > 0:
            tmp2 = abs(t[ab - 1] - s[a])
            ret = min(ret, tmp + tmp2)

    b = bisect.bisect_left(t, x)
    tmp = 0
    if b < len(t):
        tmp = abs(t[b] - x)
        ba = bisect.bisect_left(s, t[b])
        if ba < len(s):
            tmp2 = abs(s[ba] - t[b])
            ret = min(ret, tmp + tmp2)
        if ba > 0:
            tmp2 = abs(s[ba - 1] - t[b])
            ret = min(ret, tmp + tmp2)
    if b > 0:
        b -= 1
        tmp = abs(t[b] - x)
        ba = bisect.bisect_left(s, t[b])
        if ba < len(s):
            tmp2 = abs(s[ba] - t[b])
            ret = min(ret, tmp + tmp2)

        if ba > 0:
            tmp2 = abs(s[ba - 1] - t[b])
            ret = min(ret, tmp + tmp2)
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
import bisect
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]

for q in range(Q):
    x = int(input())
    ans = INF
    a_i = bisect.bisect_right(s, x)
    b_i = bisect.bisect_right(t, x)
    for a in [s[a_i], s[a_i - 1]]:
        for b in [t[b_i], t[b_i - 1]]:
            ans = min(ans, abs(x - a) + abs(b - a), abs(x - b) + abs(b - a))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
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:
    ans = float('inf')
    if x < S[0]:
        right_S = S[0]
        left_S = None
    elif x > S[-1]:
        right_S = None
        left_S = S[-1]

    else:
        index_right_S = bisect.bisect_right(S, x)
        if index_right_S < A and S[index_right_S] == x:
            left_S = x
            right_S = x
        else:
            if index_right_S - 1 >= 0:
                left_S = S[index_right_S - 1]
            if index_right_S < A:
                right_S = S[index_right_S]

    if x < T[0]:
        right_T = T[0]
        left_T = None
    elif x > T[-1]:
        right_T = None
        left_T = T[-1]

    else:
        index_right_T = bisect.bisect_right(T, x)
        if index_right_T < B and T[index_right_T] == x:
            left_T = x
            right_T = x
        else:
            if index_right_T - 1 >= 0:
                left_T = T[index_right_T - 1]
            if index_right_T < B:
                right_T = T[index_right_T]

    if left_T and right_S:
        ans = min(ans, abs(left_T - x) + abs(left_T - right_S),
                                   abs(right_S - x) + abs(left_T - right_S))

    if left_T and left_S:
        ans = min(ans, abs(left_T - x) + abs(left_T - left_S),
                                  abs(left_S - x) + abs(left_T - left_S))

    if right_T and left_S:
        ans = min(ans, abs(right_T - x) + abs(right_T - left_S),
                                   abs(left_S - x) + abs(right_T - left_S))

    if right_T and right_S:
        ans = min(ans, abs(right_T - x) + abs(right_T - right_S),
                                  abs(right_S - x) + abs(right_T - right_S))

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
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:
    if x < S[0]:
        right_S = S[0]
        left_S = None
    elif x > S[-1]:
        right_S = None
        left_S = S[-1]

    else:
        index_right_S = bisect.bisect_right(S, x)
        if index_right_S < A and S[index_right_S] == x:
            left_S = x
            right_S = x
        else:
            if index_right_S - 1 >= 0:
                left_S = S[index_right_S - 1]
            if index_right_S < A:
                right_S = S[index_right_S]

    if x < T[0]:
        right_T = T[0]
        left_T = None
    elif x > T[-1]:
        right_T = None
        left_T = T[-1]

    else:
        index_right_T = bisect.bisect_right(T, x)
        if index_right_T < B and T[index_right_T] == x:
            left_T = x
            right_T = x
        else:
            if index_right_T - 1 >= 0:
                left_T = T[index_right_T - 1]
            if index_right_T < B:
                right_T = T[index_right_T]

    if left_T and right_S:
        score_left_T_right_S = min(abs(left_T - x) + abs(left_T - right_S),
                                   abs(right_S - x) + abs(left_T - right_S))
    else:
        score_left_T_right_S = float('inf')

    if left_T and left_S:
        score_left_T_left_S = min(abs(left_T - x) + abs(left_T - left_S),
                                  abs(left_S - x) + abs(left_T - left_S))
    else:
        score_left_T_left_S = float('inf')

    if right_T and left_S:
        score_right_T_left_S = min(abs(right_T - x) + abs(right_T - left_S),
                                   abs(left_S - x) + abs(right_T - left_S))
    else:
        score_right_T_left_S = float('inf')

    if right_T and right_S:
        score_right_T_right_S = min(abs(right_T - x) + abs(right_T - right_S),
                                  abs(right_S - x) + abs(right_T - right_S))
    else:
        score_right_T_right_S = float('inf')

    print(min(score_left_T_right_S, score_left_T_left_S, score_right_T_left_S, score_right_T_right_S))

Submission status: Time Limit Exceeded

Solution code:
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)]

score_left_T_right_S = float('inf')
score_left_T_left_S = float('inf')
score_right_T_left_S = float('inf')
score_right_T_right_S = float('inf')
for x in X:
    if x < S[0]:
        right_S = S[0]
        left_S = None
    elif x > S[-1]:
        right_S = None
        left_S = S[-1]

    else:
        index_right_S = bisect.bisect_right(S, x)
        if index_right_S < A and S[index_right_S] == x:
            left_S = x
            right_S = x
        else:
            if index_right_S - 1 >= 0:
                left_S = S[index_right_S - 1]
            if index_right_S < A:
                right_S = S[index_right_S]

    if x < T[0]:
        right_T = T[0]
        left_T = None
    elif x > T[-1]:
        right_T = None
        left_T = T[-1]

    else:
        index_right_T = bisect.bisect_right(T, x)
        if index_right_T < B and T[index_right_T] == x:
            left_T = x
            right_T = x
        else:
            if index_right_T - 1 >= 0:
                left_T = T[index_right_T - 1]
            if index_right_T < B:
                right_T = T[index_right_T]

    if left_T and right_S:
        score_left_T_right_S = min(abs(left_T - x) + abs(left_T - right_S),
                                   abs(right_S - x) + abs(left_T - right_S))

    if left_T and left_S:
        score_left_T_left_S = min(abs(left_T - x) + abs(left_T - left_S),
                                  abs(left_S - x) + abs(left_T - left_S))

    if right_T and left_S:
        score_right_T_left_S = min(abs(right_T - x) + abs(right_T - left_S),
                                   abs(left_S - x) + abs(right_T - left_S))

    if right_T and right_S:
        score_right_T_right_S = min(abs(right_T - x) + abs(right_T - right_S),
                                  abs(right_S - x) + abs(right_T - right_S))

    print(min(score_left_T_right_S, score_left_T_left_S, score_right_T_left_S, score_right_T_right_S))

    score_left_T_right_S = float('inf')
    score_left_T_left_S = float('inf')
    score_right_T_left_S = float('inf')
    score_right_T_right_S = float('inf')

Submission status: Time Limit Exceeded

Solution code:
import bisect

A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
# print(S)
T = [int(input()) for _ in range(B)]
# print(T)
X = [int(input()) for _ in range(Q)]

score_left_T_right_S = float('inf')
score_left_T_left_S = float('inf')
score_right_T_left_S = float('inf')
score_right_T_right_S = float('inf')
for x in X:
    index_left_S = bisect.bisect_left(S, x)
    index_right_S = bisect.bisect_right(S, x)
    if index_left_S < A and S[index_left_S] == x:
        left_S = x
        right_S = x
    else:
        if index_right_S - 1 >= 0:
            left_S = S[index_right_S - 1]
        elif index_right_S - 1 < 0:
            left_S = None
        if index_right_S < A:
            right_S = S[index_right_S]
        elif index_right_S >= A:
            right_S = None

    index_left_T = bisect.bisect_left(T, x)
    index_right_T = bisect.bisect_right(T, x)
    if index_right_T < B and T[index_right_T] == x:
        left_T = x
        right_T = x
    else:
        if index_right_T - 1 >= 0:
            left_T = T[index_right_T - 1]
        elif index_right_T - 1 < 0:
            left_T = None
        if index_right_T < B:
            right_T = T[index_right_T]
        elif index_right_T >= B:
            right_T = None

    if left_T and right_S:
        score_left_T_right_S = min(abs(left_T - x) + abs(left_T - right_S), abs(right_S - x) + abs(left_T - right_S))

    if left_T and left_S:
        score_left_T_left_S = min(abs(left_T - x) + abs(left_T - left_S), abs(left_S - x) + abs(left_T - left_S))

    if right_T and left_S:
        score_right_T_left_S = min(abs(right_T - x) + abs(right_T - left_S),
                                   abs(left_S - x) + abs(right_T - left_S))

    if right_T and right_S:
        score_right_T_right_S = min(abs(right_T - x) + abs(right_T - right_S),
                                  abs(right_S - x) + abs(right_T - right_S))

    print(min(score_left_T_right_S, score_left_T_left_S, score_right_T_left_S, score_right_T_right_S))

    score_left_T_right_S = float('inf')
    score_left_T_left_S = float('inf')
    score_right_T_left_S = float('inf')
    score_right_T_right_S = float('inf')