Problem: 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.)

Submission attempts (in order):
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))

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

Verdicts: Runtime Error

Accepted