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 solve(string):
    a, b, q, *stx = map(int, string.split())
    s = sorted(stx[:a] + [-10**10, 2 * 10**10])
    t = sorted(stx[a:a + b] + [-10**10, 2 * 10**10])
    x = {_q: -1 for _q in stx[-q:]}
    i_s = 0
    i_t = 0
    for _x in sorted(x.keys()):
        while s[i_s] < _x:
            i_s += 1
        while t[i_t] < _x:
            i_t += 1
        l_s, r_s = s[i_s - 1], s[i_s]
        l_t, r_t = t[i_t - 1], t[i_t]
        r_max = max(r_s, r_t)
        l_min = min(l_s, l_t)
        x[_x] = min([
            r_max - _x, _x - l_min, r_t - l_s + min(r_t - _x, _x - l_s),
            r_s - l_t + min(r_s - _x, _x - l_t)
        ])
    return "\n".join([str(x[_x]) for _x in stx[-q:]])


if __name__ == '__main__':
    n, m, l = map(int, input().split())
    print(solve('{} {} {}\n'.format(n, m, l) + '\n'.join([input() for _ in range(n + m + l)])))

Solution (Python):

import sys
import bisect





























def solve(A: int, B: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"):
    s = [-1 * 10**18] + s + [10**18]
    t = [-1 * 10**18] + t + [10 ** 18]
    for query in x:
        nearS = bisect.bisect_right(s, query)
        nearT = bisect.bisect_right(t, query)
        res = 10 ** 18
        for S in [s[nearS - 1], s[nearS]]:
            for T in [t[nearT - 1], t[nearT]]:
                d1 = abs(T - query) + abs(T - S)
                d2 = abs(S - query) + abs(T - S)
                res = min(res, d1, d2)
        print(res)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    Q = int(next(tokens))  
    s = [int(next(tokens)) for _ in range(A)]  
    t = [int(next(tokens)) for _ in range(B)]  
    x = [int(next(tokens)) for _ in range(Q)]  
    solve(A, B, Q, s, t, x)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
A=list(map(int,input().split()))
S,T,X=[[int(input()) for i in range(A[j])] for j in range(3)]
def binary(a,l,):
	low=0
	high=len(l)-1
	while high-low>1:
		mid=(low+high)//2
		if l[mid]<a:
			low=mid
		else:
			high=mid
	return [l[high],l[low]]
			
for x in X:
	s=set(binary(x,S))
	t=set(binary(x,T))
	Min=20000000000
	for i in s:
		for j in t:
			Min=min(Min,min(abs(x-i),abs(x-j))+abs(i-j))
	print(Min)

Solution (Python):
A=list(map(int,input().split()))
S,T,X=[[int(input()) for i in range(A[j])] for j in range(3)]
def binary(a,l,):
	low=0
	high=len(l)-1
	while high-low>1:
		mid=(low+high)//2
		if l[mid]<a:
			low=mid
		else:
			high=mid
	return [l[high],l[low]]
			
for x in X:
	s=set(binary(x,S))
	t=set(binary(x,T))
	Min=20000000000
	for i in s:
		for j in t:
			Min=min(Min,min(abs(x-i),abs(x-j))+abs(i-j))
	print(Min)

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):
import bisect
import sys
input = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
ans = ['' for _ in range(q)]
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans[i] = str(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     x - min(s[idxs - 1], t[idxt -1])))
print('\n'.join(ans))

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans.append(str(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     x - min(s[idxs - 1], t[idxt -1]))))
print('\n'.join(ans))

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    ans.append(min(x - min(s[idxs - 1], t[idxt -1]),
                     x - s[idxs - 1] + (t[idxt] - s[idxs - 1]),
                     t[idxt] - x + (t[idxt] - s[idxs - 1]),
                     x - t[idxt - 1] + (s[idxs] - t[idxt - 1]),
                     s[idxs] - x + (s[idxs] - t[idxt - 1]),
                     max(s[idxs], t[idxt]) - x,
                     x - min(s[idxs - 1], t[idxt -1])))
for i in range(q):
    print(ans[i])

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
ans = []
for i in range(q):
    x = int(input())
    idxs = bisect.bisect(s, x)
    idxt = bisect.bisect(t, x)
    lis = []
    lis.append(x - min(s[idxs - 1], t[idxt -1]))
    lis.append(x - s[idxs - 1] + (t[idxt] - s[idxs - 1]))
    lis.append(t[idxt] - x + (t[idxt] - s[idxs - 1]))
    lis.append(x - t[idxt - 1] + (s[idxs] - t[idxt - 1]))
    lis.append(s[idxs] - x + (s[idxs] - t[idxt - 1]))
    lis.append(max(s[idxs], t[idxt]) - x)
    ans.append(min(lis))
for i in range(q):
    print(ans[i])

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

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

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





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

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

Solution (Python):
import bisect
read = input
bisect_right = bisect.bisect_right

a ,b , q = map(int, read().split())
INF = 10**18
A =[-INF] + [int(read()) for i in range(a)] +[INF]
B =[-INF] + [int(read()) for i in range(b)] +[INF]
A.sort()
B.sort()
try:
    for i in range(q):
        x = int(read())
        sa = bisect_right(A, x)
        sb = bisect_right(B, x)
        ans = INF
        for pa in [A[sa - 1 ], A[sa]]:
            for pb in [B[sb - 1 ], B[sb]]:
                d1 = abs(x - pa) + abs(pa - pb)
                d2 = abs(x - pb) + abs(pb - pa)
                ans = min(ans, d1, d2)
        print(ans)
except:
    print(x, sa, sb)

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

def bifind(r,z):
    if z < r[0]:
        return [r[0],r[0]]
    elif z > r[-1]:
        return [r[-1],r[-1]]
    y = bisect.bisect_left(r,z)-1
    return [r[y],r[y+1]]

for i in range(0,q):
    ls = bifind(s,x[i])
    lt = bifind(t,x[i])
    ans = list()
    for j in range(0,2):
        for k in range(0,2):
            ans.append(abs(ls[j]-x[i])+abs(lt[k]-ls[j]))
            ans.append(abs(lt[k]-x[i])+abs(lt[k]-ls[j]))
    print(min(ans))

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

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

for i in x:
    s_point = bisect.bisect_left(s, i)
    t_point = bisect.bisect_left(t, i)
    dist1 = max(i-s[s_point-1], i-t[t_point-1])
    dist2 = max(s[s_point]-i, t[t_point]-i)
    dist3 = min(2*(i-s[s_point-1])+(t[t_point]-i), (i-s[s_point-1])+2*(t[t_point]-i))
    dist4 = min(2*(i-t[t_point-1])+(s[s_point]-i), (i-t[t_point-1])+2*(s[s_point]-i))
    print(min(dist1, dist2, dist3, dist4))

Solution (Python):
def search(x,r):
  low = 0
  high = len(r) - 1
  while low < high:
    mid = (low+high)//2
    if x < r[mid]:
      high = mid
    else:
      low = mid + 1
  return(abs(r[low-1]-x),abs(r[low]-x))
      
a, b, q = [int(i) for i in input().split()]
s = [-float('inf')] + [int(input()) for _ in range(a)] + [float('inf')]
t = [-float('inf')] + [int(input()) for _ in range(b)] + [float('inf')]
x = [int(input()) for _ in range(q)]

for xx in x:
  
  s_l,s_r=search(xx,s)
  t_l,t_r=search(xx,t)
  
  if s_l <= t_l:
    l = t_l
    lr = s_l*2 + t_r
  else:
    l = s_l
    lr = t_l*2 + s_r
  if s_r <= t_r:
    r = t_r
    rl = s_r*2 + t_l
  else:
    r = s_r
    rl = t_r*2 + s_l
 
  ans = min(l,r,lr,rl)
  print(ans)

Solution (Python):
from bisect import bisect

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

for xx in x:
  
  s_i = bisect(s,xx)
  s_r = abs(xx - s[s_i])
  s_l = abs(xx - s[s_i-1])
  t_i = bisect(t,xx)
  t_r = abs(xx - t[t_i])
  t_l = abs(xx - t[t_i-1])

  
  
  
  
  
  if s_l <= t_l:
    l = t_l
    lr = s_l*2 + t_r
  else:
    l = s_l
    lr = t_l*2 + s_r
  if s_r <= t_r:
    r = t_r
    rl = s_r*2 + t_l
  else:
    r = s_r
    rl = t_r*2 + s_l
 
  ans = min(l,r,lr,rl)
  print(ans)

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
LONG_MIN = -9223372036854775808
LONG_MAX = 9223372036854775807

num_a,num_b,num_q = map(int, input().split(' '))
lsa = [int(input()) for _ in range(num_a)] + [LONG_MIN, LONG_MAX]
lsb = [int(input()) for _ in range(num_b)] + [LONG_MIN, LONG_MAX]
lsa.sort()
lsb.sort()
lsq = [int(input()) for _ in range(num_q)]

for ques in lsq:
    lower_bound_a = bisect(lsa, ques)
    lower_bound_b = bisect(lsb, ques)
    upper_bound_a = lower_bound_a - 1
    upper_bound_b = lower_bound_b - 1

    res = min(
        max(lsa[lower_bound_a], lsb[lower_bound_b]) - ques, 
        ques - min(lsa[upper_bound_a], lsb[upper_bound_b]),
        lsb[lower_bound_b] - ques + lsb[lower_bound_b] - lsa[upper_bound_a],
        ques - lsa[upper_bound_a] + lsb[lower_bound_b] - lsa[upper_bound_a],
        lsa[lower_bound_a] - ques + lsa[lower_bound_a] - lsb[upper_bound_b],
        ques - lsb[upper_bound_b] + lsa[lower_bound_a] - lsb[upper_bound_b],
    )
    
    print(res)

Solution (Python):
from bisect import bisect

num_a,num_b,num_q = map(int, input().split(' '))

lsa = [int(input()) for _ in range(num_a)]
lsb = [int(input()) for _ in range(num_b)]
lsa.sort()
lsb.sort()


lsq = [int(input()) for _ in range(num_q)]

for ques in lsq:
    lower_bound_a = bisect(lsa, ques)
    lower_bound_b = bisect(lsb, ques)
    upper_bound_a = lower_bound_a - 1
    upper_bound_b = lower_bound_b - 1

    min_cand = 9223372036854775807
    if lower_bound_a < num_a and lower_bound_b < num_b:
        min_cand = min(min_cand, max(lsa[lower_bound_a], lsb[lower_bound_b]) - ques)
    if upper_bound_a >= 0 and upper_bound_b >= 0:
        min_cand = min(min_cand, ques - min(lsa[upper_bound_a], lsb[upper_bound_b]))
    if upper_bound_a >= 0 and lower_bound_b < num_b:
        min_cand = min(
            min_cand, 
            lsb[lower_bound_b] - ques + lsb[lower_bound_b] - lsa[upper_bound_a],
            ques - lsa[upper_bound_a] + lsb[lower_bound_b] - lsa[upper_bound_a],
        )
    if lower_bound_a < num_a and upper_bound_b >= 0:
        min_cand = min(
            min_cand, 
            lsa[lower_bound_a] - ques + lsa[lower_bound_a] - lsb[upper_bound_b],
            ques - lsb[upper_bound_b] + lsa[lower_bound_a] - lsb[upper_bound_b],
        )
    
    print(min_cand)

Solution (Python):
import bisect


def solve(a_list, b_list, position):
    
    a_right_key = bisect.bisect_left(a_list, position)
    a_left_key = a_right_key - 1
    
    b_right_key = bisect.bisect_left(b_list, position)
    b_left_key = b_right_key - 1

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

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


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

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

    
    for _ in range(Q):
        position = int(input())
        ans = solve(a_list, b_list, position)
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

shrines_many, temples_many, x_many = map(int, input().split())
shrine_positions = [int(input()) for _ in range(shrines_many)]
temple_positions = [int(input()) for _ in range(temples_many)]

inf = int(1e20)
shrine_positions = [-inf] + sorted(shrine_positions) + [inf]
temple_positions = [-inf] + sorted(temple_positions) + [inf]


