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:
from functools import lru_cache

@lru_cache(maxsize=100000)
def nearest(now, flag):
    disps = adisps if flag == 0 else bdisps
    idx = np.argmin(np.abs(disps - now))
    nearest_point = disps[idx]
    cost = np.abs(now - nearest_point)
    return nearest_point, cost

for qdisp in qdisps:

    #A→B
    pos_a1, cost_a1 = nearest(qdisp, 0)
    pos_b1, cost_b1 = nearest(pos_a1, 1)
    cost1 = cost_a1 + cost_b1

    #B→A
    pos_b2, cost_b2 = nearest(qdisp, 1)
    pos_a2, cost_a2 = nearest(pos_b2, 0)
    cost2 = cost_a2 + cost_b2

    ans = min(cost1, cost2)
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

ST=[]
for i in range(a):
    k=int(input())
    ST.append([k,0,0])
for i in range(b):
    k=int(input())
    ST.append([k,1,0])

ST.sort()

le0=-1
le1=-1
ri0=1
ri1=1

for i in range(a+b):
    if ST[i][1]==0 :
        pa=1
        
        moyori=np.inf

        if le1>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le1][0]))
        if ri1<=i :
            ri1=i+1
            while ri1<=a+b-1 and ST[ri1][1]==ty :
                ri1+=1
        if ri1<=a+b-1:
            moyori=min(moyori,abs(ST[i][0]-ST[ri1][0]))
        ST[i][2]=moyori
        le0=i
    else:
        pa=0
        
        moyori=np.inf

        if le0>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le0][0]))
        if ri0<=i :
            ri0=i+1
            while ri0<=a+b-1 and ST[ri0][1]==ty :
                ri0+=1
        if ri0<=a+b-1:
            moyori=min(moyori,abs(ST[i][0]-ST[ri0][0]))
        ST[i][2]=moyori
        le1=i



#print(ST)

for i in range(q):
    k=int(input())
    ST.append([k,2,0,i])

ST.sort()

ANS=[]
le0=-1
le1=-1
ri0=1
ri1=1
for i in range(a+b+q):
    if ST[i][1]==0 :
        le0=i
    elif ST[i][1]==1 :
        le1=i
    elif ST[i][1]==2 :
        moyori=np.inf
    
            
        if le0>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le0][0])+ST[le0][2])
            #print("L"+str(le[typ]))
            #print(moyori)
        if ri0<=i :
            ri0=i+1
            while ri0<=a+b+q-1 and (ST[ri0][1]==1 or ST[ri0][1]==2):
                ri0+=1
        if ri0<=a+b+q-1 :
            moyori=min(moyori,abs(ST[i][0]-ST[ri0][0])+ST[ri0][2])
            #print("R"+str(ri[typ]))
            #print(moyori)

        if le1>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le1][0])+ST[le1][2])
            #print("L"+str(le[typ]))
            #print(moyori)
        if ri1<=i :
            ri1=i+1
            while ri1<=a+b+q-1 and (ST[ri1][1]==0 or ST[ri1][1]==2):
                ri1+=1
        if ri1<=a+b+q-1 :
            moyori=min(moyori,abs(ST[i][0]-ST[ri1][0])+ST[ri1][2])
            #print("R"+str(ri[typ]))
            #print(moyori)



        ST[i][2]=moyori
        ANS.append([ST[i][3],moyori])

ANS.sort()

#print(ST)

for i in range(q):
    print(ANS[i][1])

Submission status: Runtime Error

Solution code:
a, b, q = list(map(int, input().split()))
ss, tt, xx = [], [], []
for _ in range(a):
    ss.append(int(input()))
for _ in range(b):
    tt.append(int(input()))
for _ in range(q):
    xx.append(int(input()))

ss.sort()
tt.sort()

import bisect
def get_left(aa, bb, pos):
    a1 = bisect.bisect_left(aa, pos)
    b1 = bisect.bisect_left(bb, pos)
    if a1 == b1 == 0:
        return 10 ** 9
    if a1 == 0 or (b1 > 0 and bb[b1 - 1] > aa[a1 - 1]):
        dists = [10 ** 9]
        if a1 < len(aa):
            dists.append(pos - bb[b1 - 1] + aa[a1] - bb[b1 - 1])
        if a1 > 0:
            dists.append(pos - aa[a1 - 1])
        return min(dists)
    dists = [10 ** 9]
    if b1 < len(bb):
        dists.append(pos - aa[a1 - 1] + bb[b1] - aa[a1 - 1])
    if b1 > 0:
        dists.append(pos - bb[b1 - 1])
    return min(dists)

