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: Time Limit Exceeded

Solution code:
a,b,q = map(int,input().split())
inf = 10 ** 18
s = [-inf]
for _ in range(a):
  s.append(int(input()))
s.append(inf)
t = [-inf]
for _ in range(b):
  t.append(int(input()))
t.append(inf)
import bisect
st = [inf]
for i in range(1,a+1):
  here = bisect.bisect_left(t,s[i])
  st.append(min(abs(t[here-1]-s[i]),abs(t[here]-s[i])))
st.append(inf)
ts = [inf]
for j in range(1,b+1):
  here = bisect.bisect_left(s,t[j])
  ts.append(min(abs(s[here-1]-t[j]),abs(s[here]-t[j])))
ts.append(inf)
for _ in range(q):
  now = int(input())
  shere = bisect.bisect_left(s,now)
  there = bisect.bisect_left(t,now)
  z1 = abs(now-s[shere-1]) + st[shere-1]
  z2 = abs(now-s[shere  ]) + st[shere  ]
  z3 = abs(now-t[there-1]) + ts[there-1]
  z4 = abs(now-t[there  ]) + ts[there  ]
  print(min(z1,z2,z3,z4))

Submission status: Time Limit Exceeded

Solution code:
I = lambda n: [int(input()) for _ in range(n)]
f = lambda S, x: min(S, key = lambda s: abs(x-s))
g = lambda x, s, t: abs(x-s)+abs(s-t)
a, b, q = map(int, input().split())
S, T, X = I(a), I(b), I(q)
for x in X:
    s, t = f(S, x), f(T, x)
    print(min(g(x, s, f(T, s)), g(x, t, f(S, t))))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

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

sentinel = lambda x: [-9e20] + x + [9e20]
s = sentinel(s)
t = sentinel(t)

for i in range(q):
    x = int(input())
    ss = bisect(s, x)
    tt = bisect(t, x)

    ll = max(s[ss], t[tt]) - x
    rr = x - min(s[ss-1], t[tt-1])
    rl = min((s[ss]- x) + s[ss] - t[tt-1], (t[tt]- x) + t[tt] - s[ss-1])
    lr = min((x - s[ss-1]) + t[tt] - s[ss-1], (x - t[tt-1]) + s[ss] - t[tt-1])

    print(min(ll, rr, rl, lr))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

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

sentinel = lambda x: [-9e20] + x + [9e20]
s = sentinel(s)
t = sentinel(t)

for i in x:
    ss = bisect(s, i)
    tt = bisect(t, i)

    ll = max(s[ss], t[tt]) - i
    rr = i - min(s[ss-1], t[tt-1])
    rl = min((s[ss]- i) + s[ss] - t[tt-1], (t[tt]- i) + t[tt] - s[ss-1])
    lr = min((i - s[ss-1]) + t[tt] - s[ss-1], (i - t[tt-1]) + s[ss] - t[tt-1])

    print(min(ll, rr, rl, lr))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

#n = int(input())
#n,a = [int(i) for i in readline().split()]
a,b,q = [int(i) for i in readline().split()]
s = [-10**12]+[int(input()) for i in range(a)]+[10**12]
t = [-10**12]+[int(input()) for i in range(b)]+[10**12]

xx = [int(input()) for i in range(q)]

from bisect import *
for x in xx:
    ans = 10**11
    i = bisect_left(s,x)
    j = bisect_left(t,x)
    d1 = max(s[i],t[j])-x
    d2 = x- min(s[i-1],t[j-1])

    d3 = (x-s[i-1]) + (t[j]-x) + min((x-s[i-1]), (t[j]-x))
    d4 = (x-t[j-1]) + (s[i]-x) + min((x-t[j-1]), (s[i]-x))

#    print(ans,d1,d2,d3,d4)
    ans = min(ans,d1,d2,d3,d4)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
A,B,Q = map(int,input().split())
S = [-float('inf')]+list(int(input())for i in range(A))+[float('inf')]
T = [-float('inf')]+list(int(input())for i in range(B))+[float('inf')]
x = list(int(input())for i in range(Q))

