Problem p03112 (python)

Fixed submission:
from bisect import bisect_left as bl
A,B,Q=map(int,input().split())
S=[]
T=[]
X=[]
INF=10**18
S.append(-INF)
T.append(-INF)
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
S.append(INF)
T.append(INF)
for i in range(Q):
    x=int(input())
    b,d=bl(S,x),bl(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)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left as bl
from collections import deque
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy

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()))
def rl(X,a):
    ind=bl(X,a)
    if ind>=len(X):
        return ind-1
    return ind
def ll(X,a):
    ind=bl(X,a)-1
    if ind<0:
        return ind+1
    return ind
t=[]
for i in range(A):
    if abs(T[rl(T,S[i])]-S[i])>=abs(T[ll(T,S[i])]-S[i]):
        t.append(abs(T[ll(T,S[i])]-S[i]))
    else:
        t.apend(abs(T[rl(T,S[i])])-S[i])
s=[]
for i in range(B):
    if abs(S[rl(S,T[i])]-T[i])>=abs(S[ll(S,T[i])]-T[i]):
        s.append(abs(S[ll(S,T[i])]-T[i]))
    else:
        s.append(abs(S[rl(S,T[i])])-T[i])

for i in range(Q):
    x=int(input())
    a=rl(S,x)
    a=t[a]+abs(S[a]-x)

    b=ll(S,x)
    b=t[b]+abs(S[b]-x)

    c=rl(T,x)
    c=s[c]+abs(T[c]-x)

    d=ll(T,x)
    d=s[d]+abs(T[d]-x)

    print(min(a,b,c,d))

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left as bl
from collections import deque
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy

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()))
def rl(X,a):
    ind=bl(X,a)
    if ind>=len(X):
        return ind-1
    return ind
def ll(X,a):
    ind=bl(X,a)-1
    if ind<0:
        return ind+1
    return ind
t=[]
for i in range(A):
    if abs(T[rl(T,S[i])]-S[i])>=abs(T[ll(T,S[i])]-S[i]):
        t.append(abs(T[ll(T,S[i])]-S[i]))
    else:
        t.apend(abs(T[rl(T,S[i])])-S[i])
s=[]
for i in range(B):
    if abs(S[rl(S,T[i])]-T[i])>=abs(S[ll(S,T[i])]-T[i]):
        s.append(abs(S[ll(S,T[i])]-T[i]))
    else:
        s.append(abs(S[rl(S,T[i])])-T[i])

for i in range(Q):
    x=int(input())
    a=rl(S,x)
    a=t[a]+abs(S[a]-x)

    b=ll(S,x)
    b=t[b]+abs(S[b]-x)

    c=rl(T,x)
    c=s[c]+abs(T[c]-x)

    d=ll(T,x)
    d=s[d]+abs(T[d]-x)

    print(min(a,b,c,d))

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left as bl
from collections import deque
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy

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()))
def rl(X,a):
    ind=bl(X,a)
    if ind>=len(X):
        return ind-1
    return ind
def ll(X,a):
    ind=bl(X,a)-1
    if ind<0:
        return ind+1
    return ind
t=[]
for i in range(A):
    if abs(T[rl(T,S[i])]-S[i])>=abs(T[ll(T,S[i])]-S[i]):
        t.append(abs(T[ll(T,S[i])]-S[i]))
    else:
        t.apend(abs(T[rl(T,S[i])])-S[i])
s=[]
for i in range(B):
    if abs(S[rl(S,T[i])]-T[i])>=abs(S[ll(S,T[i])]-T[i]):
        s.append(abs(S[ll(S,T[i])]-T[i]))
    else:
        s.apend(abs(S[rl(S,T[i])])-T[i])

for i in range(Q):
    x=int(input())
    a=rl(S,x)
    a=t[a]+abs(S[a]-x)

    b=ll(S,x)
    b=t[b]+abs(S[b]-x)

    c=rl(T,x)
    c=s[c]+abs(T[c]-x)

    d=ll(T,x)
    d=s[d]+abs(T[d]-x)

    print(min(a,b,c,d))