Problem:


Score : 400 points

Problem Statement

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.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.



Sample input:
2 3 4
100
600
400
900
1000
150
2000
899
799


Sample output:
350
1400
301
399

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


class BisectWrapper:
    try:
        from typing import List
    except:
        List = list
    from bisect import bisect, bisect_left, bisect_right

    def __init__(self):
        pass

    # aにxが存在するか
    @staticmethod
    def exist(a: List, x: int):
        i = bisect_left(a, x)
        if i != len(a) and a[i] == x:
            return True
        return False

    # xのindex(なければ-1)
    @staticmethod
    def index(a: List, x: int):
        i = bisect_left(a, x)
        if i != len(a) and a[i] == x:
            return i
        return -1

    # y < xのようなyの中でもっとも右のindex
    @staticmethod
    def index_lt(a: List, x: int):
        i = bisect_left(a, x)
        if i:
            return i - 1
        return -1

    # y < xのようなyの個数
    @staticmethod
    def num_lt(a: List, x: int):
        return bisect_left(a, x)

    # y <= xのようなyの中でもっとも右のindex
    @staticmethod
    def index_lte(a: List, x: int):
        i = bisect_right(a, x)
        if i:
            return i - 1
        return -1

    # y < xのようなyの個数
    @staticmethod
    def num_lte(a: List, x: int):
        return bisect_right(a, x)

    # y > xのようなyの中でもっとも左のindex
    @staticmethod
    def index_gt(a: List, x: int):
        i = bisect_right(a, x)
        if i != len(a):
            return i
        return -1

    # y > xのようなyの個数
    @staticmethod
    def num_gt(a: List, x: int):
        return len(a) - bisect_right(a, x)

    # y >= xのようなyの中でもっとも左のindex
    @staticmethod
    def index_gte(a: List, x: int):
        i = bisect_left(a, x)
        if i != len(a):
            return i
        return -1

    # y >= xのようなyの個数
    @staticmethod
    def num_gte(a: List, x: int):
        return len(a) - bisect_left(a, x)


def main():
    A, B, Q = map(int, input().split())
    S = list(sorted([int(input()) for _ in range(A)]))
    T = list(sorted([int(input()) for _ in range(B)]))

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

        s_l = BisectWrapper.index_lte(S, x)
        s_r = BisectWrapper.index_gte(S, x)

        t_l = BisectWrapper.index_lte(T, x)
        t_r = BisectWrapper.index_gte(T, x)

        ans = INF
        if s_l != -1:
            if t_l != -1:
                s, t = S[s_l], T[t_l]
                ans = min(ans, abs(x - s) + abs(s - t))
                ans = min(ans, abs(x - t) + abs(t - s))
            if t_r != -1:
                s, t = S[s_l], T[t_r]
                ans = min(ans, abs(x - s) + abs(s - t))
                ans = min(ans, abs(x - t) + abs(t - s))
        elif s_r != -1:
            if t_l != -1:
                s, t = S[s_r], T[t_l]
                ans = min(ans, abs(x - s) + abs(s - t))
                ans = min(ans, abs(x - t) + abs(t - s))
            if t_r != -1:
                s, t = S[s_r], T[t_r]
                ans = min(ans, abs(x - s) + abs(s - t))
                ans = min(ans, abs(x - t) + abs(t - s))

        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect

A, B, Q = list(map(int, input().split()))
s = [0] * A
for i in range(A):
    s[i] = int(input())

t = [0] * B
for i in range(B):
    t[i] = int(input())


def find_index(arr, xi):
    t = bisect.bisect_left(arr, xi)
    if t == 0:
        return [-1e10-1, arr[0]]
    elif t == len(arr):
        return [arr[-1], 1e10+1]
    else:
        return [arr[t-1], arr[t]]


def func1(a, b, x):
    return min(x-a, b-x)


