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:
input = sys.stdin.readline

A,B,Q = map(int,input().split(" "))
s,t,q = [],[],[]
for paramlist, N in zip([s,t,q],[A,B,Q]):
    for n in range(N):
        paramlist.append(int(input()))   

s,t,q = [np.array(l,dtype=np.int64) for l in [s,t,q]]
def least_neighbor_t(s):
    sw = 10**5 if t[0] > s else s - np.max(t[t<s])
    se = 10**5 if t[-1] < s else np.min(t[t>s]) - s
    return min(sw,se)
    
def least_neighbor_s(t):
    tw = 10**5 if s[0] > t else t - np.max(s[s<t])
    te = 10**5 if s[-1] < t else np.min(s[s>t]) - t
    return min(tw,te)

s2t = np.frompyfunc(least_neighbor_t,1,1)(s)
t2s = np.frompyfunc(least_neighbor_s,1,1)(t)

def getdist(x):
    return min(np.min(np.abs(s - x) + s2t,axis=0),np.min(np.abs(t - x) + t2s,axis=0))

result = np.frompyfunc(getdist,1,1)(q)
print('\n'.join(list(map(str,result))))

Submission status: Runtime Error

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

A,B,Q = map(int,input().split(" "))
s,t,q = [],[],[]
for paramlist, N in zip([s,t,q],[A,B,Q]):
    for n in range(N):
        paramlist.append(int(input()))   

s,t,q = [np.array(l,dtype=np.int64) for l in [s,t,q]]
ss,tt = np.meshgrid(s,t)
s_t = np.abs(ss - tt)
s2t = np.min(s_t,axis=0)
t2s = np.min(s_t,axis=1)

def getdist(x):
    return min(np.min(np.abs(s - x) + s2t,axis=0),np.min(np.abs(t - x) + t2s,axis=0))

