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):
def get_nearest(point, lst):
    shortest = 1000000000000000
    nearest = float("inf")
    for x in lst:
        dist = abs(x - point)
        if dist < shortest:
            shortest = dist
            nearest = x
    return nearest

def exist_between(a, b, lst):
    for x in lst:
        if a <= x and x <= b:
            return True
    else:
        return False

A, B, Q = [int(x) for x in input().split(" ")]
a = []
b = []
for i in range(A):
    a.append(int(input()))
for i in range(B):
    b.append(int(input()))
for i in range(Q):
    q = int(input())
    nearest_a = get_nearest(q, a)
    nearest_b = get_nearest(q, b)
#   print(q, nearest_a, nearest_b)
    farer = nearest_b if abs(nearest_a - q) < abs(nearest_b - q) else nearest_a
    nearer = nearest_a if abs(nearest_a - q) < abs(nearest_b - q) else nearest_b
    not_farer_list = a if abs(nearest_a - q) < abs(nearest_b - q) else b
    if exist_between(q, farer, not_farer_list):
        print(abs(farer - q))
    else:
        print(abs(nearer - q) + abs(farer - nearer))


import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
        d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
        res = min(res, d1, d2)
        print(res)

import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
    for S in [s[b - 1], s[b]]:
        for T in [t[d - 1], t[d]]:
            d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
            res = min(res, d1, d2)
            print(res)

import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
    for S in [s[b - 1], s[b]]:
        for T in [t[d - 1], t[d]]:
            d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
            res = min(res, d1, d2)
    print(res)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted