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: Accepted

Solution code:
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF]
t = [-INF]
for _ in range(A):
    s.append(int(input()))
s.append(INF)
for _ in range(B):
    t.append(int(input()))
t.append(INF)
for q in range(Q):
    x = int(input())
    i, j = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    ans = min(
        max(abs(s[i] - x), abs(t[j] - x)),
        max(abs(s[i - 1] - x), abs(t[j - 1] - x)),
        2 * min(abs(s[i] - x), abs(t[j - 1] - x)) + max(abs(s[i] - x), abs(t[j - 1] - x)),
        2 * min(abs(s[i - 1] - x), abs(t[j] - x)) + max(abs(s[i - 1] - x), abs(t[j] - x))
    )
    print(ans)

Submission status: Accepted

Solution code:
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    i, j = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    ans = min(
        max(abs(s[i] - x), abs(t[j] - x)),
        max(abs(s[i - 1] - x), abs(t[j - 1] - x)),
        2 * min(abs(s[i] - x), abs(t[j - 1] - x)) + max(abs(s[i] - x), abs(t[j - 1] - x)),
        2 * min(abs(s[i - 1] - x), abs(t[j] - x)) + max(abs(s[i - 1] - x), abs(t[j] - x))
    )
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import bisect
 
(A,B,Q) = map(int,input().split())
inf = 10**12
s = np.empty(A+2,dtype=np.object)
t = np.empty(B+2,dtype=np.object)
s[0] = -inf
t[0] = -inf
s[A+1] = inf
t[B+1] = inf
 
for i in range(A):
    s[i+1] = int(input())
for i in range(B):
    t[i+1] = int(input())
 
for i in range(Q):
    x = int(input())
    p = bisect.bisect_left(s,x)
    q = bisect.bisect_left(t,x)
    ans = min(max(s[p]-x,t[q]-x),
              max(x-s[p-1],x-t[q-1]),
                  (s[p]-x)*2 + (x-t[q-1]),
                  (s[p]-x) + (x-t[q-1])*2,
                  (t[q]-x)*2 + (x-s[p-1]),
                  (t[q]-x) + (x-s[p-1])*2)
    print(ans)

Submission status: Accepted

Solution code:
import bisect

A, B, Q = map(int, input().split())
s = [0]*A
for i in range(A):
    s[i] = int(input())
t = [0]*B
for i in range(B):
    t[i] = int(input())
X = [0]*Q
for i in range(Q):
    X[i] = int(input())

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

for x in X:
    sr, tr = bisect.bisect_right(s, x), bisect.bisect_left(t, x)
    res = 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)
            res = min(res, d1, d2)
    print(res)

Submission status: Accepted

Solution code:
import bisect
A, B, Q = map(int, input().split())
INF = float("inf")
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]

for q in range(Q):
    x = int(input())
    ans = INF
    a_i = bisect.bisect_right(s, x)
    b_i = bisect.bisect_right(t, x)
    for a in [s[a_i], s[a_i - 1]]:
        for b in [t[b_i], t[b_i - 1]]:
            ans = min(ans, abs(x - a) + abs(b - a), abs(x - b) + abs(b - a))
    print(ans)

Submission status: Accepted

Solution code:
import bisect
A, B, Q = list(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:
    s_min = bisect.bisect_left(s, x)
    t_min = bisect.bisect_left(t, x)

    cost = INF
    for S in [s[s_min-1], s[s_min]]:
        for T in [t[t_min-1], t[t_min]]:
            cost = min(cost, abs(T-S)+abs(T-x), abs(T-S)+abs(S-x))

    print(cost)

Submission status: Accepted

Solution code:
from bisect import bisect_right
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_right(s, x), 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)

Submission status: Accepted

Solution code:
import bisect

INF = 10 ** 18

def main():
    A, B, Q = list(map(int, input().split()))
    s = []
    t = []
    for i in range(A):
        s.append(input())
    for i in range(B):
        t.append(input())
    s = [-INF] + sorted(list(map(int, s))) + [INF]
    t = [-INF] + sorted(list(map(int, t))) + [INF]

    for i in range(Q):
        x = int(input())
        a = bisect.bisect_right(s, x)
        b = bisect.bisect_right(t, x)
        ret = INF
        for j in [s[a-1], s[a]]:
            for k in [t[b-1], t[b]]:
                ret = min(ret, abs(x-j)+abs(j-k), abs(x-k)+abs(j-k))
        print(ret)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
