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:
from bisect import bisect
import sys
input = sys.stdin.readline

A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
s.sort()
t = [int(input()) for _ in range(B)]
t.sort()
for _ in range(Q):
    x = int(input())
    i = bisect(s, x)
    sl = sr = 0
    if i == 0:
        sl = -float('inf')
        sr = s[i]
    elif i == A:
        sl = s[i-1]
        sr = float('inf')
    else:
        sl = s[i-1]
        sr = s[i]
    j = bisect(t, x)
    tl = tr = 0
    if j == 0:
        tl = -float('inf')
        tr = t[j]
    elif j == B:
        tl = t[j-1]
        tr = float('inf')
    else:
        tl = t[j-1]
        tr = t[j]
    ans = min(max(sr, tr)-x, x-min(sl, tl), x+sr-2*tl, x+tr-2*sl, 2*sr-x-tl, 2*tr-x-sl)
    print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

INF = 2 * 10**10

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)]
ans = []
for x in X:
    sr = bisect_left(S, x) 
    sl = sr if S[sr] == x else sr -1
    tr = bisect_left(T, x)
    tl = tr if T[tr] == x else tr -1

    ll = x - min(S[sl], T[tl])
    rr = max(S[sr], T[tr]) - x
    lsrt = 2*(x-S[sl]) + (T[tr] - x)
    ltrs = 2*(x-T[tl]) + (S[sr] - x)
    rslt = 2*(S[sr] - x) + (x - T[tl])
    rtls = 2*(T[tr] - x) + (x - S[sl])

    ans.append(min(ll, rr, lsrt, ltrs, rslt, rtls))
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from bisect import bisect_left

INF = 2 * 10**10

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)]
ans = []
for x in X:
    sr = bisect_left(S, x) 
    sl = sr -1
    if sr != len(S) and S[sr] == x:
        sl = sr
    tr = bisect_left(T, x)
    tl = tr - 1
    if tr != len(T) and T[tr] == x:
        tl = tr

    ll = INF if sl == -1 or tl == -1 else x - min(S[sl], T[tl])
    rr = INF if sr == len(S) or tr == len(T) else max(S[sr], T[tr]) - x
    lsrt = INF if sl == -1 or tr == len(T) else 2*(x-S[sl]) + (T[tr] - x)
    ltrs = INF if tl == -1 or sr == len(S) else 2*(x-T[tl]) + (S[sr] - x)
    rslt = INF if tl == -1 or sr == len(S) else 2*(S[sr] - x) + (x - T[tl])
    rtls = INF if sl == -1 or tr == len(T) else 2*(T[tr] - x) + (x - S[sl])

    ans.append(min(ll, rr, lsrt, ltrs, rslt, rtls))
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
A,B,Q = map(int,input().split())
S = []
T = []
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
for i in range(Q):
    x = int(input())
    values = []
    p1 = bisect_left(S,x)
    if p1 == 0:
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    elif p1 == A:
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
    else:
        #-1
        p1 -= 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        p1 += 1
        p2 = bisect_left(T,S[p1])
        if p2 == 0:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
        elif p2 == B:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))
        else:
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2]))
            values.append(abs(x-S[p1])+abs(S[p1]-T[p2-1]))

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines

# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

inf=10**18

import bisect

def main():
	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 i in x:
		si=bisect.bisect_left(s,i)
		ti=bisect.bisect_left(t,i)
		
		ans=inf
		for S in [s[si-1],s[si]]:
			for T in [t[ti-1],t[ti]]:
				ans=min(ans,abs(S-i)+abs(S-T),abs(T-i)+abs(S-T))
		print(ans)
		
		
		
	

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
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]
for q in range(Q):
    x = int(input())
    b = bisect.bisect_right(s, x)
    d = bisect.bisect_right(t, x)
    ans = INF
    for S in (s[b - 1], s[b]):
        for T in (t[d - 1], t[d]):
            d1 = abs(S - x) + abs(T - S)
            d2 = abs(T - x) + abs(S - T)
            ans = min(ans, d1, d2)
    print(ans)

Submission status: Accepted

