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 bisect import bisect_left
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]

s.sort()
t.sort()

for k in range(Q):
    x = int(input())
    ans = []
    ms = bisect_left(s, x)
    for i in [-1, 0]:
        if 0 <= ms + i <= A - 1:
            mt = bisect_left(t, s[ms + i])
            for j in [-1, 0]:
                if 0 <= mt + j <= B - 1:
                    ans.append(abs(s[ms + i] - x) + abs(t[mt + j] - s[ms + i]))
    mt = bisect_left(t, x)
    for i in [-1, 0]:
        if 0 <= mt + i <= B - 1:
            ms = bisect_left(s, t[mt + i])
            for j in [-1, 0]:
                if 0 <= ms + j <= A - 1:
                    ans.append(abs(t[mt + i] - x) + abs(s[ms + j] - t[mt + i]))
    print((ans))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect

a,b,q = map(int,readline().split())
s = [int(readline()) for i in range(a)]
t = [int(readline()) for i in range(b)]

for i in range(q):
  x = int(input())
  ss = bisect(s,x)
  if ss == 0:
    sR = s[0]
    sL = s[0]
  elif ss == len(s):
    sR = s[-1]
    sL = s[-1]
  else:
    sR = s[ss]
    sL = s[ss-1]
    
  sRt = bisect(t,sR)
  sLt = bisect(t,sL)
  if sRt == len(t):
    sRtR = t[-1]
  else:
    sRtR = t[sRt]
  if sLt == 0:
    sLtL = t[0]
  else:
    sLtL = t[sLt-1]
  chk = min(abs(x-sL)+abs(sL-sLtL),abs(x-sR)+abs(sR-sRtR))
  ss = bisect(t,x)
  if ss == 0:
    sR = t[0]
    sL = t[0]
  elif ss == len(t):
    sR = t[-1]
    sL = t[-1]
  else:
    sR = t[ss]
    sL = t[ss-1]
  sRt = bisect(s,sR)
  sLt = bisect(s,sL)
  if sRt == len(s):
    sRtR = s[-1]
  else:
    sRtR = s[sRt]
  if sLt == 0:
    sLtL = s[0]
  else:
    sLtL = s[sLt-1]  
  chk = min(chk,abs(x-sL)+abs(sL-sLtL),abs(x-sR)+abs(sR-sRtR))
  print(chk)

Submission status: Wrong Answer

Solution code:
#左右それぞれで一番近い神社
#その神社それぞれの左右それぞれで一番近い寺
#はい２ぶたん

INF = 10**10

from bisect import bisect_right
 
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
ans =[]
for i in range(0,Q,1):
    tmpans = INF

    q = int(input())
    sr = bisect_right(s,q)
    sl = sr-1
    sr = min(sr,A-1)
    sl = max(0,sl)
    for j in (sr,sl):
        tr = bisect_right(t,s[j])
        tl = tr-1
        tr = min(tr,B-1)
        tl = max(0,tl)
        ansR=abs(s[j]-q)+abs(s[j]-t[tr])
        ansL=abs(s[j]-q)+abs(s[j]-t[tl])
        tmpans = min(tmpans,ansR,ansL)

    
    tr = bisect_right(t,q)
    tl = tr-1
    tr = min(tr,B-1)
    tl = max(0,tl)
    for j in (tr,tl):
        sr = bisect_right(s,t[j])
        sl = sr-1
        sr = min(sr,A-1)
        sl = max(0,sl)
        ansR=abs(t[j]-q)+abs(t[j]-s[sr])
        ansL=abs(t[j]-q)+abs(t[j]-s[sl])
        tmpans = min(tmpans,ansR,ansL)
    
    ans.append(tmpans)

