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:
import bisect
import math

a, b, q = map(int, input().split())
s = [-math.inf]
t = [-math.inf]
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()))
s.append(math.inf)
t.append(math.inf)

for i in x:
    ws = abs(s[bisect.bisect(s, i) - 1] - i)
    ws += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])-1] - s[bisect.bisect(s, i) - 1]), \
              abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])] - s[bisect.bisect(s, i) - 1]))
    
    es = abs(s[bisect.bisect(s, i)] - i)
    es += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i)])-1] - s[bisect.bisect(s, i)]), \
              abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])] - s[bisect.bisect(s, i)]))
    
    wt = abs(t[bisect.bisect(t, i) - 1] - i)
    wt += min(abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])-1] - t[bisect.bisect(t, i) - 1]), \
              abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])] - t[bisect.bisect(t, i) - 1]))
    
    et = abs(t[bisect.bisect(t, i)] - i)
    et += min(abs(s[bisect.bisect(s, t[bisect.bisect(t, i)])-1] - t[bisect.bisect(t, i)]), \
              abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])] - t[bisect.bisect(t, i)]))
    
    print(min(ws, es, wt, et))

Submission status: Wrong Answer

Solution code:
import bisect
import math

a, b, q = map(int, input().split())
s = [-math.inf]
t = [-math.inf]
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()))
s.append(math.inf)
t.append(math.inf)

for i in x:
    ws = abs(s[bisect.bisect(s, i) - 1] - i)
    ws += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])-1] - s[bisect.bisect(s, i) - 1]), \
              abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])] - s[bisect.bisect(s, i) - 1]))
    
    es = abs(s[bisect.bisect(s, i)] - i)
    es += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i)])-1] - s[bisect.bisect(s, i)]), \
              abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])] - s[bisect.bisect(s, i)]))
    
    wt = abs(t[bisect.bisect(t, i) - 1] - i)
    wt += min(abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])-1] - t[bisect.bisect(t, i) - 1]), \
              abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])] - t[bisect.bisect(t, i) - 1]))
    
    et = abs(t[bisect.bisect(t, i)] - i)
    et += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])-1] - s[bisect.bisect(s, i) - 1]), \
              abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])] - s[bisect.bisect(s, i) - 1]))
    
    print(min(ws, es, wt, et))

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def neighborhood(L, now, c):
        tmp = bisect_left(L, now)
        if tmp == 0:
            idx = 0
        elif tmp == c:
            idx = tmp - 1
        else:
            if abs(now - L[tmp]) < abs(now - L[tmp - 1]):
                idx = tmp
            else:
                idx = tmp - 1
        return L[idx]

    def dist_calc(now, p1, p2):
        res1 = abs(now - p1) + abs(p1 - p2)
        res2 = abs(now - p2) + abs(p1 - p2)
        return min(res1, res2)

    a, b, q = map(int, input().split())
    S = list(int(input()) for _ in range(a))
    T = list(int(input()) for _ in range(b))
    query = list(int(input()) for _ in range(q))

    for x in query:
        s = neighborhood(S, x, a)
        t = neighborhood(T, s, b)
        dist1 = dist_calc(x, s, t)

        t = neighborhood(T, x, b)
        s = neighborhood(S, t, a)
        dist2 = dist_calc(x, s, t)

        res = min(dist1, dist2)
        print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def shrine(now, dist):
        s = bisect_left(S, now)
        if s == 0:
            dist += abs(now - S[s])
            now = S[s]
        elif s == a:
            dist += abs(now - S[s - 1])
            now = S[s - 1]
        else:
            if abs(now - S[s]) < abs(now - S[s - 1]):
                dist += abs(now - S[s])
                now = S[s]
            else:
                dist += abs(now - S[s - 1])
                now = S[s - 1]
        return now, dist

    def temple(now, dist):
        t = bisect_left(T, now)
        if t == 0:
            dist += abs(now - T[t])
            now = T[t]
        elif t == b:
            dist += abs(now - T[t - 1])
            now = T[t - 1]
        else:
            if abs(now - T[t]) < abs(now - T[t - 1]):
                dist += abs(now - T[t])
                now = T[t]
            else:
                dist += abs(now - T[t - 1])
                now = T[t - 1]
        return now, dist

    a, b, q = map(int, input().split())
    S = list(int(input()) for _ in range(a))
    T = list(int(input()) for _ in range(b))
    query = list(int(input()) for _ in range(q))

    for x in query:
        now, dist1 = shrine(x, 0)
        _, dist1 = temple(now, dist1)

        now, dist2 = temple(x, 0)
        _, dist2 = shrine(now, dist2)

        res = min(dist1, dist2)
        print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import bisect

