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 solve(string):
    a, b, q, *stx = map(int, string.split())
    s = sorted(stx[:a] + [-10**10, 2 * 10**10])
    t = sorted(stx[a:a + b] + [-10**10, 2 * 10**10])
    x = {_q: -1 for _q in stx[-q:]}
    i_s = 0
    i_t = 0
    for _x in sorted(x.keys()):
        while s[i_s] < _x:
            i_s += 1
        while t[i_t] < _x:
            i_t += 1
        l_s, r_s = s[i_s - 1], s[i_s]
        l_t, r_t = t[i_t - 1], t[i_t]
        r_max = max(r_s, r_t)
        r_min = min(r_s, r_t)
        l_max = max(l_s, l_t)
        l_min = min(l_s, l_t)

        x[_x] = min([r_max - _x, _x - l_min, r_min - l_max + min(r_min - _x, _x - l_max)])
    return "\n".join([str(x[_x]) for _x in stx[-q:]])


if __name__ == '__main__':
    n, m, l = map(int, input().split())
    print(solve('{} {}\n'.format(n, m, l) + '\n'.join([input() for _ in range(n + m + l)])))


from bisect import bisect_left


def solve(string):
    a, b, q, *stx = map(int, string.split())
    s = [-10**10] + stx[:a] + [2 * 10**10]
    t = [-10**10] + stx[a:a + b] + [2 * 10**10]
    
    ans = []
    i_s = 0
    i_t = 0
    for _x in stx[-q:]:
        """
        while s[i_s] < _x:
            i_s += 1
        while t[i_t] < _x:
            i_t += 1
        """
        i_s, i_t = bisect_left(s, _x), bisect_left(t, _x)
        ls, rs = _x - s[i_s - 1], s[i_s] - _x
        lt, rt = _x - t[i_t - 1], t[i_t] - _x
        r_max = max(rs, rt)
        l_min = _x - min(ls, lt)
        """
        x[_x] = min([
            r_max - _x, _x - l_min, rt - ls + min(rt - _x, _x - ls),
            rs - lt + min(rs - _x, _x - lt)
        ])
        """
        ans.append(str(min([r_max, l_min, rt - ls + min(rt, ls), rs - lt + min(rs, lt)])))
    # return "\n".join([str(x[_x]) for _x in stx[-q:]])
    return "\n".join(ans)


if __name__ == '__main__':
    n, m, l = map(int, input().split())
    print(solve('{} {} {}\n'.format(n, m, l) + '\n'.join([input() for _ in range(n + m + l)])))


from bisect import bisect_left


def solve(string):
    a, b, q, *stx = map(int, string.split())
    bignum = 3 * 10**10
    s = [-bignum] + stx[:a] + [bignum]
    t = [-bignum] + stx[a:a + b] + [bignum]
    ans = []
    for _x in stx[-q:]:
        i, j = bisect_left(s, _x), bisect_left(t, _x)
        ls, rs = _x - s[i - 1], s[i] - _x
        lt, rt = _x - t[j - 1], t[j] - _x
        ans.append(str(min(max(rs, rt), max(ls, lt), rt + ls + min(rt, ls),
                           rs + lt + min(rs, lt))))
    return "\n".join(ans)


if __name__ == '__main__':
    n, m, l = map(int, input().split())
    print(solve('{} {} {}\n'.format(n, m, l) + '\n'.join([input() for _ in range(n + m + l)])))


Verdicts: Wrong Answer

Wrong Answer

Accepted