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 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 = min(bisect.bisect_right(s, x), A-1), min(bisect.bisect_right(t, x), B-1)
  a, c = max(b-1, 0), max(d-1, 0)
  
  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)

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)
    a, c = max(b-1, 0), max(d-1, 0)

    res = INF
    print(a, b, c, d)
    for S in s[b-1:b+1]:
        for T in t[d-1:d+1]:
            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 = min(bisect.bisect_right(s, x), A+1), \
        min(bisect.bisect_right(t, x), B+1)
    a, c = max(b-1, 0), max(d-1, 0)

    res = INF
    print(a, b, c, d)
    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)

A, B, Q = [int(s) for s in input().split()]


shrines = []
temples = []

for _ in range(A):
    s = int(input())
    shrines.append((s, 10**10))



ji = 0
for _ in range(B):
    tp, tc = int(input()), 10**10
    sp, sc = shrines[ji]

    while abs(tp - sp) < tc:
        tc = abs(tp - sp)

        if abs(tp - sp) < sc:
            shrines[ji] = (sp, abs(tp - sp))

        if ji + 1 < A:
            ji += 1
            sp, sc = shrines[ji]
        else:
            break

    temples.append((tp, tc))

road = sorted(temples + shrines, key=lambda x: x[0])


def search(x):
    global A, B

    l, r = 0, A + B
    while r - l > 1:
        m = (l + r) // 2
        mp, mc = road[m]

        if mp < x:
            l = m
        else:
            r = m

    p1, c1 = road[l]
    if l == A + B - 1 or A + B == 1:
        return abs(x - p1) + c1

    p2, c2 = road[l+1]
    return min(abs(x-p1) + c1, abs(x-p2) + c2)


for _ in range(Q):
    q = int(input())

    y = search(q)
    print(q, y)


A, B, Q = [int(s) for s in input().split()]


shrines = []
temples = []

for _ in range(A):
    s = int(input())
    shrines.append((s, 10**10))



ji = 0
for _ in range(B):
    tp, tc = int(input()), 10**10
    sp, sc = shrines[ji]

    while abs(tp - sp) < tc:
        tc = abs(tp - sp)

        if abs(tp - sp) < sc:
            shrines[ji] = (sp, abs(tp - sp))

        if ji + 1 < A:
            ji += 1
            sp, sc = shrines[ji]
        else:
            break

    temples.append((tp, tc))

road = sorted(temples + shrines, key=lambda x: x[0])


def search(x):
    global A, B

    l, r = 0, A + B
    while r - l > 1:
        m = (l + r) // 2
        mp, mc = road[m]

        if mp < x:
            l = m
        else:
            r = m

    p1, c1 = road[l]
    if l == A + B - 1 or A + B == 1:
        return abs(x - p1) + c1

    p2, c2 = road[l+1]
    return min(abs(x-p1) + c1, abs(x-p2) + c2)


for _ in range(Q):
    q = int(input())

    y = search(q)
    print(q, y)


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:b+1]:
        for T in t[d-1:d+1]:
            d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
            res = min(res, d1, d2)
    print(res)


Verdicts: Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Accepted