a, b, q = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
x = [int(input()) for _ in range(q)]

for i in x:
    si = bisect.bisect_left(s, i)
    ti = bisect.bisect_left(t, i)
    sl, sr = i-s[si-1], s[si]-i
    tl, tr = i-t[ti-1], t[ti]-i
    print(min(max(sl,tl), sl+tr+min(sl,tr), sr+tl+min(sr,tl), max(tl,tr)))

Submission status: Wrong Answer

Solution code:
A,B,Q=list(map(int,input().split()))
l_shr=[int(input()) for i in range(A)]
l_tmp=[int(input()) for i in range(B)]
l_shr.sort()
l_tmp.sort()
inf=float("inf")
from bisect import bisect_left
for i in range(Q):
   mid=0
   ans1=0
   ans2=0
   q=int(input())
   pos=bisect_left(l_shr,q)
   minus,plus=l_shr[pos-1] if pos>0 else inf,l_shr[pos] if pos<A else inf
   mid=minus if abs(q-minus)<abs(q-plus) else plus
   ans1=abs(q-mid)
   pos=bisect_left(l_tmp,mid)
   ans1+=min(abs(mid-l_tmp[pos-1] if pos>0 else inf), abs(mid-l_tmp[pos] if pos<B else inf))
   pos=bisect_left(l_tmp,q)
   minus,plus=l_tmp[pos-1] if pos>0 else inf,l_tmp[pos] if pos<B else inf
   mid=minus if abs(q-minus)<abs(q-plus) else plus
   ans2=abs(q-mid)
   pos=bisect_left(l_shr,mid)
   ans2+=min(abs(mid-l_shr[pos-1] if pos>0 else inf), abs(mid-l_shr[pos] if pos<A else inf))
   print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from sys import stdin
input = stdin.buffer.readline

def main():
    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)]

    ans = [0] * q
    for i in range(q):
        start = x[i]
        si = bisect_left(s, start, hi=a-1)
        ti = bisect_left(t, start, hi=b-1)

        sl = (s[si-1], s[si])
        tl = (t[ti-1], t[ti])

        distance = 100000000000
        for j in sl:
            for k in tl:
                if abs(start - j) + abs(j - k) < distance:
                    distance = abs(start - j) + abs(j - k)
                if abs(start - k) + abs(k - j) < distance:
                    distance = abs(start - k) + abs(k - j)
                print(start, j, k)

        ans[i] = distance

    for i in ans:
        print(i)