result = np.frompyfunc(getdist,1,1)(q)
print('\n'.join(list(map(str,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]
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:
import bisect
a, b, q = list(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)]
s.sort()
t.sort()
for i in x:
    si = bisect.bisect(s, i)
    ti = bisect.bisect(t, i)
    sl = s[si-1] if si > 0 else s[0]
    sr = s[si] if si < a else s[-1]
    tl = t[ti-1] if ti > 0 else t[0]
    tr = t[ti] if ti < a else t[-1]
    print(min(
        abs(i-sl)+abs(sl-tl),
        abs(i-sl)+abs(sl-tr),
        abs(i-sr)+abs(sr-tl),
        abs(i-sr)+abs(sr-tl),
        abs(i-tl)+abs(sl-tl),
        abs(i-tl)+abs(sr-tl),
        abs(i-tr)+abs(sl-tr),
        abs(i-tr)+abs(sr-tr),
    ))

Submission status: Runtime Error

Solution code:
def zengo(l,n):
    if n<l[0]:
        return 0,l[0]-n
    elif n>l[-1]:
        return n-l[-1],0
    else:
        st=0
        ed=len(l)
        while ed-st>1:
            md=(st+ed)//2
            if n<l[md]:
                ed=md
            else:
                st=md
        return n-l[st],l[ed]-n

near=lambda x,y:min(x,y) if x*y else max(x,y)

jn,tn,qn,*tmp=map(int,open(0).read.split())
jz=tmp[:jn]
tz=tmp[jn:jn+tn]
q=tmp[jn+tn:]
for i in q:
    jb,jf=zengo(jz,i)
    tb,tf=zengo(tz,i)
    ans=[]
    if jb*tb:
        ans.append(max(jb,tb))
    if jf*tf:
        ans.append(max(jf,tf))
    jd=near(jb,jf)
    td=near(tb,tf)
    ans.append(jd+td+min(jd,td))
    print(min(ans))

Submission status: Runtime Error

Solution code:
def zengo(l,n):
    if n<l[0]:
        return 0,l[0]-n
    elif n>l[-1]:
        return n-l[-1],0
    else:
        st=0
        ed=len(l)
        while ed-st>1:
            md=(st+ed)//2
            if n<l[md]:
                ed=md
            else:
                st=md
        return n-l[st],l[ed]-n

near=lambda x,y:min(x,y) if x*y else max(x,y)

jn,tn,qn,*tmp=map(int,open(0).split())
jz=tmp[:jn]
tz=tmp[jn:jn+tn]
q=tmp[jn+tn:]
for i in q:
    jb,jf=zengo(jz,i)
    tb,tf=zengo(tz,i)
    ans=[]
    if jb*tb:
        ans.append(max(jb,tb))
    if jf*tf:
        ans.append(max(jf,tf))
    jd=near(jb,jf)
    td=near(tb,tf)
    ans.append(jd+td+min(jd,td))
    print(min(ans))

Submission status: Runtime Error

Solution code:
with open("td") as inputfile:
    open0=inputfile.read()

def zengo(l,n):
    if n<l[0]:
        return 0,l[0]-n
    elif n>l[-1]:
        return n-l[-1],0
    else:
        st=0
        ed=len(l)
        while ed-st>1:
            md=(st+ed)//2
            if n<l[md]:
                ed=md
            else:
                st=md
        return n-l[st],l[ed]-n

near=lambda x,y:min(x,y) if x*y else max(x,y)

jn,tn,qn,*tmp=map(int,open0.split())
jz=tmp[:jn]
tz=tmp[jn:jn+tn]
q=tmp[jn+tn:]
for i in q:
    jb,jf=zengo(jz,i)
    tb,tf=zengo(tz,i)
    ans=[]
    if jb*tb:
        ans.append(max(jb,tb))
    if jf*tf:
        ans.append(max(jf,tf))
    jd=near(jb,jf)
    td=near(tb,tf)
    ans.append(jd+td+min(jd,td))
    print(min(ans))

Submission status: Runtime Error

Solution code:
def bisearch(li,target):
    l=0
    r=len(li)-1
    if(target<li[0][0]):
        return li[m][0]-target+li[m][1]
    if(li[-1][0]<target):
        return target-li[-1][0]+li[-1][1]
    while(l<=r):
        m=int((l+r)/2)
        if(li[m][0]<target and target<li[m+1][0]):
            return min(target-li[m][0]+li[m][1],li[m+1][0]-target+li[m+1][1])
        if(target<li[m][0]):
            r = m-1
        else:
            l = m+1


inf = 10**15
A,B,Q = [int(x) for x in input().split()]
s = [0]*(A+1)
t = [0]*(B+1)
x = [0]*Q


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

li = []
a = b = 0
while(a<A or b<B):
    if(s[a]<t[b]):
        li.append([s[a],min(abs(t[b]-s[a]),abs(t[b-1]-s[a]))])
        a+=1
    else:
        li.append([t[b],min(abs(t[b]-s[a]),abs(t[b]-s[a-1]))])
        b+=1
        

for i in x:
    print(bisearch(li,i))

Submission status: Runtime Error

Solution code:
def bisearch(li,target):
    l=0
    r=len(li)-1
    if(target<li[0][0]):
        return li[m][0]-target+li[m][1]
    elif(li[-1][0]<target):
        return target-li[-1][0]+li[-1][1]
    while(l<=r):
        m=int((l+r)/2)
        if(li[m][0]<target and target<li[m+1][0]):
            return min(target-li[m][0]+li[m][1],li[m+1][0]-target+li[m+1][1])
        if(target<li[m][0]):
            r = m-1
        else:
            l = m+1


inf = 10**15
A,B,Q = [int(x) for x in input().split()]
s = [0]*(A+1)
t = [0]*(B+1)
x = [0]*Q


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

li = []
a = b = 0
while(a<A or b<B):
    if(s[a]<t[b]):
        li.append([s[a],min(abs(t[b]-s[a]),abs(t[b-1]-s[a]))])
        a+=1
    else:
        li.append([t[b],min(abs(t[b]-s[a]),abs(t[b]-s[a-1]))])
        b+=1

x.append(500)

for i in x:
    print(bisearch(li,i))

Submission status: Runtime Error

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

a_list = [-100001]+[int(input()) for _ in range(A)] + [100001]
b_list = [-100001]+[int(input()) for _ in range(B)] + [100001]
for _ in range(Q):
    x = int(input())
    right_s = abs(x - a_list[bisect.bisect_left(a_list, x)])
    right_t = abs(x - b_list[bisect.bisect_left(b_list, x)])
    left_s = abs(x - a_list[bisect.bisect_right(a_list, x)-1])
    left_t = abs(x - b_list[bisect.bisect_right(b_list, x)-1])
    print(
            min(
                max(left_t, left_s),
                max(right_t, right_s),
                left_t + (left_t + right_s),
                left_s + (left_s + right_t),
                right_t + (right_t + left_s),
                right_s + (right_s + left_t)
            )
    )

Submission status: Runtime Error

Solution code:
import bisect

A, B, Q = [int(i) for i in input().split()]

s = [-10**12] + [int(input()) for i in range(A)] + [10**12]
t = [-10**12] + [int(input()) for i in range(B)] + [10**12]
x = [int(input()) for i in range(Q)]

for item in x:
    b, d = bisect.bisect_left(s, item), bisect.bisect_left(t, item)
    min_value = 10**12
    for item_a in [s[a], s[a+1]]:
        for item_c in [t[c], t[c+1]]:
            pata = abs(item_a - item) + abs(item_a - item_c)
            patb = abs(item_c - item) + abs(item_a - item_c)
            min_value = min(min_value, pata, patb)
    print(min_value)

Submission status: Runtime Error

Solution code:
import numpy as np

A, B, Q = [int(i) for i in input().split()]

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

xtot = np.abs(x-t.T).argmin(axis=0)
xtos = np.abs(s-x.T).argmin(axis=1)

t = np.array([t[0, xtot]])
s = np.array([s[0, xtos]])

stot = np.abs(s-t.T).argmin(axis=0)
ttos = np.abs(s-t.T).argmin(axis=1)

pata = abs(x - t) + abs(t - s[0, ttos]) 
patb = abs(x - s) + abs(s - t[0, stot]) 

for i in range(Q):
    if pata[0, i] >= patb[0, i]:
        print(patb[0, i])
    else:
        print(pata[0, i])

# for i, (a, b) in enumerate(zip(xtot, xtos)):
#     pata = abs(x[0, i] - t[0, i]) + abs(t[0, i] - s[0, ttos[i]]) 
#     patb = abs(x[0, i] - s[0, i]) + abs(s[0, i] - t[0, stot[i]]) 
#     if pata <= patb:
#         print(pata)
#     else:
#         print(patb)

Submission status: Runtime Error

Solution code:
import numpy as np

A, B, Q = [int(i) for i in input().split()]

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

stot = np.abs(s-t.T).argmin(axis=0)
ttos = np.abs(s-t.T).argmin(axis=1)
xtot = np.abs(x-t.T).argmin(axis=0)
xtos = np.abs(s-x.T).argmin(axis=1)

for i, (a, b) in enumerate(zip(xtot, xtos)):
    pata = abs(x[0, i] - t[0, a]) + abs(t[0, a] - s[0, ttos[a]]) 
    patb = abs(x[0, i] - s[0, b]) + abs(s[0, b] - t[0, stot[b]]) 
    if pata <= patb:
        print(pata)
    else:
        print(patb)

Submission status: Runtime Error

Solution code:
import numpy as np
INF=10**18

A,B,Q = list(map(int, input().split()))
s = np.asarray([-INF] + [int(input()) for _ in range(A)] + [INF])
t = np.asarray([-INF] + [int(input()) for _ in range(B)] + [INF])
x = [int(input()) for _ in range(Q)]

r1 = np.asarray([*s[1:-1],*t[1:-1]])
r2 = np.asarray([])
r3 = np.asarray([])
for tmp in s[1:-1]:
    r2 = np.append(r2,min(abs(tmp - np.max(t[tmp>t])),abs(np.min(t[tmp<t]) - tmp)))
for tmp in t[1:-1]:
    r2 = np.append(r2,min(abs(tmp - np.max(s[tmp>s])),abs(np.min(s[tmp<s]) - tmp)))

r1 = np.asarray([-INF,*r1,INF])
r2 = np.asarray([-INF,*r2,INF])

for tmp in x:
    left = np.max(r1[tmp>r1])
    #print(r2[r1==left])
    left = abs(left - tmp) + r2[r1==left]
    right = np.min(r1[tmp<r1])
    right = abs(right - tmp) + r2[r1==right]
    r3 = np.append(r3,min(left,right))

for tmp in r3:print(int(tmp))

Submission status: Runtime Error

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

jinjya = []
otera = []

for i in range(a):
    jinjya.append(int(input()))
for i in range(b):
    otera.append(int(input()))

def find(arr,tar,start,end):
    if start == end:
        return start
    bar = (start+end)//2
    if arr[bar]<tar:
        return find(arr,tar,bar+1,end)
    elif arr[bar]>tar:
        return find(arr,tar,start,bar-1)
    else:
        return bar
for i in range(q):
    jinjyaEast = 0
    jinjyaWest = 0
    oteraEast = 0
    oteraWest = 0
    pos = int(input())
    idx = find(jinjya,pos,0,len(jinjya)-1)
    if(jinjya[idx] < pos):
        jinjyaWest = jinjya[idx]
        if idx<len(jinjya)-1:
            jinjyaEast = jinjya[idx+1]
    else:
        jinjyaEast = jinjya[idx]
        if idx>0:
            jinjyaWest = jinjya[idx-1]
    idx = find(otera,pos,0,len(otera)-1)
    if(otera[idx] < pos):
        oteraWest = otera[idx]
        if idx<len(otera)-1:
            oteraEast = otera[idx+1]
    else:
        oteraEast = otera[idx]
        if idx>0:
            oteraWest = otera[idx-1]
    res = []
    j = []
    o = []
    if jinjyaWest != 0:
        j.append(jinjyaWest)
    if jinjyaEast != 0:
        j.append(jinjyaEast)
    if oteraWest != 0:
        o.append(oteraWest)
    if oteraEast != 0:
        o.append(oteraEast)
    for a in j:
        for b in o:
            res.append(min(abs(pos-a)+abs(b-a),abs(pos-b)+abs(a-b)))
    print(min(res))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
# sn, tn, qn = map(int, input().split())

sa = []
ta = []
qa = []
for i in range(sn):
    sa.append(int(input()))
for i in range(tn):
    ta.append(int(input()))
for i in range(qn):
    qa.append(int(input()))


def find_nearest(q, al, s, e):
    if e - s < 2:
        d1 = abs(q - al[s])
        d2 = abs(q - al[e])
        if d1 < d2:
            return al[s]
        return al[e]
    idx = (e - s) // 2 + s
    if al[idx] == q:
        return al[idx]
    if al[idx] > q:
        return find_nearest(q, al, s, idx)
    return find_nearest(q, al, idx, e)

def find_range(q1, q2, al, s, e):
    if al[s] >= q1 and al[s] <= q2:
        return True
    if al[e] >= q1 and al[e] <= q2:
        return True
    if al[s] >= q2 or al[e] <= q1 or e - s < 2:
        return False
    idx = (e - s) // 2 + s
    if al[idx] < q1:
        return find_range(q1, q2, al, s, idx)
    return find_range(q1, q2, al, idx, e)

def move_cost(q, al, bl):
    mx = find_nearest(q, al, 0, len(al) - 1)
    min_x = min(mx, q)
    max_x = max(mx, q)
    if find_range(min_x, max_x, bl, 0, len(bl) - 1):
        return max_x - min_x

    mx2 = find_nearest(mx, bl, 0, len(bl) - 1)
    return (max_x - min_x) + abs(mx2 - mx)

for q in qa:
    a1 = move_cost(q, sa, ta)
    a2 = move_cost(q, ta, sa)
    print(min(a1, a2))

Submission status: Runtime Error

Solution code:
a, b, q = map(int, input().split(' '))
s, t, x = [], [], []
for i in range(a):
  s.append(int(input()))
for i in range(b):
  t.append(int(input()))
for i in range(q):
  x.append(int(input()))
  
def func(lis, val):
  l, h = 0, 0
  if val < lis[0]:
    l, h = -10**10, lis[0]
  elif val > lis[-1]:
    l, h = lis[-1], -10**10
  else:
    l = list(filter(lambda w: w < val, lis))[-1]
    h = filter(lambda w: w > val, lis)[0]
  return l, h
 
for i in range(q):
  sl, sh = func(s, x[i])
  tl_sl, th_sl = func(t, sl)
  tl_sh, th_sh = func(t, sh)
  tl, th = func(t, x[i])
  sl_tl, sh_tl = func(s, tl)
  sl_th, sh_th = func(s, th)
  a1 = abs(sl - x[i]) + abs(tl_sl - sl)
  a2 = abs(sl - x[i]) + abs(th_sl - sl)
  b1 = abs(sh - x[i]) + abs(tl_sh - sh)
  b2 = abs(sh - x[i]) + abs(th_sh - sh)
  c1 = abs(tl - x[i]) + abs(sl_tl - tl)
  c2 = abs(tl - x[i]) + abs(sh_tl - tl)
  d1 = abs(th - x[i]) + abs(sl_th - th)
  d2 = abs(th - x[i]) + abs(sh_th - th)
  j = min(a1, a2, b1, b2, c1, c2, d1, d2)
  print(j)

Submission status: Runtime Error

Solution code:
a, b, q = map(int, input().split(' '))
s, t, x = [], [], []
for i in range(a):
  s.append(int(input()))
for i in range(b):
  t.append(int(input()))
for i in range(q):
  x.append(int(input()))
  
def func(lis, val):
  l, h = 0, 0
  if val < lis[0]:
    l, h = -10**10, lis[0]
  elif val > lis[-1]:
    l, h = lis[-1], -10**10
  else:
    l = reversed(filter(lambda w: w < val, lis))[0]
    h = filter(lambda w: w > val, lis)[0]
  return l, h

for i in range(q):
  sl, sh = func(s, x[i])
  tl_sl, th_sl = func(t, sl)
  tl_sh, th_sh = func(t, sh)
  tl, th = func(t, x[i])
  sl_tl, sh_tl = func(s, tl)
  sl_th, sh_th = func(s, th)
  a1 = abs(sl - x[i]) + abs(tl_sl - sl)
  a2 = abs(sl - x[i]) + abs(th_sl - sl)
  b1 = abs(sh - x[i]) + abs(tl_sh - sh)
  b2 = abs(sh - x[i]) + abs(th_sh - sh)
  c1 = abs(tl - x[i]) + abs(sl_tl - tl)
  c2 = abs(tl - x[i]) + abs(sh_tl - tl)
  d1 = abs(th - x[i]) + abs(sl_th - th)
  d2 = abs(th - x[i]) + abs(sh_th - th)
  j = min(a1, a2, b1, b2, c1, c2, d1, d2)
  print(j)

Submission status: Runtime Error

Solution code:
from bisect import*
A,B=abs,bisect
a,b,q,*l=map(int,open(0).read().split())
f=2**60
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i,j=B(s,x),B(t,x)
  for v in s[i-1,i+1]:
    for w in t[j-1,j+1]:
      m=min(m,A(x-v)+A(v-w),A(x-w)+A(w-v))
  print(m)

Submission status: Runtime Error

Solution code:
N = list(map(int,input().split()))
for i in range(N[0]):
  s[i] = int(input())
for i in range(N[1]):
  t[i] = int(input())
for i in range(N[2]):
  x[i] = int(input())
print(s,t,x)

Submission status: Runtime Error

Solution code:
import sys
import bisect
input = sys.stdin.readline

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

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

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

Submission status: Runtime Error

Solution code:
import sys
import bisect
input = sys.stdin.readline

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

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

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

Submission status: Runtime Error

Solution code:
#AVL

##search(0,x):O(logN)
# xがある場合indexを、ない場合Noneを返す
def search(root,key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            return None
        else:
            return search(avl_left[root],key)
    if avl_key[root] < key:
        if avl_right[root] == None:
            return None
        else:
            return search(avl_right[root],key)
    return root


##search_lower(0,x,None):O(logN)
# xより小さいものの中で最も大きいものを出力。ない場合はNoneを出力
def search_lower(root,key,lower_key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            return lower_key
        else:
            return search_lower(avl_left[root],key,lower_key)
    if avl_key[root] < key:
        lower_key = avl_key[root]
        if avl_right[root] == None:
            return lower_key
        else:
            return search_lower(avl_right[root],key,lower_key)
    
    # == 
    return key

##search_higher(0,x,None):O(logN)
# xより大きいものの中で最も小さいものを出力。ない場合はNoneを出力
def search_higher(root,key,higher_key):
    if avl_key[root] > key:
        higher_key = avl_key[root]
        if avl_left[root] == None:
            return higher_key
        else:
            return search_higher(avl_left[root],key,higher_key)
    if avl_key[root] < key:
        if avl_right[root] == None:
            return higher_key
        else:
            return search_higher(avl_right[root],key,higher_key)
    
    # == 
    return key



##Rotation,replace,insertx:insertで使用
def DoubleRightRotation(x):
    tl = avl_left[x]
    avl_left[x] = avl_right[avl_right[tl]]
    avl_right[avl_right[tl]] = x
    tlr = avl_right[tl]
    avl_right[tl] = avl_left[tlr]
    avl_left[tlr] = tl
    if balance[tlr] == -1:
        balance[avl_right[tlr]] = 1
        balance[avl_left[tlr]] = 0
    elif balance[tlr] == 1:
        balance[avl_right[tlr]] = 0
        balance[avl_left[tlr]] = -1
    else:
        balance[avl_right[tlr]] = 0
        balance[avl_left[tlr]] = 0
    balance[tlr] = 0
    return tlr

def DoubleLeftRotation(x):
    tr = avl_right[x]
    avl_right[x] = avl_left[avl_left[tr]]
    avl_left[avl_left[tr]] = x
    trl = avl_left[tl]
    avl_left[tr] = avl_right[trl]
    avl_right[trl] = tr
    if balance[trl] == 1:
        balance[avl_right[trl]] = 0
        balance[avl_left[trl]] = -1
    elif balance[trl] == -1:
        balance[avl_left[trl]] = 0
        balance[avl_right[trl]] = 1
    else:
        balance[avl_right[trl]] = 0
        balance[avl_left[trl]] = 0
    balance[trl] = 0
    return trl

def SingleLeftRotation(x):
    tr = avl_right[x]
    balance[tr] = 0
    balance[x] = 0
    avl_right[x] = avl_left[tr]
    avl_left[tr] = x
    return tr

def SingleRightRotaion(x):
    tl = avl_left[x]
    balance[tl] = 0
    balance[x] = 0
    avl_left[x] = avl_right[tl]
    avl_right[tl] = x
    return tl

def replace(x,p,v):
    if avl_left[p] == x:
        avl_left[p] = v
    else:
        avl_right[p] = v

def insertx(root,p,key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            avl_key.append(key)
            avl_left[root] = len(avl_key)-1
        else:
            if not insertx(avl_left[root],root,key):
                return False
        if balance[root] == 1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = -1
            return True
        else:
            if balance[avl_left[root]] == 1:
                replace(root,p,DoubleRightRotation(root))
            elif balance[avl_left[root]] == -1:
                replace(root,p,SingleRightRotation(root))
            return False
    if avl_key[root] < key:
        if avl_right[root] == None:
            avl_key.append(key)
            avl_right[root] = len(avl_key)-1
        else:
            if not insertx(avl_right[root],root,key):
                return False
        if balance[root] == -1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = 1
            return True
        else:
            if balance[avl_right[root]] == -1:
                replace(root,p,DoubleLeftRotation(root))
            elif balance[avl_right[root]] == 1:
                replace(root,p,SingleLeftRotation(root))
            return False
    return False


##insert(0,x):O(logN)
#x追加
def insert(root,key):
    if key < avl_key[root]:
        if avl_left[root] == None:
            avl_key.append(key)
            avl_left[root] = len(avl_key)-1
        else:
            insertx(avl_left[root],root,key)
    elif key > avl_key[root]:
        if avl_right[root] == None:
            avl_key.append(key)
            avl_right[root] = len(avl_key)-1
        else:
            insertx(avl_right[root],root,key)
    else:
        pass

########################################################
##初期化（要素は一つ入れておく）

#avl_key:値,avl_left:左の子のindex,avl_right:右の子のindex
#balance[i]: {"Even":0,"Left":-1,"Right":1}

#avl_keyには初めに何か加えておく
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)]

avl_key = [s[0]]
avl_left = [None]*(a+2)
avl_right = [None]*(a+2)
balance = [0]*(a+2)
for i in range(1,a):
    insert(0,s[i])
insert(0,float("inf"))
insert(0,-float("inf"))

for i in range(q):
    print(search_higher(0,x[i],None))
    print(search_lower(0,x[i],None))

avl_key = [t[0]]
avl_left = [None]*b
avl_right = [None]*b
balance = [0]*b
insert(0,float("inf"))
insert(0,-float("inf"))
for i in range(1,b):
    insert(0,t[i])
for i in range(q):
    print(search_higher(0,x[i],None))
    print(search_lower(0,x[i],None))