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:
inf = float("INF")
a,b,q = map(int, input().split())
s = [-inf, -inf]+[int(input()) for _ in range(a)]+[inf]
t = [-inf, -inf]+[int(input()) for _ in range(b)]+[inf]

from bisect import bisect_left

def f(s,t,x,ss):
  tr = bisect_left(t, s[ss])
  tl = tr - 1
  return abs(x - s[ss]) + min(abs(s[ss] - t[tl]), abs(s[ss] - t[tr]))


for _ in range(q):
  ans = inf
  x = int(input())
  sr = bisect_left(s, x)
  ans = min(ans, f(s,t,x,sr))
  ans = min(ans, f(s,t,x,sr - 1))
  tr = bisect_left(t, x)
  ans = min(ans, f(t,s,x,tr))
  ans = min(ans, f(t,s,x,tr - 1))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
from functools import reduce

A, B, Q = map(int,input().split())
INF = float("inf")
s = [-1*INF]
t = [-1*INF]
for _ in range(A):
    s.append(int(input()))
s.append(INF)
for _ in range(B):
    t.append(int(input()))
t.append(INF)
for _ in range(Q):
    x = int(input())
    s_index = bisect.bisect_left(s,x)
    t_index = bisect.bisect_left(t,x)
    s_l = x - s[s_index-1]
    s_r = s[s_index] - x
    t_l = x - t[t_index-1]
    t_r = t[t_index] - x
    ans = reduce(min, [max(s_l,t_l), s_l + t_r*2, s_r + t_l*2, s_l*2 + t_r, s_r*2 + t_l, max(s_r,t_r)])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

A, B, Q = map(int,input().split())
INF = float("inf")
s = [-1*INF]
t = [-1*INF]
for _ in range(A):
    s.append(int(input()))
s.append(INF)
for _ in range(B):
    t.append(int(input()))
t.append(INF)
for _ in range(Q):
    x = int(input())
    s_index = bisect.bisect_left(s,x)
    t_index = bisect.bisect_left(t,x)
    s_l = x - s[s_index-1]
    s_r = s[s_index] - x
    t_l = x - t[t_index-1]
    t_r = t[t_index] - x
    print(min(max(s_l,t_l), s_l + t_r*2, s_r + t_l*2, s_l*2 + t_r, s_r*2 + t_l, max(s_r,t_r)))

Submission status: Time Limit Exceeded

Solution code:
import bisect

A, B, Q = map(int,input().split())
INF = 30000000000
s = [-1*INF]
t = [-1*INF]
for _ in range(A):
    s.append(int(input()))
s.append(INF)
for _ in range(B):
    t.append(int(input()))
t.append(INF)
for _ in range(Q):
    x = int(input())
    s_index = bisect.bisect_left(s,x)
    t_index = bisect.bisect_left(t,x)
    s_l = x - s[s_index-1]
    s_r = s[s_index] - x
    t_l = x - t[t_index-1]
    t_r = t[t_index] - x
    print(min(max(s_l,t_l), s_l + t_r*2, s_r + t_l*2, s_l*2 + t_r, s_r*2 + t_l, max(s_r,t_r)))

Submission status: Time Limit Exceeded

Solution code:
# list(map(int, input().split()))

a,b,q = map(int, input().split())

s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]

#二部探索
import bisect

for i in range(q):
    x = int(input())
    s_ind = bisect.bisect_left(s, x)
    t_ind = bisect.bisect_left(t, x)
    s_subind = 0
    t_subind = 0
    if s_ind != 0:
        s_subind = s_ind-1
    if t_ind != 0:
        t_subind = t_ind-1
    kouho = []
    if s_ind != a and t_ind != b:
        if s[s_ind] > t[t_ind]:
            kouho.append(abs(s[s_ind]-x))
        else:
            kouho.append(abs(t[t_ind] - x))
    if s_subind != s_ind and t_subind != t_ind:
        if s[s_subind] > t[t_subind]:
            kouho.append(abs(x - t[t_subind]))
        else:
            kouho.append(abs(x - s[s_subind]))
    if s_ind != a:
        kouho.append(2*abs(s[s_ind]-x) + abs(x-t[t_subind]))
        kouho.append(abs(s[s_ind] - x) + 2*abs(x - t[t_subind]))
    if t_ind != b:
        kouho.append(2*abs(t[t_ind]-x) + abs(x-s[s_subind]))
        kouho.append(abs(t[t_ind] - x) + 2*abs(x-s[s_subind]))
    print(min(kouho))