main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
for i in range(Q):
    x=int(input())
    si=bisect_left(S,x)
    ti=bisect_left(T,x)
    if 0<si<A and 0<ti<B:
        sl,sr=abs(x-S[si-1]),abs(x-S[si])
        tl,tr=abs(x-T[ti-1]),abs(x-T[ti])
        print(min(max(sr,tr),max(sl,tl),sl+tr+min(sl,tr),sr+tl+min(sr,tl)))
    elif si==0 and ti==0:
        sr=abs(x-S[si])
        tr=abs(x-T[ti])
        print(sr,tr)
    elif si==A and ti==B:
        sl=abs(x-S[si-1])
        tl=abs(x-T[ti-1])
        print(max(sl,tl))
    elif si==0 and ti==B:
        sr=abs(x-S[si])
        tl=abs(x-T[ti-1])
        print(sr+tl+min(sr,tl))
    elif si==A and ti==0:
        sl=abs(x-S[si-1])
        tr=abs(x-T[ti])
        print(sl+tr+min(sl,tr))
    elif si==0:
        sr=abs(x-S[si])
        tl,tr=abs(x-T[ti-1]),abs(x-T[ti])
        print(min(max(sr,tr),sr+tl+min(sr,tl)))
    elif si==A:
        sl=abs(x-S[si-1])
        tl,tr=abs(x-T[ti-1]),abs(x-T[ti])
        print(min(max(sl,tl),sl+tr+min(sl,tr)))
    elif ti==0:
        sl,sr=abs(x-S[si-1]),abs(x-S[si])
        tr=abs(x-T[ti])
        print(min(max(sr,tr),sl+tr+min(sl,tr)))
    elif ti==B:
        sl,sr=abs(x-S[si-1]),abs(x-S[si])
        tl=abs(x-T[ti-1])
        print(min(max(sl,tl),sr+tl+min(sr,tl)))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
for i in range(Q):
    x=int(input())
    si=bisect_left(S,x)
    ti=bisect_left(T,x)
    if 0<si<A and 0<ti<B:
        sl,sr=abs(x-S[si-1]),abs(x-S[si])
        tl,tr=abs(x-T[ti-1]),abs(x-T[ti])
        print(min(max(sr,tr),max(sl,tl),min(sl,tl)+min(sr,tr)+min(sl,sr,tl,tr)))
    elif si==0 and ti==0:
        print(max(S[0]-x,T[0]-x))
    elif si==A and ti==B:
        print(max(x-S[-1],x-T[-1]))
    elif si==0 and ti==B:
        s=abs(S[0]-x)
        t=abs(x-T[-1])
        print(s+t+min(s,t))
    elif si==A and ti==0:
        s=abs(x-S[-1])
        t=abs(T[0]-x)
        print(s+t+min(s,t))
    elif si==0:
        print(min(max(S[0]-x,T[ti]-x),S[0]-x+x-T[ti-1]+min(S[0]-x,x-T[ti-1])))
    elif si==A:
        print(min(max(x-S[-1],x-T[ti-1]),x-S[-1]+T[ti]-x+min(x-S[-1],T[ti]-x)))
    elif ti==0:
        print(min(max(T[0]-x,S[si]-x),T[0]-x+x-S[si-1]+min(T[0]-x,x-S[si-1])))
    elif ti==B:
        print(min(max(x-T[-1],x-S[si-1]),x-T[-1]+S[si]-x+min(x-T[-1],S[si]-x)))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
for i in range(Q):
    x=int(input())
    si=bisect_left(S,x)
    ti=bisect_left(T,x)
    if 0<si<A and 0<ti<B:
        sl,sr=abs(x-S[si-1]),abs(x-S[si])
        tl,tr=abs(x-T[ti-1]),abs(x-T[ti])
        print(min(max(sr,tr),max(sl,tl),min(sl,tl)+min(sr,tr)+min(sl,sr,tl,tr)))
    elif si==0 and ti==0:
        print(max(S[0]-x,T[0]-x))
    elif si==A and ti==B:
        print(max(x-S[-1],x-T[-1]))
    elif si==0 and ti==B:
        s=abs(x-S[0])
        t=abs(T[-1]-x)
        print(s+t+min(s,t))
    elif si==A and ti==0:
        s=abs(S[-1]-x)
        t=abs(x-T[0])
        print(s+t+min(s,t))
    elif si==0:
        print(min(max(S[0]-x,T[ti]-x),S[0]-x+x-T[ti-1]+min(S[0]-x,x-T[ti-1])))
    elif si==A:
        print(min(max(x-S[-1],x-T[ti-1]),x-S[-1]+T[ti]-x+min(x-S[-1],T[ti]-x)))
    elif ti==0:
        print(min(max(T[0]-x,S[si]-x),T[0]-x+x-S[si-1]+min(T[0]-x,x-S[si-1])))
    elif ti==B:
        print(min(max(x-T[-1],x-S[si-1]),x-T[-1]+S[si]-x+min(x-T[-1],S[si]-x)))