for i in range(0,Q,1):
    print(ans[i])

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())
    i=bisect.bisect_right(S,x)
    j=bisect.bisect_right(T,x)
    d=INF
    print(i)
    for s in [S[i-1],S[i]]:
        for t in [T[j-1],T[j]]:
            d1=abs(s-x)+abs(s-t)
            d2=abs(t-x)+abs(s-t)
            d=min(d,d1,d2)
    print(d)

Submission status: Wrong Answer

Solution code:
a,b,q = map(int,input().split())
s = sorted([int(input()) for i in range(a)])
t = sorted([int(input()) for i in range(b)])

from bisect import bisect

for i in range(q):
    x = int(input())

    s_i = bisect(s,x)
    d1 = d2 = 10**11
    if s_i<a:
        d1 = s[s_i]-x
        t_i = bisect(t,s[s_i])
        buf = 10**11
        if t_i<b:
            buf = t[t_i]-s[s_i]
        if t_i>0:
            buf = min(buf,s[s_i]-t[t_i-1])
        d1 += buf
    if s_i>0:
        d2 = x-s[s_i-1]
        t_i = bisect(t,s[s_i-1])
        buf = 10**11
        if t_i<b:
            buf = t[t_i]-s[s_i-1]
        if t_i>0:
            buf = min(buf,s[s_i-1]-t[t_i-1])
        d2 += buf
    
    t_i = bisect(t,x)
    d3 = d4 = 10**11
    if t_i<b:
        d3 = t[t_i]-x
        s_i = bisect(s,t[t_i])
        buf = 10**11
        if s_i<a:
            buf = s[s_i]-t[t_i]
        if s_i>0:
            buf = min(buf,t[t_i]-s[s_i-1])
        d3 += buf
    if t_i>0:
        d4 = x-t[t_i-1]
        s_i = bisect(s,t[t_i-1])
        buf = 10**11
        if s_i<a:
            buf = s[s_i]-t[t_i-1]
        if t_i>0:
            buf = min(buf,t[t_i-1]-s[s_i-1])
        d4 += buf
    print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
a,b,q = map(int,input().split())
s = sorted([int(input()) for i in range(a)])
t = sorted([int(input()) for i in range(b)])

from bisect import bisect

for i in range(q):
    x = int(input())

    s_i = bisect(s,x)
    d1 = d2 = float('inf')
    if s_i<a:
        d1 = s[s_i]-x
        t_i = bisect(t,s[s_i])
        buf = float('inf')
        if t_i<b:
            buf = t[t_i]-s[s_i]
        if t_i>0:
            buf = min(buf,s[s_i]-t[t_i-1])
        d1 += buf
    if s_i>0:
        d2 = x-s[s_i-1]
        t_i = bisect(t,s[s_i-1])
        buf = float('inf')
        if t_i<b:
            buf = t[t_i]-s[s_i-1]
        if t_i>0:
            buf = min(buf,s[s_i-1]-t[t_i-1])
        d2 += buf
    
    t_i = bisect(t,x)
    d3 = d4 = float('inf')
    if t_i<b:
        d3 = t[t_i]-x
        s_i = bisect(s,t[t_i])
        buf = float('inf')
        if s_i<a:
            buf = s[s_i]-t[t_i]
        if s_i>0:
            buf = min(buf,t[t_i]-s[s_i-1])
        d3 += buf
    if t_i>0:
        d4 = x-t[t_i-1]
        s_i = bisect(s,t[t_i-1])
        buf = float('inf')
        if s_i<a:
            buf = s[s_i]-t[t_i-1]
        if t_i>0:
            buf = min(buf,t[t_i-1]-s[s_i-1])
        d4 += buf
    print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
