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:
#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


##end_lower/higher:search_lower/higherで使用
def end_lower(root):
    if avl_left[root] == None:
        return avl_key[root]
    else:
        return end_lower(avl_left[root])

def end_higher(root):
    if avl_right[root] == None:
        return avl_key[root]
    else:
        return end_higher(avl_right[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)
    
    # == 
    if avl_left[root] == None:
        return lower_key
    else:
        if lower_key == None:
            return end_higher(avl_left[root])
        else:
            return max(lower_key,end_higher(avl_left[root]))

##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)
    
    # == 
    if avl_right[root] == None:
        return higher_key
    else:
        if higher_key == None:
            return end_lower(avl_right[root])
        else:
            return min(higher_key,end_lower(avl_right[root]))



##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)]
avl_key = [s]
avl_left = [None]*a
avl_right = [None]*a
balance = [0]*a

for i in range(1,a):
    insert(0,s[i])
t = [int(input()) for i in range(b)]
avl_key = [t[0]]
avl_left = [None]*b
avl_right = [None]*b
balance = [0]*b
for i in range(1,b):
    insert(0,t[i])

print(1)

Submission status: Runtime Error

Solution code:
import bisect
import sys
A, B, Q = map(int, input().split())
INF = 10 ** 13
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
xs = [int(sys.stdin.readline()) for i in range(Q)]
for x in range(xs):
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
    for S in [s[b - 1], s[b]]:
        for T in [t[d - 1], t[d]]:
            d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
            res = min(res, d1, d2)
    print(res)

Submission status: Runtime Error

Solution code:
def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))
 
    jinja_list = []
 
    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))
 
    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))
 
    from bisect import bisect
 
    for i in range(q):
        pos = int(input().rstrip("\n"))
 
        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]
 
        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]
 
        distances = []
        #distances.append((pos, jinja_left, jinja_right, temple_left, temple_right))
        if jinja_left and temple_left:
            distances.append(pos - min(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(max(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = (pos - jinja_left[0]) * 2 + (temple_right[0] - pos)
            right_left = (pos - jinja_left[0]) + (temple_right[0] - pos) * 2
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = ((pos - temple_left[0]) * 2) + (jinja_right[0] - pos)
            right_left = (pos - temple_left[0]) + ((jinja_right[0] - pos) * 2)
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))

if __name __ == '__main__':
  main()

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
#%%
import bisect

#%%
a,b,q = map(int, input().split())
INF = 10**18
shrines = [-INF] + [int(input() for _ in range(a))] + [INF]
temples = [-INF] + [int(input() for _ in range(b))] + [INF]

for _ in range(q):

    start = int(input())

    # スタート位置より右にある神社のうち、もっともスタート位置に近いもの
    shrine_east = shrines[bisect.bisect_right(shrines, start)]
    # スタート位置より右にある寺のうち、もっともスタート位置に近いもの
    temple_east = temples[bisect.bisect_right(temples, start)]

    # スタート位置より左にある神社のうち、もっともスタート位置に近いもの
    shrine_west = shrines[bisect.bisect_right(shrines, start) - 1]
    # スタート位置より左にある寺のうち、もっともスタート位置に近いもの
    temple_west = temples[bisect.bisect_right(temples, start) - 1]

    ans = INF

    for shrine in [shrine_east, shrine_west]:
        for temple in [temple_east,temple_west]:
            cur = min(abs(shrine-start), abs(temple-start)) + abs(shrine-temple)
            ans = min(ans,cur)

    print(ans)



#%%
arr = [4,8,12,16,20]

#%%
bisect.bisect_left(arr,12)

#%%
bisect.bisect_right(arr,12)

#%%

Submission status: Runtime Error

Solution code:
import bisect

#%%
a,b,q = map(int, input().split())
INF = 10**18
shrines = [-INF] + [int(input() for _ in range(a))] + [INF]
temples = [-INF] + [int(input() for _ in range(b))] + [INF]

for _ in range(q):

    start = int(input())

    # スタート位置より右にある神社のうち、もっともスタート位置に近いもの
    shrine_east = shrines[bisect.bisect_right(shrines, start)]
    # スタート位置より右にある寺のうち、もっともスタート位置に近いもの
    temple_east = temples[bisect.bisect_right(temples, start)]

    # スタート位置より左にある神社のうち、もっともスタート位置に近いもの
    shrine_west = shirines[bisect.bisect_right(shrines, start) - 1]
    # スタート位置より左にある寺のうち、もっともスタート位置に近いもの
    temple_west = temples[bisect.bisect_right(temples, start) - 1]

    ans = INF

    for shrine in [shrine_east, shrine_west]:
        for temple in [temple_east,temple_west]:
            cur = min(abs(shrine-start), abs(temple-start)) + abs(shrine-temple)
            ans = min(ans,cur)

    print(ans)

Submission status: Runtime Error

Solution code:
#%%
import numpy as np
#%%

s,t,q = list(map(int, input().split()))
shrines = []
temples = []
questions = []
answers = []
dist = np.empty((s,t),dtype='int64')

for _ in range(s):
    shrines.append(int(input()))
shrines = np.array(shrines)

for _ in range(t):
    temples.append(int(input()))
temples = np.array(temples)

for _ in range(q):
    questions.append(int(input()))

for i, shrine in enumerate(shrines):
    dist[i]= [abs(shrine - temple) for temple in temples]

#%%
print(shrines,temples,dist)

#%%
for start in questions:
    min_shrine_start = float('inf')
    min_temple_start = float('inf')

    for i, st_s in enumerate(shrines - start):
        abs_st_s = abs(st_s)

        if abs_st_s < min_shrine_start:
            abs_st_s += np.min(dist[i])

            if abs_st_s < min_shrine_start:
                min_shrine_start = abs_st_s

    for j, st_t in enumerate(temples - start):
        abs_st_t = abs(st_t)

        if abs_st_t < min_temple_start:
            abs_st_t += np.min(dist[:,j])

            if abs_st_t < min_temple_start:
                min_temple_start = abs_st_t
    
    answers.append(min(min_shrine_start,min_temple_start))


for ans in answers:
    print(ans)

Submission status: Runtime Error

Solution code:
#%%
import numpy as np
#%%

s,t,q = list(map(int, input().split()))
shrines = []
temples = []
questions = []
answers = []
dist = np.empty((s,t),dtype='int32')

for _ in range(s):
    shrines.append(int(input()))
shrines = np.array(shrines)

for _ in range(t):
    temples.append(int(input()))
temples = np.array(temples)

for _ in range(q):
    questions.append(int(input()))


for i, shrine in enumerate(shrines):
    dist[i]= [abs(shrine - temple) for temple in temples]

for start in questions:
    min_shrine_start = float('inf')
    min_temple_start = float('inf')

    for i, st_s in enumerate(shrines - start):
        abs_st_s = abs(st_s)

        if abs_st_s < min_shrine_start:
            abs_st_s += np.min(dist[i])

            if abs_st_s < min_shrine_start:
                min_shrine_start = abs_st_s

    for j, st_t in enumerate(temples - start):
        abs_st_t = abs(st_t)

        if abs_st_t < min_temple_start:
            abs_st_t += np.min(dist[:,j])

            if abs_st_t < min_temple_start:
                min_temple_start = abs_st_t
    
    answers.append(min(min_shrine_start,min_temple_start))


for ans in answers:
    print(ans)

Submission status: Runtime Error

Solution code:
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        dist1[0]=s[0]-x[k]
        dist1[1]=s[0]-x[k]
    elif p1==a:
        dist1[0]=x[k]-s[a-1]
        dist1[1]=x[k]-s[a-1]
    else:
        dist1[0]=x[k]-s[p1-1]
        dist1[1]=s[p1]-x[k]
 
    p11=bisect.bisect_left(t,sp1[0])

    if p11==0:
        dist2[0]=t[0]-sp1[0]
        dist2[1]=t[0]-sp1[0]
    elif p11==b:
        dist2[0]=sp1[0]-t[b-1]
        dist2[1]=sp1[0]-t[b-1]
    else:
        dist2[0]=sp1[0]-t[p11-1]
        dist2[1]=t[p11]-sp1[0]
        
    p11=bisect.bisect_left(t,sp1[1])

    if p11==0:
        dist2[2]=t[0]-sp1[1]
        dist2[3]=t[0]-sp1[1]
    elif p11==b:
        dist2[2]=sp1[1]-t[b-1]
        dist2[3]=sp1[1]-t[b-1]
    else:
        dist2[2]=sp1[1]-t[p11-1]
        dist2[3]=t[p11]-sp1[1]
        
    dist[0]=dist1[0]+dist2[0]
    dist[1]=dist1[0]+dist2[1]
    dist[2]=dist1[1]+dist2[2]
    dist[3]=dist1[1]+dist2[3]
                

    p1=bisect.bisect_left(t,x[k])

    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Submission status: Runtime Error

Solution code:
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        sp1[0]=s[0]
        sp1[1]=s[0]
        dist1[0]=sp1[0]-x[k]
    elif p1==a:
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
        dist1[1]=x[k]-sp1[0]
    else:
        sp1[0]=s[p1-1]
        dist1[1]=x[k]-sp1[0]
        sp1[1]=s[p1]
        dist1[0]=sp1[0]-x[k]
    for i in range(2):
        p11=bisect.bisect_left(t,sp1[i])

        if p11==0:
            tp1[0+2*i]=t[0]
            tp1[1+2*i]=t[0]
        elif p11==b:
            tp1[0+2*i]=t[b-1]
            tp1[1+2*i]=t[b-1]
        else:
            tp1[0+2*i]=t[p11-1]
            tp1[1+2*i]=t[p11]

    dist[0]=dist1[0]+abs(sp1[0]-tp1[0])
    dist[1]=dist1[0]+abs(sp1[0]-tp1[1])
    dist[2]=dist1[1]+abs(sp1[1]-tp1[2])
    dist[3]=dist1[1]+abs(sp1[1]-tp1[3])
                

    p1=bisect.bisect_left(t,x[k])

    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Submission status: Runtime Error

Solution code:
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
sp1=[0 for i in range(4)]
tp1=[0 for i in range(4)]

import bisect
for k in range(q):
    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        sp1[0]=s[0]
        sp1[1]=s[0]
    elif p1==a:
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
    else:
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]

    for i in range(2):
        p11=bisect.bisect_left(t,sp1[i])

        if p11==0:
            tp1[0+2*i]=t[0]
            tp1[1+2*i]=t[0]
        elif p11==b:
            tp1[0+2*i]=t[b-1]
            tp1[1+2*i]=t[b-1]
        else:
            tp1[0+2*i]=t[p11-1]
            tp1[1+2*i]=t[p11]

    dist[0]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[1]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[2]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[3]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])

    p1=bisect.bisect_left(t,x[k])
    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Submission status: Runtime Error

