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: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import bisect

inf = float('inf')

def solve(A: int, B: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"):
    s.sort()
    t.sort()
    for q in x:
        i_s = bisect.bisect(s, q)
        i_t = bisect.bisect(t, q)
        s_l = inf
        if i_s > 0:
            s_l = q - s[i_s-1]
        s_r = inf
        if i_s < A:
            s_r = s[i_s] - q
        t_l = inf
        if i_t > 0:
            t_l = q - t[i_t-1]
        t_r = inf
        if i_t < A:
            t_r = t[i_t] - q
        # 同じ方向に行く場合
        sltl = max([s_l, t_l])
        srtr = max([s_r, t_r])

        # 違う方向に行く場合
        sltr = min([s_l, t_r]) * 2 + max([s_l, t_r])
        srtl = min([s_r, t_l]) * 2 + max([s_r, t_l])

        min_d = min([sltl, srtr, sltr, srtl])

        print(min_d)
        
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    s = [ int(next(tokens)) for _ in range(A) ]  # type: "List[int]"
    t = [ int(next(tokens)) for _ in range(B) ]  # type: "List[int]"
    x = [ int(next(tokens)) for _ in range(Q) ]  # type: "List[int]"
    solve(A, B, Q, s, t, x)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
a, b, q = [int(i) for i in input().split()]
s = [-10000000000]
t = [-10000000000]
x = []
for i in range(a):
  s.append(int(input()))
for i in range(b):
  t.append(int(input()))
for i in range(q):
  x.append(int(input()))

  
  
label_j = [0] * 10000
label_t = [0] * 10000
for i in range(a-1):
  label_j[s[i]:s[i+1]] = [i]*(s[i+1] - s[i])
label_j[s[a]:] = [a] * (10000-s[a])
for i in range(b-1):
  label_t[t[i]:t[i+1]] = [i]*(t[i+1] - t[i])
label_t[t[b]:] = [b] * (10000-t[b])  
for i in range(q):
  left_j = s[label_j[x[i]]]
  if x[i] > s[a]:
    right_j = 20000000000
  else:
    right_j = s[label_j[x[i]]+1]
  left_t = t[label_t[x[i]]]
  if x[i] > t[b]:
    right_t = 20000000000
  else:
    right_t = t[label_t[x[i]]+1]
  print(left_j,right_j,left_t,right_t)
  ans = min(max(right_j - x[i], right_t -x[i]),max(x[i] - left_j, x[i] - left_t),
            min(right_j - x[i], right_t -x[i])*2 + min(x[i] - left_j, x[i] - left_t),
            min(right_j - x[i], right_t -x[i]) + min(x[i] - left_j, x[i] - left_t)*2)
  print(ans)

Submission status: Runtime Error

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_left(s, x), bisect.bisect_left(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: Runtime Error

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

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

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

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

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
        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
        else:
          pass

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

Submission status: Runtime Error

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
    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
    else:
      pass

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

Submission status: Runtime Error

Solution code:
a, b, q = map(int, input().split())
sno = []
tno = []
dis = []
 
for i in range(a):
  sno.append(int(input()))
sno.append(10000)
 
for i in range(b):
  tno.append(int(input()))
tno.append(10000)
 
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]:
      sl = dis[i] - sno[j]
      sr = sno[j+1] - dis[i]
      break
    else:
      pass
                
  for k in range(b):
    if tno[k] <= dis[i]:
      tl = dis[i] - tno[k]
      tr = tno[k+1] - dis[i]
      break
    else:
      pass
  
  lds = max(sl, tl)
  rds = max(sr, tr)
  dis = min(lds, rds)
  print(dis)

Submission status: Runtime Error

Solution code:
a, b, q = map(int, input().split())
sno = []
tno = []
dis = []
 
for i in range(a):
  sno.append(int(input()))
sno.append(10000)
 
for i in range(b):
  tno.append(int(input()))
tno.append(10000)

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]:
      sl = dis[i] - sno[j]
      sr = sno[j+1] - dis[i]
      break
    else:
      pass
                
  for k in range(b):
    if tno[k] <= dis[i]:
      tl = dis[i] - tno[k]
      tr = tno[k+1] - dis[i]
      break
    else:
      pass
  
  lds = max(sl, tl)
  rds = max(sr, tr)
  dis = min(lds, rds)
  print(dis)

Submission status: Runtime Error

Solution code:
a, b, q = map(int, input().split())
sno = []
tno = []
dis = []
 
for i in range(a):
  sno.append(int(input()))