def get_right(aa, bb, pos):
    a1 = bisect.bisect_left(aa, pos)
    b1 = bisect.bisect_left(bb, pos)
    if a1 == len(aa) and b1 == len(bb):
        return 10 ** 9
    if a1 == len(aa) or (b1 < len(bb) and bb[b1] < aa[a1]):
        dists = [10 ** 9]
        if a1 > 0:
            dists.append(- pos + bb[b1] - aa[a1 - 1] + bb[b1])
        if a1 < len(aa):
            dists.append(aa[a1] - pos)
        return min(dists)
    dists = [10 ** 9]
    if b1 > 0:
        dists.append(- pos + aa[a1] - bb[b1 - 1] + aa[a1])
    if b1 > 0:
        dists.append(bb[b1] - pos)
    return min(dists)

for x in xx:
    print(min(get_left(ss, tt, x), get_right(ss, tt, x)))

Submission status: Runtime Error

Solution code:
import sys

A, B, Q = map(int, input().split())
S = [int(sys.stdin.readline()) for _ in range(A)]
T = [int(sys.stdin.readline()) for _ in range(B)]
X = [int(sys.stdin.readline()) for _ in range(Q)]
INF = 10**12

def binary_search(query, arr):
    arr = [-INF] + arr + [INF]
    low = 0
    high = len(arr)
    t = (low+high)//2
    for _ in range(100):
        if low<high-1:
            break
        if query>arr[t]:
            low = t
        else:
            high = t
        t = (low+high)//2
    return arr[low], arr[high]

def find(query):
    s1, s2 = binary_search(query, S)
    t1, t2 = binary_search(query, T)
    return s1, s2, t1, t2

def main():
    for x in X:
        s1, s2, t1, t2 = find(x)
        cands = [
            abs(t1-x) + abs(s2-t1),
            abs(s1-x) + abs(t2-s1),
            abs(s2-x) + abs(t1-s2),
            abs(t2-x) + abs(s1-t2),
            abs(t1-x) + abs(s1-t1),
            abs(s1-x) + abs(t1-s1),
            abs(s2-x) + abs(t2-s2),
            abs(t2-x) + abs(s2-t2)
        ]
        print(min(cands))
    return

main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
a, b ,q = map(int, input().split())
a_list = [int(input()) for i in range(a)]
b_list = [int(input()) for i in range(b)]
for n in range(q):
    q_pt = int(input())
    l = max(a_list) + max(b_list)
    for a_pt in a_list:
        for b_pt in b_list:
            l = min(l, abs(q_pt-a_pt)+abs(a_pt-b_pt)), abs(q_pt-b_pt)+abs(b_pt-a_pt)
    print(l)

Submission status: Runtime Error

Solution code:
A,B,Q=[int(x) for x in input().split()]
LA=[]#神社
LB=[]#寺
for i in range(A):
    LA.append(int(input()))
for i in range(B):
    LB.append(int(input()))
mx=200000000001

for i in range(Q):
    x=int(input())
    if LA[-1]<x:
        sa=mx
        sb=x-LA[-1]
    else:
        if x<LA[int(len(LA)/2)]:        
            for js,s in enumerate(LA):
                if x<s:
                    if js==0:
                        sb=mx
                    else:
                        sb=x-LA[js-1]
                    sa=s-x
                    break
        else:
            LA.reverse()
            for js,s in enumerate(LA):
                if s<x:
                    sa=x-LA[js-1]
                    sb=s-x
                    break
    if LB[-1]<x:
        ta=mx
        tb=x-LB[-1]
    else:
        if x<LB[int(len(LB)/2)]:
            for jt,t in enumerate(LB):
                if x<t:
                    if jt==0:
                        tb=mx
                    else:
                        tb=x-LB[jt-1]
                    ta=t-x
                    break
        else:
            LB.reverse()
            for jt,t in enumerate(LB):
                if t<x:
                    sa=x-LA[j-1]
                    tb=t-x
                    break
    
    print(min(max(ta,sa),max(tb,sb),tb+sa+min(tb,sa),ta+sb+min(ta,sb)))