Submission status: Wrong Answer

Solution code:
import bisect

a,b,q=map(int,input().split())
s = []
t = []
for i in range(a):
  s.append(int(input()))
for i in range(b):
  t.append(int(input()))
for _ in range(q):
  x = int(input())
  
  ans = [0,0,0]
  
  if x in s:
    sl = 0
    sr = 0
  else:
    i = bisect.bisect_left(s,x)
    if i == 0:
      sl = 100000000000
    else:
      sl = abs(s[i-1]-x)
    if i == a:
      sr = 100000000000
    else:
      sr = abs(s[i]-x)
  if x in t:
    tl = 0
    tr = 0
  else:
    i = bisect.bisect_left(t,x)
    if i == 0:
      tl = 100000000000
    else:
      tl = abs(t[i-1]-x)
    if i == b:
      tr = 100000000000
    else:
      tr = abs(t[i]-x)
  ans[0] = max(tl,sl)
  ans[1] = max(tr,sr)
  ans[2] = min(2*tl+sr,2*sl+tr)
  print(min(ans))

Submission status: Wrong Answer

Solution code:

from bisect import bisect_right

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

for x in X:
    # Shrine -> Temple
    idx_s = bisect_right(S, x)
    if abs(S[min(A - 1, idx_s)] - x) > abs(S[max(0, idx_s - 1)] - x):
        x_s = S[max(0, idx_s - 1)]
    else:
        x_s = S[min(A - 1, idx_s)]

    idx_t = bisect_right(T, x_s)
    if abs(T[min(B - 1, idx_t)] - x_s) > abs(T[max(0, idx_t - 1)] - x_s):
        x_t = T[max(0, idx_t - 1)]
    else:
        x_t = T[min(B - 1, idx_t)]

    cand_0 = abs(x_s - x) + abs(x_t - x_s)

    # Temple -> Shrine
    idx_t = bisect_right(T, x)
    if abs(T[min(B - 1, idx_t)] - x) > abs(T[max(0, idx_t - 1)] - x):
        x_t = T[max(0, idx_t - 1)]
    else:
        x_t = T[min(B - 1, idx_t)]

    idx_s = bisect_right(S, x_t)
    if abs(S[min(A - 1, idx_s)] - x_t) > abs(S[max(0, idx_s - 1)] - x_t):
        x_s = S[max(0, idx_s - 1)]
    else:
        x_s = S[min(A - 1, idx_s)]

    cand_1 = abs(x_t - x) + abs(x_s - x_t)

    print(min(cand_0, cand_1))

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

A, B, Q = map(int, sys.stdin.readline().split())
S = []
for _ in range(A):
    S.append(int(sys.stdin.readline()))
T = []
for _ in range(B):
    T.append(int(sys.stdin.readline()))

# S.sort()
# T.sort()
for _ in range(Q):
    x = int(sys.stdin.readline())

    #print("x", x)
    # 前後の神社、寺の距離を求める
    s_pos = bisect_left(S, x)
    if s_pos == 0:
        s_left = -float("inf")
    else:
        s_left = S[s_pos-1]
    if s_pos == A:
        s_right = float("inf")
    else:
        s_right = S[s_pos]
    #print("s", s_left, s_right)

    t_pos = bisect_left(T, x)
    if t_pos == 0:
        t_left = -float("inf")
    else:
        t_left = T[t_pos-1]
    if t_pos == B:
        t_right = float("inf")
    else:
        t_right = T[t_pos]
    #print("t", t_left, t_right)

    left_min = min(x - s_left, x - t_left)
    left_max = max(x - s_left, x - t_left)
    right_min = min(s_right - x, t_right - x)
    right_max = max(s_right - x, t_right - x)
    print(min(left_max, right_max, 2 * (x - s_left) + (t_right - x), 2 * (t_right - x) + (x - s_left)))

Submission status: Wrong Answer

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