def calc(xi):
    global s, t
    if xi < s[0] and xi < t[0]:
        return max(s[0] - xi, t[0] - xi)

    if xi > s[-1] and xi > t[-1]:
        return max(xi - s[-1], xi - t[-1])

    s1, s2 = find_index(s, xi)
    t1, t2 = find_index(t, xi)

    if t1 < s1 and s2 < t2:
        return func1(t1, t2, xi)

    elif s1 < t1 and t2 < s2:
        return func1(s1, s2, xi)

    elif s1 < t1 and s2 < t2:
        a1 = s2 - xi + s2 - t1
        a2 = xi - t1 + s2 - t1
        return min(a1, a2, func1(s1, t2, xi))

    else:
        a1 = t2 - xi + t2 - s1
        a2 = xi - s1 + t2 - s1
        return min(a1, a2, func1(t1, s2, xi))


for i in range(Q):
    xi = int(input())
    print(calc(xi))

Submission status: Wrong Answer

Solution code:
# input
A, B, Q = map(int, input().split())
S, T, X = [], [], []
for _ in range(A):
    S.append(int(input()))

for _ in range(B):
    T.append(int(input()))

for _ in range(Q):
    X.append(int(input()))

# end of world
EOW = 10 ** 10

# boundary info
tbounds = [(T[i+1] + T[i]) // 2 for i in range(B-1)]
sbounds = [(S[i+1] + S[i]) // 2 for i in range(A-1)]

# add pivot
tbounds.insert(0, 0)
tbounds.append(EOW)
sbounds.insert(0, 0)
sbounds.append(EOW)

# temple to shrine
ttos = []
cur = 0
for i in range(B):
    while True:
        if T[i] <= sbounds[cur]:
            break
        cur += 1
    ttos.append(cur-1)

# shrine to temple
stot = []
cur = 0
for i in range(A):
    while True:
        if S[i] <= tbounds[cur]:
            break
        cur += 1
    stot.append(cur-1)

for x in X:
    svisit = 0
    tnear = 0
    best = EOW * 2
    # search
    for i in range(A):
        tnear = stot[i]
        dist = abs(S[i]-x) + abs(T[tnear]-S[i])
        if best > dist:
            best = dist
        else:
            break
    for i in range(max(0, tnear-3), min(tnear+3, B)):
        snear = ttos[i]
        dist = abs(T[i]-x) + abs(S[snear]-T[i])
        if best > dist:
            best = dist
    print(best)
    # print(min(min(abs(S[i]-x) + abs(T[nt]-S[i]) for (i, nt) in enumerate(stot)), min(abs(T[i]-x) + abs(S[ns]-T[i]) for (i, ns) in enumerate(ttos))))

Submission status: Wrong Answer

Solution code:
# input
A, B, Q = map(int, input().split())
S, T, X = [], [], []
for _ in range(A):
    S.append(int(input()))

for _ in range(B):
    T.append(int(input()))

for _ in range(Q):
    X.append(int(input()))

# end of world
EOW = 10 ** 10

# boundary info
tbounds = [(T[i+1] + T[i]) // 2 for i in range(B-1)]
sbounds = [(S[i+1] + S[i]) // 2 for i in range(A-1)]

# add pivot
tbounds.insert(0, 0)
tbounds.append(EOW)
sbounds.insert(0, 0)
sbounds.append(EOW)

# temple to shrine
ttos = []
cur = 0
for i in range(B):
    while True:
        if T[i] <= sbounds[cur]:
            break
        cur += 1
    ttos.append(cur-1)

# shrine to temple
stot = []
cur = 0
for i in range(A):
    while True:
        if S[i] <= tbounds[cur]:
            break
        cur += 1
    stot.append(cur-1)

    
for x in X:
	print(0)
#    print(min(min(abs(S[i]-x) + abs(T[nt]-S[i]) for (i, nt) in enumerate(stot)), min(abs(T[i]-x) + abs(S[ns]-T[i]) for (i, ns) in enumerate(ttos))))

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split(" "))

s_s = [int(input()) for _ in range(a)]
t_s = [int(input()) for _ in range(b)]
x_s = [int(input()) for _ in range(q)]

for x in x_s:
  mind = 10**10
  for s in s_s:
    for t in t_s:
      if x > t and x > s:
        mind = min(mind, x - min(t, s))
        continue
      if x < t and x < s:
        mind = min(mind, max(t, s) - x)
        continue
      mind = min(mind, abs(t-s) + min(abs(x-s), abs(x-t)))
  print(mind)

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split(" "))
maps = dict()
for _ in range(a):
  maps[int(input())] = "s"

for _ in range(b):
  maps[int(input())] = "t"

xs = [int(input()) for _ in range(q)]

sorted_maps = list(sorted(maps.items(), key=lambda x:x[0]))

for x in xs:
  spos = -1
  for idx, p in enumerate(sorted_maps):
    if p[0] > x:
      spos = idx
      break
  ls = list(reversed(sorted_maps[:spos]))
  rs = sorted_maps[spos:]
  
  costs = []
  
  #left side costs
  if len(ls) > 1:
    ls_f = ls[0]
    for i in range(1, len(ls)):
      if ls_f[1] != ls[i][1]:
        costs.append(x-ls[i][0])
        break
  
  #right side costs
  if len(rs) > 1:
    rs_f = rs[0]
    for i in range(1, len(rs)):
      if rs_f[1] != rs[i][1]:
        costs.append(rs[i][0]-x)
        break
  
  #round trip pattern
  if len(rs) > 0 and len(ls) > 0 and rs[0][1] != ls[0][1]:
    c_r = rs[0][0] - x
    c_l = x - ls[0][0]
    costs.append(c_r + c_l + min(c_r, c_l))

  print(min(costs))

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split())
sno = [-10e6] + [int(input()) for _ in range(a)] + [10e6]
tno = [-10e6] + [int(input()) for _ in range(b)] + [10e6]
dis = [int(input()) for _ in range(q)]
 
for i in range(q):
 
    sl = 10e6
    sr = 10e6
    tl = 10e6
    tr = 10e6
 
    for j in range(a+2):
        if sno[j] >= dis[i]:
            sl = dis[i] - sno[j-1]
            sr = sno[j] - dis[i]
            break
        else:
            pass
 
    for k in range(b+2):
        if tno[k] >= dis[i]:
            tl = dis[i] - tno[k-1]
            tr = tno[k] - dis[i]
            break
        else:
            pass
 
    lds = max(sl, tl)
    rds = max(sr, tr)
    ans = min(lds, rds)
    print(ans)

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split())
sno = [-10e6] + [int(input()) for _ in range(a)] + [10e6]
tno = [-10e6] + [int(input()) for _ in range(b)] + [10e6]
dis = [int(input()) for _ in range(q)]
 
