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] + [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:
from bisect import bisect_left,bisect_right

a,b,q=map(int,input().split())
s=[-10**15]
s+=[int(input()) for _ in range(a)]
s+=[10**15]
t=[-10**15]
t+=[int(input()) for _ in range(b)]
t+=[10**15]
x=[int(input()) for _ in range(q)]
for i in range(q):
  sl=s[bisect_left(s,x[i])-1]
  sr=s[bisect_right(s,x[i])]
  tl=t[bisect_left(t,x[i])-1]
  tr=t[bisect_right(t,x[i])]
  ans1=max(x[i]-sl,x[i]-tl)
  ans2=max(sr-x[i],tr-x[i])
  ans3=(x[i]-sl)*2+(tr-x[i])
  ans4=(x[i]-tl)*2+(sr-x[i])
  ans5=(sr-x[i])*2+(x[i]-tl)
  ans6=(tr-x[i])*2+(x[i]-sl)
  print(min(ans1,ans2,ans3,ans4,ans5,ans6))

Submission status: Accepted

Solution code:
from sys import stdin
import bisect

A, B, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = [int(stdin.readline().rstrip()) for _ in range(A)]
T = [int(stdin.readline().rstrip()) for _ in range(B)]
X = [int(stdin.readline().rstrip()) for _ in range(Q)]
S.append(10 ** 18)
T.append(10 ** 18)
S.insert(0, -10 ** 18)
T.insert(0, -10 ** 18)

for x in X:
    index_s = bisect.bisect_left(S, x)
    index_t = bisect.bisect_left(T, x)
    print(min(
        abs(S[index_s - 1] - T[index_t - 1]) + abs(x - S[index_s - 1]),
        abs(S[index_s - 1] - T[index_t - 1]) + abs(x - T[index_t - 1]),
        abs(S[index_s] - T[index_t - 1]) + abs(x - S[index_s]),
        abs(S[index_s] - T[index_t - 1]) + abs(x - T[index_t - 1]),
        abs(S[index_s] - T[index_t]) + abs(x - S[index_s]),
        abs(S[index_s] - T[index_t]) + abs(x - T[index_t]),
        abs(S[index_s - 1] - T[index_t]) + abs(x - S[index_s - 1]),
        abs(S[index_s - 1] - T[index_t]) + abs(x - T[index_t]),
    ))

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

inf = 10**11
s = [-inf]+[int(input()) for i in range(a)]+[inf,inf]
t = [-inf]+[int(input()) for i in range(b)]+[inf,inf]
X = [int(input()) for i in range(q)]

for x in X:
	s_idx = bisect_left(s,x)
	t_idx = bisect_left(t,x)
	sr = s[s_idx]
	sl = s[s_idx-1]
	tr = t[t_idx]
	tl = t[t_idx-1]
	# print(s_idx,t_idx)
	ans1 = max(sr-x,tr-x)
	ans2 = max(x-sl,x-tl)
	ans3 = (x-sl)*2+(tr-x)
	ans4 = (tr-x)*2+(x-sl)
	ans5 = (x-tl)*2+(sr-x)
	ans6 = (sr-x)*2+(x-tl)
	# print(ans1,ans2,ans3,ans4,ans5,ans6)
	print(min(ans1,ans2,ans3,ans4,ans5,ans6))

Submission status: Accepted

Solution code:
import bisect


A, B, Q = map(int, input().split())
s = [-10 ** 12] + [int(input()) for _ in range(A)] + [10 ** 12]
t = [-10 ** 12] + [int(input()) for _ in range(B)] + [10 ** 12]
x = [int(input()) for _ in range(Q)]
for i in x:
    si = bisect.bisect_left(s, i)
    ti = bisect.bisect_left(t, i)
    s1, s2 = s[si - 1], s[si]
    t1, t2 = t[ti - 1], t[ti]
    ans = 10 ** 12
    for ss in [s1, s2]:
        for tt in [t1, t2]:
            ans = min(ans, abs(i - ss) + abs(ss - tt),
                      abs(i - tt) + abs(ss - tt))
    print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect
INF = float('inf')
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 x in X:
    ans = INF
    i = bisect(S,x)
    for j in {min(i,A-1),max(i-1,0)}:
        s = S[j]
        k = bisect(T,s)
        for l in {min(k,B-1),max(k-1,0)}:
            t = T[l]
            ans = min(ans,abs(x-s)+abs(s-t))
    i = bisect(T,x)
    for j in {min(i,B-1),max(i-1,0)}:
        t = T[j]
        k = bisect(S,t)
        for l in {min(k,A-1),max(k-1,0)}:
            s = S[l]
            ans = min(ans,abs(x-t)+abs(s-t))
    print(ans)

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)]
"""
def bsr(a, v, lo=0, hi=None):
	if None == hi:
		hi = len(a) - 1
	while lo <= hi:
		mi = (lo + hi) // 2
		if a[mi] <= v:
			lo = mi + 1
		else:
			hi = mi - 1
	return lo
"""
bsr = bisect.bisect_right

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