def jinja(x): #移動距離、神社の位置を出力
  if x <= S[0]:
    ret = [S[0]-x,0]
  elif x >= S[-1]:
    ret = [x-S[-1],A-1]
  else:
    ok = 0
    ng = A
    while abs(ok-ng) > 1:
      mid = (ok+ng)//2
      if S[mid] <= x:
        ok = mid
      else:
        ng = mid
    if x - S[ok] < S[ng] - x:
      ret = [x - S[ok],ok]
    elif x - S[ok] > S[ng] - x:
      ret = [S[ng] - x,ng]
    else:
      ret = [x - S[ok],ok,ng]
  return ret

def tera(x): #移動距離、神社の位置を出力
  if x <= T[0]:
    ret = [T[0]-x,0]
  elif x >= T[-1]:
    ret = [x-T[-1],B-1]
  else:
    ok = 0
    ng = B
    while abs(ok-ng) > 1:
      mid = (ok+ng)//2
      if T[mid] <= x:
        ok = mid
      else:
        ng = mid
    if x - T[ok] < T[ng] - x:
      ret = [x - T[ok],ok]
    elif x - T[ok] > T[ng] - x:
      ret = [T[ng] - x,ng]
    else:
      ret = [x - T[ok],ok,ng]
  return ret
      

#P = tera(100)
#print(P)
for i in range(Q):
  INPUT = int(input())
  j = jinja(INPUT)
  #print(j)
  if len(j) == 2:
    x = j[0]; loc = S[j[1]]
    jt = tera(loc)
    #print(x,loc,jt)
    x += jt[0]
  else:
    x = j[0]; loc1 = S[j[1]]; loc2 = S[j[2]]
    jt1 = tera(loc1); jt2 = tera(loc2)
    x += min(jt1[0],jt2[0])
  t = tera(INPUT)
  #print(t)
  if len(t) == 2:
    y = t[0]; loc = T[t[1]]
    tj = jinja(loc)
    #print(y,loc,tj)
    y += tj[0]
  else:
    y = t[0]; loc1 = T[t[1]]; loc2 = T[t[2]]
    tj1 = jinja(loc1); tj2 = jinja(loc2)
    y += min(tj1[0],tj2[0])
  #print(x,y)
  print(min(x,y))

Submission status: Wrong Answer

Solution code:
import heapq
A, B, Q = map(int, input().split())
S = list(int(input()) for _ in range(A)) 
T = list(int(input()) for _ in range(B))

D = []

j = 0
for i in range(A):
    s = S[i]
    if s < T[0]:
        D.append([s, T[0]-s])
    elif T[-1] < s:
        D.append([s, s-T[-1]])
    else:
        for k in range(j, B-2):
            if T[j] < s and s < T[j+1]:
                D.append([s, min(s-T[j], T[j+1]-s)])
                j = k
                break

j = 0
for i in range(B):
    t = T[i]
    if t < S[0]:
        D.append([t, S[0]-t])
    elif S[-1] < t:
        D.append([t, t-S[-1]])
    else:
        for k in range(j, A-2):
            if S[j] < t and t < S[j+1]:
                D.append([t, min(t-S[j], S[j+1]-t)])
                j = k
                break

D.sort()

j = 0
for _ in range(Q):
    x = int(input())
    if x < D[0][0]:
        print(D[0][0]-x+D[0][1])
    elif D[-1][0] < x:
        print(x-D[-1][0]+D[-1][1])
    else:
        ok = 0
        ng = A+B-1
        while ok+1 < ng:
            tmp = (ok+ng)//2
            if D[tmp][0] < x:
                ok = tmp
            else:
                ng = tmp
        print(min(x-D[ok][0]+D[ok][1], D[ng][0]-x+D[ng][1]))

Submission status: Wrong Answer

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

j = 0
for i in range(A):
    s = S[i]
    if s < T[0]:
        D.append([s, T[0]-s])
    elif T[-1] < s:
        D.append([s, s-T[-1]])
    else:
        for k in range(j, B-2):
            if T[j] < s and s < T[j+1]:
                D.append([s, min(s-T[j], T[j+1]-s)])
                j = k