import bisect
import sys
input = sys.stdin.readline

(A,B,Q) = map(int,input().split())
inf = 10**12
s = [0] * (A+2)
t = [0] * (B+2)
s[0] = -inf
t[0] = -inf
s[A+1] = inf
t[B+1] = inf

for i in range(A):
    s[i+1] = int(input())
for i in range(B):
    t[i+1] = int(input())

for _ in range(Q):
    x = int(input())
    p = bisect.bisect_left(s,x)
    q = bisect.bisect_left(t,x)
    spx = s[p] - x
    tqx = t[q] - x
    xtq1 = x - t[q-1]
    xsp1 = x - s[p-1]
    ans = min(max(spx, tqx),
              max(xsp1, xtq1),
                  spx*2 + xtq1,
                  spx + xtq1*2,
                  tqx*2 + xsp1,
                  t[q]-x + xsp1*2)
    print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

A, B, Q = map(int, input().split())
INF = 10**18
s = [-INF]
t = [-INF]
for _ in range(A):
    s.append(int(input()))
s.append(INF)
for _ in range(B):
    t.append(int(input()))
t.append(INF)
x = list(map(int, (input() for _ in range(Q))))
for y in x:
    i = bisect_right(s, y)
    j = bisect_right(t, y)
    # ans = INF
    # for S in [s[i - 1], s[i]]:
    #     for T in [t[j - 1], t[j]]:
    #         d1, d2 = abs(S - y) + abs(T - S), abs(T - y) + abs(S - T)
    #         ans = min(ans, d1, d2)
    ans = min(
        max(abs(s[i] - y), abs(t[j] - y)),
        max(abs(s[i - 1] - y), abs(t[j - 1] - y)),
        2 * min(abs(s[i] - y), abs(t[j - 1] - y)) + max(abs(s[i] - y), abs(t[j - 1] - y)),
        2 * min(abs(s[i - 1] - y), abs(t[j] - y)) + max(abs(s[i - 1] - y), abs(t[j] - y))
    )
    print(ans)

Submission status: Accepted

Solution code:
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
	res = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
			res = min(res, d1, d2)
	print(res)

Submission status: Accepted

Solution code:
import bisect

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

def solve(start):
    ans=[]
    c,d=bisect.bisect_left(s,start),bisect.bisect_left(t,start)
    for S in [s[c],s[c-1]]:
        for T in [t[d],t[d-1]]:
            ans.append(min(abs(start-S)+abs(S-T),abs(start-T)+abs(S-T)))

    print(min(ans))

for e in q:
    solve(e)

Submission status: Accepted

Solution code:
from bisect import bisect

A, B, Q = map(int, input().split())
INF = 10**18
s = [-INF]
t = [-INF]
for _ in range(A):
    s.append(int(input()))
s.append(INF)
for _ in range(B):
    t.append(int(input()))
t.append(INF)
x = list(map(int, (input() for _ in range(Q))))
for y in x:
    i = bisect(s, y)
    j = bisect(t, y)
    ans = INF
    for S in [s[i - 1], s[i]]:
        for T in [t[j - 1], t[j]]:
            d1, d2 = abs(S - y) + abs(T - S), abs(T - y) + abs(S - T)
            ans = min(ans, d1, d2)
    # ans = min(
    #     max(abs(s[i] - y), abs(t[j] - y)),
    #     max(abs(s[i - 1] - y), abs(t[j - 1] - y)),
    #     2 * min(abs(s[i] - y), abs(t[j - 1] - y)) + max(abs(s[i] - y), abs(t[j - 1] - y)),
    #     2 * min(abs(s[i - 1] - y), abs(t[j] - y)) + max(abs(s[i - 1] - y), abs(t[j] - y))
    # )
    print(ans)

Submission status: Accepted

Solution code:
import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])+min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])+min(abs(cc-aa[aaa]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[a-1]),abs(cc - bb[0])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1],2*bb[bbb] - cc - aa[aaa-1],bb[bbb] + cc - 2 * aa[aaa-1])
		print(aaaa)

Submission status: Accepted

Solution code:
from bisect import bisect