for x in X:
	sp = bsr(s, x)
	tp = bsr(t, x)
	cost = []
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	print(min(cost))

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

def bsr(a, v, lo=0, hi=None):
	if None == hi:
		hi = len(a) - 1
	while lo <= hi:
		mi = (lo + hi) // 2
		if a[mi] <= v:
			lo = mi + 1
		else:
			hi = mi - 1
	return lo

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

for x in X:
	sp = bsr(s, x)
	tp = bsr(t, x)
	cost = []
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	print(min(cost))

Submission status: Accepted

Solution code:
import sys
import bisect
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 i in range(q):
    x = int(input())
    sh = bisect.bisect_left(s, x)
    te = bisect.bisect_left(t, x)
    dist_sh_e = float("inf")
    dist_sh_w = float("inf")
    dist_te_e = float("inf")
    dist_te_w = float("inf")
    if sh != 0:
        dist_sh_w = x - s[sh - 1]
    if sh != a:
        dist_sh_e = s[sh] - x
    if te != 0:
        dist_te_w = x - t[te - 1]
    if te != b:
        dist_te_e = t[te] - x
    print(min(max(dist_sh_w, dist_te_w), max(dist_sh_e, dist_te_e), dist_sh_w * 2 + dist_te_e, dist_te_w * 2 + dist_sh_e, dist_sh_e * 2 + dist_te_w, dist_te_e * 2 + dist_sh_w))

Submission status: Accepted

Solution code:
import bisect

A,B,Q=map(int,input().split())
slist = []
for i in range(A):
  slist.append(int(input()))
#print(slist)

tlist = []
for i in range(B):
  tlist.append(int(input()))
#print(tlist)