Solution code:
def halfsplitsearch(list,start,end,minmax):
    if minmax=='min':
        while end-start>1:
            test=(end+start)//2
            if condition(list[test][0]):
                end=test
            else:
                start=test
        if condition(list[start][0]):
            return start
        else:
            return end
    else:
        while end-start>1:
            test=(end+start)//2
            if condition(list[test][0]):
                start=test
            else:
                end=test
        if condition(list[end][0]):
            return end
        else:
            return start

import sys

input=sys.stdin.readline

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

list=s+t
list.sort()
s.sort()
t.sort()

sans=[0 for i in range(0,A)]
for i in range(0,A):
    ans=0
    def condition(num):
        return num<s[i][0]
    ans=halfsplitsearch(t,0,len(t)-1,'max')
    if ans!=B-1:
        sans[i]=min(abs(s[i][0]-t[ans][0]),abs(s[i][0]-t[ans+1][0]))
    else:
        sans[i]=abs(s[i][0]-t[ans][0])

tans=[0 for i in range(0,B)]
for i in range(0,B):
    ans=0
    def condition(num):
        return num<t[i][0]
    ans=halfsplitsearch(s,0,len(s)-1,'max')
    if ans!=A-1:
        tans[i]=min(abs(t[i][0]-s[ans][0]),abs(t[i][0]-s[ans+1][0]))
    else:
        tans[i]=abs(t[i][0]-s[ans][0])


for i in range(0,Q):
    a=0
    b=0
    test1=10**100
    test2=10**100
    def condition(num):
        return num<x[i]
    a=halfsplitsearch(list,0,len(list)-1,'max')
    if list[a][1]=='s':
        test1=abs(x[i]-list[a][0])+sans[list[a][2]]
    else:
        test1=abs(x[i]-list[a][0])+tans[list[a][2]]
    if a!=len(list)-1:
        if list[a+1][1]=='s':
            test2=abs(x[i]-list[a+1][0])+sans[list[a+1][2]]
        else:
            test2=abs(x[i]-list[a+1][0])+tans[list[a+1][2]]
    print(min(test1,test2))

Submission status: Accepted

Solution code:
A, B, Q = [int(x) for x in input().split()]
s = [-10 ** 10] + [None] * A + [2 * (10 ** 10)]  # s[0], s[A+1]はダミー
t = [-10 ** 10] + [None] * B + [2 * (10 ** 10)]  # t[0], t[B+1]はダミー
x = [0] * Q
for i in range(1, A + 1):
    s[i] = int(input())
for j in range(1, B + 1):
    t[j] = int(input())
for k in range(Q):
    x[k] = int(input())

for i in range(Q):
    # Find s[j] such that s[j] <= x[i] < s[j+1]
    ok = 0       # s[0] < x[i]
    ng = A + 1   # x[i] < s[A+1]
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if s[mid] < x[i]:
            ok = mid
        else:
            ng = mid
    j = ok

    ok = 0      # t[0] < x[i]
    ng = B + 1  # x[i] < t[A+1]
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if t[mid] < x[i]:
            ok = mid
        else:
            ng = mid
    k = ok
 
    ans = min(  max(s[j + 1], t[k + 1]) - x[i], # 右
                x[i] - min(s[j], t[k]),         # 左
                2 * s[j + 1] - t[k] - x[i],     # 右 s[j+1] -> 左 t[k]
                s[j + 1] - 2 * t[k] + x[i],     # 左 t[k] -> 右 s[j+1]
                -s[j] + 2 * t[k + 1] - x[i],    # 右 t[k+1] -> 左 s[j]
                -2 * s[j] + t[k + 1] + x[i])    # 左 s[j] -> 右 t[k+1]

    print(ans)

Submission status: Accepted

Solution code:
a, b, q = map(int, input().split())
S = [int(input()) for _ in range(a)]
T = [int(input()) for _ in range(b)]
S.sort()
T.sort()
INF = 10**18
S = [-INF] + S + [INF]
T = [-INF] + T + [INF]

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from bisect import bisect