for i in range(q):
 
    sl = 0
    sr = 0
    tl = 0
    tr = 0
 
    for j in range(a+2):
        if sno[j] >= dis[i]:
            sl = dis[i] - sno[j-1]
            sr = sno[j] - dis[i]
            break
        else:
            pass
 
    for k in range(b+2):
        if tno[k] >= dis[i]:
            tl = dis[i] - tno[k-1]
            tr = tno[k] - dis[i]
            break
        else:
            pass
 
    lds = max(sl, tl)
    rds = max(sr, tr)
    ans = min(lds, rds)
    print(ans)

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s = [0]*A
t = [0]*B

for i in range(A):
    s[i] = int(input())

for i in range(B):
    t[i] = int(input())

INF = 10**12
for _ in range(Q):
    x = int(input())

    flg_sh = 0
    if x <= s[0]:
        dis_sh_r = s[0] - x
        dis_sh_l = INF
        flg = -1
    elif x > s[-1]:
        dis_sh_l = x - s[-1]
        dis_sh_r = INF
        flg = 1
    else:
        flg_sh = 1
        for j in range(A):
            if x > s[j]:
                dis_sh_l = x - s[j]
                dis_sh_r = s[j+1] - x
                break
    flg_te = 0
    if x <= t[0]:
        dis_te_r = t[0] - x
        dis_te_l = INF
        flg = -1
    elif x > t[-1]:
        dis_te_l = x - t[-1]
        dis_te_r = INF
        flg = 1
    else:
        flg_te = 1
        for j in range(A):
            if x > t[j]:
                dis_te_l = x - t[j]
                dis_te_r = t[j+1] - x
                break

    path_l = max(dis_sh_l, dis_te_l)
    path_r = max(dis_sh_r, dis_te_r)

    if flg_sh and flg_te:
        if (t[j]-s[j]) * (t[j+1]-s[j+1]) > 0:
            l_min, r_min = min(dis_sh_l, dis_te_l), min(dis_sh_r, dis_te_r)
            path_m = l_min + r_min + min(l_min, r_min)
        else:
            path_m = dis_sh_l + dis_te_r + min(dis_sh_l, dis_te_r)
            path_m = min(path_m, dis_sh_r + dis_te_l + min(dis_sh_r, dis_te_l))
    elif flg_sh and not flg_te:
        path_m = min(dis_te_l, dis_te_r)
        if flg == -1:
            path_m += min(dis_sh_l, path_m) + dis_sh_l
        else:
            path_m += min(dis_sh_r, path_m) + dis_sh_r
    elif flg_te and not flg_sh:
        path_m = min(dis_sh_l, dis_sh_r)
        if flg == -1:
            path_m += min(dis_te_l, path_m) + dis_te_l
        else:
            path_m += min(dis_te_r, path_m) + dis_te_r
    else:
        a, b = min(dis_sh_l, dis_sh_r), min(dis_te_l, dis_te_r)
        path_m = a + b + min(a, b)

    ans = min(path_l, path_r)
    ans = min(ans, path_m)
    print(ans)

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s = [0]*A
t = [0]*B