for _ in range(x_many):
    x = int(input())
    b = bisect.bisect_right(shrine_positions, x)
    d = bisect.bisect_right(temple_positions, x)
    min_distance = inf
    for shrine_position in [shrine_positions[b-1], shrine_positions[b]]:
        for temple_position in [temple_positions[d-1], temple_positions[d]]:
            distance1 = abs(x - shrine_position) + abs(shrine_position - temple_position)
            distance2 = abs(x - temple_position) + abs(shrine_position - temple_position)
            min_distance = min(min_distance, distance1, distance2)

    print(min_distance)

Solution (Python):
import sys
import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
INF = 10**20

A,B,Q = map(int,input().split())
s = [-INF]+list(map(int, (sys.stdin.readline() for _ in [0]*A)))+[INF]
t = [-INF]+list(map(int, (sys.stdin.readline() for _ in [0]*B)))+[INF]
queries=list(map(int,sys.stdin))
for q in queries:
    b,d = bisect.bisect_right(s,q),bisect.bisect_right(t,q)
    ans = INF
    for S in [s[b-1],s[b]]:
        for T in [t[d-1],t[d]]:
            d1,d2 = abs(S-q)+abs(T-S),abs(T-q)+abs(S-T)
            ans = min(ans,d1,d2)
    print(ans)

Solution (Python):

import sys
from bisect import bisect_left

def main():
	A, B, Q = map(int, input().split(" "))
	S = []
	S.append(-100000000000)
	for _ in range(A):
		tmp = int(input())
		S.append(tmp)
	S.append(100000000000)
	
	T = []
	T.append(-100000000000)
	for _ in range(B):
		tmp = int(input())
		T.append(tmp)
	T.append(100000000000)
	
	X = []
	for _ in range(Q):
		tmp = int(input())
		X.append(tmp)
		
	
	for i, val in enumerate(X):
		tmp = [[-1 for _ in range(2)] for _ in range(2)]
		tmp[0][0] = bisect_left(S, val)
		tmp[0][1] = tmp[0][0] - 1
		tmp[1][0] = bisect_left(T, val)
		tmp[1][1] = tmp[1][0] - 1
		
		ptn1 = abs(val - S[tmp[0][0]]) + abs(S[tmp[0][0]] - T[tmp[1][0]])
		ptn2 = abs(val - S[tmp[0][0]]) + abs(S[tmp[0][0]] - T[tmp[1][1]])
		ptn3 = abs(val - S[tmp[0][1]]) + abs(S[tmp[0][1]] - T[tmp[1][0]])
		ptn4 = abs(val - S[tmp[0][1]]) + abs(S[tmp[0][1]] - T[tmp[1][1]])
		
		ptn5 = abs(val - T[tmp[1][0]]) + abs(T[tmp[1][0]] - S[tmp[0][0]])
		ptn6 = abs(val - T[tmp[1][0]]) + abs(T[tmp[1][0]] - S[tmp[0][1]])
		ptn7 = abs(val - T[tmp[1][1]]) + abs(T[tmp[1][1]] - S[tmp[0][0]])
		ptn8 = abs(val - T[tmp[1][1]]) + abs(T[tmp[1][1]] - S[tmp[0][1]])
		
		print(min(ptn1,ptn2,ptn3,ptn4,ptn5,ptn6,ptn7,ptn8))
	
	
if __name__ == "__main__":
	main()

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

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

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

Solution (Python):
import bisect 
INF = 10**18

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)]
s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i in x:
    index_s = bisect.bisect_left(s, i)
    
    index_t = bisect.bisect_left(t, i)
    
    l_s = s[index_s-1]
    r_s = s[index_s-0]
    l_t = t[index_t-1]
    r_t = t[index_t-0]
    ret = INF
    for ss in [l_s,r_s]:
        for tt in [l_t,r_t]:
            d1 = abs(ss-i) + abs(tt-ss)
            d2 = abs(tt-i) + abs(ss-tt)
            ret = min(d1,d2,ret)
    print(ret)

Solution (Python):

import bisect

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

Solution (Python):

"""
D - Lazy Faith
https://atcoder.jp/contests/abc119/tasks/abc119_d

"""
import sys
from bisect import bisect_left, bisect_right