sno.append(10000)
 
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]:
      sl = dis[i] - sno[j]
      sr = sno[j+1] - dis[i]
      break
    else:
      pass
                
  for k in range(b):
    if sno[k] <= dis[i]:
      tl = dis[i] - sno[j]
      tr = sno[k+1] - dis[i]
      break
    else:
      pass
  
  lds = max(sl, tl)
  rds = max(sr, tr)
  dis = min(lds, rds)
  print(dis)

Submission status: Runtime Error

Solution code:
sa, sb, sq = input().strip().split()
a = int(sa)
b = int(sb)
q = int(sq)
sno = []
tno = []
dis = []
 
for i in range(a):
  sno.append(int(input()))
sno.append(10000)
 
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]:
      sl = dis[i] - sno[j]
      sr = sno[j+1] - dis[i]
      break
    else:
      pass
                
  for k in range(b):
    if sno[k] <= dis[i]:
      tl = dis[i] - sno[j]
      tr = sno[k+1] - dis[i]
      break
    else:
      pass
  
  lds = max(sl, tl)
  rds = max(sr, tr)
  dis = min(lds, rds)
  print(dis)

Submission status: Runtime Error

Solution code:
a, b, q = (int(X) for x in input().split())
sno = []
tno = []
dis = []

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

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]:
      sl = dis[i] - sno[j]
      sr = sno[j+1] - dis[i]
      break
    else:
      pass
                
  for k in range(b):
    if sno[k] <= dis[i]:
      tl = dis[i] - sno[j]
      tr = sno[k+1] - dis[i]
      break
    else:
      pass
  
  lds = max(sl, tl)
  rds = max(sr, tr)
  dis = min(lds, rds)
  print(dis)

Submission status: Runtime Error

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)]
INF = 10 ** 9


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
    return l


s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i, x_i in enumerate(x):
    sa = bsearch(x_i, s)
    ta = bsearch(x_i, t)
    lmin = INF
    for si in range(sa, sa + 2):
        for ti in range(ta, ta + 2):
            l = min(abs(x_i - s[si]) + abs(s[si] - t[ti]), abs(x_i - t[ti]) + abs(t[ti] - s[si]))
            lmin = min(l, lmin)
    print(lmin)

Submission status: Runtime Error

Solution code:
    from bisect import bisect
    A,B,Q=map(int, input().split())
    s=[int(input()) for _ in range(A)] + [99**99]
    t=[int(input()) for _ in range(B)] + [99**99]
    snt=[0]*A + [99**99]
    tns=[0]*B + [99**99]
    for i in range(A):
    	p = bisect(t, s[i])
    	snt[i] = min(abs(s[i] - t[p-1]), abs(s[i] - t[p]))
    for i in range(B):
    	p = bisect(s, t[i])
    	tns[i] = min(abs(t[i] - s[p-1]), abs(t[i] - s[p]))
    for _ in range(Q):
    	x = int(input())
    	ans = []
    	p = bisect(s, x)
    	ans.append(abs(s[p] - x) + snt[p])
    	ans.append(abs(s[p-1] - x) + snt[p-1])
    	p = bisect(t, x)
    	ans.append(abs(t[p] - x) + tns[p])
    	ans.append(abs(t[p-1] - x) + tns[p-1])
    	print(min(ans))

Submission status: Runtime Error

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
    elif x < s:
      ceil = half
    else:
      floor = half
      ceil = half+1
  nearest_x_S = [S[floor]-x, S[ceil]-x]
        
  floor = 0
  ceil = len(S)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    t = T[half]
    if t < x:
      floor = half
    elif x < t:
      ceil = half
    else:
      floor = half
      ceil = half+1
  nearest_x_T = [T[floor]-x, T[ceil]-x]
  
  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: Runtime Error

Solution code:
import numpy as np

A,B,Q = map(int, input().split())
S=np.zeros([A])
T=np.zeros([B])
X=np.zeros([Q])
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
if A ==1:
  S = np.concatenate([S,[float('inf')]],axis=0)
if B ==1:
  T = np.concatenate([T,[float('inf')]],axis=0)