for i in x:
    si = bisect.bisect_left(S,i)
    ti = bisect.bisect_left(T,i)
    ans = min(max(S[si],T[ti])-i,i-min(S[si-1],T[ti-1]),\
            2*S[si]-i-T[ti-1],2*T[ti]-i-S[si-1],\
             T[ti]+i-2*S[si-1],S[si]+i-2*T[ti-1])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

A,B,Q = map(int,input().split())

s = [-1 * float("inf")]
t = [-1 * float("inf")]

for i in range(A):

    S = int(input())
    s.append(S)

for i in range(B):

    T = int(input())
    t.append(T)

s.append(float("inf"))
t.append(float("inf"))

for i in range(Q):

    x = int(input())

    si = bisect.bisect_left(s,x)
    ti = bisect.bisect_left(t,x)

    now = min( max(s[si],t[ti]) - x  ,  x - min(s[si-1],t[ti-1]) )
    now = min( now , 2 * min( s[si] - x , x - t[ti-1] ) + max(s[si] - x , x - t[ti-1]))
    now = min( now , 2 * min( t[ti] - x , x - s[si-1] ) + max(t[ti] - x , x - s[si-1]))

    print (now)

Submission status: Time Limit Exceeded

Solution code:
a,b,q=map(int,input().split())
s=[]
for _ in range(a):
    s.append(int(input()))
t=[]
for _ in range(b):
    t.append(int(input()))
import bisect
for _ in range(q):
    x=int(input())
    sb=bisect.bisect_left(s,x)
    tb=bisect.bisect_left(t,x)
    ss=[x-s[max(sb-1,0)],x-s[min(sb,a-1)]]
    tt=[x-t[max(tb-1,0)],x-t[min(tb,b-1)]]
    l=[]
    for i in ss:
        for j in tt:
            if i*j>=0:
                l.append(max(abs(i),abs(j)))
            else:
                l.append(max(abs(i),abs(j))+2*min(abs(i),abs(j)))
    print(min(l))

Submission status: Time Limit Exceeded

Solution code:
a,b,q=map(int,input().split())
s=sorted([int(input()) for _ in range(a)])
t=sorted([int(input()) for _ in range(b)])
import bisect
for _ in range(q):
    x=int(input())
    ss=[x-s[max(bisect.bisect_left(s,x)-1,0)],x-s[min(bisect.bisect_right(s,x),a-1)]]
    tt=[x-t[max(bisect.bisect_left(t,x)-1,0)],x-t[min(bisect.bisect_right(t,x),b-1)]]
    l=[]
    for i in ss:
        for j in tt:
            if i*j>=0:
                l.append(max(abs(i),abs(j)))
            else:
                l.append(max(abs(i),abs(j))+2*min(abs(i),abs(j)))
    print(min(l))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
  q = int(input())
  temp = inf
  i = bl(Sh, q)
  que = []
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Te, Sh[j])
    temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q))
    if k != 0:
      temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q))
  i = bl(Te, q)
  if abs(Te[i] - q) < temp:
    que.append(i)
  if abs(Te[i - 1] - q) < temp:
    que.append(i)
    if i != 0:
      que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Sh, Te[j])
    temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q))
    if k != 0:
      temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q))
  print(temp)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [int(input()) for _ in range(A)] + [inf, inf]
Te = [int(input()) for _ in range(B)] + [inf, inf]

for _ in range(Q):
  q = int(input())
  temp = inf
  i = bl(Sh, q)
  #print(q, Sh[i - 1])
  que = []
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Te, Sh[j])
    #print(Sh[j], Te[k])
    temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q))
    if k != 0:
      temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q))
    
  i = bl(Te, q)
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Sh, Te[j])
    temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q))
    if k != 0:
      temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q))
  print(temp)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as bl
from collections import deque
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [int(input()) for _ in range(A)] + [inf, inf]
Te = [int(input()) for _ in range(B)] + [inf, inf]