Submission status: Time Limit Exceeded

Solution code:
import bisect
a,b,q = map(int,input().split())
s = [-10**11]+[int(input()) for _ in range(a)]+[10**11]
t = [-10**11]+[int(input()) for _ in range(b)]+[10**11]
for i in range(q):
  ans = 10**11
  x = int(input())
  l = bisect.bisect(s,x)
  m = bisect.bisect(t,x)
  arr = [[s[l-1],s[l]],[t[m-1],t[m]]]
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += (now-arr[j][i >> j & 1])*(-1)**(((now-arr[j][i >> j & 1])<0))
      now = arr[j][i >> j & 1]
    if ans > S:
      ans = S
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += (now-arr[1-j][i >> j & 1])*((-1)**((now-arr[1-j][i >> j & 1])<0))
      now = arr[1-j][i >> j & 1]
    if ans > S:
      ans = S
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
a,b,q = map(int,input().split())
s = [-10**11]+[int(input()) for _ in range(a)]+[10**11]
t = [-10**11]+[int(input()) for _ in range(b)]+[10**11]
for i in range(q):
  ans = 10**11
  x = int(input())
  l = bisect.bisect(s,x)
  m = bisect.bisect(t,x)
  arr = [[s[l-1],s[l]],[t[m-1],t[m]]]
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += abs(now-arr[j][i >> j & 1])
      now = arr[j][i >> j & 1]
    ans = min(ans,S)
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += abs(now-arr[1-j][i >> j & 1])
      now = arr[1-j][i >> j & 1]
    ans = min(ans,S)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
a,b,q = map(int,input().split())
s = [-10**11]+[int(input()) for _ in range(a)]+[10**11]
t = [-10**11]+[int(input()) for _ in range(b)]+[10**11]
for i in range(q):
  ans = 10**11
  x = int(input())
  l = bisect.bisect(s,x)
  m = bisect.bisect(t,x)
  arr = [[s[l-1],s[l]],[t[m-1],t[m]]]
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += abs(now-arr[j][i >> j & 1])
      now = arr[j][i >> j & 1]
    ans = min(ans,S)
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += abs(now-arr[1-j][i >> j & 1])
      now = arr[1-j][i >> j & 1]
    ans = min(ans,S)
  print(ans)

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

def binsearch(threshold, lst):
    left, right = -1, len(lst)

    while right - left > 1:
        mid = (left + right) // 2
        if lst[mid] > threshold:
            right = mid
        else:
            left = mid
    return (left, right)

for q in x:
    ans = 10 ** 11
    for i in range(2):
        for j in range(2):
            a = binsearch(q, s)[i]
            b = binsearch(q, t)[j]
            #print(i, j, a, b)
            if 0 <= a <= A - 1 and 0 <= b <= B - 1:
                    #print(s[a], t[b], q)
                    ans = min(ans, abs(s[a] -t[b]) + min(abs(s[a]-q), abs(t[b]-q)))
    print(ans)

Submission status: Time Limit Exceeded

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

import numpy as np

def main():
    a, b, q = map(int, input().split())
    huge = 10 ** 18
    s = np.array([-huge] + [int(input()) for i in range(a)] + [huge])
    t = np.array([-huge] + [int(input()) for i in range(b)] + [huge])
    for i in range(q):
        x = int(input())
        b = np.searchsorted(s, x, "right")
        d = np.searchsorted(t, x, "right")
        res = huge
        for sh in s[b - 1:b + 1]:
            for te in t[d - 1:d + 1]:
                d1 = abs(sh - x) + abs(sh - te)
                d2 = abs(te - x) + abs(sh - te)
                res = min(res, d1, d2)
        print(res)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
a, b, q = map(int, input().split())
inf = 10**18
shrine = [-inf] + [int(input()) for _ in range(a)] + [inf]
temple = [-inf] + [int(input()) for _ in range(b)] + [inf]