for i in range(A):
    s[i] = int(input())

for i in range(B):
    t[i] = int(input())

INF = 10**12
for _ in range(Q):
    x = int(input())
    if x <= s[0]:
        dis_sh_r = s[0] - x
        dis_sh_l = INF
    elif x > s[-1]:
        dis_sh_l = x - s[-1]
        dis_sh_r = INF
    else:
        for j in range(A):
            if x > s[j]:
                dis_sh_l = x - s[j]
                dis_sh_r = s[j+1] - x
                break
    if x <= t[0]:
        dis_te_r = t[0] - x
        dis_te_l = INF
    elif x > t[-1]:
        dis_te_l = x - t[-1]
        dis_te_r = INF
    else:
        for j in range(A):
            if x > t[j]:
                dis_te_l = x - t[j]
                dis_te_r = t[j+1] - x
                break
    l_min, r_min = min(dis_sh_l, dis_te_l), min(dis_sh_r, dis_te_r)
    path_l = max(dis_sh_l, dis_te_l)
    path_r = max(dis_sh_r, dis_te_r)
    path_m = l_min + r_min + min(l_min, r_min)
    ans = min(path_l, path_r)
    ans = min(ans, path_m)
    print(ans)

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split())
sno = [-1000000,]
tno = [-1000000,]
dis = []

for i in range(a):
    sno.append(int(input()))
sno.append(1000000)

for i in range(b):
    tno.append(int(input()))
tno.append(1000000)

for i in range(q):
    dis.append(int(input()))

for i in range(q):

    sl = 0
    sr = 0
    tl = 0
    tr = 0

    for j in range(a+2):
        if sno[j] >= dis[i]:
            sl = dis[i] - sno[j-1]
            sr = sno[j] - dis[i]
            break
        else:
            pass

    for k in range(b+2):
        if tno[k] >= dis[i]:
            tl = dis[i] - tno[k-1]
            tr = tno[k] - dis[i]
            break
        else:
            pass

    lds = max(sl, tl)
    rds = max(sr, tr)
    ans = min(lds, rds)
    print(ans)

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split())
sno = []
tno = []
dis = []

for i in range(a):
    sno.append(int(input()))

for i in range(b):
    tno.append(int(input()))

for i in range(q):
    dis.append(int(input()))

for i in range(q):

    sl = 0
    sr = 0
    tl = 0
    tr = 0

    for j in range(a):
        if sno[j] >= dis[i]:
            if j == 0:
                sl = 10000
                sr = sno[0] - dis[i]
                break
            else:
                sl = dis[i] - sno[j-1]
                sr = sno[j] - dis[i]
                break
        elif j == a-1:
            sl = dis[i] - sno[a-1]
            sr = 10000
            break
        else:
            pass

    for k in range(b):
        if tno[k] >= dis[i]:
            if k == 0:
                tl = 10000
                tr = tno[0] - dis[i]
                break
            else:
                tl = dis[i] - tno[k-1]
                tr = tno[k] - dis[i]
                break
        elif k == b-1:
            tl = dis[i] - tno[b-1]
            tr = 10000
            break
        else:
            pass

    lds = max(sl, tl)
    rds = max(sr, tr)
    ans = min(lds, rds)
    print(ans)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