def resolve():
    A, B, Q = list(map(int, input().split()))
    S = sorted([int(input()) for _ in range(A)])
    T = sorted([int(input()) for _ in range(B)])
    X = [int(input()) for _ in range(Q)]
    #print("#####")
    import bisect
    for x in X:
        ans = float("inf")
        for Ary1, Ary2 in [[S, T], [T, S]]:
            idx1 = bisect.bisect_left(Ary1, x)
            if idx1 == 0 or (idx1 < len(Ary1) and abs(x-Ary1[idx1]) < abs(x-Ary1[idx1-1])):
                nearest_pos_1 = Ary1[idx1]
            else:
                nearest_pos_1 = Ary1[idx1-1]
            idx2 = bisect.bisect_left(Ary2, nearest_pos_1)
            if idx2 == 0 or (idx2 < len(Ary2) and abs(nearest_pos_1-Ary2[idx2]) < abs(nearest_pos_1-Ary2[idx2-1])):
                nearest_pos_2 = Ary2[idx2]
            else:
                nearest_pos_2 = Ary2[idx2-1]
            ans = min(ans, abs(x-nearest_pos_1)+abs(nearest_pos_1-nearest_pos_2))
        print(ans)




if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
from functools import lru_cache
from bisect import bisect
from sys import stdin
input = stdin.readline
A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]

@lru_cache(maxsize=None)
def func(f, x):
    L = S if f else T
    i = bisect(L, x)
    if i == 0:
        return abs(x - L[0])
    elif i == len(L):
        return abs(x - L[-1])
    else:
        return min(abs(x - L[i]), abs(x - L[i - 1]))

for x in [int(input()) for _ in range(Q)]:
    ans = float("inf")
    i = bisect(S, x)
    if i != 0:
        ans = min(ans, func(0, S[i - 1]) + x - S[i - 1])
    if i != A:
        ans = min(ans, func(0, S[i]) + x + S[i])
    i = bisect(T, x)
    if i != 0:
        ans = min(ans, func(1, T[i - 1]) + x - T[i - 1])
    if i != B:
        ans = min(ans, func(1, T[i]) + T[i] - x)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect
def main():
    a,b,q=map(int,input().split())
    S=[int(input())for _ in range(a)]
    T=[int(input())for _ in range(b)]

    def dfs(cur,flag1,flag2):
        if flag2==1:
            index_s=bisect(S,cur)
            if index_s==a:
                nx=S[-1]
            elif index_s==0:
                nx=S[0]
            else:
                if  abs(S[index_s]-cur)<abs(S[index_s-1]-cur):
                    nx=S[index_s]
                else:
                    nx=S[index_s-1]
            if flag1==1:
                return dfs(nx,0,0)+abs(nx-cur)
            else:
                return abs(nx-cur)
        else:
            index_t=bisect(T,cur)
            if index_t==b:
                nx=T[-1]
            elif index_t==0:
                nx=T[0]
            else:
                if  abs(T[index_t]-cur)<abs(T[index_t-1]-cur):
                    nx=T[index_t]
                else:
                    nx=T[index_t-1]
            if flag1==1:
                return dfs(nx,0,1)+abs(nx-cur)
            else:
                return abs(nx-cur)

    for i in range(q):
        x=int(input())
        print(min(dfs(x,1,1),dfs(x,1,0)))


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect
a, b, q = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
for _ in range(q):
    x = int(input())
    ans = 0
    s_index = bisect.bisect_left(s, x)
    t_index = bisect.bisect_left(t, x)
    if (s_index == a) or (s_index and abs(x-s[s_index-1]) < abs(x-s[s_index])):
        s_index -= 1
    if (t_index == b) or (t_index and abs(x-t[t_index-1]) < abs(x-t[t_index])):
        t_index -= 1
    if abs(x-s[s_index]) < abs(x-t[t_index]):
        ans += abs(x-s[s_index])
        x = s[s_index]
        t_index = bisect.bisect_left(t, x)
        if (t_index == b) or (t_index and abs(x-t[t_index-1]) < abs(x-t[t_index])):
            t_index -= 1
        ans += abs(x-t[t_index])
    else:
        ans += abs(x-t[t_index])
        x = t[t_index]
        s_index = bisect.bisect_left(s, x)
        if (s_index == a) or (s_index and abs(x-s[s_index-1]) < abs(x-s[s_index])):
            s_index -= 1
        ans += abs(x-s[s_index])
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
a, b, q = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
for _ in range(q):
    x = int(input())
    ans = 0
    s_index = bisect.bisect_left(s, x)
    t_index = bisect.bisect_left(t, x)
    if s_index == 0: s_min = abs(x-s[0])
    elif s_index == a: s_min = abs(x-s[s_index-1])
    else: s_min = min(abs(x-s[s_index-1]), abs(x-s[s_index]))
    if t_index == 0: t_min = abs(x-t[0])
    elif t_index == b: t_min = abs(x-t[t_index-1])
    else: t_min = min(abs(x-t[t_index-1]), abs(x-t[t_index]))   
    if s_min > t_min:
        ans += t_min
        s_index = bisect.bisect_left(s, t_min)
        if s_index == 0: s_min = abs(t_min-s[0])
        elif s_index == a: s_min = abs(t_min-s[s_index-1])
        else: s_min = min(abs(t_min-s[s_index-1]), abs(t_min-s[s_index]))
        ans += s_min
    else:
        ans += s_min
        t_index = bisect.bisect_left(t, s_min)
        if t_index == 0: t_min = abs(s_min-t[0])
        elif t_index == a: t_min = abs(s_min-t[t_index-1])
        else: t_min = min(abs(s_min-t[t_index-1]), abs(s_min-t[t_index]))
        ans += t_min
    print(ans)