A, B, Q = map(int, input().split())
s = [-10**15] + [int(input()) for i in range(A)] + [10**15]
t = [-10**15] + [int(input()) for i in range(B)] + [10**15]
# s = [-10**15]
# t = [-10**15]
# for _ in range(A):
#     s.append(int(input()))
# s.append(10**15)
# for _ in range(B):
#     t.append(int(input()))
# t.append(10**15)
x = list(map(int, (input() for _ in range(Q))))
for y in x:
    i = bisect(s, y)
    j = bisect(t, y)
    ans = min(
        max(abs(s[i] - y), abs(t[j] - y)),
        max(abs(s[i - 1] - y), abs(t[j - 1] - y)),
        2 * min(abs(s[i] - y), abs(t[j - 1] - y)) + max(abs(s[i] - y), abs(t[j - 1] - y)),
        2 * min(abs(s[i - 1] - y), abs(t[j] - y)) + max(abs(s[i - 1] - y), abs(t[j] - y))
    )
    print(ans)

Submission status: Accepted

Solution code:
from bisect import *
A,B,Q=map(int,input().split())
s=[-10**11]+[int(input()) for i in range(A)]+[10**11]
t=[-10**11]+[int(input()) for i in range(B)]+[10**11]
x=[int(input()) for i in range(Q)]
for i in range(Q):
    S=[0,0]
    T=[0,0]
    sidx=bisect_left(s,x[i])-1
    S[0]=s[sidx]
    S[1]=s[sidx+1]
    tidx=bisect_left(t,x[i])-1
    T[0]=t[tidx]
    T[1]=t[tidx+1]
    ans=10**12
    seq=[(0,1),(1,0),(1,1),(0,0)]
    for a,b in seq:
        tmp=abs(x[i]-S[a])+abs(S[a]-T[b])
        ans=min(ans,tmp)
        tmp=abs(x[i]-T[a])+abs(T[a]-S[b])
        ans=min(ans,tmp)
    print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
A,B,Q=map(int,input().split())
inf=float("inf")
S=[-inf]+[int(input()) for i in range(A)]+[inf]
T=[-inf]+[int(input()) for i in range(B)]+[inf]
X=[int(input()) for i in range(Q)]
S.sort()
T.sort()
for x in X:
  s_index=bisect_left(S,x)
  t_index=bisect_left(T,x)
  s_l=abs(S[s_index-1]-x)
  s_r=abs(S[s_index]-x)
  t_l=abs(T[t_index-1]-x)
  t_r=abs(T[t_index]-x)
  print(min(max(s_l,t_l),max(s_r,t_r),2*s_l+t_r,2*s_r+t_l,2*t_l+s_r,2*t_r+s_l))

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
a,b,q=map(int,input().split())
I=2<<60
s,t=[-I]+[int(input()) for _ in range(a)]+[I],[-I]+[int(input()) for _ in range(b)]+[I]
for question in range(q):
    x=int(input())
    b,d,r=br(s,x),br(t,x),I
    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)
            r=min(r,d1,d2)
    print(r)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import bisect

INF = 1e20

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

for x in X:
    ans = INF
    iS, iT = bisect.bisect_left(S, x), bisect.bisect_left(T, x)

    for s in [S[iS - 1], S[iS]]:
        for t in [T[iT - 1], T[iT]]:
            a1, a2 = abs(s - x) + abs(s - t), abs(t - x) + abs(t - s)
            ans = min(ans, a1, a2)

    print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


inf = float('inf')


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_list_of_int():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


# inputs
A, B, Q = 0, 0, 0
S = []
T = []
X = []


def set_inputs():
    global A, B, Q, S, T, X
    A, B, Q = get_list_of_int()
    for _ in range(A):
        S.append(get_int())
    for _ in range(B):
        T.append(get_int())
    for _ in range(Q):
        X.append(get_int())


def main():
    set_inputs()

    for i in range(Q):
        nsi = bisect_left(S, X[i])
        if nsi == 0:
            ns = inf
        else:
            ns = X[i] - S[nsi-1]
        psi = bisect_left(S, X[i])
        if psi == A:
            ps = inf
        else:
            ps = S[nsi] - X[i]
        nti = bisect_left(T, X[i])
        if nti == 0:
            nt = inf
        else:
            nt = X[i] - T[nti-1]
        pti = bisect_left(T, X[i])
        if pti == B:
            pt = inf
        else:
            pt = T[nti] - X[i]
        print(min(max(ns, nt), max(ps, pt), ns * 2 + pt, ns + pt * 2, nt * 2 + ps, nt + ps * 2))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