for _ in range(q):
    ans = inf
    x = int(input())
    index_s = bisect_left(shrine, x)
    index_t = bisect_right(temple, x)
    for i in range(index_s-1, index_s+1):
        s = shrine[i]
        i_t = bisect_left(temple, s)
        t0, t1 = temple[i_t-1], temple[i_t]
        ans = min(
            ans,
            abs(x - s) + abs(s - t0),
            abs(x - s) + abs(s - t1),
        )

    for i in range(index_t-1, index_t+1):
        t = temple[i]
        i_s = bisect_left(shrine, t)
        s0 = shrine[i_s - 1]
        s1 = shrine[i_s]
        ans = min(
            ans,
            abs(x - t) + abs(t - s0),
            abs(x - t) + abs(t - s1),
        )

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
a, b, q = map(int, input().split())
shrine = [int(input()) for _ in range(a)]
temple = [int(input()) for _ in range(b)]

inf = 10**18
for _ in range(q):
    ans = inf
    x = int(input())
    index_s = bisect_left(shrine, x)
    index_t = bisect_right(temple, x)
    for i in range(index_s-1, index_s+1):
        if i == a or i < 0:
            continue
        s = shrine[i]
        i_t = bisect_left(temple, s)
        
        if i_t != 0:
            ans = min(
                ans,
                abs(x - s) + abs(s - temple[i_t - 1])
            )

        if i_t <= b-1:
            t1 = temple[i_t]
            ans = min(
                ans,
                abs(x - s) + abs(s - t1),
            )

    for i in range(index_t-1, index_t+1):
        if i == b or i < 0:
            continue
        t = temple[i]
        i_s = bisect_left(shrine, t)
        if i_s != 0:
            s0 = shrine[i_s - 1]
            ans = min(
                ans,
                abs(x - t) + abs(t - s0)
            )
            
        if i_s <= a-1:
            s1 = shrine[i_s]
            ans = min(
                ans,
                abs(x - t) + abs(t - s1),
            )

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
a, b, q = map(int, input().split())
shrine = [int(input()) for _ in range(a)]
temple = [int(input()) for _ in range(b)]

inf = 10**18
for _ in range(q):
    ans = inf
    x = int(input())
    index_s = bisect_left(shrine, x)
    index_t = bisect_right(temple, x)
    for i in range(index_s-1, index_s+1):
        if i == a or i < 0:
            continue
        s = shrine[i]
        i_t = bisect_left(temple, s)

        if i_t == 0:
            t0 = inf
        else:
            t0 = temple[i_t - 1]
            
        if i_t <= b-1:
            t1 = temple[i_t]
        else:
            t1 = inf

        ans = min(
            ans,
            abs(x - s) + abs(s - t0),
            abs(x - s) + abs(s - t1),
        )
    for i in range(index_t-1, index_t+1):
        if i == b or i < 0:
            continue
        t = temple[i]
        i_s = bisect_left(shrine, t)
        if i_s == 0:
            s0 = inf
        else:
            s0 = shrine[i_s - 1]

        if i_s <= a-1:
            s1 = shrine[i_s]
        else:
            s1 = inf
        ans = min(
            ans,
            abs(x - t) + abs(t - s0),
            abs(x - t) + abs(t - s1),
        )

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def reverse(arr, index):
    if len(arr) == index:
        return arr[-1]
    return arr[index]


from bisect import bisect_left, bisect_right
a, b, q = map(int, input().split())
shrine = [int(input()) for _ in range(a)]
temple = [int(input()) for _ in range(b)]

inf = 10**18
for _ in range(q):
    ans = inf
    x = int(input())
    s0 = shrine[bisect_right(shrine, x)-1]
    t0 = temple[bisect_right(temple, x)-1]
    s1 = reverse(shrine, bisect_left(shrine, x))
    t1 = reverse(temple, bisect_left(temple, x))

    for start in [s0, s1]:
        ta = reverse(temple, bisect_left(temple, start))
        tb = temple[bisect_right(temple, start)-1]
        ans = min(
            ans,
            abs(x - start) + abs(ta - start),
            abs(x - start) + abs(tb - start),
        )

    for start in [t0, t1]:
        sa = reverse(shrine, bisect_left(shrine, start))
        sb = shrine[bisect_right(shrine, start)-1]
        ans = min(
            ans,
            abs(x - start) + abs(sa - start),
            abs(x - start) + abs(sb - start),
        )
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def bisect(s,t):
    ok,ng=len(s),-1
    while ok-ng>1:
        mid=(ok+ng)//2
        if s[mid]>t:ok=mid
        else:ng=mid
    return ok