A,B,Q = map(int,input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
X = [int(input()) for i in range(Q)]

INF = float('inf')
st = []
for s in S:
    i = bisect(T,s)
    x0,x1 = -INF,INF
    if i > 0: x0 = T[i-1]
    if i < B: x1 = T[i]
    st.append(min(s-x0, x1-s))
ts = []
for t in T:
    i = bisect(S,t)
    x0,x1 = -INF,INF
    if i > 0: x0 = S[i-1]
    if i < A: x1 = S[i]
    ts.append(min(t-x0, x1-t))

ans = []
for x in X:
    d0 = d1 = d2 = d3 = INF
    si = bisect(S,x)
    if si > 0:
        d0 = x-S[si-1] + st[si-1]
    if si < A:
        d1 = S[si]-x + st[si]
    ti = bisect(T,x)
    if ti > 0:
        d2 = x-T[ti-1] + ts[ti-1]
    if ti < B:
        d3 = T[ti]-x + ts[ti]
    ans.append(min(d0,d1,d2,d3))
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
A,B,Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
import bisect
for _ in range(Q):
    x = int(input())
    s_idx = bisect.bisect_left(S, x)
    t_idx = bisect.bisect_left(T, x)
    if s_idx == A and t_idx == B:
        ans = x-min(S[-1], T[-1])
    elif s_idx == A and t_idx == 0:
        ans = min(x-S[-1]*2+T[0], T[0]*2-x-S[-1])
    elif t_idx == B and s_idx == 0:
        ans = min(x-T[-1]*2+S[0], S[0]*2-x-T[-1])
    elif s_idx == A:
        ans = min(x-min(S[-1], T[t_idx-1]), x-S[-1]*2+T[t_idx], T[t_idx]*2-x-S[-1])
    elif t_idx == B:
        ans = min(x-min(T[-1], S[s_idx-1]), x-T[-1]*2+S[s_idx], S[s_idx]*2-x-T[-1])
    elif s_idx == 0 and t_idx == 0:
        ans = max(S[0], T[0])-x
    elif s_idx == 0:
        ans = min(max(S[0], T[t_idx])-x, S[0]*2-x-T[t_idx-1], x-T[t_idx-1]*2+S[0])
    elif t_idx == 0:
        ans = min(max(T[0], S[s_idx])-x, T[0]*2-x-S[s_idx-1], x-S[s_idx-1]*2+T[0])
    else:
        ans = min(max(S[s_idx], T[t_idx])-x, x-min(S[s_idx-1], T[t_idx-1]), S[s_idx]*2-x-T[t_idx-1], T[t_idx]*2-x-S[s_idx-1])
        ans = min(ans, x-T[t_idx-1]*2+S[s_idx], x-S[s_idx-1]*2+T[t_idx])
    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
A, B, Q = map(int, input().split())
inf = 10**18
S = [-inf]
for _ in range(A):
    S.append(int(input()))
S.append(inf)
T = [-inf]
for _ in range(B):
    T.append(int(input()))
T.append(inf)
def f_s(x, l):
    mn, mx = -1, len(l)
    idx = mx//2
    while mx-mn>1:
        if l[idx] < x:
            idx, mn = (idx + mx)//2, idx
            continue
        idx, mx = (idx + mn)//2, idx
    return idx

for _ in range(Q):
    q = int(input())
    xi, yi = f_s(q, S), f_s(q, T)
    s1, s2 = S[xi], S[xi+1]
    t1, t2 = T[yi], T[yi+1]
    print(min([
        max(q-s1,q-t1),
        max(s2-q, t2-q),
        2*(s2-q) + q-t1,
        2*(t2-q) + q-s1,
        2*(q-s1) + t2-q,
        2*(q-t1) + s2-q,
        ]))
#    print(q, s1, s2, t1, t2)

#    print(S[xi], T[yi])

Submission status: Accepted

Solution code:
import sys, math, bisect
sys.setrecursionlimit(200010)

def input():
    return sys.stdin.readline()[:-1]

def main():
    A, B, Q = map(int,input().split())
    s = [-10**12] + sorted([int(input()) for k in range(A)]) + [10**12]
    t = [-10**12] + sorted([int(input()) for k in range(B)]) + [10**12]
    for _ in range(Q):
        q = int(input())
        s1 = s[bisect.bisect_left(s,q)]
        s2 = s[bisect.bisect_left(s,q)-1]
        t1 = t[bisect.bisect_left(t,q)]
        t2 = t[bisect.bisect_left(t,q)-1]
        d1 = max(q-s2,q-t2)
        d2 = max(s1-q,t1-q)
        d3 = 2*(q-s2)+t1-q
        d4 = 2*(q-t2)+s1-q
        d5 = 2*(s1-q)+q-t2
        d6 = 2*(t1-q)+q-s2
        print(min(d1,d2,d3,d4,d5,d6))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import bisect
input = sys.stdin.readline

A, B, Q = map(int, input().split())
tera = [int(input()) for _ in range(A)]
sha = [int(input()) for _ in range(B)]
query = [int(input()) for _ in range(Q)]
res = []

for x in query:
    tl = bisect.bisect_left(tera, x)
    tr = bisect.bisect_right(tera, x)
    sl = bisect.bisect_left(sha, x)
    sr = bisect.bisect_right(sha, x)

    if tl == 0:
        temple = [tera[0]]
    elif tr == A:
        temple = [tera[A - 1]]
    else:
        temple = [tera[tl-1], tera[tl]]

    if sl == 0:
        shrine = [sha[0]]
    elif sr == B:
        shrine = [sha[B - 1]]
    else:
        shrine = [sha[sl-1], sha[sl]]

    ans = 10**18
    for t in temple:
        for s in shrine:
            tx = t - x
            sx = s - x
            if tx * sx >= 0:
                a = max(abs(tx), abs(sx))
            else:
                a = min(2 * abs(tx) + abs(sx), 2 * abs(sx) + abs(tx))
            ans = min(ans, a)
    res.append(ans)

print(*res, sep="\n")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from bisect import *

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

for _ in range(Q):
    x = int(input())
    idx1 = bisect_right(s, x)
    idx2 = bisect_right(t, x)
    ls, rs = s[idx1-1], s[idx1]
    lt, rt = t[idx2-1], t[idx2]
    ans = 10**18
    
    for p1, p2 in [(ls, lt), (ls, rt), (rs, lt), (rs, rt), (lt, ls), (lt, rs), (rt, ls), (rt, rs)]:
        ans = min(ans, abs(p1-x)+abs(p2-p1))
    
    print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left

def solve():
    input = sys.stdin.readline
    A, B, Q = map(int, input().split())
    S = [int(input()) for _ in range(A)]
    T = [int(input()) for _ in range(B)]
    for _ in range(Q):
        x = int(input())
        si = bisect_left(S, x)
        ti = bisect_left(T, x)
        minLength = 10 **20
        both_left = 10 ** 20
        both_right = 10 ** 20
        s_t = 10 ** 20
        t_s = 10 ** 20
        if si > 0 and ti > 0: both_left = max(x - S[si-1], x - T[ti - 1])
        if si <= A - 1 and ti <= B - 1: both_right = max(S[si] - x, T[ti] - x)
        if si > 0 and ti <= B - 1: s_t = T[ti] - S[si-1] + min(x - S[si-1], T[ti] - x)
        if ti > 0 and si <= A - 1: t_s = S[si] - T[ti-1] + min(x - T[ti-1], S[si] - x)
        minLength = min([both_left, both_right, s_t, t_s])
        print(minLength)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from bisect import bisect_left
A, B, Q, *L = map(int, open(0).read().split())
s = L[:A]
t = L[A:A+B]
x = L[A+B:]

s = [-float('inf')]+s+[float('inf')]
t = [-float('inf')]+t+[float('inf')]
for c in x:
  br = bisect_left(t,c)
  ar = bisect_left(s,c)
  bl = br-1
  al = ar-1
  print(min(s[ar]-t[bl]+min(s[ar]-c,c-t[bl]),t[br]-s[al]+min(t[br]-c,c-s[al]),
            c-min(t[bl],s[al]),max(s[ar],t[br])-c))

Submission status: Accepted

Solution code:
import numpy as np
A, B, Q, *L = map(int, open(0).read().split())
s = L[:A]
t = L[A:A+B]
x = L[A+B:]

s = [-float('inf')]+s+[float('inf')]
t = [-float('inf')]+t+[float('inf')]
sls = np.searchsorted(s,x)
tls = np.searchsorted(t,x)
for ar, br, c in zip(sls,tls,x):
  bl = br-1
  al = ar-1
  n = s[ar]-t[bl]+min(s[ar]-c,c-t[bl])
  m = t[br]-s[al]+min(t[br]-c,c-s[al])
  p = c-min(t[bl],s[al])
  q = max(s[ar],t[br])-c
  print(min(n,m,p,q))

Submission status: Accepted

Solution code:
from bisect import bisect_left
import sys
def input():
    return sys.stdin.readline()[:-1]
A,B,Q=map(int,input().split())
s=[int(input()) for i in range(A)]
t=[int(input()) for i in range(B)]
for i in range(Q):
    x=int(input())
    si=bisect_left(s,x)
    ti=bisect_left(t,x)
    a=[]
    if si and si!=A:
        sl=s[si-1]
        sr=s[si]
    elif si:
        sl=s[-1]
        sr=0
    else:
        sl=0
        sr=s[0]
    if ti and ti!=B:
        tl=t[ti-1]
        tr=t[ti]
    elif ti:
        tl=t[-1]
        tr=0
    else:
        tl=0
        tr=t[0]
    if sl and tl:
        a.append(x-min(sl,tl))
    if sr and tr:
        a.append(max(tr,sr)-x)
    if sl and tr:
        a.append(tr-x+tr-sl)
        a.append(x-sl+tr-sl)
    if sr and tl:
        a.append(x-tl+sr-tl)
        a.append(sr-x+sr-tl)
    print(min(a))

Submission status: Accepted

Solution code:
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')]
 
 
for _ in range(q):
    
    x = int(input())
    
    s_index = bisect.bisect_right(s, x)
    s_right = s[s_index]
    s_left = s[s_index - 1]
    t_index = bisect.bisect_right(t, x)
    t_right = t[t_index]
    t_left = t[t_index - 1]
    
    ans = float('inf')
    
    # 最初に寺を訪れる場合
    ans = min(
        ans, 
        abs(x - s_right) + min(abs(s_right - t_right), abs(s_right - t_left)), 
        abs(x - s_left) + min(abs(s_left - t_right), abs(s_left - t_left)), 
    )
    # 最初に神社を訪れる場合
    ans = min(
        ans, 
        abs(x - t_right) + min(abs(t_right - s_right), abs(t_right - s_left)), 
        abs(x - t_left) + min(abs(t_left - s_right), abs(t_left - s_left)), 
    )
        
    print(ans)

Submission status: Accepted

Solution code:
#119_D
A,B,Q=map(int,input().split())
INF=10**15
S=[-INF]+[int(input()) for _ in range(A)]+[INF]
T=[-INF]+[int(input()) for _ in range(B)]+[INF]

from bisect import bisect_left

for _ in range(Q):
    x=int(input())
    s=bisect_left(S,x)
    t=bisect_left(T,x)
    
    sr=abs(S[s]-x)
    sl=abs(S[s-1]-x)
    tr=abs(T[t]-x)
    tl=abs(T[t-1]-x)
    
    Ans=min(max(sr,tr),max(sl,tl),2*sr+tl,sr+2*tl,2*tr+sl,tr+2*sl)
    
    print(Ans)

Submission status: Accepted

Solution code:
import sys
import bisect
from itertools import product
def input(): return sys.stdin.readline().strip()
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
stlist = [s,t]
ab = [A,B]
for i in range(Q):
    x = int(input())
    cand = []
    for i in range(2):
        st = stlist[i]
        cand1 = set()
        l1 = bisect.bisect_left(st,x)
        if 0 <= l1 <= ab[i]-1:
            cand1.add(st[l1])
        if 0 <= l1-1 <= ab[i]-1:
            cand1.add(st[l1-1])
        cand.append(cand1)
    ans = float('inf')
    for a,b in product(cand[0],cand[1]):
        c = min(abs(x-a)+abs(b-a),abs(x-b)+abs(b-a))
        ans = min(ans,c)
    print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
from itertools import product
def input(): return sys.stdin.readline().strip()
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
stlist = [s,t]
ab = [A,B]
for i in range(Q):
    x = int(input())
    cand = []
    for i in range(2):
        st = stlist[i]
        cand1 = set()
        l1 = bisect.bisect_left(st,x)
        r1 = bisect.bisect_right(st,x)
        if 0 <= l1 <= ab[i]-1:
            cand1.add(st[l1])
        if 0 <= l1-1 <= ab[i]-1:
            cand1.add(st[l1-1])
        cand.append(cand1)
        
    ans = float('inf')
    for a,b in product(cand[0],cand[1]):
        c = min(abs(x-a)+abs(b-a),abs(x-b)+abs(b-a))
        ans = min(ans,c)
    print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
from itertools import product
def input(): return sys.stdin.readline().strip()
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
stlist = [s,t]
ab = [A,B]
for i in range(Q):
    x = int(input())
    cand = []
    for i in range(2):
        st = stlist[i]
        cand1 = set()
        l1 = bisect.bisect_left(st,x)
        r1 = bisect.bisect_right(st,x)
        if 0 <= l1 <= ab[i]-1:
            cand1.add(st[l1])
        if 0 <= l1-1 <= ab[i]-1:
            cand1.add(st[l1-1])  
        if 0 <= r1 <= ab[i]-1:
            cand1.add(st[r1])
        if 0 <= r1+1 <= ab[i]-1:
            cand1.add(st[r1+1])
        cand.append(cand1)
        
    ans = float('inf')
    for a,b in product(cand[0],cand[1]):
        c = min(abs(x-a)+abs(b-a),abs(x-b)+abs(b-a))
        ans = min(ans,c)
    print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
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()))
import bisect
def get(x):
    ls,rs,lt,rt=10**13,10**13,10**13,10**13
    s_ind = bisect.bisect_left(s,x)
    t_ind = bisect.bisect_left(t,x)
    if s_ind!=0:
        ls=abs(x - s[s_ind-1])
    if s_ind!=a:
        rs=abs(x - s[s_ind])
    if t_ind!=0:
        lt=abs(x - t[t_ind-1])
    if t_ind!=b:
        rt=abs(x - t[t_ind])
    return ls,rs,lt,rt