import sys
#input = sys.stdin.readline
A, B, Q = list(map(int, input().split()))
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
s.append(-100000000000);s.append(100000000000)
t.append(-100000000000);t.append(100000000000)
s = sorted(s, reverse=False)
t = sorted(t, reverse=False)

for i in range(Q):
    xi = int(input())
    sr = s[bisect.bisect_left(s, xi)]
    sl = s[bisect.bisect_left(s, xi) - 1]
    tr = t[bisect.bisect_left(t, xi)]
    tl = t[bisect.bisect_left(t, xi) - 1]

    ans = min(max(sr, tr) - xi, xi - min(sl, tl),
              tr - sl + min(tr - xi, xi - sl), sr - tl + min(sr - xi, xi - tl))
    print(ans)

Submission status: Accepted

Solution code:
import bisect
import sys
input = sys.stdin.readline
A, B, Q = list(map(int, input().split()))
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
s.append(-100000000000);s.append(100000000000)
t.append(-100000000000);t.append(100000000000)
s = sorted(s, reverse=False)
t = sorted(t, reverse=False)

for i in range(Q):
    xi = int(input())
    sr = s[bisect.bisect_left(s, xi)]
    sl = s[bisect.bisect_left(s, xi) - 1]
    tr = t[bisect.bisect_left(t, xi)]
    tl = t[bisect.bisect_left(t, xi) - 1]

    ans = min(max(sr, tr) - xi, xi - min(sl, tl),
              tr - sl + min(tr - xi, xi - sl), sr - tl + min(sr - xi, xi - tl))
    print(ans)

Submission status: Accepted

Solution code:
A, B, Q = [int(_) for _ in input().split()]
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
Q = [int(input()) for i in range(Q)]

import bisect

def calc_nearest_distance(xs, ys):
    result = []
    for x in xs:
        i = bisect.bisect(ys, x)
        if i == 0:
            d = abs(ys[i] - x)
        elif i < len(ys):
            d = min(abs(ys[i - 1] - x), abs(ys[i] - x))
        else:
            d = abs(ys[i - 1] - x)
        result.append(d)
    return result

sn = calc_nearest_distance(S, T)
tn = calc_nearest_distance(T, S)

def solve(q, S, T, sn, tn):
    nsp = bisect.bisect(S, q)
    ntp = bisect.bisect(T, q)

    rs = []

    if 0 <= nsp < len(S):
        rs.append(abs(S[nsp] - q) + sn[nsp])
    if 0 <= nsp - 1 < len(S):
        rs.append(abs(S[nsp - 1] - q) + sn[nsp - 1])

    if 0 <= ntp < len(T):
        rs.append(abs(T[ntp] - q) + tn[ntp])
    if 0 <= ntp - 1 < len(T):
        rs.append(abs(T[ntp - 1] - q) + tn[ntp - 1])

    return min(rs)

for q in Q:
    print(solve(q, S, T, sn, tn))

Submission status: Accepted

Solution code:
import bisect


def solve(A, B, Q, Ss, Ts, Xs):
    ans = []
    Ss.append(10 ** 21)
    Ss.insert(0, -10 ** 21)
    Ts.append(10 ** 21)
    Ts.insert(0, -10 ** 21)
    for x in Xs:
        s_right = bisect.bisect_right(Ss, x)
        s_left = max(s_right - 1, 0)
        t_right = bisect.bisect_right(Ts, x)
        t_left = max(t_right - 1, 0)

        s_right = Ss[s_right]
        s_left = Ss[s_left]
        t_right = Ts[t_right]
        t_left = Ts[t_left]

        d_s_right = abs(s_right - x)
        d_s_left = abs(s_left - x)
        d_t_right = abs(t_right - x)
        d_t_left = abs(t_left - x)

        tans = 10 ** 35
        if x <= s_right and x <= t_right:
            tans = min(tans, max(d_t_right, d_s_right))
        if s_left <= x and t_left <= x:
            tans = min(tans, max(d_s_left, d_t_left))
        tans = min(tans, min(d_s_right, d_t_left) * 2 + max(d_s_right, d_t_left))
        tans = min(tans, min(d_t_right, d_s_left) * 2 + max(d_t_right, d_s_left))
        ans.append(str(tans))
    return "\n".join(ans)


if __name__ == "__main__":
    A, B, Q = tuple(map(int, input().split(" ")))
    Ss = [int(input()) for _ in range(A)]
    Ts = [int(input()) for _ in range(B)]
    Xs = [int(input()) for _ in range(Q)]
    print(solve(A, B, Q, Ss, Ts, Xs))