inf=10**12

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

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

    sl,sr,tl,tr=s[p-1],s[p],t[q-1],t[q]

    sltl=x-min(sl,tl)
    srtr=max(sr,tr)-x
    sltr=min(x-sl*2+tr,-x-sl+tr*2)
    srtl=min(x-tl*2+sr,-x-tl+sr*2)
    print(min(sltl,srtr,sltr,srtl))

Submission status: Time Limit Exceeded

Solution code:
import bisect
inf=10**12

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

for i in range(q):
    x=int(input())
    p=bisect.bisect_left(s,x)
    q=bisect.bisect_left(t,x)

    sl,sr,tl,tr=s[p-1],s[p],t[q-1],t[q]

    sltl=x-min(sl,tl)
    srtr=max(sr,tr)-x
    sltr=min(x-sl*2+tr,-x-sl+tr*2)
    srtl=min(x-tl*2+sr,-x-tl+sr*2)
    print(min(sltl,srtr,sltr,srtl))

Submission status: Time Limit Exceeded

Solution code:
import math
import bisect
inf=10**12
a,b,q=map(int,input().split())
s=[-inf]+[int(input()) for _ in range(a)]+[inf];s.sort()
t=[-inf]+[int(input()) for _ in range(b)]+[inf];t.sort()

for i in range(q):
    x=int(input())
    p=bisect.bisect_left(s,x)
    q=bisect.bisect_left(t,x)

    sl,sr,tl,tr=s[p-1],s[p],t[q-1],t[q]

    sltl=x-min(sl,tl)
    srtr=max(sr,tr)-x
    sltr=min(x-sl*2+tr,-x-sl+tr*2)
    srtl=min(x-tl*2+sr,-x-tl+sr*2)
    print(min(sltl,srtr,sltr,srtl))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

INF = 10**18

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 _ in range(Q):
    x = int(input())
    si = bisect_left(S,x)
    ti = bisect_left(T,x)
    res = INF
    res = min(res, max(S[si] - x, T[ti] - x))
    res = min(res, max(x - S[si - 1], x - T[ti - 1]))
    res = min(res, x + T[ti] - 2 * S[si - 1])
    res = min(res, -x + 2 * T[ti] - S[si - 1])
    res = min(res, x + S[si] - 2 * T[ti - 1])
    res = min(res, -x + 2 * S[si] - T[ti - 1])
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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 f(nums, a):
  if a<=nums[0]:
    return nums[0]-a
  if nums[-1]<=a:
    return a-nums[-1]
  tmp=np.searchsorted(nums, a, side='left')
  return min(a-nums[tmp-1], nums[tmp]-a)