for _ in range(Q):
  q = int(input())
  temp = inf
  i = bl(Sh, q)
  #print(q, Sh[i - 1])
  que = deque([])
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.popleft()
    k = bl(Te, Sh[j])
    #print(Sh[j], Te[k])
    temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q))
    if k != 0:
      temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q))
    
  i = bl(Te, q)
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.popleft()
    k = bl(Sh, Te[j])
    temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q))
    if k != 0:
      temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q))
  print(temp)

Submission status: Time Limit Exceeded

Solution code:
A,B,Q = (int(i) for i in 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)]

def len_L(s,x):
    L= 99999999999
    for i in s:
        if i <= x:
            L = x - i
        else:
            break
    return L

def len_R(s,x):
    for i in s:
        if i >= x:
            return i - x
    return 99999999999

for i in x:
    ls = len_L(s,i)
    lt = len_L(t,i)
    rs = len_R(s,i)
    rt = len_R(t,i)

    mini = min([2*ls+rt , ls+2*rt ,2*rs+lt , rs+2*lt , max([lt,ls]) , max([rt,rs])])
    print(mini)

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

Solution code:
A,B,Q = list(map(int,input().split()))
shrine_list = []
temple_list = []
question_list = []
for _ in range(A):
    shrine_list.append(int(input()))
for _ in range(B):
    temple_list.append(int(input()))
shrine_list.sort()
temple_list.sort()
for i in range(Q):
    question_list.append(int(input()))
for i in range(Q):
    east_shrine_list = []
    west_shrine_list = []
    east_temple_list = []
    west_temple_list = []
    current = question_list[i]
    for shrine in shrine_list:
        if shrine < current:
            east_shrine_list.append(shrine)
        else:
            west_shrine_list.append(shrine)
    for temple in temple_list:
        if temple < current:
            east_temple_list.append(temple)
        else:
            west_temple_list.append(temple)
    if east_shrine_list:
        most_east_shrine = max(east_shrine_list)
    if west_shrine_list:
        most_west_shrine = min(west_shrine_list)
    if east_temple_list:
        most_east_temple = max(east_temple_list)
    if west_temple_list:
        most_west_temple = min(west_temple_list)
    likelyhood_list = []
    if east_shrine_list:
        if east_temple_list:
            likelyhood_list.append(abs(current - most_east_shrine) + abs(most_east_shrine - most_east_temple))
            likelyhood_list.append(abs(current - most_east_temple) + abs(most_east_shrine - most_east_temple))
        if west_temple_list:
            likelyhood_list.append(abs(current - most_east_shrine) + abs(most_east_shrine - most_west_temple))
            likelyhood_list.append(abs(current - most_west_temple) + abs(most_east_shrine - most_west_temple))
    if west_shrine_list:
        if east_temple_list:
            likelyhood_list.append(abs(current - most_west_shrine) + abs(most_west_shrine - most_east_temple))
            likelyhood_list.append(abs(current - most_east_temple) + abs(most_west_shrine - most_east_temple))
        if west_temple_list:
            likelyhood_list.append(abs(current - most_west_shrine) + abs(most_west_shrine - most_west_temple))
            likelyhood_list.append(abs(current - most_west_temple) + abs(most_west_shrine - most_west_temple))
    print(min(likelyhood_list))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 10**100

