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):
A, B, Q = map(int, input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for _ in range(Q)]

s_list.sort()
t_list.sort()
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    if s_i == A and t_i == B:
        tmp = abs(x - min(s_list[-1], t_list[-1]))
        ans.append(tmp)
        continue
    if s_i == A:
        if t_i != 0:
            tmp = abs(s_list[-1] - x)
            if s_list[-1] <= t_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(t_list[t_i] - x) * 2, abs(t_list[t_i - 1] - s_list[-1]))
        else:
            tmp = abs(min(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)) * 2 + max(abs(s_list[s_i - 1] - x),
                                                                                  abs(t_list[0] - x)))
        ans.append(tmp)
        continue
    if t_i == B:
        if s_i != 0:
            tmp = abs(t_list[-1] - x)
            if t_list[-1] <= s_list[s_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(s_list[s_i] - x) * 2, abs(s_list[s_i - 1] - t_list[-1]))
        else:
            tmp = abs(min(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)) * 2 + max(abs(t_list[t_i - 1] - x),
                                                                                  abs(s_list[0] - x)))
        ans.append(tmp)
        continue

    # S1, T1
    if s_i != 0 and t_i != 0:
        tmp = min(tmp, abs(min(s_list[s_i - 1], t_list[t_i - 1]) - x))
    elif s_i == 0 and t_i == 0:
        tmp = min(tmp, abs(x - max(s_list[0], t_list[0])))
    elif s_i == 0 and t_i != 0:
        tmp = min(tmp, abs(s_list[0] - t_list[t_i]))
    else:
        tmp = min(tmp, abs(t_list[0] - s_list[s_i]))

    # S2, T2
    tmp = min(tmp, abs(max(s_list[s_i], t_list[t_i]) - x))

    # S1, T2
    if s_i != 0:
        tmp = min(tmp, abs(min(abs(t_list[t_i] - x), abs(s_list[s_i - 1] - x)) * 2 + max(abs(t_list[t_i] - x),
                                                                                         abs(s_list[s_i - 1] - x))))
    else:
        tmp = min(tmp, abs(max(t_list[t_i], s_list[0]) - x))
    # S2, T1
    if t_i != 0:
        tmp = min(tmp, abs(min(abs(s_list[s_i] - x), abs(t_list[t_i - 1] - x)) * 2 + max(abs(s_list[s_i] - x),
                                                                                         abs(t_list[t_i - 1] - x))))
    else:
        tmp = min(tmp, abs(max(s_list[s_i], t_list[0]) - x))
    ans.append(tmp)
for a in ans:
    print(a)


A, B, Q = map(int, input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for _ in range(Q)]

s_list.sort()
t_list.sort()
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    if s_i == A and t_i == B:
        tmp = abs(x - min(s_list[-1], t_list[-1]))
        ans.append(tmp)
        continue
    if s_i == A:
        if t_i != 0:
            tmp = abs(s_list[-1] - x)
            if s_list[-1] <= t_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(t_list[t_i] - x) * 2, abs(t_list[t_i - 1] - s_list[-1]))
        else:
            tmp = abs(min(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)) * 2 + max(abs(s_list[s_i - 1] - x),
                                                                                  abs(t_list[0] - x)))
        ans.append(tmp)
        continue
    if t_i == B:
        if s_i != 0:
            tmp = abs(t_list[-1] - x)
            if t_list[-1] <= s_list[s_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(s_list[s_i] - x) * 2, abs(s_list[s_i - 1] - t_list[-1]))
        else:
            tmp = abs(min(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)) * 2 + max(abs(t_list[t_i - 1] - x),
                                                                                  abs(s_list[0] - x)))
        ans.append(tmp)
        continue

    # S1, T1
    if s_i != 0 and t_i != 0:
        tmp = min(tmp, abs(min(s_list[s_i - 1], t_list[t_i - 1]) - x))
    elif s_i == 0 and t_i == 0:
        tmp = min(tmp, abs(x - max(s_list[0], t_list[0])))
    elif s_i == 0 and t_i != 0:
        tmp = min(tmp, abs(s_list[0] - t_list[t_i]))
    else:
        tmp = min(tmp, abs(t_list[0] - s_list[s_i]))

    # S2, T2
    tmp = min(tmp, abs(max(s_list[s_i], t_list[t_i]) - x))

    # S1, T2
    if s_i != 0:
        tmp = min(tmp, abs(min(abs(t_list[t_i] - x), abs(s_list[s_i - 1] - x)) * 2 + max(abs(t_list[t_i] - x),
                                                                                         abs(s_list[s_i - 1] - x))))
    else:
        tmp = min(tmp, abs(max(t_list[t_i], s_list[0]) - x))
    # S2, T1
    if t_i != 0:
        tmp = min(tmp, abs(min(abs(s_list[s_i] - x), abs(t_list[t_i - 1] - x)) * 2 + max(abs(s_list[s_i] - x),
                                                                                         abs(t_list[t_i - 1] - x))))
    else:
        tmp = min(tmp, abs(max(s_list[s_i], t_list[0]) - x))
    ans.append(tmp)
for a in ans:
    print(a)


from bisect import bisect_left

A, B, Q = map(int, input().split())
s_list = [int(input()) for _ in range(A)]
t_list = [int(input()) for _ in range(B)]
x_list = [int(input()) for _ in range(Q)]

inf = float("inf")

s_list = [-inf] + s_list + [inf]
t_list = [-inf] + t_list + [inf]

for x in x_list:

    s_i = bisect_left(s_list, x)
    t_i = bisect_left(t_list, x)

    ans = inf
    for s in (s_list[s_i - 1], s_list[s_i]):
        for t in (t_list[t_i - 1], t_list[t_i]):
            d1 = abs(s - x) + abs(t - s)
            d2 = abs(t - x) + abs(s - t)
            ans = min(ans, d1, d2)
    print(ans)


Verdicts: Wrong Answer

Wrong Answer

Accepted