j = 0
for i in range(B):
    t = T[i]
    if t < S[0]:
        D.append([t, S[0]-t])
    elif S[-1] < t:
        D.append([t, t-S[-1]])
    else:
        for k in range(j, B-2):
            if S[j] < t and t < S[j+1]:
                D.append([t, min(t-S[j], S[j+1]-t)])
                j = k

D.sort()

for _ in range(Q):
    x = int(input())
    if x < D[0][0]:
        print(D[0][0]-x+D[0][1])
    elif D[-1][0] < x:
        print(x-D[-1][0]+D[-1][1])
    else:
        ok = 0
        ng = A+B
        while ok+1 < ng:
            tmp = (ok+ng)//2
            if D[tmp][0] < x:
                ok = tmp
            else:
                ng = tmp
        print(min(x-D[ok][0]+D[ok][1], D[ng][0]-x+D[ng][1]))

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left
from math import ceil, floor


def read():
    return sys.stdin.readline().rstrip()


def nearest(arr, x):
    i = bisect_left(arr, x)
    if i == 0:
        return arr[0] - x, 0
    if i == len(arr):
        return x - arr[-1], len(arr) - 1
    a, b = arr[i] - x, x - arr[i - 1]
    if a == b:
        return a, i - 0.5
    if a < b:
        return a, i
    if a > b:
        return b, i - 1


def main():
    a, b, q = map(int, read().split())
    s = [int(read()) for _ in range(a)]
    t = [int(read()) for _ in range(b)]
    st = [nearest(t, si)[0] for si in s]
    ts = [nearest(s, ti)[0] for ti in t]
    for _ in range(q):
        x = int(read())
        xs, si = nearest(s, x)
        xt, ti = nearest(t, x)
        print(min(min(xs, xt) * 2 + max(xs, xt), xs + min(st[floor(si)], st[ceil(si)]), xt + min(ts[floor(ti)], ts[ceil(ti)])))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left
from math import ceil, floor


def add(arr, i, size):
    while i < size:
        arr[i] += 1
        i += i & -i


def read():
    return sys.stdin.readline().rstrip()


def nearest(arr, x):
    i = bisect_left(arr, x)
    if i == 0:
        return arr[0] - x, 0
    if i == len(arr):
        return x - arr[-1], len(arr) - 1
    a, b = arr[i] - x, x - arr[i - 1]
    if a == b:
        return a, i - 0.5
    if a < b:
        return a, i
    if a > b:
        return b, i - 1


def main():
    a, b, q = map(int, read().split())
    s = [int(read()) for _ in range(a)]
    t = [int(read()) for _ in range(b)]
    st = [nearest(t, si)[0] for si in s]
    ts = [nearest(s, ti)[0] for ti in t]
    for _ in range(q):
        x = int(read())
        xs, si = nearest(s, x)
        xt, ti = nearest(t, x)
        print(min(xs + min(st[floor(si)], st[ceil(si)]), xt + min(ts[floor(ti)], ts[ceil(ti)])))


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)]
X = [int(input()) for _ in range(Q)]

def D(a,b,c):
    return abs(a-b) + abs(b-c)

def getAns(x):
    ret = 10**9
    U = [S, T]
    for k,u in enumerate(U):
        v = U[(k-1)%2]
        idx = bisect.bisect_left(u, x)
        for i in [idx-1, idx]:
            if not 0<=i<len(u):
                continue
            idxx = bisect.bisect_left(v, u[i])
            for j in [idxx-1, idxx]:
                if 0<=j<len(v):
                    ret = min(ret, D(x, u[i], v[j]))
    return ret

for x in X:
    print(getAns(x))

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
S = [-10**18]+sorted([int(input()) for _ in range(A)])+[10**18]
T = [-10**18]+sorted([int(input()) for _ in range(B)])+[10**18]
import bisect
for _ in range(Q):
  x = int(input())
  i = bisect.bisect_left(S, x)
  j = bisect.bisect_left(T, x)
  p, q, r, s = S[i-1], S[i], T[j-1], T[j]
  res_1 = x-min(p, r)
  res_2 = max(q, s)-x
  res_3 = min(q, s)-max(p, r)+min(x-max(p, r), min(q, s)-x) 
  print(min(res_1, res_2, res_3))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