def solve(x):
    i = bisect_right(shrines, x)
    s_left = x - shrines[i-1]
    j = bisect_right(temples, x)
    t_left = x - temples[j-1]

    i = bisect_left(shrines, x)
    s_right = shrines[i] - x
    j = bisect_left(temples, x)
    t_right = temples[j] - x

    ans1 = max(s_left, t_left)
    ans2 = max(s_right, t_right)
    ans3 = min(s_right, t_left) * 2 + max(s_right, t_left)
    ans4 = min(s_left, t_right) * 2 + max(s_left, t_right)
    return min(ans1, ans2, ans3, ans4)


shrines = None
temples = None

def main(args):
    global shrines, temples
    A, B, Q = map(int, input().split())
    shrines = [float('-inf')] + [int(input()) for _ in range(A)] + [float('inf')]
    temples = [float('-inf')] + [int(input()) for _ in range(B)] + [float('inf')]
    for _ in range(Q):
        x = int(input())
        ans = solve(x)
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):

"""
D - Lazy Faith
https://atcoder.jp/contests/abc119/tasks/abc119_d

"""
import sys
from bisect import bisect_left, bisect_right

def solve(x):
    i = bisect_right(shrines, x)
    ds = x - shrines[i-1]
    j = bisect_right(temples, x)
    dt = x - temples[j-1]
    ans1 = max(ds, dt)

    i = bisect_left(shrines, x)
    ds = shrines[i] - x
    j = bisect_left(temples, x)
    dt = temples[j] - x
    ans2 = max(ds, dt)

    i = bisect_left(shrines, x)
    ds = shrines[i] - x
    j = bisect_right(temples, x)
    dt = x - temples[j-1]
    ans3 = min(ds, dt) * 2 + max(ds, dt)

    i = bisect_right(shrines, x)
    ds = x - shrines[i-1]
    j = bisect_left(temples, x)
    dt = temples[j] - x
    ans4 = min(ds, dt) * 2 + max(ds, dt)

    return min(ans1, ans2, ans3, ans4)


shrines = None
temples = None

def main(args):
    global shrines, temples
    A, B, Q = map(int, input().split())
    shrines = sorted([int(input()) for _ in range(A)] + [float('-inf'), float('inf')])
    temples = sorted([int(input()) for _ in range(B)] + [float('-inf'), float('inf')])
    for _ in range(Q):
        x = int(input())
        ans = solve(x)
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):

"""
D - Lazy Faith
https://atcoder.jp/contests/abc119/tasks/abc119_d

"""
import sys
from bisect import bisect_left, bisect_right

def solve(shrines, temples, x):
    i = bisect_right(shrines, x)
    ds = x - shrines[i-1]
    j = bisect_right(temples, x)
    dt = x - temples[j-1]
    ans = max(ds, dt)

    i = bisect_left(shrines, x)
    ds = shrines[i] - x
    j = bisect_left(temples, x)
    dt = temples[j] - x
    ans = min(ans, max(ds, dt))

    i = bisect_left(shrines, x)
    ds = shrines[i] - x
    j = bisect_right(temples, x)
    dt = x - temples[j-1]
    d = min(ds, dt) * 2 + max(ds, dt)
    ans = min(ans, d)

    i = bisect_right(shrines, x)
    ds = x - shrines[i-1]
    j = bisect_left(temples, x)
    dt = temples[j] - x
    d = min(ds, dt) * 2 + max(ds, dt)
    ans = min(ans, d)

    return ans


def main(args):
    A, B, Q = map(int, input().split())
    shrines = sorted([int(input()) for _ in range(A)] + [float('-inf'), float('inf')])
    temples = sorted([int(input()) for _ in range(B)] + [float('-inf'), float('inf')])
    for _ in range(Q):
        x = int(input())
        ans = solve(shrines, temples, x)
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import bisect

A, B, Q = list(map(int, input().split()))
S = [-10**20] + [int(input()) for i in range(A)] + [10**20]
T = [-10**20] + [int(input()) for i in range(B)] + [10**20]