Submission status: Accepted

Solution code:
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 _ in range(Q)]

s_list.sort()
t_list.sort()
s_list = [-float("inf")] + s_list + [float("inf")]
t_list = [-float("inf")] + t_list + [float("inf")]
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    for s in (s_list[s_i - 1], s_list[s_i]):
        for t in (t_list[t_i - 1], t_list[t_i]):
            d1 = abs(s - x) + abs(t - s)
            d2 = abs(t - x) + abs(s - t)
            tmp = min(tmp, d1, d2)
    ans.append(tmp)
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
import bisect
A, B, Q = map(int, input().split())
INF = 10**18
shrine = [-INF] + [int(input()) for i in range(A)] + [INF]
temple = [-INF] + [int(input()) for i in range(B)] + [INF]

for i in range(Q):
    pos = int(input())
    b,d = bisect.bisect_right(shrine, pos), bisect.bisect_right(temple, pos)
    res = INF
    for S in [shrine[b-1], shrine[b]]:
        for T in [temple[d-1], temple[d]]:
            d1, d2 = abs(S-pos)+abs(T-S), abs(T-pos)+abs(S-T)
            res = min(res, d1, d2)
    print(res)

Submission status: Accepted

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

inf = 10000000000000000000000
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]

S.append(inf)
S.append(-inf)
T.append(inf)
T.append(-inf)

X = [int(input()) for _ in range(Q)]

for x in X:
    ans = 0
    S_ind = bisect.bisect_left(S,x,0,A)
    T_ind = bisect.bisect_left(T,x,0,B)

    # print(S_ind,T_ind,'tar=',x)
    # L_s,R_s = S[S_ind-1],S[S_ind]
    # L_t,R_t = T[T_ind-1],T[T_ind]

    # print(L_s,L_t, x, R_s, R_t)

    tmp1,tmp2,tmp3,tmp4 = 100000000000,100000000000,100000000000,100000000000


    L_s,R_s = S[S_ind-1],S[S_ind]
    L_t,R_t = T[T_ind-1],T[T_ind]

    if True:# 0 < S_ind < A and 0 < T_ind < B:

        # L_s,R_s = S[S_ind-1],S[S_ind]
        # L_t,R_t = T[T_ind-1],T[T_ind]

        #同じ方向に進むとき
        tmp1 = max(x - L_s, x - L_t)
        tmp2 = max(-(x - R_s), -(x - R_t))

        #行って戻るとき
        if L_s < L_t and R_s < R_t:
            tmp3 = 2*min(x - L_t, R_s - x) + max(x - L_t, R_s - x)
        if L_t < L_s and R_t < R_s:
            tmp4 = 2*min(x - L_s, R_t - x) + max(x - L_s, R_t - x)

        ans = min(tmp1,tmp2,tmp3,tmp4)

    print(ans)

Submission status: Accepted