A, B, Q = mapint()
Ss = [-10**20]+[int(input()) for _ in range(A)]+[10**20]
Ts = [-10**20]+[int(input()) for _ in range(B)]+[10**20]
Ss.sort()
Ts.sort()
from bisect import bisect_left

for _ in range(Q):
    x = int(input())
    xs = bisect_left(Ss, x)
    xt = bisect_left(Ts, x)

    rr = max(Ss[xs]-x, Ts[xt])
    ll = max(x-Ss[xs-1], x-Ts[xt-1])
    lr = min(abs(x-Ss[xs-1])*2+abs(x-Ts[xt]), abs(x-Ts[xt-1])*2+abs(x-Ss[xs]))
    rl = min(abs(x-Ss[xs-1])+abs(x-Ts[xt])*2, abs(x-Ts[xt-1])+abs(x-Ss[xs])*2)
    print(min(rr, ll, rl, lr))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

A, B, Q = mapint()
Ss = [-10**20]+[int(input()) for _ in range(A)]+[10**20]
Ts = [-10**20]+[int(input()) for _ in range(B)]+[10**20]
Ss.sort()
Ts.sort()
from bisect import bisect_left

for _ in range(Q):
    x = int(input())
    xs = bisect_left(Ss, x)-1
    xt = bisect_left(Ts, x)-1

    rr = max(Ss[xs+1]-x, Ts[xt+1])
    ll = max(x-Ss[xs], x-Ts[xt])
    lr = min(abs(x-Ss[xs])*2+abs(x-Ts[xt+1]), abs(x-Ts[xt])*2+abs(x-Ss[xs+1]))
    rl = min(abs(x-Ss[xs])+abs(x-Ts[xt+1])*2, abs(x-Ts[xt])+abs(x-Ss[xs+1])*2)
    print(min(rr, ll, rl, lr))

Submission status: Wrong Answer

Solution code:
import bisect
a, b, q = map(int, input().split())
ss = [int(input()) for _ in range(a)]
ts = [int(input()) for _ in range(b)]
xs = [int(input()) for _ in range(q)]
ss = [-float('inf')] + ss + [float('inf')]
ts = [-float('inf')] + ts + [float('inf')]
for x in xs:
    s_r = bisect.bisect_left(ss, x)
    t_r = bisect.bisect_left(ts, x)
    s_l = s_r - 1
    t_l = t_r - 1

    res = float('inf')
    if s_l > t_l:
        res = min(res, x - ts[t_l], (x - ss[s_l])*2 + (ts[t_r] - x))
    elif s_l < t_l:
        res = min(res, x - ss[s_l], (x - ts[t_l])*2 + (ss[s_r] - x))
    else:
        res = min(res, (x - ss[s_l])*2 + (ss[s_r] - x), x - ss[s_l]*2 + (ts[t_r] - x))
    if s_r > t_r:
        res = min(res, ss[s_r] - x, (ts[t_r] - x)*2 + (x - ss[s_l]))
    elif s_r < t_r:
        res = min(res, ts[t_r] - x, (ss[s_r] - x)*2 + (x - ts[t_l]))
    else:
        res = min(res, (ss[s_r] - x)*2 + (x - ss[s_l]), (ss[s_r] - x)*2 + (x - ts[t_l]))
    print(res)

Submission status: Wrong Answer