for i in range(Q):
  x = int(input())
  min_dist = 10**11
  
  # x->s->t
  if x < slist[0]:
    si = [0]
  elif slist[-1] < x:
    si = [len(slist)-1]
  else:
    sl = bisect.bisect(slist,x)
    si = [sl-1,sl]
    
  for index in si:
    s = slist[index]
    dist = abs(x-s)

    if s < tlist[0]:
      dist += tlist[0]-s
    elif tlist[-1] < s:
      dist += s-tlist[-1]
    else:
      tl = bisect.bisect(tlist,s)  
      dist += min(s-tlist[tl-1],tlist[tl]-s)
    
    if dist < min_dist:
      min_dist = dist

  # x->t->s
  if x < tlist[0]:
    ti = [0]
  elif tlist[-1] < x:
    ti = [len(tlist)-1]
  else:
    tl = bisect.bisect(tlist,x)
    ti = [tl-1,tl]
    
  for index in ti:
    t = tlist[index]
    dist = abs(x-t)

    if t < slist[0]:
      dist += slist[0]-t
    elif slist[-1] < t:
      dist += t-slist[-1]
    else:
      sl = bisect.bisect(slist,t)
      dist += min(t-slist[sl-1],slist[sl]-t)
    
    if dist < min_dist:
      min_dist = dist

  print(min_dist)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
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 range(Q):
	min_s = 0
	max_s = A
	ind_s = (min_s + max_s) // 2
	
	while min_s < max_s:
		ind_s = (min_s + max_s) // 2
		if s[ind_s] > x[i]:
			max_s = ind_s
		else:
			min_s = ind_s + 1
	
	ind_s = (min_s + max_s) // 2
	
	min_t = 0
	max_t = B
	ind_t = (min_t + max_t) // 2
	
	while min_t < max_t:
		ind_t = (min_t + max_t) // 2
		if t[ind_t] > x[i]:
			max_t = ind_t
		else:
			min_t = ind_t + 1
	
	ind_t = (min_t + max_t) // 2
		
	if ind_s == A or ind_t == B:
		ptn1 = 3 * 10 ** 10
	else:
		ptn1 = max((s[ind_s] - x[i]), (t[ind_t] - x[i]))
		
	if ind_s == 0 or ind_t == 0:
		ptn2 = 3 * 10 ** 10
	else:
		ptn2 = max((x[i] - s[ind_s - 1]), (x[i] - t[ind_t - 1]))
		
	if ind_s == A or ind_t == 0:
		ptn3 = 3 * 10 ** 10
	else:
		ptn3 = min(2 * (s[ind_s] - x[i]) + (x[i] - t[ind_t - 1]), (s[ind_s] - x[i]) + 2 * (x[i] - t[ind_t - 1]))
		
	if ind_s == 0 or ind_t == B:
		ptn4 = 3 * 10 ** 10
	else:
		ptn4 = min(2 * (x[i] - s[ind_s - 1]) + (t[ind_t] - x[i]), (x[i] - s[ind_s - 1]) + 2 * (t[ind_t] - x[i]))
		
	print(min(ptn1, ptn2, ptn3, ptn4))

Submission status: Accepted

Solution code:
import sys
import bisect


input = sys.stdin.readline
a, b, q = map(int, input().split())
inf = 10**12
s_list = [-inf] + [int(input()) for _ in range(a)] + [inf]
s_list.sort()
t_list = [-inf] + [int(input()) for _ in range(b)] + [inf]
t_list.sort()
ans_list =[]
for _ in range(q):
    x = int(input())
    s_idx = bisect.bisect_right(s_list, x)
    t_idx = bisect.bisect_right(t_list, x)
    ans = inf
    for s in [s_list[s_idx-1], s_list[s_idx]]:
        for t in [t_list[t_idx-1], t_list[t_idx]]:
            ans = min(ans, min(abs(x-s), abs(x-t)) + abs(s-t))
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from bisect import bisect_right
INF = 10**18
A, B, Q = map(int, input().split())
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]

for q in range(Q):
    x = int(input())
    se, te = bisect_right(S, x), bisect_right(T, x)
    ans = INF
    for s in [S[se-1], S[se]]:
        for t in [T[te-1], T[te]]:
            ds, dt = abs(s-x)+abs(s-t), abs(t-x)+abs(t-s)
            ans = min(ans, ds, dt)
    print(ans)

Submission status: Accepted

Solution code:
A,B,Q=map(int, input().split())
S=[-(10**10)]+[int(input()) for i in range(A)]+[2*(10**10)]
T=[-(10**10)]+[int(input()) for i in range(B)]+[2*(10**10)]
X=[int(input()) for i in range(Q)]
from bisect import bisect
for x in X:
    s=bisect(S,x)
    sl=S[s-1]
    sr=S[s]
    t=bisect(T,x)
    tl=T[t-1]
    tr=T[t]
    print(min((x-sl)+abs(tl-sl),(x-sl)+(tr-sl),\
        (sr-x)+abs(tr-sr),(sr-x)+(sr-tl),\
            (x-tl)+abs(tl-sl),(x-tl)+(sr-tl),\
                (tr-x)+abs(tr-sr),(tr-x)+(tr-sl)))

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:
A,B,Q=map(int,input().split())
s=[]
t=[]
answer=[]
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
s=[-10**21]+s+[10**21]
t=[-10**21]+t+[10**21]