Submission status: Wrong Answer

Solution code:
def find_min_fuka_idx(lst, x):
  ok = -1
  ng = len(lst)
  while abs(ok-ng)>1:
    mid = (ok+ng)//2
    if x>=lst[mid]:
      ok = mid
    else:
      ng = mid
  return ng

a,b,q = map(int, input().split())
s = [-10**18]
s_mid = [int(input()) for i in range(a)]
s.extend(s_mid)
s.append(10**18)
  
t = [-10**18]
t_mid = [int(input()) for i in range(b)]
t.extend(t_mid)
t.append(10**18)
x_list = [(int(input()), i) for i in range(q)]
x_list = sorted(x_list, key=lambda p: p[0])
si = 1
tj = 1
ans = []
for i in range(q):
  x = x_list[i][0]
  si = find_min_fuka_idx(s[si-1:], x)
  tj = find_min_fuka_idx(t[tj-1:], x)
  ssi = s[si]
  ssi1 = s[si-1]
  ttj = t[tj]
  ttj1 = t[tj-1]
  ans.append((min(abs(x-ssi)+abs(ssi-ttj),\
            abs(x-ssi)+abs(ssi-ttj1),\
           abs(x-ttj)+abs(ttj-ssi),\
            abs(x-ttj)+abs(ttj-ssi1),\
            abs(x-ssi1)+abs(ssi1-ttj),\
            abs(x-ssi1)+abs(ssi1-ttj1),\
           abs(x-ttj1)+abs(ttj1-ssi),\
            abs(x-ttj1)+abs(ttj1-ssi1)), x_list[i][1]))

ans = sorted(ans, key=lambda p: p[1])
for p in range(q):
  print(ans[p][0])

Submission status: Wrong Answer