for i in range(Q):
  x = int(input())
  dist_x_S = S - x
  dist_x_S_abs = np.abs(dist_x_S)
  min_index = np.argmin(dist_x_S_abs)
  if dist_x_S[min_index] > 0:
    nearest_x_S = [dist_x_S[min_index-1], dist_x_S[min_index]]
  elif dist_x_S[min_index] < 0:
    nearest_x_S = [dist_x_S[min_index], dist_x_S[min_index+1]]
  else:
    nearest_x_S = [0, dist_x_S[np.argmin([np.abs(dist_x_S[min_index-1]),np.argmin(dist_x_S[min_index+1])])]]
  
  dist_x_T = T - x
  dist_x_T_abs = np.abs(dist_x_T)
  min_index = np.argmin(dist_x_T_abs)
  if dist_x_T[min_index] > 0:
    nearest_x_T = [dist_x_T[min_index-1], dist_x_T[min_index]]
  elif dist_x_T[min_index] < 0:
    nearest_x_T = [dist_x_T[min_index], dist_x_T[min_index+1]]
  else:
    nearest_x_T = [0, dist_x_T[np.argmin([np.abs(dist_x_T[min_index-1]),np.argmin(dist_x_T[min_index+1])])]]  
  
  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: Runtime Error

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"))
s.sort()
t.sort()
for i in range(q):
    x=int(input())
    ans=float("inf")
    tmp_s_1=bisect.bisect_left(s,x)-1
    tmp_s_2=tmp_s_1+1
    tmp_s_1_t=bisect.bisect_left(t,s[tmp_s_1])
    tmp_s_2_t=bisect.bisect_left(t,s[tmp_s_2])
    ans=min(abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t-1]), abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t-1]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t]))
    tmp_t_1=bisect.bisect_left(t,x)-1
    tmp_t_2=tmp_t_1+1
    tmp_t_1_s=bisect.bisect_left(s,t[tmp_t_1])
    tmp_t_2_s=bisect.bisect_left(s,t[tmp_t_2])
    ans=min(ans, abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s-1]), abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s-1]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s]))
    print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left as bl
from collections import deque
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy

A,B,Q=map(int,input().split())
S=[]
T=[]
X=[]
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
def rl(X,a):
    ind=bl(X,a)
    if ind>=len(X):
        return ind-1
    return ind
def ll(X,a):
    ind=bl(X,a)-1
    if ind<0:
        return ind+1
    return ind
t=[]
for i in range(A):
    if abs(T[rl(T,S[i])]-S[i])>=abs(T[ll(T,S[i])]-S[i]):
        t.append(abs(T[ll(T,S[i])]-S[i]))
    else:
        t.apend(abs(T[rl(T,S[i])])-S[i])
s=[]
for i in range(B):
    if abs(S[rl(S,T[i])]-T[i])>=abs(S[ll(S,T[i])]-T[i]):
        s.append(abs(S[ll(S,T[i])]-T[i]))
    else:
        s.append(abs(S[rl(S,T[i])])-T[i])

for i in range(Q):
    x=int(input())
    a=rl(S,x)
    a=t[a]+abs(S[a]-x)

    b=ll(S,x)
    b=t[b]+abs(S[b]-x)

    c=rl(T,x)
    c=s[c]+abs(T[c]-x)

    d=ll(T,x)
    d=s[d]+abs(T[d]-x)

    print(min(a,b,c,d))

Submission status: Runtime Error

Solution code:
from bisect import bisect_left as bl
from collections import deque
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy

A,B,Q=map(int,input().split())
S=[]
T=[]
X=[]
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
def rl(X,a):
    ind=bl(X,a)
    if ind>=len(X):
        return ind-1
    return ind
def ll(X,a):
    ind=bl(X,a)-1
    if ind<0:
        return ind+1
    return ind
t=[]
for i in range(A):
    if abs(T[rl(T,S[i])]-S[i])>=abs(T[ll(T,S[i])]-S[i]):
        t.append(abs(T[ll(T,S[i])]-S[i]))
    else:
        t.apend(abs(T[rl(T,S[i])])-S[i])
s=[]
for i in range(B):
    if abs(S[rl(S,T[i])]-T[i])>=abs(S[ll(S,T[i])]-T[i]):
        s.append(abs(S[ll(S,T[i])]-T[i]))
    else:
        s.apend(abs(S[rl(S,T[i])])-T[i])

for i in range(Q):
    x=int(input())
    a=rl(S,x)
    a=t[a]+abs(S[a]-x)

    b=ll(S,x)
    b=t[b]+abs(S[b]-x)

    c=rl(T,x)
    c=s[c]+abs(T[c]-x)

    d=ll(T,x)
    d=s[d]+abs(T[d]-x)

    print(min(a,b,c,d))

Submission status: Runtime Error

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