for i in range(Q):
  out=2*10**10
  if x[i]<=s[0]:
    s2=s[0]
    out=min(out, s2-x[i]+f(t, s2))
  elif s[-1]<=x[i]:
    s1=s[-1]
    out=min(out, x[i]-s1+f(t, s1))
  else:
    tmp=np.searchsorted(s, x[i], side='left')
    s1=s[tmp-1]
    out=min(out, x[i]-s1+f(t, s1))
    s2=s[tmp]
    out=min(out, s2-x[i]+f(t, s2))
    
  if x[i]<=t[0]:
    t2=t[0]
    out=min(out, t2-x[i]+f(s, t2))
  elif t[-1]<=x[i]:
    t1=t[-1]
    out=min(out, x[i]-t1+f(s, t1))
  else:
    tmp=np.searchsorted(t, x[i], side='left')
    t1=t[tmp-1]
    out=min(out, x[i]-t1+f(s, t1))
    t2=t[tmp]
    out=min(out, t2-x[i]+f(s, t2))
    
  print(out)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    import sys
    input=sys.stdin.readline
    bisect=bisect_left
    INF=float('inf')
    a,b,q=map(int,input().split())
    s=[int(input()) for i in range(a)]+[INF]
    t=[int(input()) for i in range(b)]+[INF]
    for i in range(q):
        x=int(input())
        ans=INF
        y=bisect(s,x)
        s1,s2=s[y-1],s[y]
        y=bisect(t,s1)
        t1,t2=t[y-1],t[y]
        ans=min(ans,abs(x-s1)+min(abs(s1-t1),abs(s1-t2)))

        y=bisect(t,s2)
        t1,t2=t[y-1],t[y]
        ans=min(ans,abs(x-s2)+min(abs(s2-t1),abs(s2-t2)))

        y=bisect(t,x)
        t1,t2=t[y-1],t[y]
        y=bisect(s,t1)
        s1,s2=s[y-1],s[y]
        ans=min(ans,abs(x-t1)+min(abs(t1-s1),abs(t1-s2)))
        
        y=bisect(s,t2)
        s1,s2=s[y-1],s[y]
        ans=min(ans,abs(x-t2)+min(abs(t2-s1),abs(t2-s2)))
        
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input=sys.stdin.readline
bisect=bisect_left
INF=float('inf')
a,b,q=map(int,input().split())
s=tuple([int(input()) for i in range(a)]+[INF])
t=tuple([int(input()) for i in range(b)]+[INF])
for i in range(q):
    x=int(input())
    ans=INF
    y=bisect(s,x)
    s1,s2=s[y-1],s[y]
    y=bisect(t,s1)
    t1,t2=t[y-1],t[y]
    ans=min(ans,abs(x-s1)+min(abs(s1-t1),abs(s1-t2)))

    y=bisect(t,s2)
    t1,t2=t[y-1],t[y]
    ans=min(ans,abs(x-s2)+min(abs(s2-t1),abs(s2-t2)))

    y=bisect(t,x)
    t1,t2=t[y-1],t[y]
    y=bisect(s,t1)
    s1,s2=s[y-1],s[y]
    ans=min(ans,abs(x-t1)+min(abs(t1-s1),abs(t1-s2)))
    
    y=bisect(s,t2)
    s1,s2=s[y-1],s[y]
    ans=min(ans,abs(x-t2)+min(abs(t2-s1),abs(t2-s2)))
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input=sys.stdin.readline
bisect=bisect_left
INF=float('inf')
a,b,q=map(int,input().split())
s=[int(input()) for i in range(a)]+[INF]
t=[int(input()) for i in range(b)]+[INF]
for i in range(q):
    x=int(input())
    ans=INF
    y=bisect(s,x)
    s1,s2=s[y-1],s[y]
    y=bisect(t,s1)
    t1,t2=t[y-1],t[y]
    ans=min(ans,abs(x-s1)+min(abs(s1-t1),abs(s1-t2)))

    y=bisect(t,s2)
    t1,t2=t[y-1],t[y]
    ans=min(ans,abs(x-s2)+min(abs(s2-t1),abs(s2-t2)))

    y=bisect(t,x)
    t1,t2=t[y-1],t[y]
    y=bisect(s,t1)
    s1,s2=s[y-1],s[y]
    ans=min(ans,abs(x-t1)+min(abs(t1-s1),abs(t1-s2)))
    
    y=bisect(s,t2)
    s1,s2=s[y-1],s[y]
    ans=min(ans,abs(x-t2)+min(abs(t2-s1),abs(t2-s2)))
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
bisect=bisect_left
INF=float('inf')
a,b,q=map(int,input().split())
s=[int(input()) for i in range(a)]+[INF]
t=[int(input()) for i in range(b)]+[INF]
for i in range(q):
    x=int(input())
    ans=INF
    y=bisect(s,x)
    s1,s2=s[y-1],s[y]
    y=bisect(t,s1)
    t1,t2=t[y-1],t[y]
    ans=min(ans,abs(x-s1)+min(abs(s1-t1),abs(s1-t2)))

    y=bisect(t,s2)
    t1,t2=t[y-1],t[y]
    ans=min(ans,abs(x-s2)+min(abs(s2-t1),abs(s2-t2)))

    y=bisect(t,x)
    t1,t2=t[y-1],t[y]
    y=bisect(s,t1)
    s1,s2=s[y-1],s[y]
    ans=min(ans,abs(x-t1)+min(abs(t1-s1),abs(t1-s2)))
    
    y=bisect(s,t2)
    s1,s2=s[y-1],s[y]
    ans=min(ans,abs(x-t2)+min(abs(t2-s1),abs(t2-s2)))
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
bisect=bisect_left
INF=float('inf')
a,b,q=map(int,input().split())
s=[int(input()) for i in range(a)]+[INF]
t=[int(input()) for i in range(b)]+[INF]
for i in range(q):
    x=int(input())
    ans=INF
    s1,s2=s[bisect(s,x)-1],s[bisect(s,x)]
    t1,t2=t[bisect(t,s1)-1],t[bisect(t,s1)]
    ans=min(ans,abs(x-s1)+min(abs(s1-t1),abs(s1-t2)))
    
    t1,t2=t[bisect(t,s2)-1],t[bisect(t,s2)]
    ans=min(ans,abs(x-s2)+min(abs(s2-t1),abs(s2-t2)))
    
    t1,t2=t[bisect(t,x)-1],t[bisect(t,x)]
    s1,s2=s[bisect(s,t1)-1],s[bisect(s,t1)]
    ans=min(ans,abs(x-t1)+min(abs(t1-s1),abs(t1-s2)))

    s1,s2=s[bisect(s,t2)-1],s[bisect(s,t2)]
    ans=min(ans,abs(x-t2)+min(abs(t2-s1),abs(t2-s2)))
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
bisect=bisect_left
INF=float('inf')
a,b,q=map(int,input().split())
s=[int(input()) for i in range(a)]+[INF]
t=[int(input()) for i in range(b)]+[INF]
for i in range(q):
    x=int(input())
    ans=INF
    s1,s2=s[bisect(s,x)-1],s[bisect(s,x)]
    for j in [s1,s2]:
        t1,t2=t[bisect(t,j)-1],t[bisect(t,j)]
        ans=min(ans,abs(x-j)+min(abs(j-t1),abs(j-t2)))
    t1,t2=t[bisect(t,x)-1],t[bisect(t,x)]
    for j in [t1,t2]:
        s1,s2=s[bisect(s,j)-1],s[bisect(s,j)]
        ans=min(ans,abs(x-j)+min(abs(j-s1),abs(j-s2)))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    import sys
    input=sys.stdin.readline
    inf=10**11
    
    a,b,q=map(int,input().split())
    S=np.array([-inf]+[int(input()) for _ in range(a)]+[inf])
    T=np.array([-inf]+[int(input()) for _ in range(b)]+[inf])
    Q=np.array([int(input()) for _ in range(q)])
    idxs=np.searchsorted(S,Q)
    idxt=np.searchsorted(T,Q)
    
    for i in range(q):
        x=Q[i]
        s=idxs[i]
        t=idxt[i]
        ans=max(T[t],S[s])-x
        ans=min(ans,S[s]-T[t-1]+min(S[s]-x,x-T[t-1]))
        ans=min(ans,x-min(S[s-1],T[t-1]))
        ans=min(ans,T[t]-S[s-1]+min(T[t]-x,x-S[s-1]))
        print(ans)
        
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    import sys
    input=sys.stdin.readline
    inf=10**11
    
    a,b,q=map(int,input().split())
    S=np.array([-inf]+[int(input()) for _ in range(a)]+[inf])
    T=np.array([-inf]+[int(input()) for _ in range(b)]+[inf])
    Q=np.array([int(input()) for _ in range(q)])
    idxs=np.searchsorted(S,Q)
    idxt=np.searchsorted(T,Q)
    
    for i in range(q):
        x=Q[i]
        s=idxs[i]
        t=idxt[i]
        ans=inf
        ans=min(ans,max(T[t],S[s])-x)
        ans=min(ans,S[s]-T[t-1]+min(S[s]-x,x-T[t-1]))
        ans=min(ans,x-min(S[s-1],T[t-1]))
        ans=min(ans,T[t]-S[s-1]+min(T[t]-x,x-S[s-1]))
        print(ans)
        
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left,bisect_right
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 i in range(Q):
    x = int(input())
    values = []
    p1 = bisect_left(S,x)
    if p1 == 0:
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    elif p1 == A:
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    else:
        #-1
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        p1 += 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))

    p1 = bisect_left(T,x)
    if p1 == 0:
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    elif p1 == B:
        p1 -= 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    else:
        #-1
        p1 -= 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        p1 += 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    d = min(values)
    print(d)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left,bisect_right
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 i in range(Q):
    x = int(input())
    values = []
    p1 = bisect_left(S,x)
    if p1 == 0:
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    elif p1 == A:
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    else:
        #-1
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        p1 += 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))

    p1 = bisect_left(T,x)
    if p1 == 0:
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    elif p1 == B:
        p1 -= 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    else:
        #-1
        p1 -= 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        p1 += 1
        p2 = bisect_left(S,T[p1])
        if p2 == 0:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
        elif p2 == A:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
        else:
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2]))
            values.append(abs(x-T[p1])+abs(T[p1]-S[p2-1]))
    d = min(values)
    print(d)