Solution code:
import sys
import bisect
input = sys.stdin.readline
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
s = sorted(s)
t = sorted(t)

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_right(s, x)
    idx2 = bisect.bisect_right(t, x)
    ans = float('inf')
    if idx1 > 0:
        left_s = s[idx1-1]
    else:
        left_s = float('inf')
    if idx2 > 0:
        left_t = t[idx2-1]
    else:
        left_t = float('inf')
    if idx1 < A:
        right_s = s[idx1]
    else:
        right_s = float('inf')
    if idx2 < B:
        right_t = t[idx2]
    else:
        right_t = float('inf')

    ls = abs(x-left_s)
    lt = abs(x-left_t)
    rs = abs(right_s-x)
    rt = abs(right_t-x)

    ans = min(ans, right_t-left_s + min(ls, rt))
    ans = min(ans, right_s-left_t + min(lt, rs))
    ans = min(ans, max(abs(ls), abs(lt)))
    ans = min(ans, max(abs(rs), abs(rt)))
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
input = sys.stdin.readline
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
s = sorted(s)
t = sorted(t)

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_right(s, x)
    idx2 = bisect.bisect_right(t, x)
    ans = float('inf')
    if idx1 > 0:
        left_s = s[idx1-1]
    else:
        left_s = float('inf')
    if idx2 > 0:
        left_t = t[idx2-1]
    else:
        left_t = float('inf')
    if idx1 < A:
        right_s = s[idx1]
    else:
        right_s = float('inf')
    if idx2 < B:
        right_t = t[idx2]
    else:
        right_t = float('inf')

    if left_s <= x <= right_t:
        tmp = right_t-left_s + min(x-left_s, right_t-x)
        ans = min(ans, tmp)
    if left_t <= x <= right_s:
        tmp = right_s-left_t + min(x-left_t, right_s-x)
        ans = min(ans, left_t+right_s-x)
    ans = min(ans, max(abs(x-left_s), abs(x-left_t)))
    ans = min(ans, max(abs(right_s-x), abs(right_t-x)))
    print(ans)

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

s.sort()
t.sort()

import bisect
#a=[0,2,3,4,67,8]
#a.sort()
for item in x:
    ss=bisect.bisect_left(s, item)
    tmp=[]
    if ss>0:
        tmp.append(s[ss-1])
    if ss<len(s):
        tmp.append(s[ss])
    tmp2=[]
    tt=bisect.bisect_left(t, item)
    if tt>0:
        tmp2.append(t[tt-1])
    if tt<len(t):
        tmp2.append(t[tt])

    result=10**9
    for item2 in tmp:
        for item3 in tmp2:
#            print(item,item2,item3)
            result=min(result,abs(item-item3)+abs(item3-item2),abs(item-item2)+abs(item3-item2))
    print(result)

Submission status: Wrong Answer

Solution code:
import bisect
a,b,q=map(int,input().split())
A=[-10**10-1]
B=[-10**10-1]
x=[]

for i in range(a):
  A.append(int(input()))
A.append(10**10+1)
for i in range(b):
  B.append(int(input()))
B.append(10**10+1)
for i in range(q):
  x.append(int(input()))
for i in x:
  d=10010010010
  s=bisect.bisect_left(A,i)
  t=bisect.bisect_left(B,i)
  ss=[A[s],A[s-1]]
  tt=[B[t],B[t-1]]
  for j in ss:
    for k in tt:
      d=min(d,abs(i-j)+abs(j-k))
  for j in tt:
    for k in ss:
      d=min(d,abs(i-j)+abs(j-k))
  print(d)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]

sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
# ----------------------------------------------------------- #

a, b, q = Is()
S = [-INF] + sorted(IR(a)) + [INF]
T = [-INF] + sorted(IR(b)) + [INF]
X = IR(q)

for x in X:
    sr = bisect_left(S, x)
    tr = bisect_left(T, x)
    sl = sr-1
    tl = tr-1

    ssr = S[sr]
    ssl = S[sl]
    ttr = T[tr]
    ttl = T[tl]

    ans = min(abs(x - ssr) + abs(ssr - ttr), abs(x - ssr) + abs(ssr - ttl),
              abs(x - ssl) + abs(ssl - ttr), abs(x - ssl) + abs(ssl - ttl),
              abs(x - ttr) + abs(ttr - ssr), abs(x - ttr) + abs(ttr - ssl),
              abs(x - ttr) + abs(ttl - ssr), abs(x - ttl) + abs(ttl - ssl))

    print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left