def binary_search(list,target):
    left=0
    right=len(list)-1
    while left<=right:
        center=(left+right)//2
        if list[center]<target and list[center+1]>target:
            return center
            break
        elif list[center]<target:
            left=center+1
        else:
            right=center-1
    
for q in range(Q):
    x=int(input())
    i=binary_search(s,x)
    j=binary_search(t,x)
    ans=min(max(x-t[j],x-s[i]),max(s[i+1]-x,t[j+1]-x),t[j+1]-s[i]+min(x-s[i],t[j+1]-x),s[i+1]-t[j]+min(x-t[j],s[i+1]-x))
    answer.append(ans)

for k in answer:
    print(k)

Submission status: Accepted

Solution code:
import bisect
a,b,q=map(int,input().split())
s=[int(input()) for i in range(a)]
s.extend([float("-inf"),float("inf")])
s.sort()
t=[int(input()) for i in range(b)]
t.extend([float("-inf"),float("inf")])
t.sort()
for i in range(q):
    x=int(input())
    s2=bisect.bisect_right(s,x)
    s1=s2-1
    t2=bisect.bisect_right(t,x)
    t1=t2-1
    print(min(x-min(s[s1],t[t1]),max(s[s2],t[t2])-x,x-s[s1]*2+t[t2],t[t2]*2-x-s[s1],x-t[t1]*2+s[s2],s[s2]*2-x-t[t1]))

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

for _ in range(Q):
    x = int(input())
    s_pl = bisect.bisect_left(s, x) - 1
    s_pr = s_pl + 1
    t_pl = bisect.bisect_left(t, x) - 1
    t_pr = t_pl + 1
    
    dist = 10 ** 11
    if 0 <= s_pl < A and 0 <= t_pr < B:
        dist = x + t[t_pr] - 2 * s[s_pl]
        dist = min(dist, 2 * t[t_pr] - s[s_pl] - x)
    if 0 <= s_pr < A and 0 <= t_pr < B:
        dist = min(dist, max(s[s_pr], t[t_pr]) - x)
    if 0 <= s_pl < A and 0 <= t_pl < B:
        dist = min(dist, x - min(s[s_pl], t[t_pl]))
    if 0 <= s_pr < A and 0 <= t_pl < B:
        dist = min(dist, x + s[s_pr] - 2 * t[t_pl])
        dist = min(dist, 2 * s[s_pr] - t[t_pl] - x)

    print(dist)

Submission status: Accepted

Solution code:
A,B,Q = list(map(int,input().split()))
s = []
for i in range(A):
    s.append(int(input()))
t = []
for i in range(B):
    t.append(int(input()))

s.sort()
t.sort()

from bisect import bisect

for i in range(Q):
    q=int(input())
    a=bisect(s,q)
    b=bisect(t,q)
    if a==0:
        sl = -10**27
    else:
        sl = s[a-1]
    if b==0:
        tl = -10**27
    else:
        tl = t[b-1]
    if a==A:
        sr = 10**27
    else:
        sr = s[a]
    if b==B:
        tr = 10**27
    else:
        tr = t[b]
    c1 = max(sr,tr)-q
    c2 = q-min(sl,tl)
    c3 = min(2*(sr-q)+(q-tl),2*(tr-q)+(q-sl),(tr-q)+2*(q-sl),(sr-q)+2*(q-tl))
    print(min(c1,c2,c3))

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