for que in x:
    ls,rs,lt,rt = get(que)

    rr = max(rs,rt)
    ll = max(ls,lt)
    lsrt = 2*ls+rt
    ltrs = 2*lt+rs
    rslt = 2*rs+lt
    rtls = 2*rt+ls
    ans = min([rr,ll,lsrt,ltrs,rslt,rtls])
    print(ans)

Submission status: Accepted

Solution code:
a, b, q = map(int, input().split())
s = []
t = []
x = []
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
for i in range(q):
    x.append(int(input()))
import bisect
def get(x):
    ls,rs,lt,rt=10**13,10**13,10**13,10**13
    s_ind = bisect.bisect_left(s,x)
    t_ind = bisect.bisect_left(t,x)
    if s_ind!=0:
        ls=abs(x - s[s_ind-1])
    if s_ind!=a:
        rs=abs(x - s[s_ind])
    if t_ind!=0:
        lt=abs(x - t[t_ind-1])
    if t_ind!=b:
        rt=abs(x - t[t_ind])
    return ls,rs,lt,rt

for que in x:
    ls,rs,lt,rt = get(que)

    rr = max(rs,rt)
    ll = max(ls,lt)
    lsrt = 2*ls+rt
    ltrs = 2*lt+rs
    rslt = 2*rs+lt
    rtls = 2*rt+ls
    ans = min([rr,ll,lsrt,ltrs,rslt,rtls])
    print(ans)

