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.

Solution (Python):
def main():

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    if __name__ != '__main__':
        
        sys.stdin = open('sample.txt')
        
    
    

    A, B, Q = R()
    S = [int(r()) for _ in range(A)]
    T = [int(r()) for _ in range(B)]
    X = [int(r()) for _ in range(Q)]
    for x in X:
        s = bisect_left(S, x)
        t = bisect_left(T, x)
        if s == 0:
            sl = -INF
            sr = S[s]
        elif s == A:
            sl = S[s-1]
            sr = INF
        else:
            sl = S[s-1]
            sr = S[s]
        if t == 0:
            tl = -INF
            tr = T[t]
        elif t == B:
            tl = T[t-1]
            tr = INF
        else:
            tl = T[t-1]
            tr = T[t]
        print(min(min(abs(x-sl), abs(x-tr))+abs(sl-tr),
            min(abs(x-sr), abs(x-tl))+abs(sr-tl),
            max(abs(x-sl), abs(x-tl)),
            max(abs(x-sr), abs(x-tr)))
              )
        
    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """
    
    """memo: how to solve the problem
    creating simple test/answer
    greed
    simple dp
    graph
    """

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect as B
I=lambda z:[int(input()) for _ in range(z)]
a,b,q=map(int,input().split())
F=float("inf")
s=[-F]+I(a)+[F];t=[-F]+I(b)+[F];X=I(q)
for x in X:
	i=B(s,x);j=B(t,x)
	o,p=x-s[i-1],s[i]-x
	q,r=x-t[j-1],t[j]-x
	print(min(2*o+r,o+r*2,2*p+q,p+q*2,max(o,q),max(p,r)))

Solution (Python):
from bisect import bisect
a,b,q=map(int,input().split())
INF=float("inf")
s=[-INF]+[int(input()) for _ in range(a)]+[INF]
t=[-INF]+[int(input()) for _ in range(b)]+[INF]
xx=[int(input()) for _ in range(q)]
for x in xx:
	si=bisect(s,x)
	ti=bisect(t,x)
	sf,sb=x-s[si-1],s[si]-x
	tf,tb=x-t[ti-1],t[ti]-x
	print(min(2*sf+tb,sf+tb*2,2*sb+tf,sb+tf*2,max(sf,tf),max(sb,tb)))

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

Solution (Python):
from bisect import bisect_right

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

INF = 10 ** 18
s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i in range(Q):
    b = bisect_right(s, x[i])
    d = bisect_right(t, x[i])
    res = INF
    for n in [s[b - 1], s[b]]:
        for m in [t[d - 1], t[d]]:
            d1 = abs(n - x[i]) + abs(m - n)
            d2 = abs(m - x[i]) + abs(n - m)
            res = min(res, d1, d2)
    print(res)

Solution (Python):
import bisect
INF = 10 ** 18
a, b, Q = map(int, input().split())
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())
  s_idx = bisect.bisect_left(s, x)
  t_idx = bisect.bisect_left(t, x)
  ans = INF
  for i in [s_idx, s_idx-1]:
    for j in [t_idx, t_idx-1]:
      temp1 = abs(s[i] - x) + abs(s[i] - t[j])
      temp2 = abs(t[j] - x) + abs(s[i] - t[j])
      ans = min(ans, temp1, temp2)
  print(ans)

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

s = [-9e20] + s + [9e20]
t = [-9e20] + t + [9e20]
 
for i in range(q):
    x = int(input())
    p = bisect.bisect_left(s,x)
    m = bisect.bisect_left(t,x)
    
    ll = max(s[p], t[m]) - x
    lr = min(s[p] * 2 - t[m-1] - x, t[m] * 2 - s[p-1] - x)
    rl = min(s[p] - t[m-1] * 2 + x, t[m] - s[p-1] * 2 + x)
    rr = x - min(s[p-1], t[m-1])

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

Solution (Python):
import bisect

A,B,Q = list(map(int,input().split()))
INF = 10 ** 18
shrine_list = [-INF] + [int(input()) for _ in range(A)] + [INF]
temple_list = [-INF] + [int(input()) for _ in range(B)] + [INF]
question_list = [int(input()) for _ in range(Q)]
for i in range(Q):
    x = question_list[i]
    index_s = bisect.bisect_right(shrine_list,x)
    index_t = bisect.bisect_right(temple_list,x)
    most_east_shrine = shrine_list[index_s - 1]
    most_east_temple = temple_list[index_t - 1]
    most_west_shrine = shrine_list[index_s]
    most_west_temple = temple_list[index_t]
    likelyhood_list = []
    likelyhood_list.append(abs(x - most_east_shrine) + abs(most_east_shrine - most_east_temple))
    likelyhood_list.append(abs(x - most_east_temple) + abs(most_east_shrine - most_east_temple))
    likelyhood_list.append(abs(x - most_east_shrine) + abs(most_east_shrine - most_west_temple))
    likelyhood_list.append(abs(x - most_west_temple) + abs(most_east_shrine - most_west_temple))
    likelyhood_list.append(abs(x - most_west_shrine) + abs(most_west_shrine - most_east_temple))
    likelyhood_list.append(abs(x - most_east_temple) + abs(most_west_shrine - most_east_temple))
    likelyhood_list.append(abs(x - most_west_shrine) + abs(most_west_shrine - most_west_temple))
    likelyhood_list.append(abs(x - most_west_temple) + abs(most_west_shrine - most_west_temple))
    print(min(likelyhood_list))

Solution (Python):
import numpy as np
import math
import sys
import bisect
sys.setrecursionlimit(10000000)
INF = 1e11

def main():
    
    A, B, Q = [int(c) for c in input().split()]
    S = []
    T = []
    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()))
    S.append(INF)
    T.append(INF)

    for i, x in enumerate(X):
        
        sa = bisect.bisect_left(S, x) - 1
        sb = sa + 1

        
        tc = bisect.bisect_left(T, x) - 1
        td = tc + 1
        distance = INF
        for s in [sa, sb]:
            for t in [tc, td]:
                dis1 = abs(x - S[s]) + abs(S[s] - T[t])
                dis2 = abs(x - T[t]) + abs(T[t] - S[s])
                distance = min(dis1, dis2, distance)
        print(distance)



if __name__ == '__main__':
    main()

Solution (Python):
import sys
import bisect
input = sys.stdin.readline

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

ans = 10**100

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



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
    
    for j in s[s_idx+0],s[s_idx+1]:
        for k in t[t_idx+0],t[t_idx+1]:
            ans = min(ans,abs(x - j) + abs(j - k),abs(x - k) + abs(j - k))
            
    print(ans)

Solution (Python):
import sys
import bisect
input = sys.stdin.readline

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

ans = 10**100

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



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
    
    for j in s[s_idx+0],s[s_idx+1]:
        for k in t[t_idx+0],t[t_idx+1]:
            ans = min(ans,abs(x - j) + abs(j - k),abs(x - k) + abs(j - k))
            
    print(ans)

Solution (Python):
import sys
import bisect
input = sys.stdin.readline

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

ans = 10**100

s = [-ans] + [int(input()) for i in range(a)] + [ans]
t = [-ans] + [int(input()) for i in range(b)] + [ans]
x = [int(input()) for i in range(q)]


ans = 10**100

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

Solution (Python):
import sys
import bisect
input = sys.stdin.readline

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

ans = 10**100

s = [-ans] + [int(input()) for i in range(a)] + [ans]
t = [-ans] + [int(input()) for i in range(b)] + [ans]
x = [int(input()) for i in range(q)]


ans = 10**100

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

Solution (Python):
import sys
import bisect
input = sys.stdin.readline

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

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


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

Solution (Python):
import sys
import bisect
input = sys.stdin.readline

def f(x):
    if 0<=x and a-1>= x:
        return True
    return False
def g(x):
    if 0<=x and b-1>= x:
        return True
    return False

a,b,q = map(int,input().split())
s = []
t = []
for i in range(a):
    S = int(input())
    s.append(S)
for i in range(b):
    T = int(input())
    t.append(T)
s.sort()
t.sort()
 
for i in range(q):
    x = int(input())
    p = bisect.bisect_left(s,x)
    m = bisect.bisect_left(t,x)
    
    res = []
    if f(p) and g(m):
        res.append(max(t[m],s[p])-x)
    if f(p) and g(m-1):
        res.append(min(s[p]-x,x-t[m-1])+s[p]-t[m-1])
    if f(p-1) and g(m):
        res.append(t[m]-s[p-1]+min(t[m]-x,x-s[p-1]))
    if f(p-1) and g(m-1):
        res.append(x-min(t[m-1],s[p-1]))
    print(min(res))

Solution (Python):
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):
    ans = 10**100
    s_idx = bisect.bisect_left(s,x[i])
    t_idx = bisect.bisect_left(t,x[i])
    s_idx -= 1
    t_idx -= 1
    
    for j in range(2):
        for k in range(2):
            ans = min(ans,abs(x[i] - s[min(s_idx+j,a-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]),abs(x[i] - t[min(t_idx+k,b-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]))
            
    print(ans)

Solution (Python):
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):
    ans = 10**100
    s_idx = bisect.bisect_left(s,x[i])
    t_idx = bisect.bisect_left(t,x[i])
    s_idx -= 1
    t_idx -= 1
    
    for j in range(2):
        for k in range(2):
            ans = min(ans,abs(x[i] - s[min(s_idx+j,a-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]))
            ans = min(ans,abs(x[i] - t[min(t_idx+k,b-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]))
            
    print(ans)

Solution (Python):
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):
    ans = 10**100
    s_idx = bisect.bisect_left(s,x[i])
    t_idx = bisect.bisect_left(t,x[i])
    s_idx -= 1
    t_idx -= 1
    
    for j in range(2):
        for k in range(2):
            ans = min(ans,abs(x[i] - s[min(s_idx+j,a-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]))
            ans = min(ans,abs(x[i] - t[min(t_idx+k,b-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]))
            
    print(ans)

Solution (Python):
INF = 25*10**9
import bisect
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()))
for i in range(q):
    x.append(int(input()))
s.append(INF)
s.insert(0,-INF)
t.append(INF)
t.insert(0,-INF)    
for i in range(q):
    a1 = bisect.bisect_left(s,x[i])
    b1 = bisect.bisect_left(t,x[i])
    c1 = max(s[a1],t[b1])-x[i]
    c2 = x[i]-min(s[a1-1],t[b1-1])
    c3 = 2 * s[a1]-x[i]-t[b1-1]
    c4 = 2 * t[b1]-x[i]-s[a1-1]
    c5 = s[a1]+x[i]-2*t[b1-1]
    c6 = t[b1]+x[i]-2*s[a1-1]
    print(min(c1,c2,c3,c4,c5,c6))

Solution (Python):
import sys
input = sys.stdin.readline

a, b, q = map(int, input().split())
INF = 10 ** 11
s = [int(input()) for _ in range(a)] + [INF]
t = [int(input()) for _ in range(b)] + [INF]
x = [[int(input()), int(i), INF, INF, INF, INF] for i in range(q)]
x.sort(key=lambda x: x[0])
si = 0
ti = 0
for i in range(q):
    xi = x[i][0]
    while xi > s[si]:
        si += 1
    ls = abs(xi - s[si - 1])
    rs = abs(xi - s[si])

    while xi > t[ti]:
        ti += 1
    lt = abs(xi - t[ti - 1])
    rt = abs(xi - t[ti])

    x[i][2] = ls
    x[i][3] = rs
    x[i][4] = lt
    x[i][5] = rt

x.sort(key=lambda x: x[1])
for xi, i, ls, rs, lt, rt in x:
    ans = min(max(ls, lt),
              max(rs, rt),
              min(ls, rt) * 2 + max(ls, rt),
              min(rs, lt) * 2 + max(rs, lt))
    print(ans)

Solution (Python):
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [int(input()) for _ in range(a)] + [INF]
t = [int(input()) for _ in range(b)] + [INF]
x = [[int(input()), int(i), INF, INF, INF, INF] for i in range(q)]
x.sort(key=lambda x: x[0])
si = 0
ti = 0
for i in range(q):
    xi = x[i][0]
    while xi > s[si]:
        si += 1
    ls = abs(xi - s[si - 1])
    rs = abs(xi - s[si])

    while xi > t[ti]:
        ti += 1
    lt = abs(xi - t[ti - 1])
    rt = abs(xi - t[ti])

    x[i][2] = ls
    x[i][3] = rs
    x[i][4] = lt
    x[i][5] = rt

x.sort(key=lambda x: x[1])
for xi, i, ls, rs, lt, rt in x:
    ans = min(max(ls, lt),
              max(rs, rt),
              min(ls, rt) * 2 + max(ls, rt),
              min(rs, lt) * 2 + max(rs, lt))
    print(ans)

Solution (Python):
from bisect import bisect
import sys
input = sys.stdin.readline

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

for xx in x:
    ind = bisect(s, xx)
    ss = []
    if ind > 0:
        ss.append(s[ind-1])
    if ind < a:
        ss.append(s[ind])
    tt = []
    ind = bisect(t, xx)
    if ind > 0:
        tt.append(t[ind-1])
    if ind < b:
        tt.append(t[ind])
    ans = float('inf')
    for y in ss:
        ind = bisect(t, y)
        tmpt = []
        if ind > 0:
            tmpt.append(abs(y-t[ind - 1]))
        if ind < b:
            tmpt.append(abs(y-t[ind]))
        st = min(tmpt)
        tmp = abs(xx-y) + st
        if tmp < ans:
            ans = tmp
    for y in tt:
        ind = bisect(s, y)
        tmps = []
        if ind > 0:
            tmps.append(abs(y-s[ind - 1]))
        if ind < a:
            tmps.append(abs(y-s[ind]))
        ts = min(tmps)
        tmp = abs(xx-y) + ts
        if tmp < ans:
            ans = tmp
    print(ans)

Solution (Python):
import bisect
A, B, Q = map(int, input().split())
S_list = [int(input()) for i in range(A)]
T_list = [int(input()) for i in range(B)]
X_list = [int(input()) for i in range(Q)]



for X in X_list:
    
    left_S_index = bisect.bisect(S_list, X)
    if left_S_index == 0:
        left_S = -10**11
    else:
        left_S = S_list[left_S_index-1]
    if left_S_index <= A-1:
        right_S = S_list[left_S_index]
    else:
        right_S = 10 ** 11

    left_T_index = bisect.bisect(T_list, X)
    if left_T_index == 0:
        left_T = -10 ** 11
    else:
        left_T = T_list[left_T_index-1]

    if left_T_index <= B-1:
        right_T = T_list[left_T_index]
    else:
        right_T = 10 ** 11

    a1 = right_T-X + abs(right_S-right_T)  
    a2 = right_T-X + abs(left_S-right_T)  
    a3 = X-left_T + abs(right_S-left_T)  
    a4 = X-left_T + abs(left_S-left_T)  
    a5 = right_S-X + abs(right_T-right_S)  
    a6 = right_S-X + abs(left_T-right_S)  
    a7 = X-left_S + abs(right_T-left_S)  
    a8 = X-left_S + abs(left_T-left_S)  

    print(min(a1, a2, a3, a4, a5, a6, a7, a8))

Solution (Python):



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)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import bisect

mod = 10**13


def LI(): return list(map(int, sys.stdin.readline().split()))

A,B,Q = LI()
s = [0 for i in range(A)]
t = [0 for i in range(B)]
x = [0 for i in range(Q)]
for i in range(A):
    s[i] = int(input())
for i in range(B):
    t[i] = int(input())
for i in range(Q):
    x[i] = int(input())


def solve(pos):
    ans1,ans2,ans3,ans4 = mod,mod,mod,mod
    s_left = bisect.bisect_left(s,pos)-1
    t_left = bisect.bisect_left(t,pos)-1
    
    if s_left >= 0 and t_left >= 0:
        ans1 = pos - min(s[s_left],t[t_left])
    if s_left+1 < A and t_left >= 0:
        ans2 = s[s_left+1] - t[t_left] + min(s[s_left+1]-pos, pos - t[t_left])
    if t_left + 1 < B and s_left>= 0:
        ans3 = t[t_left+1] - s[s_left] + min(t[t_left+1] - pos, pos-s[s_left])
    if s_left+1 < A and t_left+1 < B:
        ans4 = max(s[s_left+1],t[t_left+1]) - pos
    
    ans = min(ans1,ans2,ans3,ans4)
    return ans


for i in range(Q):
    print(solve(x[i]))

Solution (Python):
A,B,Q = map(int,input().split())
S = [-10**11] + [int(input()) for _ in [0]*A] + [10**11]
T = [-10**11] + [int(input()) for _ in [0]*B] + [10**11]
X = [int(input()) for _ in [0]*Q]

def binary_search(A,func):
    l = 0
    r = len(A)-1
    if func(0,A):return -1
    if not func(r,A):return r
    while l+1<r:
        i = (l+r)//2
        if func(i,A):
            r = i
        else:
            l = i
    return l

for x in X:
    a = binary_search(S,lambda i,A:A[i]>=x)
    s = S[a:a+2]
    b = binary_search(T,lambda i,A:A[i]>=x)
    t = T[b:b+2]
    print(min(x-min(s[0],t[0]),
              max(s[1],t[1])-x,
              x-s[0]+t[1]-x+min(x-s[0],t[1]-x),
              x-t[0]+s[1]-x+min(x-t[0],s[1]-x) ))

Solution (Python):
A,B,Q = map(int,input().split())
S = [-10**11]
S.extend(int(input()) for _ in [0]*A)
S.extend([10**11])
T = [-10**11]
T.extend(int(input()) for _ in [0]*B)
T.extend([10**11])
X = [int(input()) for _ in [0]*Q]

def binary_search(A,func):
    l = 0
    r = len(A)-1
    if func(0,A):return -1
    if not func(r,A):return r
    while l+1<r:
        i = (l+r)//2
        if func(i,A):
            r = i
        else:
            l = i
    return l

for x in X:
    a = binary_search(S,lambda i,A:A[i]>=x)
    s = S[a:a+2]
    b = binary_search(T,lambda i,A:A[i]>=x)
    t = T[b:b+2]
    print(min(x-min(s[0],t[0]),
              max(s[1],t[1])-x,
              x-s[0]+t[1]-x+min(x-s[0],t[1]-x),
              x-t[0]+s[1]-x+min(x-t[0],s[1]-x) ))

Solution (Python):
from bisect import bisect
a,b,q=map(int,input().split())
INF=10**11+7
ls=[-INF]+[int(input()) for _ in range(a)]+[INF]
lt=[-INF]+[int(input()) for _ in range(b)]+[INF]
lq=[int(input()) for _ in range(q)]
for i in lq:
    inds=bisect(ls,i)
    indt=bisect(lt,i)
    s1,s2=i-ls[inds-1],ls[inds]-i
    t1,t2=i-lt[indt-1],lt[indt]-i
    print(min(max(s1,t1),max(s2,t2),min(2*s1+t2,s1+2*t2),min(2*t1+s2,t1+2*s2)))

Solution (Python):
from bisect import bisect_left

A, B, Q = list(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 q in range(Q):
    x = int(input())
    i = bisect_left(S, x)
    j = bisect_left(T, x)

    sl, sr = x-S[i-1], S[i]-x
    tl, tr = x-T[j-1], T[j]-x

    print(min(max(sl, tl),
              max(sr, tr),
              sl*2 + tr,
              tl*2 + sr,
              sr*2 + tl,
              tr*2 + sl))

Solution (Python):
import bisect

INF = 10 ** 18
A, B, Q = map(int, input().split())
shrine_list = [-INF] + [int(input()) for i in range(A)] + [INF]
temple_list = [-INF] + [int(input()) for i in range(B)] + [INF]
question_list = [int(input()) for i in range(Q)]
for q in question_list:
    b, d = bisect.bisect_right(shrine_list, q), bisect.bisect_right(temple_list, q)
    ans = INF
    for s in [shrine_list[b - 1], shrine_list[b]]:
        for t in [temple_list[d - 1], temple_list[d]]:
            ans = min(abs(q - s) + abs(t - s), abs(q - t) + abs(s - t), ans)
    print(ans)

'''
二分検索
深さ優先検索
幅優先検索
累積和
尺取法
動的計画法
mod
XOR
データ構造
'''