import bisect
from itertools import product

A, B, Q = map(int, input().split())
S = [-10**10] + [int(input()) for _ in range(A)] + [10**10]
T = [-10**10] + [int(input()) for _ in range(B)] + [10**10]

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_left(S, x)
    s_cand = [S[idx1 - 1], S[idx1]]
    idx2 = bisect.bisect_left(T, x)
    t_cand = [T[idx2 - 1], T[idx2]]
    dist = 10**15
    for s, t in product(s_cand, t_cand):
        temp = min(abs(s - x), abs(t - x)) + abs(s - t)
        if temp < dist:
            dist = temp
    print(dist)

Submission status: Wrong Answer

Solution code:
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)]


def bsearch(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    if abs(l - m) <= abs(r - m):
        return l
    else:
        return r


def bsearch_l(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    return l


def calc_distance(x, l, c, near_idx):
    ret = abs(x - l)
    if c == 's':
        next_l = t[near_idx]
    else:
        next_l = s[near_idx]
    ret += abs(next_l - l)
    return ret


st = []
for i, s_i in enumerate(s):
    t_i = bsearch(s_i, t)
    st.append((s_i, 's', t_i))

for i, t_i in enumerate(t):
    s_i = bsearch(t_i, s)
    st.append((t_i, 't', s_i))

st = sorted(st)
starr = sorted(s + t)

ans_list = []
for i, x_i in enumerate(x):
    st_i = bsearch_l(x_i, starr)
    l, c, near_idx = st[st_i]
    ans = calc_distance(x_i, l, c, near_idx)
    if st_i < len(st) - 1:
        l, c, near_idx = st[st_i + 1]
        ans = min(ans, calc_distance(x_i, l, c, near_idx))
    ans_list.append(ans)

for i, ans in enumerate(ans_list):
    print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect
A,B,Q=map(int, input().split())
s=[int(input()) for _ in range(A)] + [10**10]
t=[int(input()) for _ in range(B)] + [10**10]
for _ in range(Q):
	x = int(input())
	ans = []
	ps = bisect(s, x)
	pt = bisect(t, x)
	for vs in (s[ps], s[ps-1]):
		for vt in (t[pt], t[pt-1]):
			ans.append(abs(x - vs) + abs(vs - vt))
			ans.append(abs(x - vt) + abs(vt - vs))
	print(min(ans))

Submission status: Wrong Answer

Solution code:
import bisect
import numpy as np
 
hint = list(map(int, input().split()))
s = [int(input()) for i in range(sum(hint))]
# print(s)

def search_min_pos(arr, target):
  index = bisect.bisect_left(arr, target)
  if 0 == index:
    dist = arr[index]
  elif len(arr) == index:
    dist = arr[index-1]
  else:
    dist = arr[index-1] if abs(arr[index-1]-target)< abs(arr[index]-target) else arr[index]
  return dist

a = s[:hint[0]]
b = s[hint[0]:-hint[2]]
sum = 0
for t in s[-hint[2]:]:
  a_pos = search_min_pos(a, t)
  b_pos = search_min_pos(b, t)
  #print(t, a_pos, b_pos)
  print(min([abs(t-a_pos)+abs(search_min_pos(b, a_pos)-a_pos), abs(t-b_pos)+abs(search_min_pos(a, b_pos)-b_pos)]))

Submission status: Wrong Answer

Solution code:
import math

A,B,Q = map(int, input().split())
S=[0]*A
T=[0]*B
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
S.append(10**11)
S.insert(0,(-1)*(10**11))
T.append(10**11)
T.insert(0,(-1)*(10**11))
for i in range(Q):
  x = int(input())
  floor = 0
  ceil = len(S)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    s = S[half]
    if s < x:
      floor = half
    else:
      ceil = half
  nearest_x_S = [S[floor]-x, S[ceil]-x]
        
  #floor = 0
  #ceil = len(T)-1
  #while ceil-floor > 1:
  #  half = math.floor((floor+ceil)/2)
  #  t = T[half]
  #  if t < x:
  #    floor = half
  #  else:
  #    ceil = half
  #nearest_x_T = [T[floor]-x, T[ceil]-x]
  
  ans_cand = [0]
  #for x_S in nearest_x_S:
  #  for x_T in nearest_x_T:
  #    if x_S*x_T > 0:
  #      ans_cand.append(max(abs(x_S),abs(x_T)))
  #    else:
  #      ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))

Submission status: Wrong Answer

Solution code:
import math

A,B,Q = map(int, input().split())
S=[0]*A
T=[0]*B
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
S.append(10**11)
S.insert(0,(-1)*(10**11))
T.append(10**11)
T.insert(0,(-1)*(10**11))
for i in range(Q):
  x = int(input())
  #floor = 0
  #ceil = len(S)-1
  #while ceil-floor > 1:
  #  half = math.floor((floor+ceil)/2)
  #  s = S[half]
  #  if s < x:
  #    floor = half
  #  else:
  #    ceil = half
  #nearest_x_S = [S[floor]-x, S[ceil]-x]
        
  #floor = 0
  #ceil = len(T)-1
  #while ceil-floor > 1:
  #  half = math.floor((floor+ceil)/2)
  #  t = T[half]
  #  if t < x:
  #    floor = half
  #  else:
  #    ceil = half
  #nearest_x_T = [T[floor]-x, T[ceil]-x]
  
  ans_cand = [0]
  #for x_S in nearest_x_S:
  #  for x_T in nearest_x_T:
  #    if x_S*x_T > 0:
  #      ans_cand.append(max(abs(x_S),abs(x_T)))
  #    else:
  #      ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))

Submission status: Wrong Answer

Solution code:
import math

A,B,Q = map(int, input().split())
S=[0]*A
T=[0]*B
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
S.append(10**11)
S.insert(0,(-1)*(10**11))
T.append(10**11)
T.insert(0,(-1)*(10**11))
for i in range(Q):
  x = int(input())
  floor = 0
  ceil = len(S)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    s = S[half]
    if s < x:
      floor = half
    else:
      ceil = half
  nearest_x_S = [S[floor]-x, S[ceil]-x]
        
  floor = 0
  ceil = len(T)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    t = T[half]
    if t < x:
      floor = half
    else:
      ceil = half
  nearest_x_T = [T[floor]-x, T[ceil]-x]
  
  ans_cand = [0]
  #for x_S in nearest_x_S:
  #  for x_T in nearest_x_T:
  #    if x_S*x_T > 0:
  #      ans_cand.append(max(abs(x_S),abs(x_T)))
  #    else:
  #      ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))

Submission status: Wrong Answer

Solution code:
import math

A,B,Q = map(int, input().split())
S=[0]*A
T=[0]*B
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
S.append(10**10+1)
S.insert(0,(-1)*(10**10+1))
T.append(10**10+1)
T.insert(0,(-1)*(10**10+1))
for i in range(Q):
  x = int(input())
  floor = 0
  ceil = len(S)-1
  index = math.floor(A/2)
  while True:
    s = S[index]
    s_before = S[index-1]
    s_next = S[index+1]
    if s < x:
      if x <= s_next:
        nearest_x_S = [s-x, s_next-x]
        break
      else:
        floor = index
        index = math.floor((floor+ceil)/2)
    elif x < s:
      if s_before <= x:
        nearest_x_S = [s_before-x, s-x]
        break
      else:
        ceil = index
        index = math.floor((floor+ceil)/2)
        
  floor = 0
  ceil = len(T)-1
  index = math.floor(B/2)
  while True:
    t = T[index]
    t_before = T[index-1]
    t_next = T[index+1]
    if t < x:
      if x <= t_next:
        nearest_x_T = [t-x, t_next-x]
        break
      else:
        floor = index
        index = math.floor((floor+ceil)/2)
    elif x < t:
      if t_before <= x:
        nearest_x_T = [t_before-x, t-x]
        break
      else:
        ceil = index
        index = math.floor((floor+ceil)/2)

  ans_cand = []
  for x_S in nearest_x_S:
    for x_T in nearest_x_T:
      if x_S*x_T > 0:
        ans_cand.append(max(abs(x_S),abs(x_T)))
      else:
        ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
a,b,q=map(int,input().split())
s=[]
t=[]
x=[]
s.append(-float("inf"))
t.append(-float("inf"))
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
s.append(float("inf"))
t.append(float("inf"))
for i in range(q):
    x=int(input())
    tmp_s=bisect_left(s,x)-1
    tmp_s_1=s[tmp_s]
    tmp_s_2=s[tmp_s+1]
    tmp_t=bisect_left(t,x)-1
    tmp_t_1=t[tmp_t]
    tmp_t_2=t[tmp_t+1]
    print(min(abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_1),
    abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_2),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_1),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_2),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_1),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_2),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_1),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_2)))