Submission status: Runtime Error

Solution code:

import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1])
		print(aaaa)

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

Submission status: Runtime Error

Solution code:
import bisect

a,b,n=map(int,input().split())
l1=[int(input()) for i in range(a)]
l2=[int(input()) for i in range(b)]
x=[int(input()) for i in range(n)]
l1.sort()
l2.sort()
for i in range(n):
    idx1=bisect.bisect_left(l1,x[i])
    if idx1<a:
        p=abs(l1[idx1-1]-x[i])
        q=abs(l1[idx1]-x[i])
        for j in range(2):
            if j==0:
                po=l1[idx1-1]
                idx3=bisect.bisect_left(l2,po)
                if idx3<b:
                    dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
                else:
                    dis=po-l2[-1]
                dis1=p+dis
            else:
                po=l1[idx1]
                idx3=bisect.bisect_left(l2,po)
                if idx3<a:
                    dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
                else:
                    dis=po-l2[-1]
                dis1=min(dis1,q+dis)
    else:
        p=x[i]-l1[-1]
        po=l1[-1]
        idx3=bisect.bisect_left(l2,po)
        if idx3<b:
            dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
            dis1=dis+p
        else:
            dis1=po-l2[-1]+p
    idx2=bisect.bisect_left(l2,x[i])
    if idx2<b:
        p=abs(l2[idx2-1]-x[i])
        q=abs(l2[idx2]-x[i])
        for j in range(2):
            if j==0:
                po=l2[idx2-1]
                idx3=bisect.bisect_left(l1,po)
                if idx3<a:
                    dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
                else:
                    dis=po-l1[-1]
                dis2=p+dis
            else:
                po=l2[idx2]
                idx3=bisect.bisect_left(l1,po)
                if idx3<a:
                    dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
                else:
                    dis=po-l1[-1]
                dis2=min(dis2,q+dis)
    else:
        p=x[i]-l2[-1]
        po=l2[-1]
        idx3=bisect.bisect_left(l1,po)
        if idx3<a:
            dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
            dis2=dis+p
        else:
            dis2=po-l1[-1]+p
    print(min(dis1,dis2))

Submission status: Runtime Error

Solution code:
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()))
X  =[int(input()) for i in range(q)]
#入力