Submission status: Accepted

Solution code:
import bisect
a,b,q=map(int,input().split())
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
for i in range(q):
  x=int(input())
  idxS=bisect.bisect_left(s,x)
  idxT=bisect.bisect_left(t,x)
  d=[]
  if idxS<a and idxT<b:
    d.append(max(s[idxS]-x,t[idxT]-x))
  if 1<=idxS and 1<=idxT:
    d.append(max(x-s[idxS-1],x-t[idxT-1]))
  if 1<=idxS and idxT<b:
    d.append((t[idxT]-x)*2+x-s[idxS-1])
  if 1<=idxT and idxS<a:
    d.append((s[idxS]-x)*2+x-t[idxT-1])
  if idxT<b and 1<=idxS:
    d.append((x-s[idxS-1])*2+t[idxT]-x)
  if idxS<a and 1<=idxT:
    d.append((x-t[idxT-1])*2+s[idxS]-x)
  print(min(d))

Submission status: Accepted

Solution code:
import sys
import bisect
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

jinja = [-(10**10+10)]

for i in range(a):
    jinja.append(int(readline()))

tera = [-(10**10+10)]

for i in range(b):
    tera.append(int(readline()))

#add big number
jinja.append(2*10**10+10)
tera.append(2*10**10+10)

for query in range(q):
    x = int(readline())
    j_r = bisect.bisect_left(jinja,x)
    j_l = jinja[j_r-1]
    t_r = bisect.bisect_left(tera,x)
    t_l = tera[t_r-1]
    j_r = jinja[j_r]
    t_r = tera[t_r]
    ans = min((x-j_l)*2+(t_r-x),(x-t_l)*2+(j_r-x),(x-j_l)+(t_r-x)*2,(x-t_l)+(j_r-x)*2)
    ans = min (ans,x - min(j_l,t_l), max(j_r,t_r)-x)
    print(ans)