import sys
INT_MAX = sys.maxsize
s.insert(0, -1 * INT_MAX)
s.append(INT_MAX)
t.insert(0, -1 * INT_MAX)
t.append(INT_MAX)

import bisect
for xi in x:
    #print(xi)
    si = bisect.bisect_right(s, xi)
    #si = 0
    #for si, v in enumerate(s):
    #    if v > xi:
    #        break
    ti = bisect.bisect_right(s, xi)
    #ti = 0
    #for ti, v in enumerate(t):
    #    if v > xi:
    #        break
    #print(si, ti)
    sa = s[si]
    ta = t[ti]
    sb = s[si-1]
    tb = t[ti-1]
    #print(sa, sb,ta,tb)
    ans = min([
        abs(xi-sa)+abs(sa-ta),
        abs(xi-sa)+abs(sa-tb),
        abs(xi-sb)+abs(sb-ta),
        abs(xi-sb)+abs(sb-tb),
        abs(xi-ta)+abs(ta-sa),
        abs(xi-ta)+abs(ta-sb),
        abs(xi-tb)+abs(tb-sa),
        abs(xi-tb)+abs(tb-sb)
    ])
    print(ans)

Submission status: Runtime Error

Solution code:
import bisect
import sys
input = sys.stdin.readline
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]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans.append(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     min(x - min(s[idxs - 1], t[idxt -1]))))
for i in range(q):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import bisect
import sys
input = sys.stdin.readline
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]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans.append(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     min(x - min(s[idxs - 1], t[idxt -1]))
for i in range(q):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import bisect
import sys
A, B, Q = map(int,input().split())

S = [-10**18] + list(map(int, (sys.stdin.readline() for _ in [0]*A))) + [10**18]
T = [-10**18] + list(map(int, (sys.stdin.readline() for _ in [0]*A))) + [10**18]


for _ in range(Q):
    x = int(input())
    ans = 10**18
    for i in range(2):
        if i == 0:
            a = S
            b = T
        elif i== 1:
            a = T
            b = S

        idx_x = bisect.bisect_left(a,x)
        for j in range(2):
            ax = a[idx_x-j]
            idx_a = bisect.bisect_left(b,ax)
            for k in range(2):
                bx = b[idx_a-k]
                ans = min(ans, abs(ax-x)+abs(bx-ax))
    print(ans)

Submission status: Runtime Error

Solution code:
def search(x,r):
  low = 0
  high = len(r)-1
  mid = (low+high)//2
  while low<=high:
    rr = r[mid]-x
    ll = r[mid+1]-x
    if rr*ll<=0:
      return(abs(rr),abs(ll))
    elif r[mid]-x > 0:
      high = mid
      mid = (low+high)//2
    else:
      low = mid
      mid = (low+high)//2
      

a, b, q = [int(i) for i in input().split()]
s = []
for i in range(a):
	s.append(int(input()))
t = []
for i in range(b):
	t.append(int(input()))
x = []
for i in range(q):
	x.append(int(input()))

for xx in x:

  s_l = float('inf')
  s_r = float('inf')
  if s[-1] - xx <= 0:
    s_l = abs(s[-1] - xx)
  elif s[0] - xx >= 0:
    s_r = abs(s[0] - xx)
  else:
  	s_l,s_r=search(xx,s)
  
  t_l = float('inf')
  t_r = float('inf')
  if t[-1] - xx <= 0:
    t_l = abs(t[-1] - xx)
  elif t[0] - xx >= 0:
    t_r = abs(t[0] - xx)
  else:
  	t_l,t_r=search(xx,t)
  
  #print(s_l,s_r,t_l,t_r)
  
  #l = max(s_l,t_l)
  #r = max(s_r,t_r)
  if s_l <= t_l:
    l = t_l
    lr = s_l*2 + t_r
  else:
    r = s_l
    lr = t_l*2 + s_r
  if s_r <= t_r:
    r = t_r
    rl = s_r*2 + t_l
  else:
    r = s_r
    rl = t_r*2 + s_l
 
  ans = min(l,r,lr,rl)
  print(ans)

Submission status: Runtime Error

Solution code:
def search(x,r):
  low = 0
  high = len(r)-1
  mid = (low+high)//2
  while low<=high:
    rr = r[mid]-x
    ll = r[mid+1]-x
    if rr*ll<=0:
      return(abs(rr),abs(ll))
    elif r[mid]-x > 0:
      high = mid
      mid = (low+high)//2
    else:
      low = mid
      mid = (low+high)//2
      

a, b, q = [int(i) for i in input().split()]
s = []
for i in range(a):
	s.append(int(input()))
t = []
for i in range(b):
	t.append(int(input()))
x = []
for i in range(q):
	x.append(int(input()))

for xx in x:

  s_l = float('inf')
  s_r = float('inf')
  if s[-1] - xx <= 0:
    s_l = abs(s[-1] - xx)
  elif s[0] - xx >= 0:
    s_r = abs(s[0] - xx)
  else:
  	s_l,s_r=search(xx,s)
  
  t_l = float('inf')
  t_r = float('inf')
  if t[-1] - xx <= 0:
    t_l = abs(t[-1] - xx)
  elif t[0] - xx >= 0:
    t_r = abs(t[0] - xx)
  else:
  	t_l,t_r=search(xx,t)
  
  #print(s_l,s_r,t_l,t_r)
  
  #l = max(s_l,t_l)
  #r = max(s_r,t_r)
  if s_l <= t_l:
    l = t_l
    lr = s_l*2 + t_r
  else:
    r = s_l
    lr = t_l*2 + s_r
  if s_r <= t_r:
    r = t_r
    rl = s_r*2 + t_l
  else:
    r = s_r
    lr = t_r*2 + s_l
 
  ans = min(l,r,lr,rl)
  print(ans)

Submission status: Runtime Error

Solution code:
import bisect


def solve(a_list, b_list, position):
    # time O(log(A))
    a_right_key = bisect.bisect_left(a_list, position)
    a_left_key = a_right_key - 1
    # time O(log(B))
    b_right_key = bisect.bisect_left(b_list, position)
    b_left_key = b_right_key - 1

    a_left, a_right = a_list[a_left_key], a_list[a_right_key]
    b_left, b_right = b_list[b_left_key], b_list[b_right_key]

    ans = 10 ** 20
    for a_position in [a_left, a_right]:
        for b_position in [b_left, b_right]:
            distance1 = abs(position - a_position) + abs(a_position - b_position)
            distance2 = abs(position - b_position) + abs(a_position - b_position)
            ans = min(ans, distance1, distance2)
    return ans


def main():
    inf = 10 ** 20
    A, B, Q = map(int, input().split())

    # time O(A)
    a_list = [int(input()) for _ in range(A)]
    a_list = [-inf] + a_list + [inf]
    # time O(B)
    b_list = [int(input()) for _ in range(A)]
    b_list = [-inf] + b_list + [inf]

    # time O(Q (log(A) + log(B))
    for _ in range(Q):
        position = int(input())
        ans = solve(a_list, b_list, position)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
read = input
read = Read(False, """
1 1 3
1
10000000000
2
9999999999
5000000000
""").read
A,B,Q = list(map(int, read().split(' ')))
arrA = list(map(int, [read() for i in range(A)]))
arrB = list(map(int, [read() for i in range(B)]))
arrQ = list(map(int, [read() for i in range(Q)]))
arrA.sort()
arrB.sort()
import bisect
def cs(x, arr):
    al = bisect.bisect_left(arr, x)
    return list(map(lambda i: arr[i], filter(lambda i:i>=0 and i< len(arr), range(al-1, al + 2))))
def same_s(da,db):
    da = da / abs(da)
    db = db / abs(db)
    return da*db>0
    
def t(q,a,b):
    da = q - a
    db = q - b
    if(min(abs(da), abs(db)) == 0):
        return max(abs(da), abs(db))
    if same_s(da, db):
        return max(abs(da), abs(db))
    else :
        return 2*min(abs(da), abs(db)) + max(abs(da), abs(db))
for q in arrQ:
    optA = cs(q, arrA)
    optB = cs(q, arrB)
    ret = 10 ** 11
    for a in optA:
        for b in optB:
            ret = min(ret, t(q, a,b))
    print(ret)

Submission status: Runtime Error

Solution code:
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
INF = 10**20

A,B,Q = map(int,input().split())
s = [-INF]+[int(input()) for _ in range(A)]+[INF]
t = [-INF]+[int(input()) for _ in range(B)]+[INF]

for i in range(Q):
    q = int(input())
    b,d = bisect.bisect_right(s,q),bisect.bisect_right(t,q)
    ans = INF
    for S in [s[b-1],s[b]]:
        for T in [t[d-1],t[d]]:
            d1,d2 = abs(S-q)+abs(T-S),abs(T-q)+abs(S-T)
            ans = min(ans,d1,d2)
    print(ans)

Submission status: Runtime Error

Solution code:
abc119d
import bisect

a,b,q=map(int,raw_input().split())
inf=2<<60
s=[-inf]+[int(raw_input()) for i in xrange(a)]+[inf]
t=[-inf]+[int(raw_input()) for i in xrange(b)]+[inf]
for i in xrange(q):
 x=int(raw_input())
 b=bisect.bisect_right(s,x)
 d=bisect.bisect_right(t,x)
 res=inf
 for ss in [s[b-1],s[b]]:
  for tt in [t[d-1],t[d]]:
   d1=abs(ss-x)+abs(tt-ss)
   d2=abs(tt-x)+abs(ss-tt)
   res=min(res,d1,d2)
 print res

Submission status: Runtime Error

Solution code:
A,B,q = map(int,input().split())
import bisect
L1=[]
L2=[]
for _ in range(A):
    L1.append(int(input()))
for _ in range(B):
    L2.append(int(input()))
L1 = [-10**9] + L1 + [10**9]
L2 = [-10**9] + L2 + [10**9]
for _ in range(q):
    Q = int(input())
    cand = []
    for i in range(2):
        for j in range(2):
                t =  L1[bisect.bisect_left(L1,Q)-i]
                tmp = abs (t - Q)
                tmp  +=  abs(L2[bisect.bisect_left(L2,t)-j]  - t)
                cand.append(tmp)
                t =  L2[bisect.bisect_left(L2,Q)-i]
                tmp = abs (t - Q)
                tmp  +=  abs(L1[bisect.bisect_left(L1,t)-j]  - t)
                cand.append(tmp)
    print(min(cand))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
from bisect import bisect_left, bisect_right
import itertools

A,B,Q=map(int, sys.stdin.readline().split())

S=[  int(sys.stdin.readline()) for i in range(A) ]
T=[  int(sys.stdin.readline()) for i in range(B) ]
X=[  int(sys.stdin.readline()) for i in range(Q) ]


for x in X:
	sx=bisect_left(S, x)
	tx=bisect_left(T, x)

	s_temp=[]
	if sx==0:
		s_temp.append(S[sx])	
	elif 0<sx<A:
		s_temp.append(S[sx-1])
		s_temp.append(S[sx])
	else:
		s_temp.append(S[sx-1] )


	t_temp=[]
	if tx==0:
		t_temp.append(T[tx])	
	elif 0<tx<A:
		t_temp.append(T[tx-1])
		t_temp.append(T[tx])
	else:
		t_temp.append(T[tx-1] )


	ans=float("inf")

	for s in s_temp:
		for t in t_temp:
			ans=min(ans, abs(s-x)+abs(s-t) )

	for t in t_temp:
		for s in s_temp:	
			ans=min(ans, abs(t-x)+abs(s-t) )

	print ans

Submission status: Runtime Error

Solution code:
A,B,Q=map(int,input().split())
s=[int(input()) for i in range(A)]
t=[int(input()) for i in range(B)]
x=[int(input()) for i in range(Q)]
st={}
ts={}
k=0
for i in range(A):
    for j in range(k,B):
        if s[i]<t[0]:
            st[s[i]]=abs(s[i]-t[0])
            break
        if t[B-1]<s[i]:
            st[s[i]]=abs(s[i]-t[B-1])
            break
        if t[j]<s[i] and s[i]<t[j+1]:
            st[s[i]]=min(abs(s[i]-t[j]),abs(s[i]-t[j+1]))
            k=j
            break
k=0
for i in range(B):
    for j in range(k,A):
        if t[i]<s[0]:
            ts[t[i]]=abs(t[i]-s[0])
            break
        if s[A-1]<t[i]:
            ts[t[i]]=abs(t[i]-s[A-1])
            break
        if s[j]<t[i] and t[i]<s[j+1]:
            ts[t[i]]=min(abs(t[i]-s[j]),abs(t[i]-s[j+1]))
            k=j
            break
l=s+t
l.sort()
st.update(ts)
k=0
for i in range(Q):
    for j in range(k,A+B):
        if x[i]<l[0]:
            print(l[0]-x[j]+st[l[0]])
            break
        if l[A+B-1]<x[i]:
            print(x[i]-l[A+B-1]+st[l[A+B-1]])
            break
        if l[j]<x[i] and x[i]<l[j+1]:
            print(min((x[i]-l[j]+st[l[j]]),(l[j+1]-x[i]+st[l[j+1]])))
            k=j
            break

Submission status: Runtime Error

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)