for x in X:
    s_index= bisect.bisect_left(s,x)
    #print(s_index)
    t_index= bisect.bisect_left(t,x)
    #print(t_index)
    if s_index == 0 and t_index == 0:
        print(max(t[0]-x,s[0]-x))
    elif s_index == 0:
        if t_index != b:
            if t[t_index] < s[0]:
                print(s[0]-x)
            else:
                ans  =min(2*s[0]-x - t[t_index-1],t[t_index]-x,x+s[0]-2*t[t_index-1])
                print(ans)
        else:
            print(min(2*s[s_index]-x-t[t_index-1],s[s_index]+x-2*t[t_index-1]))
    elif t_index == 0:
        if s_index !=a:
            if s[s_index] < t[0]:
                print(t[0]-x)
            else:
                ans  =min(2*t[0]- x- s[s_index-1],s[s_index]-x,x+t[0]-2*s[s_index-1])
                print(ans)
        else:
            print(min(2*t[t_index]-x-s[s_index-1],t[t_index]+x-2*s[s_index-1]))
    elif s_index == a and t_index == b:
        print(max(x-s[a-1],x-t[b-1]))
    elif s_index == a:
        if s[a-1] < t[t_index-1]:
            print(x-s[a-1])
        else:
            ans = min(x-t[t_index-1],t[t_index]+x-2*s[a-1],2*t[t_index]-x-s[a-1])
            print(ans)
    elif t_index == b:
        if t[a-1] < s[s_index-1]:
            print(x-t[b-1])
        else:
            ans = min(x - s[s_index - 1], s[s_index] + x - 2 * t[b - 1],2*s[s_index]-x-t[b-1])
            print(ans)

    else:
        if s[s_index-1] < t[t_index-1] and s[s_index] < t[t_index]:
            ans = min(x-s[s_index-1],x+s[s_index]-2*t[t_index-1],2*s[s_index]-x-t[t_index-1],t[t_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and t[t_index] < s[s_index]:
            ans = min(x - t[t_index - 1], x + t[t_index] - 2 * s[s_index - 1], 2 * t[t_index] - x - s[s_index - 1],
                      s[s_index] - x)
            print(ans)
        if s[s_index-1] < t[t_index-1] and t[t_index] < s[s_index]:
            ans = min(x-s[s_index-1],s[s_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and s[s_index] < t[t_index]:
            ans = min(x - t[t_index - 1], t[t_index] - x)
            print(ans)

Submission status: Runtime Error

Solution code:
A,B,Q = list(map(int, input().split()))
s,t,xx=[],[],[]

for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    xx.append(int(input()))
    
import bisect
M = 10000000000000

for x in xx:
    i = bisect.bisect_left(s,x)
    j = bisect.bisect_left(t,x)
    
    if (i > 0) and (j > 0):
        ll = abs(min(s[i-1]-x, t[j-1]-x))
    else:
        ll = M
    if (i > 0) and (j < B):
        lr = abs(s[i-1]-x) * 2 + (t[j]-x)
    else:
        lr = M
    if (i < A) and (j > 0):
        lr_2 = abs(t[j-1]-x) * 2 + (s[i]-x)
    else:
        lr_2 = M
    if (i < A) and (j > 0):
        rl = (s[i]-x) * 2 + abs(t[j-1] - x)
    else:
        rl = M
    if (i > 0) and (j < B):
        rl_2 = (t[j]-x) * 2 + abs(s[i-1] - x)
    else:
        rl_2 = M
    if (i < A) and (j < B):
        rr = max(s[i]-x,t[i]-x)
    else:
        rr = M

    print(min(ll,lr,rl,rr,lr_2,rl_2))

Submission status: Runtime Error

Solution code:
A, B, Q = map(int, input().split())
jin = [int(input()) for i in range(A)]
tera = [int(input()) for i in range(B)]
x = [int(input()) for i in range(Q)]

def near_jinja(x):
    l = list(map(lambda y: abs(y-x), jin))
    near_place = l.index(min(l))
    near_dist = l[near_place]
    return [near_place, near_dist]

def near_tera(x):
    l = list(map(lambda y: abs(y-x), tera))
    near_place = l.index(min(l))
    near_dist = l[near_place]
    return [near_place, near_dist]

for i in range(Q):
    pattern1 = near_tera(tera[near_jinja(x[i])[0]]) + near_jinja(x[i])[1]
    pattern2 = near_jinja(jin[near_tera(x[i])[0]]) + near_tera(x[i])[1]
    if pattern1 < pattern2:
        print(pattern1)
    else:
        print(pattern2)

Submission status: Runtime Error

Solution code:
A,B,Q = list(map(int, input().split()))
s,t,xx=[],[],[]

for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    xx.append(int(input()))
    
import bisect
M = float('inf')

for x in xx:
    i = bisect.bisect_left(s,x)
    j = bisect.bisect_left(t,x)
    
    if (i > 0) and (j > 0):
        ll = abs(min(s[i-1]-x, t[j-1]-x))
    else:
        ll = M
    if (i > 0) and (j < B):
        lr = abs(s[i-1]-x) * 2 + (t[j]-x)
    else:
        lr = M
    if (i < A) and (j > 0):
        lr_2 = abs(t[j-1]-x) * 2 + (s[i]-x)
    else:
        lr_2 = M
    if (i < A) and (j > 0):
        rl = (s[i]-x) * 2 + abs(t[j-1] - x)
    else:
        rl = M
    if (i > 0) and (j < B):
        rl_2 = (t[j]-x) * 2 + abs(s[i-1] - x)
    else:
        rl_2 = M
    if (i < A) and (j < B):
        rr = max(s[i]-x,t[i]-x)
    else:
        rr = M

    print(min(ll,lr,rl,rr,lr_2,rl_2))

Submission status: Runtime Error

Solution code:
def solve(a, b, q, al, bl, ql):
    sql = sorted(ql)

    qi = 0
    qdic = {}

    ai = 0
    qi = 0
    asmall = None
    abig = None
    while(True):
        if al[ai] <= sql[qi]:
            asmall = al[ai]
            ai += 1
            if a <= ai:
                for i in range(qi,q):
                    qdic[sql[i]] = [asmall, None]
                break
        else:
            abig = al[ai]
            qdic[sql[qi]] = [asmall, abig]
            qi += 1
            if q <= qi:
                break
            abig = None

    
    bi = 0
    qi = 0
    bsmall = None
    bbig = None
    while(True):
        if bl[bi] <= sql[qi]:
            bsmall = bl[bi]
            bi += 1
            if b <= bi:
                for i in range(qi, q):
                    qdic[sql[qi]].append(bsmall)
                    qdic[sql[qi]].append(None)
                break
        else:
            bbig = bl[bi]
            qdic[sql[qi]].append(bsmall)
            qdic[sql[qi]].append(bbig)
            qi += 1
            if q <= qi:
                break
            bbig = None
            
    for e in ql:
        asmall, abig, bsmall, bbig = qdic[e]
        
        answer = 2 * (10 ** 10)
    
        # small , small
        if( asmall is not None) and (bsmall is not None):
            t = min(asmall,bsmall)
            v = e - t
            if v < answer:
                answer = v

        # big , big
        if( abig is not None) and (bbig is not None):
            t = max(abig,bbig)
            v = t - e
            if v < answer:
                answer = v

        # small , big
        if( asmall is not None) and (bbig is not None):
            v = (bbig - asmall) + min(bbig - e, e - asmall)
            if v < answer:
                answer = v

        # big , big
        if( abig is not None) and (bsmall is not None):
            v = (abig - bsmall) + min(abig - e, e - bsmall)
            if v < answer:
                answer = v

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

al = []
for i in range(a):
    al.append(int(input()))
bl = []
for i in range(b):
    bl.append(int(input()))
ql = []
for i in range(q):
    ql.append(int(input()))

solve(a, b, q, sorted(al), sorted(bl), ql)

Submission status: Runtime Error

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

INF = float('inf')
for _ in range(q):
    x = int(input())
    if x > s[a-1]:
        sl = x - s[a-1]
        sr = INF
    elif x < s[0]:
        sl = INF
        sr = s[0] - x
    else:
        si = bisect.bisect_right(s, x)
        sl = x - s[si-1]
        sr = s[si] - x

    if x > t[a-1]:
        tl = x - t[a-1]
        tr = INF
    elif x < t[0]:
        tl = INF
        tr = t[0] - x
    else:
        ti = bisect.bisect_right(t, x)
        tl = x - t[ti-1]
        tr = t[ti] - x

    ans = min(max(sl, tl), max(sr, tr))

    ans = min(ans, sr*2+tl)
    ans = min(ans, sr+tl*2)
    ans = min(ans, sl*2+tr)
    ans = min(ans, sl+tr*2)

    print(ans)

Submission status: Runtime Error

Solution code:
import bisect
A,B,Q = map(int, input().split())
SList=[]
TList=[]
for i in range(A):
  SList.append(int(input()))
for i in range(B):
  TList.append(int(input()))


  
for i in range(Q):
  x=int(input())
  kouho=[]
  insert_indexS=bisect.bisect_left(SList,x)
  if not insert_indexS==0:
    kyoriS0 = x-SList[insert_indexS-1]
    insert_indexT=bisect.bisect_left(TList,SList[insert_indexS-1])
    if not insert_indexT==0:
      kyoriS00 = kyoriS0+SList[insert_indexS-1]-TList[insert_indexT-1]
      kouho.append(kyoriS00)
    if not insert_indexT==len(TList):
      kyoriS01 = kyoriS0+TList[insert_indexT]-SList[insert_indexS-1]
      kouho.append(kyoriS01)
  if not insert_indexS == len(SList):
    kyoriS1=SList[insert_indexS]-x
    insert_indexT=bisect.bisect_left(TList,SList[insert_indexS])
    if not insert_indexT==0:
      kyoriS10 = kyoriS1+SList[insert_indexS]-TList[insert_indexT-1]
      kouho.append(kyoriS10)
    if not insert_indexT==len(TList):
      kyoriS11 = kyoriS1+TList[insert_indexT]-SList[insert_indexS]
      kouho.append(kyoriS11)

  insert_indexT=bisect.bisect_left(TList,x)
  if not insert_indexT==0:
    kyoriT0 = x-TList[insert_indexT-1]
    insert_indexS=bisect.bisect_left(SList,TList[insert_indexT-1])
    if not insert_indexS==0:
      kyoriT00 = kyoriT0+TList[insert_indexT-1]-SList[insert_indexS-1]
      kouho.append(kyoriT00)
    if not insert_indexT==len(TList):
      kyoriT01 = kyoriT0+SList[insert_indexS]-TList[insert_indexT-1]
      kouho.append(kyoriT01)
  if not insert_indexT == len(TList):
    kyoriT1=TList[insert_indexT]-x
    insert_indexS=bisect.bisect_left(SList,TList[insert_indexT])
    if not insert_indexS==0:
      kyoriT10 = kyoriT1+TList[insert_indexT]-SList[insert_indexS-1]
      kouho.append(kyoriT10)
    if not insert_indexS==len(SList):
      kyoriT11 = kyoriT1+SList[insert_indexS]-TList[insert_indexT]
      kouho.append(kyoriT11)
  print(min(kouho))

Submission status: Runtime Error

Solution code:
a, b, q = map(int, input().split())
shrine = []
temple = []
question = []
for i in range(a):
    shrine.append(int(input()))
for i in range(b):
    temple.append(int(input()))
for i in range(q):
    question.append(int(input()))

shrine.sort()
temple.sort()

def ans(ls, rs, lt, rt, q_i):
    ans_list = []
    if ls != 0 and lt != 0:
        ans_list.append(abs(min(ls, lt)- q_i))
    if rs != 0 and ls != 0:
        ans_list.append(abs(max(rs, rt) - q_i))
    if ls != 0 and rt != 0:
        ans_list.append(2*abs(ls-q_i) + abs(rt - q_i))
        ans_list.append(2*abs(rt-q_i) + abs(ls - q_i))
    if lt != 0 and rs != 0:
        ans_list.append(2*abs(rs-q_i) + abs(lt - q_i))
        ans_list.append(2*abs(lt-q_i) + abs(rs - q_i))
    return min(ans_list)

for q_i in question:
    ls = 0
    rs = 0
    lt = 0
    rt = 0
    for n, s_i in enumerate(shrine):
        if s_i > q_i:
            ls = shrine[n-1]
            ts = s_i
            break
    for n, t_i in enumerate(temple):
        if t_i > q_i:
            lt = temple[n-1]
            rt = t_i
            break
    print(ans(ls, rs, lt, rt, q_i))

Submission status: Runtime Error

Solution code:
from math import inf
A, B, Q = map(int, input().split())
s = []
t = []
x = []

for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
for _ in range(Q):
    x.append(int(input()))

for startP in range(Q):
    sp_place = x[startP]
    dist = inf
    nextP = None
    archi = None
    move = 0
    for i in range(A):
        tmpdist = abs(s[i] - sp_place)
        if tmpdist < dist:
            dist = tmpdist
            nextP = s[i]
            archi = 's'
    for i in range(B):
        tmpdist = abs(t[i] - sp_place)
        if tmpdist < dist:
            dist = tmpdist
            nextP = t[i]
            archi = 't'
    sp_place = nextP
    move = dist
    dist = inf
    if archi == 't':
        for i in range(A):
            tmpdist = abs(s[i] - sp_place)
            if tmpdist < dist:
                dist = tmpdist
    else: #if s
        for i in range(B):
            tmpdist = abs(t[i] - sp_place)
            if tmpdist < dist:
                dist = tmpdist
    move += dist
    print(move)

Submission status: Runtime Error

Solution code:
import bisect
from itertools import permutations
A, B, Q = map(int, input().split())
S = []
for _ in range(A):
    S.append(int(input()))
T = []
for _ in range(B):
    T.append(int(input()))

for _ in range(Q):
    x = int(input())
    sr = bisect.bisect_right(S, x)
    tr = bisect.bisect_right(T, x)
    c = []
    if sr == A:
        c.append([S[A-1], 0])
    elif sr == 0:
        c.append([S[sr], 0])
    else:
        c.append([S[sr], 0])
        c.append([S[sr-1], 0])
    if tr == B:
        c.append([T[B-1], 1])
    elif tr == 0:
        c.append([T[tr], 1])
    else:
        c.append([T[tr], 1])
        c.append([T[tl-1], 1])
    ans = 10**16
    for elem in permutations(c):
        tmp = 0
        flag = [False, False]
        now_pos = x
        flag_calc = True
        for pos, kind in elem:
            if flag[kind]:
                flag_calc = False
                break
            tmp += abs(pos-now_pos)
            now_pos = pos
            flag[kind] = True
            if flag[0] and flag[1]:
                break
        if flag_calc:
            ans = min(ans, tmp)
    print(ans)

Submission status: Runtime Error

Solution code:
A, B, Q = map(int, input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for _ in range(Q)]

s_list.sort()
t_list.sort()
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    if s_i == A and t_i == B:
        tmp = abs(x - min(s_list[-1], t_list[-1]))
        ans.append(tmp)
        continue
    if s_i == A:
        if t_i != 0:
            tmp = abs(s_list[-1] - x)
            if s_list[-1] <= t_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(t_list[t_i] - x) * 2, abs(t_list[t_i - 1] - s_list[-1]))
        else:
            tmp = abs(min(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)) * 2 + max(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)))
        ans.append(tmp)
        continue
    if t_i == B:
        if s_i != 0:
            tmp = abs(t_list[-1] - x)
            if t_list[-1] <= s_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(s_list[s_i] - x) * 2, abs(s_list[s_i - 1] - t_list[-1]))
        else:
            tmp = abs(min(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)) * 2 + max(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)))
        ans.append(tmp)
        continue
    tmp = min(tmp, max(s_list[s_i], t_list[t_i]) - x)
    if s_i != 0:
        tmp = min(tmp, s_list[s_i - 1] + t_list[t_i] - x)
    if t_i != 0:
        tmp = min(tmp, s_list[s_i] + t_list[t_i - 1] - x)
    ans.append(tmp)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
