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: Runtime Error

Solution code:
a,b,q=map(int,input().split())
loc_shr=[]
loc_tem=[]
loc_self=[]
dis=0

for i in range(a):
    loc_shr.append(int(input()))
for j in range(b):
    loc_tem.append(int(input()))
for k in range(q):
    loc_self.append(int(input()))
    

def search(a,length,target):
    if length==1:
        return a[0]
    else:
        if abs(a[(length//2)-1]-target)<abs(a[(length//2)]-target):
            return search(a[:(length//2)],length//2,target)
        else:
            return search(a[(length//2):],length-(length//2),target)
        
for l in range(q):
    des_1=search(loc_shr,a,loc_self[l])
    des_2=search(loc_tem,b,loc_self[l])
    idx_1=loc_shr.index(des_1)
    idx_2=loc_tem.index(des_2)
    gap_1=des_1-loc_self[l]
    gap_2=des_2-loc_self[l]
    if gap_1>=0:
        if gap_2>=0:
             print(max(gap_1,gap_2))
        else:
            sub_gap_1=loc_self[l]-loc_shr[idx_1-1]
            sub_gap_2=loc_shr[idx_2+1]-loc_self[l]
            if 2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(sub_gap_1,gap_2) and\
            2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(gap_1,sub_gap_2):
                print(2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1)))
            else:
                print(min(max(sub_gap_1,gap_2),max(gap_1,sub_gap_2)))
            
    else:
        if gap_2<0:
            print(max((gap_1)*(-1),(gap_2)*(-1)))
        else:
            sub_gap_1=loc_shr[idx_1+1]-loc_self[l]
            sub_gap_2=loc_self[l]-loc_shr[idx_2-1]
            if 2*min(gap_1*(-1),gap_2)+max(gap_1*(-1),gap_2)<max(sub_gap_1,gap_2) and\
            2*min(gap_1*(-1),gap_2)+max(gap_1*(-1),gap_2)<max(gap_1,sub_gap_2):
                print(2*min(gap_1*(-1),gap_2)+max(gap_1*(-1),gap_2))
            else:
                print(min(max(sub_gap_1,gap_2),max(gap_1,sub_gap_2)))

Submission status: Runtime Error

Solution code:
a,b,q=map(int,input().split())
loc_shr=[]
loc_tem=[]
loc_self=[]
dis=0

for i in range(a):
    loc_shr.append(int(input()))
for j in range(b):
    loc_tem.append(int(input()))
for k in range(q):
    loc_self.append(int(input()))
    

def search(a,length,target):
    if length==1:
        return a[0]
    else:
        if abs(a[(length//2)-1]-target)<abs(a[(length//2)]-target):
            return search(a[:(length//2)],length//2,target)
        else:
            return search(a[(length//2):],length-(length//2),target)
        
for l in range(q):
    des_1=search(loc_shr,a,loc_self[l])
    des_2=search(loc_tem,b,loc_self[l])
    idx_1=loc_shr.index(des_1)
    idx_2=loc_tem.index(des_2)
    gap_1=des_1-loc_self[l]
    gap_2=des_2-loc_self[l]
    if gap_1>=0:
        if gap_2>=0:
             print(max(gap_1,gap_2))
        else:
            sub_gap_1=loc_self[l]-loc_shr[idx_1-1]
            sub_gap_2=loc_shr[idx_2+1]-loc_self[l]
            if 2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(sub_gap_1,gap_2) and\
            2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(gap_1,sub_gap_2):
                print(2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1)))
            else:
                print(min(max(sub_gap_1,gap_2),max(gap_1,sub_gap_2)))
            
    else:
        if gap_2<0:
            print(max((gap_1)*(-1),(gap_2)*(-1)))
        else:
            sub_gap_1=loc_self[l]-loc_shr[idx_1-1]
            sub_gap_2=loc_shr[idx_2+1]-loc_self[l]
            if 2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(sub_gap_1,gap_2) and\
            2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(gap_1,sub_gap_2):
                print(2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1)))
            else:
                print(min(max(sub_gap_1,gap_2),max(gap_1,sub_gap_2)))

Submission status: Runtime Error

Solution code:
a,b,q=map(int,input().split())
loc_shr=[]
loc_tem=[]
loc_self=[]
dis=0

for i in range(a):
    loc_shr.append(int(input()))
for j in range(b):
    loc_tem.append(int(input()))
for k in range(q):
    loc_self.append(int(input()))
    

def search(a,length,target):
    if length==1:
        return a[0]
    else:
        if abs(a[(length//2)-1]-target)<abs(a[(length//2)]-target):
            return search(a[:(length//2)],length//2,target)
        else:
            return search(a[(length//2):],length-(length//2),target)
        
for l in range(q):
    des_1=search(loc_shr,a,loc_self[l])
    des_2=search(loc_tem,b,loc_self[l])
    idx_1=loc_shr.index(des_1)
    idx_2=loc_tem.index(des_2)
    gap_1=des_1-loc_self[l]
    gap_2=des_2-loc_self[l]
    if gap_1>=0:
        if gap_2>=0:
             print(max(gap_1,gap_2))
        else:
            sub_gap_1=loc_self[l]-loc_shr[idx_1-1]
            sub_gap_2=loc_shr[idx_2+1]-loc_self[l]
            if 2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(sub_gap_1,gap_2) and\
            2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(gap_1,sub_gap_2):
                print(2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1)))
            else:
                print(min(max(sub_gap_1,gap_2),max(gap_1,sub_gap_2)))
            
    else:
        if gap_2<0:
            print(max((gap_1)*(-1),(gap_2)*(-1)))
    else:
        sub_gap_1=loc_self[l]-loc_shr[idx_1-1]
        sub_gap_2=loc_shr[idx_2+1]-loc_self[l]
        if 2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(sub_gap_1,gap_2) and\
        2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(gap_1,sub_gap_2):
            print(2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1)))
        else:
            print(min(max(sub_gap_1,gap_2),max(gap_1,sub_gap_2)))

Submission status: Runtime Error

Solution code:
            sub_gap_1=loc_self[l]-loc_shr[idx_1-1]
            sub_gap_2=loc_shr[idx_2+1]-loc_self[l]
            if 2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(sub_gap_1,gap_2) and\
            2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))<max(gap_1,sub_gap_2):
                return 2*min(gap_1,gap_2*(-1))+max(gap_1,gap_2*(-1))
            else:
                return min(max(sub_gap_1,gap_2),max(gap_1,sub_gap_2))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import bisect,math
import multiprocessing
A,B,Q=map(int,raw_input().split())
s=[0]*A
t=[0]*B
for i in range(A):
    s[i]=input()
for a in range(B):
    t[i]=input()
buf=[]
def f(x):
    sl=bisect.bisect_left(s,x)-1
    sr=bisect.bisect_left(s,x)
    tl=bisect.bisect_left(t,x)-1
    tr=bisect.bisect_left(t,x)
    ans=float("inf")
    if sl!=-1 and tl!=-1:
        A=max(s[sl],t[tl])
        B=min(s[sl],t[tl])
        L=abs(x-A)+abs(A-B)
        ans=min(ans,L)
    if sl!=-1 and tr!=len(t):
        L=abs(x-s[sl])*2 + abs(t[tr]-x)        
        ans=min(ans,L)
        ans=min(ans,abs(x-s[sl])+abs(t[tr]-x)*2)
    if tl!=-1 and sr!=len(s):
        ans=min(ans,abs(x-t[tl])*2+abs(s[sr]-x))
        ans=min(ans,abs(x-t[tl])+abs(s[sr]-x)*2)
    if tr!=len(t) and sr!=len(s):
        A=min(s[sr],t[tr])
        B=max(s[sr],t[tr])
        L=abs(A-x)+abs(B-A)
        ans=min(ans,L)
    return str(ans)

I=[0]*Q
for i in xrange(Q):
    I[i]=input()
r=map(f,I)
print("\n".join(r))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import bisect,math
import multiprocessing
A,B,Q=map(int,raw_input().split())
s=[]
t=[]
for a in range(A):
    s.append(input())
for a in range(B):
    t.append(input())
buf=[]    
def f(x):
    sl=bisect.bisect_left(s,x)-1
    sr=bisect.bisect_left(s,x)
    tl=bisect.bisect_left(t,x)-1
    tr=bisect.bisect_left(t,x)
    ans=float("inf")
    if sl!=-1 and tl!=-1:
        A=max(s[sl],t[tl])
        B=min(s[sl],t[tl])
        L=abs(x-A)+abs(A-B)
        ans=min(ans,L)
    if sl!=-1 and tr!=len(t):
        L=abs(x-s[sl])*2 + abs(t[tr]-x)        
        ans=min(ans,L)
        ans=min(ans,abs(x-s[sl])+abs(t[tr]-x)*2)
    if tl!=-1 and sr!=len(s):
        ans=min(ans,abs(x-t[tl])*2+abs(s[sr]-x))
        ans=min(ans,abs(x-t[tl])+abs(s[sr]-x)*2)
    if tr!=len(t) and sr!=len(s):
        A=min(s[sr],t[tr])
        B=max(s[sr],t[tr])
        L=abs(A-x)+abs(B-A)
        ans=min(ans,L)
    return str(ans)

p=multiprocessing.Pool(8)
I=[]
for _ in xrange(Q):
    I.append(input())
r=p.map(f,I)
print("\n".join(r))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# D
from bisect import bisect_right
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(A)] + [INF]
for q in range(Q):
    x = int(input())
    b,d = bisect_right(s, x), bisect_right(t, x)
    ans = INF
    for s_visit in [s[b-1], s[b]]:
        for t_visit in [t[d-1, t[d]]]:
            dist1 = abs(s_visit - x) + abs(t_visit - s_visit)
            dist2 = abs(t_visit - x) + abs(s_visit - t_visit)
            ans = min(ans, dist1, dist2)
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
def getNearestValue(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]

def lazy():
    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)]#出発
    jin1=0
    tera1=0
    jin2=0
    tera2=0
    #print(s,t,x)
    for i in x:
        jin1=getNearestValue(s,i)
        tera1=getNearestValue(t,jin1)
        ans1=abs(jin1-i)+abs(tera1-jin1)

        tera2=getNearestValue(t,i)
        jin2=getNearestValue(s,tera2)
        ans2=abs(tera2-i)+abs(tera2-jin2)

        print(min(ans1,ans2))
    exit()
print(lazy())

Submission status: Runtime Error

Solution code:
import bisect
import numpy as np
INF=10**18
A,B,Q=map(int,input().split())
s=[int(input()) for i in range(A)]
t=[int(input()) for i in range(B)]
s=[-INF]+s+[INF]
t=[-INF]+t+[INF]
s=np.array(s)
t=np.array(t)
for q in range(G):
    X=int(input())
    t1=bisect.bisect_right(t,X)
    l1=X-t[t1]
    if (t[t1]-X)>(X-t[t1-1]):
        l1=X-t[t1-1]
    s1=bisect.bisect_right(s,X)
    l2=X-s[s1]
    if (s[s1]-X)>(X-s[s1-1]):
        l2=X-s[s1-1]
    l3=bisect.bisect_right(s,X-l1)
    l4=bisect.bisect_right(t,X-l2)
    print(min(abs(l1)+min(s[l3]-X+l1,X-l1-s[l3-1]),abs(l2)+min(t[l4]-X+l2,X-l2-t[l4-1])))

Submission status: Runtime Error

Solution code:
import bisect
A,B,Q = map(int, input().split())

INF = 1e+18

s_list = [-INF] + [int(input()) for _ in range(A)] + [INF]
t_list = [-INF] + [int(input()) for _ in range(B)] + [INF]
x_list = [int(input()) for _ in range(Q)]

def get_ans(s_list, t_list, x):
    b, d = bisect.bisect_right(s_list, x), bisect.bisect_right(t_list, x)
    ans = 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)
            ans = min(ans, d1, d2)
    return ans
        
for x in x_list:
    print(get_ans(s_list, t_list, x))

Submission status: Runtime Error

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

for i in range(q):
    Q = x[i]
    ans_s = []
    ans_t = []
    ans_s.append(s[bi.bisect_left(s,Q)])
    ans_t.append(t[bi.bisect_left(t,Q)])
    ans = 10**11
    
    if bi.bisect_right(s,Q) == len(s):
        ans_s.append(s[bi.bisect_left(s,Q)-1])
    else:
        ans_s.append(s[bi.bisect_left(s,Q)])
        
    if bi.bisect_right(t,Q) == len(t):
        ans_t.append(t[bi.bisect_left(t,Q)-1])
    else:
        ans_t.append(t[bi.bisect_left(t,Q)])
    
    for i in range(2):
        for j in range(2):
            ans = max(ans,abs(ans_s[i]-Q)+abs(ans_s[i]-ans_t[j]),abs(ans_t[j]-Q)+abs(ans_s[i]-ans_t[j]))
    
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a,b,q = map(int,input().split())
s = [-float('inf')]+[int(input()) for i in range(a)]+[float('inf')]
t = [-float('inf')]+[int(input()) for i in range(b)]+[float('inf')]
x = [int(input()) for i in range(q)]
import bisect
for x_ in x:
    s_r = bisect.bisect_right(s,x_)
    t_r = bisect.bisect_right(s,x_)
    res = float('inf')
    for S in [s[s_r-1],s[s_r]]:
        for T in [t[t_r-1],t[t_r]]:
            d1 = abs(S-x_)+abs(T-S)
            d2 = abs(T-x_)+abs(S-T)
            res = min(res,d1,d2)
    print(res)

Submission status: Runtime Error

Solution code:
a,b,q = map(int,input().split())
s = [-float('inf')]+[int(input()) for i in range(a)]+[float('inf')]
t = [-float('inf')]+[int(input()) for i in range(b)]+[float('inf')]
x = [int(input()) for i in range(q)]
import bisect
for x_ in x:
    s_r = bisect.bisect_right(s,x_)
    t_r = bisect.bisect_right(s,x_)
    res = INF
    for S in [s[s_r-1],s[s_r]]:
        for T in [t[t_r-1],t[t_r]]:
            d1 = abs(S-x_)+abs(T-S)
            d2 = abs(T-x_)+abs(S-T)
            res = min(res,d1,d2)
    print(res)

for ans_ in ans:
    print(ans_)

Submission status: Runtime Error

Solution code:
import itertools

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 i in range(Q):

    insert_index = bisect.bisect_left(s,x[i])
    if insert_index >= A:
        insert_index = insert_index -1
        tmp1 = s[insert_index]
        tmp2 = s[insert_index]
        
    else:   
        tmp1 = s[insert_index]
        tmp2 = s[insert_index-1]
    
    insert_index = bisect.bisect_left(t,x[i])
    if insert_index >= B:
        insert_index = insert_index -1
        tmp3 = t[insert_index]
        tmp4 = t[insert_index]
        
    else:
        tmp3 = t[insert_index]
        tmp4 = t[insert_index-1]
    
    result = 10 ** 11
    for j, k in ([0,1], [0,0], [1,0], [1,1]):
        tmp = 0
        if j == 0:
            tmp += abs(tmp1 - x[i])
            now = tmp1
        else:
            tmp += abs(tmp2 - x[i])
            now = tmp2
                
        if k == 0:
            tmp += abs(now - tmp3)
        else:
            tmp += abs(now - tmp4)
                
        result = min(tmp, result)
        
    for j, k in ([0,1], [0,0], [1,0], [1,1]):
        tmp = 0
        
        if k == 0:
            tmp += abs(x[i] - tmp3)
            now = tmp3
        else:
            tmp += abs(x[i]- tmp4)
            now = tmp4
            
        if j == 0:
            tmp += abs(now - tmp1)
        else:
            tmp += abs(now - tmp2)
                
        result = min(tmp, result)
                
    print(result)

Submission status: Runtime Error

Solution 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]
x_list = [int(input()) for i in range(Q)]

for x in x_list:
    
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    a = b - 1
    c = d - 1 
    ans = min(abs(s[a]-x) + min(abs(t[a]-s[a]), abs(t[b]-s[a])),
              abs(s[b]-x) + min(abs(t[a]-s[b]), abs(t[b]-s[b])),
              abs(t[a]-x) + min(abs(s[a]-t[a]), abs(s[b]-t[a])),
              abs(t[b]-x) + min(abs(s[a]-t[b]), abs(s[b]-t[b])))

    # for s in [s_list[a], s_list[b]]:
    #     for t in [t_list[c], t_list[d]]:
    #         dist1 = abs(s-x) + abs(t-s)
    #         dist2 = abs(t-x) + abs(t-s)
    #         ans = min(ans, dist1,dist2)
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import bisect
import math
A,B,Q = map(int,input().split())
a = []
b = []
x = []
for i in range(A):
  a.append(int(input()))
for j in range(B):
  b.append(int(input()))
for k in range(Q):
  x.append(int(input()))
a.insert(0,-(10**15))
a.append(10**15)
b.insert(0,-(10**15))
b.append(10**15)
for k in range(Q):  
  terafirst = 0
  jinjyafirst = 0
  p = bisect.bisect_left(a,x[k])
  q = bisect.bisect_left(b,x[k])
  terafirst = min(abs(x[k]-a[p-1]),abs(x[k]-a[p]))
  if abs(x[k]-a[p-1])<abs(x[k]-a[p]):
    r = bisect.bisect_left(b,a[p-1])
    terafirst += min(abs(b[r-1]-a[p-1]),abs(b[r]-a[p-1]))
  else:
    s = bisect.bisect_left(b,a[p])
    terafirst += min(abs(b[s-1]-a[p]),abs(b[s]-a[p]))
      
  jinjyafirst = min(abs(x[k]-b[q-1]),abs(x[k]-b[q]))
  if abs(x[k]-b[q-1])<abs(x[k]-b[q]):
    t = bisect.bisect_left(a,b[p-1])
    jinjyafirst += min(abs(a[t-1]-b[q-1]),abs(a[t]-b[q-1]))
  else:
    u = bisect.bisect_left(a,b[q])
    jinjyafirst += min(abs(a[u-1]-b[q]),abs(a[u]-b[q]))
  print(min(terafirst,jinjyafirst))

Submission status: Runtime Error

Solution code:
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())
    s_l = bisect.bisect_left(s,x)
    s_h = s_l + 1
    t_l = bisect.bisect_left(t,x)
    t_h = t_l + 1

    ans = inf
    for s in [S[s_l],S[s_h]]:
        for t in [T[t_l],T[t_h]]:
            d1 = abs(s-x)+abs(t-s)
            d2 = abs(t-x)+abs(s-t)
            ans = min(ans,d1,d2)
    
    print(ans)

Submission status: Runtime Error

Solution code:
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 x in range(Q):
  b = bisect.bisect_right(s, x)
  d = disect.bisect_right(t, x)
  dist = 0
  for sb in [s[b - 1], s[b]]:
    for td in [t[d - 1], t[d]]:
      d1, d2 = abs(x - sb) + abs(sb - td), abs(x - td) + abs(td - sb)
      dist = min(dist, d1, d2)
  print(dist)

Submission status: Runtime Error

Solution code:
import bisect

A, B, Q = map(int,input().split())
s_list = [-inf] +  [int(input()) for i in range(A)] + [inf]
t_list = [-inf] + [int(input()) for i in range(B)] + [inf]
x_list = [int(input()) for i in range(Q)]

for x in x_list:
    
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    a = b - 1
    c = d - 1 
    ans = inf
    for s in s_list[a], s_list[b]:
        for t in t_list[c], t_list[d]:
            dist1 = abs(s-x) + abs(t-s)
            dist2 = abs(t-x) + abs(t-s)
            ans = min(ans, dist1,dist2)
    print(ans)

Submission status: Runtime Error

Solution code:
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())
    s_l = bisect.bisect_left(s,x)
    s_h = s_l + 1
    t_l = bisect.bisect_left(t,x)
    t_h = t_l + 1

    ans = inf
    for s in [s_l,s_h]:
        for t in [t_l,t_h]:
            d1 = abs(s-x)+abs(t-s)
            d2 = abs(t-x)+abs(s-t)
            ans = min(ans,d1,d2)
    
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# -*- 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_type, k):
  if st_type == 's':
    st_s = s_s
  else:
    st_s = t_s

  idx = bisect.bisect_right(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):
  # s -> t
  idx = bisect.bisect_right(s_s, k)
  if idx == 0:
    mn = abs(s_s[0]-k) + get_otherlen('t', s_s[0])
  elif idx == len(s_s):
    mn = abs(s_s[-1]-k) + get_otherlen('t', s_s[-1])
  else:
    mn1 = abs(s_s[idx]-k) + get_otherlen('t', s_s[idx])
    mn2 = abs(s_s[idx-1]-k) + get_otherlen('t', s_s[idx-1])
    mn = min(mn1, mn2)

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


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

Submission status: Runtime Error

Solution code:
# -*- 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_type, k):
  if st_type == 's':
    st_s = s_s
  else:
    st_s = t_s

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


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

Submission status: Runtime Error

Solution code:
# -*- 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):
  # s -> t
  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(get_len(x))