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):
# D
from bisect import bisect_left
A, B, Q = map(int, input().split())

s_list = [0]*A
t_list = [0]*B
q_list = [0]*Q

for i in range(A):
    s_list[i] = int(input())
for i in range(B):
    t_list[i] = int(input())
for i in range(Q):
    q_list[i] = int(input())
    
def query_dist(q):
    res = 10**11
    
    ind_s = bisect_left(s_list, q)
    if ind_s != len(s_list):
        s = s_list[ind_s]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
    if ind_s != 0:
        s = s_list[ind_s-1]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
            
    ind_t = bisect_left(t_list, q)
    if ind_t != len(t_list):
        t = t_list[ind_t]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    if ind_t != len(t_list):
        t = t_list[ind_t-1]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    return res

for q in q_list:
    print(query_dist(q))

# D
from bisect import bisect_left
A, B, Q = map(int, input().split())

s_list = [0]*A
t_list = [0]*B
q_list = [0]*Q

for i in range(A):
    s_list[i] = int(input())
for i in range(B):
    t_list[i] = int(input())
for i in range(Q):
    q_list[i] = int(input())
    
def query_dist(q):
    res = 10**11
    
    ind_s = bisect_left(s_list, q)
    if ind_s != len(s_list):
        s = s_list[ind_s]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
    if ind_s != 0:
        s = s_list[ind_s-1]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
            
    ind_t = bisect_left(t_list, q)
    if ind_t != len(t_list):
        t = t_list[ind_t]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    if ind_t != 0:
        t = t_list[ind_t-1]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    return res

for q in q_list:
    print(query_dist(q))

Verdicts: Wrong Answer

Accepted