a, b, q = map(int, input().split())
shrine = []
temple = []
question = []
for i in range(a):
    shrine.append(int(input()))
for i in range(b):
    temple.append(int(input()))
for i in range(q):
    question.append(int(input()))

shrine.sort()
temple.sort()

def ans(ls, rs, lt, rt, q_i):
    ans_list = []
    if ls != 0 and lt != 0:
        ans_list.append(abs(min(ls, lt)- q_i))
    if rs != 0 and ls != 0:
        ans_list.append(abs(max(rs, rt) - q_i))
    if ls != 0 and rt != 0:
        ans_list.append(2*abs(ls-q_i) + abs(rt - q_i))
        ans_list.append(2*abs(rt-q_i) + abs(ls - q_i))
    if lt != 0 and rs != 0:
        ans_list.append(2*abs(rs-q_i) + abs(lt - q_i))
        ans_list.append(2*abs(lt-q_i) + abs(rs - q_i))
    return min(ans_list)

for q_i in question:
    ls = 0
    rs = 0
    lt = 0
    rt = 0
    for s_i in shrine:
        if s_i > q_i:
            rs = s_i
            break
        else:
            ls = s_i
    for t_i in temple:
        if t_i > q_i:
            rt = t_i
            break
        else:
            lt = t_i
    print(ans(ls, rs, lt, rt, q_i))

