Problem p03112 (python)

Fixed submission:
from bisect import bisect_right
A,B,Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
S.append(10**11)
S.insert(0,-10**11)
T = [int(input()) for _ in range(B)]
T.append(10**11)
T.insert(0,-10**11)
Ds = {i:0 for i in range(1,A+1)}
Ds[A+1] = 10**11
Ds[0] = 10**11
for i in range(1,A+1):
    ind = bisect_right(T,S[i])
    Ds[i] = min(T[ind]-S[i],S[i]-T[ind-1])
Dt = {j:0 for j in range(1,B+1)}
Dt[B+1] = 10**11
Dt[0] = 10**11
for j in range(1,B+1):
    ind = bisect_right(S,T[j])
    Dt[j] = min(S[ind]-T[j],T[j]-S[ind-1])
for _ in range(Q):
    x = int(input())
    inds = bisect_right(S,x)
    cnts = min(S[inds]-x+Ds[inds],x-S[inds-1]+Ds[inds-1])
    indt = bisect_right(T,x)
    cntt = min(T[indt]-x+Dt[indt],x-T[indt-1]+Dt[indt-1])
    print(min(cnts,cntt))

Buggy submission (verdict: Runtime Error):
from bisect import bisect_right
A,B,Q = map(int,input().split())
S = sorted([int(input()) for _ in range(A)])
T = sorted([int(input()) for _ in range(B)])
for _ in range(Q):
    x = int(input())
    inds = bisect_right(S,x)
    indt = bisect_right(T,x)
    if inds==A and indt==B:
        ans = max(x-S[inds-1],x-T[indt-1])
    elif inds==A and indt<B:
        if T[indt-1]>=S[inds-1]:
            ans = x-S[inds-1]
        else:
            ans = min(T[indt]-x+T[indt]-S[inds-1],x-T[indt-1])
    elif inds<B and indt==B:
        if S[inds-1]>=T[intd-1]:
            ans = x-T[indt-1]
        else:
            ans = min(S[inds]-x+S[inds]-T[indt-1],x-S[inds-1])
    else:
        ans = min(max(S[inds]-x,T[indt]-x),max(x-S[inds-1],x-T[indt-1]),S[inds]-x+S[inds]-T[indt-1],T[indt]-x+T[indt]-S[inds-1])
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
A,B,Q = map(int,f.readline().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
for _ in range(Q):
    x = int(input())
    indS = bisect.bisect_right(S,x)
    indT = bisect.bisect_right(T,x)
    if indS==0 and indT==0:
        print(max(S[0],T[0])-x)
    elif indS==0 and indT>0:
        if indT<len(T) and T[indT]<S[indS]:
            print(S[0]-x)
        elif indT<len(T) and T[indT]>S[indS]:
            r1 = T[indT]-x
            r2 = 2*(x-T[indT-1])+S[indS]-x
            r3 = x-T[ind-1]+2*(S[indS]-x)
            print(min(r1,r2,r3))
        elif indT==len(T):
            r1 = 2*(x-T[indT-1])+S[indS]-x
            r2 = x-T[indT-1]+2*(S[indS]-x)
            print(min(r1,r2))
    elif indT==0 and indS>0:
        if indS<len(S) and S[indS]<T[indT]:
            print(T[0]-x)
        elif indS<len(S) and S[indS]>T[indT]:
            r1 = S[indS]-x
            r2 = 2*(x-S[indS-1])+T[indT]-x
            r3 = x-S[indS-1]+2*(T[indT]-x)
            print(min(r1,r2,r3))
        elif indS==len(S):
            r1 = 2*(x-S[indS-1])+T[indT]-x
            r2 = x-S[indS-1]+2*(T[indT]-x)
            print(min(r1,r2))
    elif indS==len(S) and indT==len(T):
        print(x-min(S[-1],T[-1]))
    elif indS==len(S) and indT<len(T):
        if indT==0:
            r1 = 2*(x-S[-1])+T[0]-x
            r2 = x-S[-1]+2*(T[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if T[indT-1]>S[-1]:
                print(x-S[-1])
            elif T[indT-1]<S[-1]:
                r1 = 2*(T[indT]-x)+x-S[-1]
                r3 = T[indT]-x+2*(x-S[-1])
                r2 = x-T[indT-1]
                print(min(r1,r2,r3))
    elif indS<len(S) and indT==len(T):
        if indS==0:
            r1 = 2*(x-T[-1])+S[0]-x
            r2 = x-T[-1]+2*(S[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if S[indS-1]>T[-1]:
                print(x-T[-1])
            elif S[indS-1]<T[-1]:
                r1 = 2*(S[indS]-x)+x-T[-1]
                r3 = S[indS]-x+2*(x-T[-1])
                r2 = x-S[indt-1]
                print(min(r1,r2,r3))
    else:
        r1 = max(S[indS],T[indT])-x
        r2 = x-min(S[indS-1],T[indT-1])
        r3 = 2*(S[indS]-x)+x-T[indT-1]
        r4 = S[indS]-x+2*(x-T[indT-1])
        r5 = 2*(x-S[indS-1])+T[indT]-x
        r6 = x-S[indS-1]+2*(T[indT]-x)
        print(min(r1,r2,r3,r4,r5,r6))

Buggy submission (verdict: Runtime Error):
import bisect
A,B,Q = map(int,input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
for _ in range(Q):
    x = int(input())
    indS = bisect.bisect_right(S,x)
    indT = bisect.bisect_right(T,x)
    if indS==0 and indT==0:
        print(max(S[0],T[0])-x)
    elif indS==0 and indT>0:
        if indT<len(T) and T[indT]<S[indS]:
            print(S[0]-x)
        elif indT<len(T) and T[indT]>S[indS]:
            r1 = T[indT]-x
            r2 = 2*(x-T[indT-1])+S[indS]-x
            print(min(r1,r2))
        elif indT==len(T):
            r1 = 2*(x-T[indT-1])+S[indS]-x
            r2 = x-T[indT-1]+2*(S[indS]-x)
            print(min(r1,r2))
    elif indT==0 and indS>0:
        if indS<len(S) and S[indS]<T[indT]:
            print(T[0]-x)
        elif indS<len(S) and S[indS]>T[indT]:
            r1 = S[indS]-x
            r2 = 2*(x-S[indS-1])+T[indT]-x
            print(min(r1,r2))
        elif indS==len(S):
            r1 = 2*(x-S[indS-1])+T[indT]-x
            r2 = x-S[indS-1]+2*(T[indT]-x)
            print(min(r1,r2))
    elif indS==len(S) and indT==len(T):
        print(x-min(S[-1],T[-1]))
    elif indS==len(S) and indT<len(T):
        if indT==0:
            r1 = 2*(x-S[-1])+T[0]-x
            r2 = x-S[-1]+2*(T[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if T[indT-1]>S[-1]:
                print(x-S[-1])
            elif T[indT-1]<S[-1]:
                r1 = 2*(T[indT]-x)+x-S[-1]
                r2 = x-T[indT-1]
                print(min(r1,r2))
    elif indS<len(S) and indT==len(T):
        if indS==0:
            r1 = 2*(x-T[-1])+S[0]-x
            r2 = x-T[-1]+2*(S[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if S[indS-1]>T[-1]:
                print(x-T[-1])
            elif S[indS-1]<T[-1]:
                r1 = 2*(S[indS]-x)+x-T[-1]
                r2 = x-S[indt-1]
                print(min(r1,r2))
    else:
        r1 = max(S[indS],T[indT])-x
        r2 = x-min(S[indS-1],T[indT-1])
        print(min(r1,r2))