Submission status: Accepted

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


a,b,q=acinput()

S=[-10**15]
for i in range(a):
    S.append(II())
S.append(10**15)

T=[-10**15]
for i in range(b):
    T.append(II())
T.append(10**15)
X=[II() for i in range(q)]

import bisect


def near(arr,v):
    t=bisect.bisect(arr,v)
    
    #print(arr,v,t)
    if t>len(arr)-1:
        return len(arr)-1
    if t==0:
        return 0

    m = (abs(arr[t-1]-v) > abs(arr[t]-v))
    res = t+m-1
    #print(arr, v, res,m)
    return res


#print(S)
for i in range(q):
    x=X[i]

    s=bisect.bisect_right(S,x)
    t = bisect.bisect_right(T, x)
    res=10**19
    for ss in [S[s-1],S[s]]:
        for tt in [T[t-1],T[t]]:
            tmp=abs(ss-x)+abs(ss-tt)            
            res=min(res,tmp)
            tmp = abs(tt-x)+abs(ss-tt)
            res = min(res, tmp)
    print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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)]

import bisect
def solve(x):
    sr=bisect.bisect_right(S,x)
    tr=bisect.bisect_right(T,x)
    sl=sr-1
    tl=tr-1
    ret=10**12
    if sl>=0 and tl>=0:
        ret=min(ret,x-min(S[sl],T[tl]))
    if sr<A and tl>=0:
        ret=min(ret,S[sr]-T[tl]+min(S[sr]-x,x-T[tl]))
    if sl>=0 and tr<B:
        ret=min(ret,T[tr]-S[sl]+min(T[tr]-x,x-S[sl]))
    if sr<A and tr<B:
        ret=min(ret,max(S[sr],T[tr])-x)
    return ret

for x in X:
    print(solve(x))

Submission status: Accepted

Solution code:
import bisect

A, B, Q = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
X = [int(input()) for i in range(Q)]

def solve(x):
  ids = bisect.bisect_left(S, x)
  idt = bisect.bisect_left(T, x)
  if 0 < ids <  A:
    sm, sp = S[ids-1], S[ids]
  elif ids == 0:
    sm, sp = -10**20, S[ids]
  else:
    sm, sp = S[ids-1], x + 10**20
  if 0 < idt < B:
    tm, tp = T[idt-1], T[idt]
  elif idt == 0:
    tm, tp = -10**20, T[idt]
  else:
    tm, tp = T[idt-1], x + 10**20
  
  ans = max(tp - x, sp - x)
  ans = min(ans, max(x - tm, x - sm))
  min_ds = min(sp - x, x - sm)
  min_dt = min(tp - x, x - tm)
  ans = min(ans, 2*min(min_ds, min_dt)+max(min_ds, min_dt))
  return ans

for x in X:
  print(solve(x))