A,B,Q = map(int, input().split())
INF = float("inf")
S = [int(input()) for _ in range(A)]
S = [-INF] + S + [INF]
T = [int(input()) for _ in range(B)]
T = [-INF] + T + [INF]
X = [int(input()) for _ in range(Q)]

ans = []
for x in X:
    idx_s = bisect_left(S, x)
    idx_t = bisect_left(T, x)
    candidate = [max(abs(x - T[idx_t - 1]), abs(x - S[idx_s-1])),
                max(abs(x - S[idx_s]), abs(x - T[idx_t])),
                2*abs(x - S[idx_s-1]) + abs(x - T[idx_t]),
                2*abs(x - T[idx_t]) + abs(x - S[idx_s-1])]
    ans.append(min(candidate))

print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
import sys

def main():
  input = sys.stdin.readline
  a, b, q = 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())

  s_near = [0]*a
  k = 0
  for i in range(a):
    if k == b-1:
      s_near[i] = abs(s[i]-t[b-1])
      continue
    while True:
      sub = min([abs(s[i]-t[k]), abs(s[i]-t[k+1])])
      if sub == abs(s[i]-t[k+1]):
        k += 1
        if k == b-1:
          sub == abs(s[i]-t[k])
          break
        continue
      else:
        s_near[i] = sub
        break
  
  t_near = [0]*b
  k = 0
  for i in range(b):
    if k == a-1:
      t_near[i] = abs(t[i]-s[a-1])
      continue
    while True:
      sub = min([abs(t[i]-s[k]), abs(t[i]-s[k+1])])
      if sub == abs(t[i]-s[k+1]):
        k += 1
        if k == a-1:
          sub == abs(t[i]-s[k])
          break
        continue
      else:
        sub == abs(t[i]-s[k+1])
        break
  
  for i in range(q):
    x = int(input())
    if x <= s[0]:
      sub1 = s[0]-x+s_near[0]
    elif x >= s[a-1]:
      sub1 = x-s[a-1]+s_near[a-1]
    else:
      l, r = 0, a
      while r-l > 1:
        k = (r+l)//2
        if x <= s[k]:
          r = k
        else:
          l = k
      sub1 = min([s[r]-x+s_near[r], x-s[l]+s_near[l]])
    
    if x <= t[0]:
      sub2 = t[0]-x+t_near[0]
    elif x >= t[b-1]:
      sub2 = x-t[b-1]+t_near[b-1]
    else:
      l, r = 0, b
      while r-l > 1:
        k = (r+l)//2
        if x <= t[k]:
          r = k
        else:
          l = k
      sub2 = min([t[r]-x+t_near[r], x-t[l]+t_near[l]])
    ans = min([sub1, sub2])
    print(ans)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
import sys

def main():
  input = sys.stdin.readline
  a, b, q = 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())

  s_near = [0]*a
  k = 0
  for i in range(a):
    if k == b-1:
      s_near[i] = abs(s[i]-t[b-1])
      continue
    s_near[i] = min([abs(s[i]-t[k]), abs(s[i]-t[k+1])])
    if s_near[i] == abs(s[i]-t[k+1]):
      k += 1
  
  t_near = [0]*b
  k = 0
  for i in range(b):
    if k == a-1:
      t_near[i] = abs(t[i]-s[a-1])
      continue
    t_near[i] = min([abs(t[i]-s[k]), abs(t[i]-s[k+1])])
    if t_near[i] == abs(t[i]-s[k+1]):
      k += 1
  
  for i in range(q):
    x = int(input())
    if x <= s[0]:
      sub1 = s[0]-x+s_near[0]
    elif x >= s[a-1]:
      sub1 = x-s[a-1]+s_near[a-1]
    else:
      l, r = 0, a
      while r-l > 1:
        k = (r+l)//2
        if x <= s[k]:
          r = k
        else:
          l = k
      sub1 = min([s[r]-x+s_near[r], x-s[l]+s_near[l]])
    
    if x <= t[0]:
      sub2 = t[0]-x+t_near[0]
    elif x >= t[b-1]:
      sub2 = x-t[b-1]+t_near[b-1]
    else:
      l, r = 0, b
      while r-l > 1:
        k = (r+l)//2
        if x <= t[k]:
          r = k
        else:
          l = k
      sub2 = min([t[r]-x+t_near[r], x-t[l]+t_near[l]])
    ans = min([sub1, sub2])
    print(ans)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