#二分探索関数
def bin_search(x,seq):
    a=0
    b=len(seq)-1
    for i in range(len(seq)):
        center=((a+b)//2)
        if x>=seq[center]:
            a=center
        else:
            b=center
        if b-a<=1:
            break
    return seq[a],seq[b]
 
for i in range(Q):
    x=xx[i]
    Nb_s0,Nb_s1=bin_search(x,s)
    Nb_t0,Nb_t1=bin_search(x,t)
    p1=abs(Nb_s0-x)
    p2=abs(Nb_s1-x)
    p3=abs(Nb_t0-x)
    p4=abs(Nb_t1-x)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Submission status: Accepted

Solution code:
#D問題
import sys
A,B,Q = map(int,(input().split()))
si = [-(10**10)]+[int(input()) for _ in range(A)]+[2*(10**10)] # 範囲外に追加して、xの左右に常にデータがあるようにする
ti = [-(10**10)]+[int(input()) for _ in range(B)]+[2*(10**10)]
#xi = [int(input()) for i in range(Q)]
*xi, = map(int,"".join(sys.stdin.readlines()).split())
A +=2
B +=2

for x in xi:
    l = 0
    r = A-1
    while l+1<r:
        i = (l+r)//2
        if si[i]<x:
            l = i
        else:
            r = i
    sl = si[l]
    sr = si[r]
    
    l = 0
    r = B-1
    while l+1<r:
        i = (l+r)//2
        if ti[i]<x:
            l = i
        else:
            r = i
    tl = ti[l]
    tr = ti[r]
    dsl = x-sl
    dsr = sr-x
    dtl = x-tl
    dtr = tr-x
    print(min(max(dsr,dtr),max(dsl,dtl),dsl+dtr+min(dsl,dtr),dsr+dtl+min(dsr,dtl)))

Submission status: Accepted

Solution code:
#D問題
A,B,Q = map(int,(input().split()))
si = [-(10**10)]+[int(input()) for _ in range(A)]+[2*(10**10)] # 範囲外に追加して、xの左右に常にデータがあるようにする
ti = [-(10**10)]+[int(input()) for _ in range(B)]+[2*(10**10)]
xi = [int(input()) for i in range(Q)]
A +=2
B +=2

for x in xi:
    l = 0
    r = A-1
    while l+1<r:
        i = (l+r)//2
        if si[i]<x:
            l = i
        else:
            r = i
    sl = si[l]
    sr = si[r]
    
    l = 0
    r = B-1
    while l+1<r:
        i = (l+r)//2
        if ti[i]<x:
            l = i
        else:
            r = i
    tl = ti[l]
    tr = ti[r]
    dsl = x-sl
    dsr = sr-x
    dtl = x-tl
    dtr = tr-x
    print(min(max(dsr,dtr),max(dsl,dtl),dsl+dtr+min(dsl,dtr),dsr+dtl+min(dsr,dtl)))

Submission status: Accepted

Solution code:
inf=10**11
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=[[i,int(input())] for i in range(Q)]

x.sort(key=lambda x:x[1])
i,s_i,t_i=0,0,0
ans=[]

for i in range(Q):
    while(1):
        if x[i][1]>=s[s_i]:
            s_i+=1
        else:
            break
    while(1):
        if x[i][1]>=t[t_i]:
            t_i+=1
        else:
            break
    ans.append([x[i][0],x[i][1],s[s_i-1],s[s_i],t[t_i-1],t[t_i]])
ans.sort(key=lambda x:x[0])
for i in range(Q):
    xx=ans[i][1]
    Nb_s0=ans[i][2]
    Nb_s1=ans[i][3]
    Nb_t0=ans[i][4]
    Nb_t1=ans[i][5]

    p1=abs(Nb_s0-xx)
    p2=abs(Nb_s1-xx)
    p3=abs(Nb_t0-xx)
    p4=abs(Nb_t1-xx)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Submission status: Accepted

Solution code:
import bisect
import sys
INF = float('inf')
input = sys.stdin.readline
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):
  x = int(input())
  idx = bisect.bisect_left(s, x)
  s1 = [s[idx - 1], s[idx]]
  idx = bisect.bisect_left(t, x)
  t1 = [t[idx - 1], t[idx]]
  ans = INF
  for shr in s1:
    for tem in t1:
      ans = min(ans, abs(tem - shr) + abs(shr - x))
      ans = min(ans, abs(shr - tem) + abs(tem - x))
  print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