for i in range(q):
    x = int(input())
    ans = 10**100
    s_idx = bisect.bisect_left(s,x)
    t_idx = bisect.bisect_left(t,x)
    s_idx -= 1
    t_idx -= 1
    #print(x[i],s_idx,t_idx)
    for j in range(2):
        for k in range(2):
            ans = min(ans,abs(x - s[min(s_idx+j,a-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]),abs(x - t[min(t_idx+k,b-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]))
            
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#ABC119 D

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7


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

for _ in range(q):
    x = int(input())
    sr,tr = bisect.bisect_right(s,x),bisect.bisect_right(t,x)
    ans = float('inf')
    for S in [s[sr-1],s[sr]]:
        for T in [t[tr-1],t[tr]]:
            d1,d2 = abs(S-x) + abs(T-S), abs(T-x) + abs(S-T)
            ans = min(ans,d1,d2)
    print(ans)

Submission status: Time Limit Exceeded

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

res = []
for i in range(Q):
    x = int(input())
    ans = 0

    minimum_ws = float('inf')
    minimum_wt = float('inf')
    minimum_es = float('inf')
    minimum_et = float('inf')
    for i in range(A):
        if s[i] < x:
            minimum_ws = x - s[i]
        else:
            minimum_es = s[i] - x
            break
    for i in range(B):
        if t[i] < x:
            minimum_wt = x - t[i]
        else:
            minimum_et = t[i] - x
            break

    # 西に突っ切る
    ans = max(minimum_ws, minimum_wt)
    # 東に突っ切る
    ans = min(ans, max(minimum_es, minimum_et))
    # 西いって東
    ans = min(ans, min(minimum_ws * 2 + minimum_et, minimum_wt * 2 + minimum_es))
    # 東いって西
    ans = min(ans, min(minimum_es * 2 + minimum_wt, minimum_et * 2 + minimum_ws))
    res.append(ans)

print('\n'.join(list(map(str, res))))

Submission status: Time Limit Exceeded

Solution code:
A, B, Q = map(int, input().split())
Sh = [int(input()) for _ in range(A)]
Te = [int(input()) for _ in range(B)]
inf = 10 ** 11 + 9
def move(x, s, t, d):
  ress = inf
  rest = inf
  if s >= 1:
    if t >= 1:
      #print(d, "dです")
      return d
  mins = inf
  tempi = 0
  temp = inf
  if s == 0:
    ress = 0
    for i in range(A):
      temp = min(mins, abs(x - Sh[i]))
      if mins != temp: tempi = i
      mins = temp
    #print(Sh[tempi], "ress")
    ress += move(Sh[tempi], s + 1, t + 0, d + abs(x - Sh[tempi]))
  
  tempi = 0
  temp = inf
  mint = inf
  if t == 0:
    rest = 0
    for i in range(B):
      temp = min(mint, abs(x - Te[i]))
      if mint != temp: tempi = i
      mint = temp
    #print(Te[tempi], "rest")
    rest += move(Te[tempi], s + 0, t + 1, d + abs(x - Te[tempi]))
  #print(ress, rest)
  return min(ress, rest)
for _ in range(Q):
  print(move(int(input()), 0, 0, 0))

Submission status: Time Limit Exceeded

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)]
def check(lst, x): #検索するリストと現在地を受け取る
    ans = [[x, 10 ** 10], [x, 10 ** 10]]
    a1 = bisect.bisect_left(lst, x) -1 #自分の左側のindex
    a2 = a1 + 1 #自分の右側のindex
    if a1 < 0:
        pass
    else:
        ans[0][0] = lst[a1]
        ans[0][1] = abs(x-lst[a1])
    if a2 >= len(lst):
        pass
    else:
        ans[1][0] = lst[a2]
        ans[1][1] = abs(x-lst[a2])
    
    return ans

#S-->Tの動き方
def ST(x):
    lst1 = check(S, x)
    lst2 = check(T, lst1[0][0])
    lst3 = check(T, lst1[1][0])
    ans1 = lst1[0][1] + min(lst2[0][1], lst2[1][1])
    ans2 = lst1[1][1] + min(lst3[0][1], lst3[1][1])
    return min(ans1, ans2)

#T-->Sの動き方
def TS(x):
    lst1 = check(T, x)
    lst2 = check(S, lst1[0][0])
    lst3 = check(S, lst1[1][0])
    ans1 = lst1[0][1] + min(lst2[0][1], lst2[1][1])
    ans2 = lst1[1][1] + min(lst3[0][1], lst3[1][1])
    return min(ans1, ans2)

for i in range(Q):
    x = int(input())
    print (min(ST(x), TS(x)))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

