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 = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
x = [int(input()) for _ in range(Q)]
inf = 10 ** 18

s = [-inf] + s + [inf]
t = [-inf] + t + [inf]

def binsearch_l(seq, l, r, tar):
    if r - l < 2: return l
    #
    m = l + (r - l) // 2
    # print("##", l, r, (m, seq[m]), tar)
    if seq[m] <= tar:
        return binsearch_l(seq, m, r, tar)
    else:
        return binsearch_l(seq, l, m - 1, tar)

for x_tmp in x:
    i_l = binsearch_l(s, 0, A + 2, x_tmp)
    j_l = binsearch_l(t, 0, B + 2, x_tmp)
    #
    ans = inf
    for i, j in ((i_l, j_l), (i_l + 1, j_l), (i_l, j_l + 1), (i_l + 1, j_l + 1)):
        if i < 1 or A < i: continue
        if j < 1 or B < j: continue
        ans_tmp_s = abs(x_tmp - s[i]) + abs(s[i] - t[j])
        ans_tmp_t = abs(x_tmp - t[j]) + abs(s[i] - t[j])
        ans_tmp = min(ans_tmp_s, ans_tmp_t)
        # print("#", x_tmp, s[i], t[j], ans_tmp)
        if ans > ans_tmp:
            ans = ans_tmp
    #
    print(ans)

A, B, Q = map(int, input().split())

MY_INF = 10 ** 18

s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
x = [int(input()) for _ in range(Q)]

s = [-MY_INF] + s + [MY_INF]
t = [-MY_INF] + t + [MY_INF]

import bisect

for i, xi in enumerate(x):
    ans = 10 ** 18
    #
    
    idx_s = bisect.bisect_left(s, xi)
    #
    s_l = s[idx_s - 1]
    idx_tl = bisect.bisect_left(t, s_l)
    #
    t_ll = t[idx_tl - 1]
    ans_tmp = abs(s_l - xi) + abs(t_ll - s_l)
    ans = min(ans, ans_tmp)
    t_lr = t[idx_tl]
    ans_tmp = abs(s_l - xi) + abs(t_lr - s_l)
    ans = min(ans, ans_tmp)
    #
    s_r = s[idx_s]
    idx_tr = bisect.bisect_left(t, s_r)
    #
    t_rl = t[idx_tr - 1]
    ans_tmp = abs(s_r - xi) + abs(t_rl - s_r)
    ans = min(ans, ans_tmp)
    #
    t_rr = t[idx_tr]
    ans_tmp = abs(s_r - xi) + abs(t_rr - s_r)
    ans = min(ans, ans_tmp)
    #
    # t -> r
    idx_t = bisect.bisect_left(t, xi)
    #
    t_l = t[idx_t - 1]
    idx_sl = bisect.bisect_left(s, t_l)
    #
    s_ll = s[idx_sl - 1]
    ans_tmp = abs(t_l - xi) + abs(s_ll - t_l)
    ans = min(ans, ans_tmp)
    s_lr = s[idx_sl]
    ans_tmp = abs(t_l - xi) + abs(s_lr - t_l)
    ans = min(ans, ans_tmp)
    #
    t_r = t[idx_t]
    idx_sr = bisect.bisect_left(s, t_r)
    #
    s_rl = s[idx_sr - 1]
    ans_tmp = abs(t_r - xi) + abs(s_rl - t_r)
    ans = min(ans, ans_tmp)
    #
    s_rr = s[idx_sr]
    ans_tmp = abs(t_r - xi) + abs(s_rr - t_r)
    ans = min(ans, ans_tmp)
    #
    print(ans)


Verdicts: Wrong Answer

Accepted