Solution code:
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
sp1=[0 for i in range(2)]
tp1=[0 for i in range(4)]

import bisect
for k in range(q):
    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        sp1[0]=s[0]
        sp1[1]=s[0]
    elif p1==a:
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
    else:
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]

    for i in range(2):
        p11=bisect.bisect_left(t,sp1[i])

        if p11==0:
            tp1[0+2*i]=t[0]
            tp1[1+2*i]=t[0]
        elif p11==b:
            tp1[0+2*i]=t[b-1]
            tp1[1+2*i]=t[b-1]
        else:
            tp1[0+2*i]=t[p11-1]
            tp1[1+2*i]=t[p11]

    dist[0]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[1]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[2]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[3]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])

    p1=bisect.bisect_left(t,x[k])
    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_right 
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [sys.stdin.readline() for i in range(A)] + [INF]
t = [-INF] + [sys.stdin.readline() for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    b, d = bisect_right(s, x), bisect_right(t, x)
    #print(b,d)
    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:
from bisect import bisect.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]
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    #print(b,d)
    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 sys
from bisect import bisect.bisect_right 
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [sys.stdin.readline() for i in range(A)] + [INF]
t = [-INF] + [sys.stdin.readline() 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)
    #print(b,d)
    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.bisect_right from 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)
    #print(b,d)
    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 numpy as np