Submission status: Wrong Answer

Solution code:
import bisect
a,b,q=map(int,input().split())
s=[]
t=[]
x=[]
s.append(-float("inf"))
t.append(-float("inf"))
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
s.append(float("inf"))
t.append(float("inf"))
s.sort()
t.sort()
for i in range(q):
    x=int(input())
    tmp_s=bisect.bisect_left(s,x)-1
    tmp_s_1=s[tmp_s]
    tmp_s_2=s[tmp_s+1]
    tmp_t=bisect.bisect_left(t,x)-1
    tmp_t_1=t[tmp_t]
    tmp_t_2=t[tmp_t+1]
    print(min(abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_1),
    abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_2),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_1),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_2),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_1),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_2),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_1),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_2)))

Submission status: Wrong Answer

Solution code:
import bisect
a,b,q=map(int,input().split())
s=[]
t=[]
x=[]
s.append(-float("inf"))
t.append(-float("inf"))
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
s.append(float("inf"))
t.append(float("inf"))
s.sort()
t.sort()
for i in range(q):
    x=int(input())
    tmp_s=bisect.bisect_left(s,x)-1
    tmp_s_1=s[tmp_s]
    tmp_s_2=s[tmp_s+1]
    tmp_t=bisect.bisect_left(t,x)-1
    tmp_t_1=t[tmp_t]
    tmp_t_2=t[tmp_t+1]
    print(min(abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_1),
    abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_2),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_1),
    abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_2),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_1),
    abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_2),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_1),
    abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_2)))

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline
a,b,q = map(int,input().split())
inf = float('inf')
S = [-inf]+[int(input()) for i in range(a)]+[inf]
T = [-inf]+[int(input()) for i in range(b)]+[inf]