for i in range(Q):
    x = int(input())
    b, d = bisect.bisect_left(s, x), bisect.bisect_left(t, x)
    ans = 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)
            ans = min(ans, d1, d2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def nearestdis(x,y):
    lst_new = [abs(j-x) for j in y]
    return(min(lst_new))
    
a,b,q = map(int,input().split())
lst_s = [int(input()) for i in range(a)]
lst_t = [int(input()) for i in range(b)]
lst_x = [int(input()) for i in range(q)]


for i in lst_x:
    lst_ans = []
    lst_s_x = [abs(j-i) for j in lst_s]
    lst_t_x = [abs(j-i) for j in lst_t]
    min_s = min(lst_s_x)
    min_t = min(lst_t_x)
    if i-min_s in lst_s:
        lst_ans.append(min_s+nearestdis(i-min_s,lst_t))
    if i+min_s in lst_s:
        lst_ans.append(min_s+nearestdis(i+min_s,lst_t))
    if i-min_t in lst_t:
        lst_ans.append(min_t+nearestdis(i-min_t,lst_s))
    if i+min_t in lst_t:
        lst_ans.append(min_t+nearestdis(i+min_t,lst_s))
    print(min(lst_ans))

Submission status: Time Limit Exceeded

Solution code:
import bisect

A,B,Q=map(int,input().split())
slist = []
for i in range(A):
  slist.append(int(input()))
#print(slist)

tlist = []
for i in range(B):
  tlist.append(int(input()))
#print(tlist)

for i in range(Q):
  x = int(input())
  min_dist = 10**11
  
  # x->s->t
  if x < slist[0]:
    si = [0]
  elif slist[-1] < x:
    si = [len(slist)-1]
  else:
    sl = bisect.bisect(slist,x)
    si = [sl-1,sl]
    
  for index in si:
    s = slist[index]
    dist = abs(x-s)

    tl = bisect.bisect(tlist,s)
    if tl == 0:
      dist += abs(s-tlist[0])
    elif tl == len(tlist):
      dist += abs(s-tlist[len(tlist)-1])
    else:
      dist += min(abs(s-tlist[tl-1]),abs(s-tlist[tl]))
    
    if dist < min_dist:
      min_dist = dist

  # x->t->s
  if x < tlist[0]:
    ti = [0]
  elif tlist[-1] < x:
    ti = [len(tlist)-1]
  else:
    tl = bisect.bisect(tlist,x)
    ti = [tl-1,tl]
    
  for index in ti:
    t = tlist[index]
    dist = abs(x-t)

    sl = bisect.bisect(slist,t)
    if sl == 0:
      dist += abs(t-slist[0])
    elif sl == len(slist):
      dist += abs(t-slist[len(slist)-1])
    else:
      dist += min(abs(t-slist[sl-1]),abs(t-slist[sl]))
    
    if dist < min_dist:
      min_dist = dist

  print(min_dist)

Submission status: Time Limit Exceeded

Solution code:
import bisect

A,B,Q=map(int,input().split())
slist = []
for i in range(A):
  slist.append(int(input()))
#print(slist)
  
tlist = []
for i in range(B):
  tlist.append(int(input()))
#print(tlist)

for i in range(Q):
  x = int(input())
  min_dist = 10**11
  
  # x->s->t
  sl = bisect.bisect(slist,x)
  if sl == 0:
    si = [0]
  elif sl == len(slist):
    si = [sl-1]
  else:
    si = [sl-1,sl]
  
  for index in si:
    s = slist[index]
    dist = abs(x-s)

    tl = bisect.bisect(tlist,s)
    if tl == 0:
      dist += abs(s-tlist[0])
    elif tl == len(tlist):
      dist += abs(s-tlist[len(tlist)-1])
    else:
      dist += min(abs(s-tlist[tl-1]),abs(s-tlist[tl]))
    
    if dist < min_dist:
      min_dist = dist

  # x->t->s
  tl = bisect.bisect(tlist,x)
  if tl == 0:
    ti = [0]
  elif tl == len(tlist):
    ti = [tl-1]
  else:
    ti = [tl-1,tl]
  
  for index in ti:
    t = tlist[index]
    dist = abs(x-t)

    sl = bisect.bisect(slist,t)
    if sl == 0:
      dist += abs(t-slist[0])
    elif sl == len(slist):
      dist += abs(t-slist[len(slist)-1])
    else:
      dist += min(abs(t-slist[sl-1]),abs(t-slist[sl]))
    
    if dist < min_dist:
      min_dist = dist

  print(min_dist)

Submission status: Time Limit Exceeded

Solution code:
A,B,Q=map(int,input().split())
s=[]
t=[]
answer=[]
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
s=[-10**21]+s+[10**21]
t=[-10**21]+t+[10**21]

def binary_search(list,target):
    left=0
    right=len(list)-1
    while left<=right:
        center=(left+right)//2
        if list[center]==target:
            return center
            break
        elif list[center]<target and list[center+1]>target:
            return center
            break
        elif list[center]<target:
            left=center+1
        else:
            right=center-1
    
for q in range(Q):
    x=int(input())
    i=binary_search(s,x)
    j=binary_search(t,x)
    ans=min(max(x-t[j],x-s[i]),max(s[i+1]-x,t[j+1]-x),t[j+1]-s[i]+min(x-s[i],t[j+1]-x),s[i+1]-t[j]+min(x-t[j],s[i+1]-x))
    answer.append(ans)

for k in answer:
    print(k)

Submission status: Time Limit Exceeded

Solution code:
#D問題
A,B,Q = map(int,(input().split()))
si = [-(10**10)]+[int(input()) for _ in range(A)]+[2*(10**10)] # 範囲外に追加して、xの左右に常にデータがあるようにする
ti = [-(10**10)]+[int(input()) for _ in range(B)]+[2*(10**10)]
A +=2
B +=2

for _ in [0]*Q:
    x = int(input())
    l = 0
    r = A-1
    while l+1<r:
        i = (l+r)//2
        if si[i]<x:
            l = i
        else:
            r = i
    sl = si[l]
    sr = si[r]
    
    l = 0
    r = B-1
    while l+1<r:
        i = (l+r)//2
        if ti[i]<x:
            l = i
        else:
            r = i
    tl = ti[l]
    tr = ti[r]
    dsl = x-sl
    dsr = sr-x
    dtl = x-tl
    dtr = tr-x
    print(min(max(dsr,dtr),max(dsl,dtl),dsl+dtr+min(dsl,dtr),dsr+dtl+min(dsr,dtl)))

Submission status: Time Limit Exceeded

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

#二分探索関数
def bin_search(x,seq):
    a=0
    b=len(seq)-1
    for i in range(len(seq)):
        center=((a+b)//2)
        if x>=seq[center]:
            a=center
        else:
            b=center
        if b-a<=1:
            break
    return seq[a],seq[b]

for i in range(Q):
    x=int(input())
    Nb_s0,Nb_s1=bin_search(x,s)
    Nb_t0,Nb_t1=bin_search(x,t)
    p1=abs(Nb_s0-x)
    p2=abs(Nb_s1-x)
    p3=abs(Nb_t0-x)
    p4=abs(Nb_t1-x)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Submission status: Time Limit Exceeded

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

#二分探索関数
def bin_search(x,seq):
    a=0
    b=len(seq)-1
    for i in range(len(seq)):
        center=((a+b)//2)
        if x>=seq[center]:
            a=center
        else:
            b=center
        if b-a<=1:
            break
    return seq[a],seq[b]

for i in range(Q):
    x=int(input())
    Nb_s0,Nb_s1=bin_search(x,s)
    Nb_t0,Nb_t1=bin_search(x,t)
    k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
    k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
    k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
    k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
    k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
    k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
    k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
    k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
    print(min(k1,k2,k3,k4,k5,k6,k7,k8))

Submission status: Time Limit Exceeded

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

#二分探索関数
def bin_search(x,seq):
    a=0
    b=len(seq)-1
    while((b-a)>1):
        center=((a+b)//2)
        if x>=seq[center]:
            a=center
        else:
            b=center
    return seq[a],seq[b]

for i in range(Q):
    x=int(input())
    Nb_s0,Nb_s1=bin_search(x,s)
    Nb_t0,Nb_t1=bin_search(x,t)
    k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
    k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
    k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
    k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
    k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
    k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
    k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
    k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
    print(min(k1,k2,k3,k4,k5,k6,k7,k8))

Submission status: Time Limit Exceeded

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

#二分探索関数
def bin_search(x,seq):
    a=0
    b=len(seq)-1
    while((b-a)>1):
        center=((a+b)//2)
        if x>=seq[center]:
            a=center
        else:
            b=center
    if x<=seq[a]:
        return [a,a]
    elif x>=seq[b]:
        return [b,b]
    else:
        return [a,b]

for i in range(Q):
    x=int(input())
    Nb_s=bin_search(x,s)
    Nb_t=bin_search(x,t)
    Nb_s0=s[Nb_s[0]]
    Nb_s1=s[Nb_s[1]]
    Nb_t0=t[Nb_t[0]]
    Nb_t1=t[Nb_t[1]]

    k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
    k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
    k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
    k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
    k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
    k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
    k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
    k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
    print(min(k1,k2,k3,k4,k5,k6,k7,k8))

Submission status: Time Limit Exceeded

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

#二分探索関数
def bin_search(x,seq):
    width=[0,len(seq)-1]
    while(width[1]-width[0]>1):
        center=(sum(width)//2)
        if x>=seq[center]:
            width=[center,width[1]]
        else:
            width=[width[0],center]
    if x<=seq[width[0]]:
        return [width[0],width[0]]
    elif x>=seq[width[1]]:
        return [width[1],width[1]]
    else:
        return width

for i in range(Q):
    x=int(input())
    Nb_s=bin_search(x,s)
    Nb_t=bin_search(x,t)
    Nb_s0=s[Nb_s[0]]
    Nb_s1=s[Nb_s[1]]
    Nb_t0=t[Nb_t[0]]
    Nb_t1=t[Nb_t[1]]

    k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
    k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
    k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
    k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
    k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
    k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
    k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
    k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
    print(min(k1,k2,k3,k4,k5,k6,k7,k8))

Submission status: Time Limit Exceeded

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

#二分探索関数
def bin_search(x,seq):
	width=[0,len(seq)-1]
	if len(seq)<=1:
		return [0,0]
	if x<=seq[0]:
		return [0,0]
	elif x>=seq[width[1]]:
		return [width[1],width[1]]
	else:
		while(width[1]-width[0]>1):
			center=(sum(width)//2)
			if x>=seq[center]:
				width=[center,width[1]]
			else:
				width=[width[0],center]
	if x==seq[width[0]]:
		return [width[0],width[0]]
	elif x==seq[width[1]]:
		return [width[1],width[1]]
	else:
		return width

for i in range(Q):
	x=int(input())
	Nb_s=bin_search(x,s)
	Nb_t=bin_search(x,t)
	Nb_s0=s[Nb_s[0]]
	Nb_s1=s[Nb_s[1]]
	Nb_t0=t[Nb_t[0]]
	Nb_t1=t[Nb_t[1]]

	k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
	k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
	k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
	k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
	k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
	k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
	k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
	k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
	print(min(k1,k2,k3,k4,k5,k6,k7,k8))

Submission status: Time Limit Exceeded

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

#二分探索関数
def bin_search(x,seq):
	width=[0,len(seq)-1]
	if len(seq)<=1:
		return [0,0]
	if x<=seq[0]:
		return [0,0]
	elif x>=seq[width[1]]:
		return [width[1],width[1]]
	else:
		while(width[1]-width[0]>1):
			center=(sum(width)//2)
			if x>=seq[center]:
				width=[center,width[1]]
			else:
				width=[width[0],center]
	if x==seq[width[0]]:
		return [width[0],width[0]]
	elif x==seq[width[1]]:
		return [width[1],width[1]]
	else:
		return width

for i in range(Q):
	x=int(input())
	Nb_s0=s[bin_search(x,s)[0]]
	Nb_s1=s[bin_search(x,s)[1]]
	Nb_t0=t[bin_search(x,t)[0]]
	Nb_t1=t[bin_search(x,t)[1]]

	k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
	k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
	k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
	k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
	k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
	k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
	k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
	k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
	print(min(k1,k2,k3,k4,k5,k6,k7,k8))