import copy

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

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

def minusNearestValues(list,num):
	temp_list = copy.copy(list)
	temp_list.append(num)
	for i in range(len(temp_list)):
		temp_list[i] -= num
	temp_list.sort()
	if temp_list.index(0) == 0:
		return False
	return temp_list[temp_list.index(0) - 1] + num
	
def plusNearestValues(list,num):
	temp_list = copy.copy(list)
	temp_list.append(num)
	for i in range(len(temp_list)):
		temp_list[i] -= num
	temp_list.sort()
	if temp_list.index(0) == len(temp_list) - 1:
		return False
	return temp_list[temp_list.index(0) + 1] + num
		

def getNearestValues(list, num):
	list = np.array(list)
	temp_list = np.abs(list - num)
	dist = min(temp_list)
	return(dist)

for i in range(Q):
	first_plus = plusNearestValues(s_t,x[i])
	if first_plus == False:
		sum_plus = 10**10000
	elif s_t.index(first_plus) < A: 
		sum_plus = getNearestValues(t,first_plus) + (first_plus - x[i])
	else:
		sum_plus = getNearestValues(s,first_plus) + (first_plus - x[i])
	first_minus = minusNearestValues(s_t,x[i])
	if first_minus == False:
		sum_minus == 10**10000
	if s_t.index(first_minus) < A:
		sum_minus = getNearestValues(t,first_minus) + (x[i] - first_minus)
	else:
		sum_minus = getNearestValues(s,first_minus) + (x[i] - first_minus)
	print(min(sum_plus,sum_minus))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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