a, b, q = map(int, input().split())
INF = 10 ** 12
S = [int(input()) for i in range(a)]
S.append(INF)
T = [int(input()) for i in range(b)]
T.append(INF)
for _ in range(q):
    x = int(input())
    i = bisect_left(S, x)
    j = bisect_left(T, x)
    ans = []
    for y in [S[i-1],S[i]]:
        for z in [T[j - 1], T[j]]:
            ans.append(abs(x - y) + abs(y - z))
            ans.append(abs(x - z) + abs(z - y))
    print(min(ans))

Submission status: Accepted

Solution code:
import bisect
import sys

def input():
    return sys.stdin.readline()

A, B, Q = map(int, input().split())

shrines = []
for i in range(A):
    shrines.append(int(input()))

temples = []
for i in range(B):
    temples.append(int(input()))

shortest_s_to_t = {}
for shrine in shrines:
    index = bisect.bisect_left(temples, shrine)
    if index == 0:
        distance = abs(shrine - temples[index])
        shortest_s_to_t[shrine] = distance
    elif index == B:
        distance = abs(shrine - temples[index - 1])
        shortest_s_to_t[shrine] = distance
    else:
        distance_left = abs(shrine - temples[index - 1])
        distance_right = abs(shrine - temples[index])
        shortest_s_to_t[shrine] = min(distance_left, distance_right)

shortest_t_to_s = {}
for temple in temples:
    index = bisect.bisect_left(shrines, temple)
    if index == 0:
        distance = abs(temple - shrines[index])
        shortest_t_to_s[temple] = distance
    elif index == A:
        distance = abs(temple - shrines[index - 1])
        shortest_t_to_s[temple] = distance
    else:
        distance_left = abs(temple - shrines[index - 1])
        distance_right = abs(temple - shrines[index])
        shortest_t_to_s[temple] = min(distance_left, distance_right)

for i in range(Q):
    x = int(input())

    # shrine to temple
    index = bisect.bisect_left(shrines, x)
    ans = 10**11
    if index > 0:
        left_shrine = shrines[index - 1]
        candidate = abs(x - left_shrine) + shortest_s_to_t[left_shrine]
        ans = min(ans, candidate)
    if index < A:
        right_shrine = shrines[index]
        candidate = abs(x - right_shrine) + shortest_s_to_t[right_shrine]
        ans = min(ans, candidate)

    # temple to shrine
    index = bisect.bisect_left(temples, x)
    if index > 0:
        left_temple = temples[index - 1]
        candidate = abs(x - left_temple) + shortest_t_to_s[left_temple]
        ans = min(ans, candidate)
    if index < B:
        right_temple = temples[index]
        candidate = abs(x - right_temple) + shortest_t_to_s[right_temple]
        ans = min(ans, candidate)

    print(ans)

Submission status: Accepted

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

a, b, q =  [ int(v) for v in input().split() ]

temple_list = [ int(input()) for i in range(a) ]
shrine_list = [ int(input()) for i in range(b) ]


def excursion(x):
	global temple_list, shine_list
	temple_west = bisect.bisect_left(temple_list,x)
	shrine_west = bisect.bisect_left(shrine_list,x)

	tw, te, sw, se = temple_list[(temple_west-1)%a],temple_list[temple_west%a],shrine_list[(shrine_west-1)%b],shrine_list[(shrine_west)%b]
	print(min( abs(x-tw) + min(abs(sw-tw),abs(tw-se)) , abs(te-x) + min(abs(se-te),abs(te-sw)), abs(x-sw) + min(abs(tw-sw),abs(sw-te)) , abs(se-x) + min(abs(te-se),abs(se-tw))))
	return 


for i in range(q):
	excursion(int(input()))

Submission status: Accepted

Solution code:
import bisect

a, b, q =  [ int(v) for v in input().split() ]

temple_list = [ int(input()) for i in range(a) ]
shrine_list = [ int(input()) for i in range(b) ]