import itertools


def solve(x, s_pair, t_pair):
    assert 2 == len(s_pair) == len(t_pair)
    min_dist = 10**11  # large number
    for s, t in itertools.product(s_pair, t_pair):
        min_dist = min([
            min_dist,
            abs(x - s) + abs(s - t),
            abs(x - t) + abs(t - s)
        ])
    return min_dist


def binary_search(x, li):
    lower = 0
    upper = len(li) - 1
    while abs(upper - lower) > 1:
        mid = (lower + upper) // 2
        if li[mid] < x:
            lower = mid
        else:
            upper = mid
    return (li[lower], li[upper])


def main():
    A, B, Q = list(map(int, input().split(' ')))
    s_list = [0] + [int(input()) for _ in range(A)]
    s_list.sort()
    t_list = [int(input()) for _ in range(B)]
    t_list.sort()
    x_list = [int(input()) for _ in range(Q)]
    for x in x_list:
        s_pair = binary_search(x, s_list)
        t_pair = binary_search(x, t_list)
        print(solve(x, s_pair, t_pair))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)


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

    for i in range(Q):
        x = int(input())
        s_index = bisect_left(S, x)
        if s_index == 0:
            S1 = S[s_index]
            S2 = S[s_index]
        elif s_index == A:
            S1 = S[s_index - 1]
            S2 = S[s_index - 1]
        else:
            S1 = S[s_index - 1]
            S2 = S[s_index - 1]

        t_index = bisect_left(T, x)
        if t_index == 0:
            T1 = T[t_index]
            T2 = T[t_index]
        elif t_index == B:
            T1 = T[t_index - 1]
            T2 = T[t_index - 1]
        else:
            T1 = T[t_index]
            T2 = T[t_index - 1]
        answer = float("inf")
        for s in [S1, S2]:
            for t in [T1, T2]:
                r = min(abs(x - s) + abs(t - s), abs(x - t) + abs(t - s))
                if answer > r:
                    answer = r
        print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect
a,b,q=map(int,input().split())
S=[float("inf")];T=[float("inf")]
for _ in range(a):
    s=int(input())
    S.append(s)
for _ in range(b):
    t=int(input())
    T.append(t)
S.sort()
T.sort()

for _ in range(q):
    x=int(input())
    idx_s=bisect.bisect_left(S,x)
    idx_t=bisect.bisect_left(T,x)
    se=S[idx_s]
    sw=S[idx_s-1]
    te=T[idx_t]
    tw=T[idx_t-1]

    m1=abs(x-se)+abs(se-te)
    m2=abs(x-se)+abs(se-tw)
    m3=abs(x-sw)+abs(sw-te)
    m4=abs(x-sw)+abs(sw-tw)
    m5=abs(x-te)+abs(te-se)
    m6=abs(x-te)+abs(te-sw)
    m7=abs(x-tw)+abs(tw-se)
    m8=abs(x-tw)+abs(tw-sw)
    print(min(m1,m2,m3,m4,m5,m6,m7,m8))

Submission status: Wrong Answer

Solution code:
# D - Lazy Faith

import bisect
INF = 10**18
A, B, Q = map(int, input().split())
S = [0]*A
T = [0]*B
X = [0]*Q
for i in range(A):
    S[i] = int(input())

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

for i in range(Q):
    X[i] = int(input())