Solution code:
import bisect
a, b, q = map(int, input().split())
ss = [int(input()) for _ in range(a)]
ts = [int(input()) for _ in range(b)]
xs = [int(input()) for _ in range(q)]
ss = [-float('inf')] + ss + [float('inf')]
ts = [-float('inf')] + ts + [float('inf')]
for x in xs:
    s_r = bisect.bisect_left(ss, x)
    t_r = bisect.bisect_left(ts, x)
    s_l = s_r - 1
    t_l = t_r - 1

    res = float('inf')
    if s_l > t_l:
        res = min(res, x - ts[t_l], (x - ss[s_l])*2 + (ts[t_r] - x))
    elif s_l < t_l:
        res = min(res, x - ss[s_l], (x - ts[t_l])*2 + (ss[s_r] - x))
    if s_r > t_r:
        res = min(res, ss[s_r] - x, (ts[t_r] - x)*2 + (x - ss[s_l]))
    elif s_r < t_r:
        res = min(res, ts[t_r] - x, (ss[s_r] - x)*2 + (x - ts[t_l]))
    print(res)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    a, b, q = map(int, input().split())
    S = [-f_inf] + list(int(input()) for _ in range(a)) + [f_inf]
    T = [-f_inf] + list(int(input()) for _ in range(b)) + [f_inf]

    for _ in range(q):
        res = f_inf
        x = int(input())

        idx_s = bisect_left(S, x)
        kouho_s = [idx_s - 1, idx_s]
        for idx_s in kouho_s:
            now = S[idx_s]
            dist = abs(x - now)
            idx_t = bisect_left(T, now)
            kouho_t = [idx_t - 1, idx_t]
            for idx_t in kouho_t:
                dist += abs(now - T[idx_t])
                res = min(res, dist)

        idx_t = bisect_left(T, x)
        kouho_t = [idx_t - 1, idx_t]
        for idx_t in kouho_t:
            now = T[idx_t]
            dist = abs(x - now)
            idx_s = bisect_left(S, now)
            kouho_s = [idx_s - 1, idx_s]
            for idx_s in kouho_s:
                dist += abs(now - S[idx_s])
                res = min(res, dist)
        print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    a, b, q = map(int, input().split())
    S = list(int(input()) for _ in range(a))
    T = list(int(input()) for _ in range(b))

    for _ in range(q):
        res = f_inf
        x = int(input())

        idx_s = bisect_left(S, x)
        kouho_s = [idx_s - 1, idx_s]
        for idx_s in kouho_s:
            if idx_s == -1 or idx_s == a:
                continue
            now = S[idx_s]
            dist = abs(x - now)
            idx_t = bisect_left(T, now)
            kouho_t = [idx_t - 1, idx_t]
            for idx_t in kouho_t:
                if idx_t == -1 or idx_t == b:
                    continue
                dist += abs(now - T[idx_t])
                res = min(res, dist)

        idx_t = bisect_left(T, x)
        kouho_t = [idx_t - 1, idx_t]
        for idx_t in kouho_t:
            if idx_t == -1 or idx_t == b:
                continue
            now = T[idx_t]
            dist = abs(x - now)
            idx_s = bisect_left(S, now)
            kouho_s = [idx_s - 1, idx_s]
            for idx_s in kouho_s:
                if idx_s == -1 or idx_s == a:
                    continue
                dist += abs(now - S[idx_s])
                res = min(res, dist)
        print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    a, b, q = map(int, input().split())
    S = list(int(input()) for _ in range(a))
    T = list(int(input()) for _ in range(b))

    for _ in range(q):
        x = int(input())
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)
        kouho_s = []
        if idx_s != 0:
            kouho_s.append(idx_s - 1)
        if idx_s != a:
            kouho_s.append(idx_s)
        kouho_t = []
        if idx_t != 0:
            kouho_t.append(idx_t - 1)
        if idx_t != b:
            kouho_t.append(idx_t)

        res = f_inf
        for idx_s in kouho_s:
            dist = abs(x - S[idx_s])
            now = S[idx_s]
            for idx_t in kouho_t:
                dist += abs(now - T[idx_t])
                res = min(res, dist)
        for idx_t in kouho_t:
            dist = abs(x - T[idx_t])
            now = T[idx_t]
            for idx_s in kouho_s:
                dist += abs(now - S[idx_s])
                res = min(res, dist)
        print(res)

if __name__ == '__main__':
    resolve()