def f(S,T):
    res = 10**30 
    s = bisect_left(S,x)
    res = []

    for i in range(2):
    	for j in range(2):
            t = bisect_left(T,S[s-i])
            res.append(abs(x-S[s-i])+abs(S[s-i]-T[t-j]))

    return min(res)

for i in range(q):
    x = int(input())
    print(min(f(S,T),f(T,S)))

Submission status: Wrong Answer

Solution code:
import itertools
A,B,Q = [int(_) for _ in input().split(" ")]
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]                                                  
x = [int(input()) for _ in range(Q)]

def binary_search(arr,point,start,end):
    pivot = (start+end)//2
    if end -start > 1:
        if arr[pivot] <point :
            return binary_search(arr,point,pivot,end)
        else:
            return binary_search(arr,point,start,pivot)
    else:
        return start
    
def find_closest_path(s_1,s_2,t_1,t_2,point):
    shortest = 10**11
    for s,t in itertools.product([s_1,s_2],[t_1,t_2]):
        length = min(abs(point-t), abs(point -s )) + abs(s - t)
        if length < shortest:
            shortest = length 
    return length 


for i in range(Q):
    s_idx = binary_search(s,x[i],0,len(s)-1)
    t_idx = binary_search(t,x[i],0,len(t)-1)
    if len(s)>1:
        s_1,s_2 = s[s_idx],s[s_idx+1] 
    else:
        s_1,s_2 = s[0],s[0]
    if len(t)>1:
        t_1,t_2 = t[t_idx],t[t_idx+1]    
    else:
        t_1,t_2 = t[0],t[0]
    answer = find_closest_path(s_1,s_2,t_1,t_2,x[i])
    print(answer)