for x in X:
    #print(x)
    idxS = bisect.bisect_left(S, x)
    #print('idxS', idxS)
    if idxS==0:
        leftS = INF
        rightS = S[idxS] - x
    elif idxS==A:
        leftS = x - S[idxS-1]
        rightS = INF
    else:
        leftS = x - S[idxS-1]
        rightS = S[idxS] - x
    #print(leftS, rightS)
    idxT = bisect.bisect_left(T, x)
    if idxT==0:
        leftT = INF
        rightT = T[idxT] -x
    elif idxT==B:
        leftT = x - T[idxT-1]
        rightT = INF
    else:
        leftT = x - T[idxT-1]
        rightT = T[idxT] - x
    #print(leftT, rightT)
    ans = max(leftS, leftT)
    ans = min(ans, max(rightS, rightT))
    ans = min(ans, min(leftS, leftT) * 2 + min(rightS, rightT))
    ans = min(ans, min(leftS, leftT) + min(rightS, rightT) * 2)
    print(ans)

Submission status: Wrong Answer

Solution code:
# D - Lazy Faith

import bisect
INF = 10**18
A, B, Q = map(int, input().split())
S = [0]*A
T = [0]*B
X = [0]*Q
for i in range(A):
    S[i] = int(input())

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

for i in range(Q):
    X[i] = int(input())

for x in X:
    #print(x)
    idxS = bisect.bisect_left(S, x)
    #print('idxS', idxS)
    if idxS==0:
        leftS = INF
        rightS = S[idxS] - x
    elif idxS==A:
        leftS = x - S[idxS-1]
        rightS = INF
    else:
        leftS = x - S[idxS-1]
        rightS = S[idxS] - x
    #print(leftS, rightS)
    idxT = bisect.bisect_left(T, x)
    if idxT==0:
        leftT = INF
        rightT = T[idxT] -x
    elif idxT==B:
        leftT = x - T[idxT-1]
        rightT = INF
    else:
        leftT = x - T[idxT-1]
        rightT = T[idxT] - x
    #print(leftT, rightT)
    ans = max(leftS, leftT)
    ans = min(ans, max(rightS, rightT))
    ans = min(ans, min(leftS, leftT) * 2 + min(rightS, rightT))
    ans = min(ans, min(leftS, rightS) + min(rightS, rightT) * 2)
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
input = sys.stdin.readline

a,b,q = map(int,input().split())
s = []
t = []
s1 = []
t1 = []

for _ in range(a):
    s.append(int(input()))
for _ in range(b):
    t.append(int(input()))

for i in range(a-1):
    s1.append((s[i]+s[i+1])/2)
for i in range(b-1):
    t1.append((t[i]+t[i+1])/2)

for _ in range(q):
    x = int(input())
    s_index = bisect.bisect_left(s1, x)
    t_index = bisect.bisect_left(t1, x)
    xs = []
    xt = []
    for i in range(-2, 3):
        print(i)
        if 0 <= s_index + i < a:
            xs.append(s[s_index+i])
        if 0 <= t_index + i < b:
            xt.append(t[t_index+i])
    ans = 10**20
    for i in xs:
        for j in xt:
            ans = min(ans, abs(x-i) + abs(i-j), abs(x-j) + abs(j-i))

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import os
import math
import bisect

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    A, B, Q = il()
    J = [-MAX] + [ii() for _ in range(A)] + [MAX]
    T = [-MAX] + [ii() for _ in range(B)] + [MAX]
    que = [ii() for _ in range(Q)]
    for q in que:
        a = bisect.bisect_left(J, q)
        b = bisect.bisect_left(T, q)
        ret = MAX
        for j in J[a - 1:a]:
            for t in T[b - 1:b + 1]:
                ret = min(ret, abs(j - q) + abs(t - j), abs(t - q) + abs(j - t))
        print(ret)


if __name__ == '__main__':
    main()