Submission status: Runtime Error

Solution code:
import bisect

a,b,n=map(int,input().split())
l1=[int(input()) for i in range(a)]
l2=[int(input()) for i in range(b)]
x=[int(input()) for i in range(n)]
l1.sort()
l2.sort()
for i in range(n):
    idx1=bisect.bisect_left(l1,x[i])
    if idx1<a:
        p=abs(l1[idx1-1]-x[i])
        q=abs(l1[idx1]-x[i])
        if p>q:
            po=l1[idx1]
            dis1=q
        else:
            po=l1[idx1-1]
            dis1=p
    else:
        dis1=x[i]-l1[-1]
        po=l1[-1]
    idx3=bisect.bisect_left(l2,po)
    if idx3<b:
        dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
    else:
        dis=po-l2[-1]
    dis1+=dis
    idx2=bisect.bisect_left(l2,x[i])
    if idx2<b:
        p=abs(l2[idx2-1]-x[i])
        q=abs(l2[idx2]-x[i])
        if p>q:
            po=l2[idx2]
            dis2=q
        else:
            po=l2[idx-1]
            dis2=p
    else:
        dis2=x[i]-l2[-1]
        po=l2[-1]
    idx3=bisect.bisect_left(l1,po)
    if idx3<a:
        dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
    else:
        dis=po-l1[-1]
    dis2+=dis
    print(min(dis1,dis2))