def excursion(x):
	global temple_list, shine_list
	temple_west = bisect.bisect_left(temple_list,x)
	shrine_west = bisect.bisect_left(shrine_list,x)

	tw, te, sw, se = temple_list[(temple_west-1)%a],temple_list[temple_west%a],shrine_list[(shrine_west-1)%b],shrine_list[(shrine_west)%b]
	print(min( abs(x-tw) + min(abs(sw-tw),abs(tw-se)) , abs(te-x) + min(abs(se-te),abs(te-sw)), abs(x-sw) + min(abs(tw-sw),abs(sw-te)) , abs(se-x) + min(abs(te-se),abs(se-tw))))
	return 


for i in range(q):
	excursion(int(input()))

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


for i in x: # N
  idx_i = bisect.bisect(s, i) # log A
  z = float('inf')
  for j in s[max(0, idx_i - 1): idx_i + 1]: # 2
    idx_j = bisect.bisect(t, j) # log B

    for k in t[max(0, idx_j - 1): idx_j + 1]: # 2
      _z = min(abs(i - j) + abs(j - k), abs(i - k) + abs(k - j))
      z = min(z, _z)
  print(z)

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left
A, B, Q = map(int, input().split())
INF = float('inf')
S = [-INF]
T = [-INF]
for i in range(A):
    S.append(int(input()))

for i in range(B):
    T.append(int(input()))

S.append(INF)
T.append(INF)

for i in range(Q):
    x = int(input())
    b = bisect_right(S, x)
    d = bisect_right(T, x)
    ans = INF
    ans = min(ans, abs(x - S[b - 1]) + abs(S[b - 1] - T[d - 1]))
    ans = min(ans, abs(x - S[b]) + abs(S[b] - T[d - 1]))
    ans = min(ans, abs(x - S[b - 1]) + abs(S[b - 1] - T[d]))
    ans = min(ans, abs(x - S[b]) + abs(S[b] - T[d]))
    ans = min(ans, abs(x - T[d - 1]) + abs(T[d - 1] - S[b - 1]))
    ans = min(ans, abs(x - T[d]) + abs(T[d] - S[b - 1]))
    ans = min(ans, abs(x - T[d - 1]) + abs(T[d - 1] - S[b]))
    ans = min(ans, abs(x - T[d]) + abs(T[d] - S[b]))
    print(ans)

Submission status: Accepted

Solution code:
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]

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:
INF = 10**11
from bisect import bisect
import sys
readline = sys.stdin.readline

A, B, Q = map(int, readline().split())
s_array = [int(readline()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(readline()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(readline()) for i in range(Q)]

# print(s_array, t_array, q_array)

ans_array = []
for q in q_array:
    s_index = bisect(s_array, q) - 1
    t_index = bisect(t_array, q) - 1
    # print(s_index,t_index)
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(str(min(method_a, method_b, method_c, method_d)) + "\n")

sys.stdout.writelines(ans_array)

Submission status: Accepted

Solution code:
INF = 10**11
from bisect import bisect

A, B, Q = map(int, input().split())
s_array = [int(input()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(input()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(input()) for i in range(Q)]

# print(s_array, t_array, q_array)

ans_array = []
for q in q_array:
    s_index = bisect(s_array, q) - 1
    t_index = bisect(t_array, q) - 1
    # print(s_index,t_index)
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(min(method_a, method_b, method_c, method_d))

for ans in ans_array:
    print(ans)

Submission status: Accepted

Solution code:
INF = 10**11


def binary_search(array, value):
    start = 0
    end = len(array) - 1
    while end - start >= 2:
        tmp = (start + end) // 2
        # print(start, end, tmp)
        if array[tmp] <= value:
            start = tmp
        else:
            end = tmp
        # print(start, end, tmp)
    return start


A, B, Q = map(int, input().split())
s_array = [int(input()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(input()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(input()) for i in range(Q)]

# print(s_array, t_array, q_array)

ans_array = []
for q in q_array:
    s_index = binary_search(s_array, q)
    t_index = binary_search(t_array, q)
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(min(method_a, method_b, method_c, method_d))

for ans in ans_array:
    print(ans)