Solution code:
import bisect
a, b, q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
for question in range(q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
    for shrine in [s[b - 1], s[b]]:
        for temple in [t[d - 1], t[d]]:
            d1, d2 = abs(shrine - x) + abs(temple - shrine), abs(temple - x) + abs(shrine - temple)
            res = min(res, d1, d2)
    print(res)

Submission status: Accepted

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

Z=[-10**10]
T=[-10**10]

for i in range(A):
    s=int(input())
    Z.append(s)
Z.append(10**11)
for i in range(B):
    t=int(input())
    T.append(t)
T.append(10**11)
    
for i in range(Q):
    x=int(input())
    a=max(Z[bisect.bisect_left(Z,x)],T[bisect.bisect_left(T,x)])-x
    b=x-min(Z[bisect.bisect_left(Z,x)-1],T[bisect.bisect_left(T,x)-1])
    c=Z[bisect.bisect_left(Z,x)]-T[bisect.bisect_left(T,x)-1]+(x-T[bisect.bisect_left(T,x)-1])
    d=Z[bisect.bisect_left(Z,x)]-T[bisect.bisect_left(T,x)-1]+(Z[bisect.bisect_left(Z,x)]-x)
    e=T[bisect.bisect_left(T,x)]-Z[bisect.bisect_left(Z,x)-1]+(x-Z[bisect.bisect_left(Z,x)-1])
    f=T[bisect.bisect_left(T,x)]-Z[bisect.bisect_left(Z,x)-1]+(T[bisect.bisect_left(T,x)]-x)
    print(min(a,b,c,d,e,f))

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def bin_search(S, x):
    L = 0
    R = len(S) - 1

    while 1:
        M = (L + R)/2
        if (R - L) <= 1:
            if abs(S[R] - x) < abs(S[L] - x):
                return (R, L)
            elif abs(S[R] - x) > abs(S[L] - x):
                return (L, R)
            else:
                if L != R:
                    return (L, R)
                else:
                    return (L, R)
        if x == S[L]:
            return (L, R)
        if x == S[R]:
            return (R, R)
        if S[M] < x:
            L = M
        else:
            R = M



def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [A, B, Q] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2])]

    jinja = []
    for i in range(A):
        input_raw = read_func().strip().split()
        jinja.append(long(input_raw[0]))
    tera = []
    for i in range(B):
        input_raw = read_func().strip().split()
        tera.append(long(input_raw[0]))

    for i in range(Q):
        input_raw = read_func().strip().split()
        x = long(input_raw[0])
        dist1 = [2**63-1 for i in range(2)]
        z = bin_search(jinja, x)
        for j in range(len(z)):
            if z[j] != -1:
                dist1[j] = abs(jinja[z[j]] - x)
                y = bin_search(tera, jinja[z[j]])
                dist1[j]  += abs(tera[y[0]] - jinja[z[j]])

        dist2 = [2**63-1 for i in range(2)]
        z = bin_search(tera, x)
        for j in range(len(z)):
            if z[j] != -1:
                dist2[j] = abs(tera[z[j]] - x)
                y = bin_search(jinja, tera[z[j]])
                dist2[j]  += abs(jinja[y[0]] - tera[z[j]])
        print min(min(dist1), min(dist2))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
a, b, q = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
x = [int(input()) for _ in range(q)]
for i in x:
    si = bisect.bisect_left(s,i)
    ti = bisect.bisect_left(t,i)
    sl = 100000000000 if si == 0 else i - s[si-1]
    sr = 100000000000 if si == a else s[si] - i
    tl = 100000000000 if ti == 0 else i - t[ti-1]
    tr = 100000000000 if ti == b else t[ti] - i
    print(min(max(sl,tl), max(sr,tr), max(sl,tr) + min(sl, tr)*2, max(sr,tl) + min(sr,tl)*2))

Submission status: Accepted

Solution code:
A,B,Q=list(map(int,input().split()))
s=[0]*(A+2)
t=[0]*(B+2)
X=[0]*Q
s[0] = -float('inf')
s[A+1] = float('inf')
t[0] = -float('inf')
t[B+1] = float('inf')
for i in range(1,A+1):
    s[i] = int(input())
for i in range(1,B+1):
    t[i] = int(input())
for i in range(Q):
    X[i] = int(input())

import bisect

for x in X:
    s_i = bisect.bisect_left(s,x)
    t_i = bisect.bisect_left(t,x)
    s_l = abs(s[s_i-1]-x)
    s_r = s[s_i]-x
    t_l = abs(t[t_i-1]-x)
    t_r = t[t_i]-x
    ans = min([max(s_l,t_l),s_l+t_r+min(s_l,t_r),s_r+t_l+min(s_r,t_l),max(s_r,t_r)])

    print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
import sys
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A

#B

#C
"""
n,a,b,c = LI()
l = IR(n)
f = [True for i in range(3)]
ans = 0
for i in range(n):
    for d in range(3):
        if abs(l[i]-[a,b,c][d]) <= 10 and f[d]:
            ans += abs(l[i]-[a,b,c][d])
            f[d] = False
            l[i] = -float("inf")
k = False
for i in f:
    if i:
        k = True
        break
if not k:
    print(ans)
    quit()
"""
#D
a,b,q = LI()
s = IR(a)
t = IR(b)
for i in range(q):
    x = int(sys.stdin.readline())
    c = bisect.bisect_left(s,x)
    ans = float("inf")
    if c == a:
        k = abs(x-s[-1])
        y = s[-1]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
    else:
        k = abs(x-s[c-1])
        y = s[c-1]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
        k = abs(x-s[c])
        y = s[c]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
    c = bisect.bisect_left(t,x)
    if c == b:
        k = abs(x-t[-1])
        y = t[-1]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
    else:
        k = abs(x-t[c-1])
        y = t[c-1]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
        k = abs(x-t[c])
        y = t[c]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
    print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T