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 sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    a, b, q = map(int, input().split())
    S = [-f_inf] + list(int(input()) for _ in range(a)) + [f_inf]
    T = [-f_inf] + list(int(input()) for _ in range(b)) + [f_inf]

    for _ in range(q):
        res = f_inf
        x = int(input())

        idx_s = bisect_left(S, x)
        kouho_s = [idx_s - 1, idx_s]
        for idx_s in kouho_s:
            now = S[idx_s]
            dist = abs(x - now)
            idx_t = bisect_left(T, now)
            kouho_t = [idx_t - 1, idx_t]
            for idx_t in kouho_t:
                dist += abs(now - T[idx_t])
                res = min(res, dist)

        idx_t = bisect_left(T, x)
        kouho_t = [idx_t - 1, idx_t]
        for idx_t in kouho_t:
            now = T[idx_t]
            dist = abs(x - now)
            idx_s = bisect_left(S, now)
            kouho_s = [idx_s - 1, idx_s]
            for idx_s in kouho_s:
                dist += abs(now - S[idx_s])
                res = min(res, dist)
        print(res)


if __name__ == '__main__':
    resolve()


import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def neighborhood(L, now):
    tmp = bisect_left(L, now)
    if tmp == 0:
        return [tmp, None]
    elif tmp == len(L):
        return [tmp - 1, None]
    else:
        return [tmp - 1, tmp]


def dist_calc(now, p1, p2):
    if p1 is None or p2 is None:
        return f_inf
    res1 = abs(now - p1) + abs(p1 - p2)
    res2 = abs(now - p2) + abs(p1 - p2)
    return min(res1, res2)


def resolve():
    a, b, q = map(int, input().split())
    S = list(int(input()) for _ in range(a))
    T = list(int(input()) for _ in range(b))
    query = list(int(input()) for _ in range(q))

    for x in query:
        dist1 = f_inf
        s1, s2 = neighborhood(S, x)
        for s in [s1, s2]:
            if s is not None:
                t1, t2 = neighborhood(T, S[s])
                for t in [t1, t2]:
                    if t is not None:
                        dist1 = min(dist1, dist_calc(x, S[s], T[t]))

        dist2 = f_inf
        t1, t2 = neighborhood(T, x)
        for t in [t1, t2]:
            if t is not None:
                s1, s2 = neighborhood(S, T[t])
                for s in [s1, s2]:
                    if s is not None:
                        dist2 = min(dist2, dist_calc(x, S[s], T[t]))

        res = min(dist1, dist2)
        print(res)


if __name__ == '__main__':
    resolve()


Verdicts: Wrong Answer

Accepted