for i in range(Q):
	x = int(input())
	s = bisect.bisect_left(S, x)
	t = bisect.bisect_left(T, x)
	num = [x-min(S[s-1], T[t-1]),
				S[s]-T[t-1]+min(S[s]-x, x-T[t-1]),
				T[t]-S[s-1]+min(x-S[s-1], T[t]-x),
				max(S[s], T[t])-x]
	print(min(num))

Solution (Python):
from sys import stdin
import bisect

INF = 10 ** 11

A, B, Q = [int(x) for x in stdin.readline().rstrip().split()]
s_list = [int(stdin.readline().rstrip()) for _ in range(A)]
t_list = [int(stdin.readline().rstrip()) for _ in range(B)]
x_list = [int(stdin.readline().rstrip()) for _ in range(Q)]


def search_lr_distance(target_list, num):
    i = bisect.bisect_left(target_list, num)
    left_index = i - 1
    right_index = i
    
    if left_index < 0:
        left_distance = INF
    else:
        left_distance = num - target_list[left_index]
    
    if right_index > len(target_list)-1:
        right_distance = INF
    else:
        right_distance = target_list[right_index] - num


    return left_distance, right_distance


for x in x_list:
    min_dist = INF
    for s1, s2 in [[t_list, s_list], [s_list, t_list]]:
        move_left, move_right = search_lr_distance(s1, x)
        f_dist1, f_dist2 = search_lr_distance(s2, x+move_right)
        f_dist3, f_dist4 = search_lr_distance(s2, x-move_left)

        min_dist = min(min_dist, f_dist1+move_right, f_dist2+move_right, f_dist3+move_left, f_dist4+move_left)

    print(min_dist)

Solution (Python):
A,B,Q = map(int,input().split())
import bisect
s=[]
t=[]
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
s = [-10**20] + s + [10**20]
t = [-10**20] + t + [10**20]
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = 10**20
    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):
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):
import bisect
a, b, q = map(int, input().split())
s = []
for i in range(a):
    s.append(int(input()))
t = []
for j in range(b):
    t.append(int(input()))
inf = 10 ** 18
s = [-inf] + s + [inf]
t = [-inf] + t + [inf]
for k in range(q):
    x = int(input())
    s_r, t_r = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = inf
    for s_ in [s[s_r], s[s_r - 1]]:
        for t_ in [t[t_r], t[t_r - 1]]:
            d = min(abs(x-s_), abs(x-t_)) + abs(s_ - t_)
            res = min(d, res)
    print(res)

Solution (Python):
import numpy as np
import bisect

def main():
	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]
	X=[int(input()) for i in range(q)]

	for x in X:
		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)

def inputList():
	a=list(map(int,input().split()))
	return a

def multisort(li,index):
	return sorted(li,key=lambda x: x[index])

if(__name__=='__main__'):
	main()

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

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 get_near(e, x):
    xi = bisect.bisect_left(x,e)
    if xi == 0:
        return [x[0]]
    if xi == len(x):
        return [x[-1]]
    return [x[xi-1], x[xi]]

for e in x:
    s_ = get_near(e,s)
    t_ = get_near(e,t)
    min_dist = 10**11
    for si in s_:
        for ti in t_:
            dist = min(abs(e-si), abs(e-ti)) + abs(si-ti)
            min_dist = min(min_dist, dist)

    print(min_dist)

Solution (Python):
from bisect import bisect_left
try:
    from math import inf
except:
    inf = float("inf")


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


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

for xi in x:
    isr = bisect_left(s, xi)
    itr = bisect_left(t, xi)
    isl = isr - 1
    itl = itr - 1
    cand = []
    if 0 <= isl < A and 0 <= itl < B:
        cand.append((s[isl], t[itl]))
    if 0 <= isl < A and 0 <= itr < B:
        cand.append((s[isl], t[itr]))
    if 0 <= isr < A and 0 <= itl < B:
        cand.append((s[isr], t[itl]))
    if 0 <= isr < A and 0 <= itr < B:
        cand.append((s[isr], t[itr]))
    ans = inf
    for a, b in cand:
        ans = min(ans, way(xi, a, b), way(xi, b, a))
    print(ans)