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 as bi

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

INF=10**15

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 i in range(q):
    d=bi.bisect_right(S,X[i])
    e=bi.bisect_right(T,X[i])
    as0=abs(X[i]-S[d-1])
    as1=abs(X[i]-S[d])
    at0=abs(X[i]-T[e-1])
    at1=abs(X[i]-T[e])
    ans=min(max(as0,at0),max(as1,at1),2*as0+at1,as0+2*at1,2*at0+as1,at0+as1*2)
    print(ans)

Submission status: Accepted

Solution code:
import bisect #bisectの典型的問題
a,b,q = map(int, input().split())

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

INF=10**18
s=[-INF]+s+[INF] #グリッド系の問題では、配列外を探索しないように
t=[-INF]+t+[INF]
x  = [int(input()) for i in range(q)]
for i in range(q):
  xi = x[i] #xを一気に読み込まなくてもfor loopの中の方が良い
  si = bisect.bisect_right(s, xi)
  ti = bisect.bisect_right(t, xi)
  ans=INF
  
  for S in [s[si-1], s[si]]:
    for T in [t[ti-1], t[ti]]:
      SX = abs(S-xi)
      TX = abs(T-xi)
      ST = abs(S-T)
      ans = min(ans, SX+ST, TX+ST)
  print(ans)

Submission status: Accepted

Solution code:
import bisect
a,b,q=map(int,input().split())
INF = 12345678901234
s=[int(input())for _ in range(a)]
s.append(INF)
s.insert(0,-INF)
t=[int(input())for _ in range(b)]
t.append(INF)
t.insert(0,-INF)
for query in range(q):
    ans = INF
    x=int(input())
    idxs = bisect.bisect_left(s,x)
    idxt = bisect.bisect_left(t,x)
    ls = x - s[idxs-1]
    rs = s[idxs] - x
    lt = x - t[idxt-1]
    rt = t[idxt] - x
    
    ans = min( ans, max(ls,lt) )
    ans = min( ans, max(rt,rs) )    
    ans = min( ans, ls + rt + min(ls,rt) )
    ans = min( ans, lt + rs + min(lt,rs) )
    
    print(ans)

Submission status: Accepted

Solution code:
import bisect
a,b,q=map(int,input().split())
s=[int(input())for _ in range(a)]
s.append(12345678901234)
s.insert(0,-12345678901234)
t=[int(input())for _ in range(b)]
t.append(12345678901234)
t.insert(0,-12345678901234)
for query in range(q):
    ans = 123456789012345
    x=int(input())
    idxs = bisect.bisect_left(s,x)
    idxt = bisect.bisect_left(t,x)
    ls = s[idxs-1]
    rs=s[idxs]
    lt=t[idxt-1]
    rt=t[idxt]
    # 左
    ans = min(ans,max(abs(x-ls),abs(x-lt)))

    # 右
    ans = min(ans,max(abs(x-rs),abs(x-rt)))    

    # 行って帰って
    ans = min(ans,2*abs(x-ls)+abs(x-rt))
    ans = min(ans,2*abs(x-lt)+abs(x-rs))
    ans = min(ans,2*abs(x-rs)+abs(x-lt))
    ans = min(ans,2*abs(x-rt)+abs(x-ls))
    
    print(ans)

Submission status: Accepted

Solution code:
# D
from bisect import bisect_right
import sys
input = sys.stdin.readline
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,d = bisect_right(s, x), bisect_right(t, x)
    ans = INF
    for s_visit in [s[b-1], s[b]]:
        for t_visit in [t[d-1], t[d]]:
            dist1 = abs(s_visit - x) + abs(t_visit - s_visit)
            dist2 = abs(t_visit - x) + abs(s_visit - t_visit)
            ans = min(ans, dist1, dist2)
    print(ans)

Submission status: Accepted

Solution code:
# D
from bisect import bisect_right
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,d = bisect_right(s, x), bisect_right(t, x)
    ans = INF
    for s_visit in [s[b-1], s[b]]:
        for t_visit in [t[d-1], t[d]]:
            dist1 = abs(s_visit - x) + abs(t_visit - s_visit)
            dist2 = abs(t_visit - x) + abs(s_visit - t_visit)
            ans = min(ans, dist1, dist2)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect

INF = 10 ** 18

A, B, Q = map(int, input().split())
S = [-INF]
T = [-INF]
X = []

for i in range(A):
    s = int(input())
    S.append(s)

S.sort()
S.append(INF)

for j in range(B):
    t = int(input())
    T.append(t)

T.sort()
T.append(INF)

for i in range(Q):
    x = int(input())
    ans = INF
    idx_s, idx_t = bisect.bisect_right(S, x), bisect.bisect_right(T, x)
    for s in S[idx_s - 1], S[idx_s]:
        for t in T[idx_t - 1], T[idx_t]:
            a = abs(s - x) + abs(t - s)
            b = abs(t - x) + abs(s - t)
            ans = min(ans, a, b)
    print(ans)

Submission status: Accepted

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

for i in range(q):
    Q = x[i]
    ans_s = []
    ans_t = []
    ans_s.append(s[bi.bisect_left(s,Q)-1])
    ans_t.append(t[bi.bisect_left(t,Q)-1])
    ans = 10**11
    
    if bi.bisect_right(s,Q) == len(s):
        ans_s.append(s[bi.bisect_left(s,Q)-1])
    else:
        ans_s.append(s[bi.bisect_left(s,Q)])
        
    if bi.bisect_right(t,Q) == len(t):
        ans_t.append(t[bi.bisect_left(t,Q)-1])
    else:
        ans_t.append(t[bi.bisect_left(t,Q)])
    
    for i in range(2):
        for j in range(2):
            ans = min(ans,abs(ans_s[i]-Q)+abs(ans_s[i]-ans_t[j]),abs(ans_t[j]-Q)+abs(ans_s[i]-ans_t[j]))
    
    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)]
x=[int(input()) for _ in range(q)]
s.append(1<<64)
t.append(1<<64)
import bisect
for i in range(q):
    sl=s[bisect.bisect_left(s,x[i])-1]
    sr=s[bisect.bisect_left(s,x[i])]
    tl=t[bisect.bisect_left(t,x[i])-1]
    tr=t[bisect.bisect_left(t,x[i])]
    #print(sl,sr,tl,tr)
    z=[max(abs(x[i]-sl),abs(x[i]-tl)),
       max(abs(sr-x[i]),abs(tr-x[i])),
       abs(x[i]-sl)*2+abs(tr-x[i]),
       abs(x[i]-tl)*2+abs(sr-x[i]),
       abs(tr-x[i])*2+abs(x[i]-sl),
       abs(sr-x[i])*2+abs(x[i]-tl),]
    #print(z)
    print(min(z))

Submission status: Accepted

Solution code:
import sys,collections,math,itertools,bisect;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

A,B,Q = Is()
As,Bs = [],[]
for i in range(A):
    As.append(I())
for i in range(B):
    Bs.append(I())
for i in range(Q):
    x = I()
    a = bisect.bisect_left(As,x)
    b = bisect.bisect_left(Bs,x)
    ans = 10**11
    As.append(10**11) if a == A else 0
    Bs.append(10**11) if b == B else 0
    AR = As[a] - x
    AL = x - As[a-1] if 0 < a  else 10**11
    BR = Bs[b] - x 
    BL = x - Bs[b-1] if 0 < b  else 10**11
    print(min(max(AL,BL),max(AR,BR),AL+BR+min(AL,BR),BL+AR+min(BL,AR)))

Submission status: Accepted

Solution code:
import bisect

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


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

Submission status: Accepted

Solution code:
import bisect
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


def dist(x):
    pS = bisect.bisect_right(S, x)
    pT = bisect.bisect_right(T, x)

    d1 = max(S[pS], T[pT])-x  # x ->s->t or x->t->s
    d2 = x - min(S[pS-1], T[pT-1])  # s->t->x or t->s->x
    d3 = min(x-T[pT-1], S[pS]-x) + S[pS]-T[pT-1]  # t->x->s
    d4 = min(x-S[pS-1], T[pT]-x)+T[pT]-S[pS-1]  # s->x->t]
    return min(d1, d2, d3, d4)


ans = []
for _ in range(Q):
    x = int(input())
    ans.append(dist(x))

print("\n".join(map(str, ans)))

Submission status: Accepted

Solution code:
import sys
from bisect import bisect
F = sys.stdin
A, B, Q = map(int, F.readline().strip("\n").split(" "))
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(F.readline().strip("\n"))
for i in range(B):
    T[i+1] = int(F.readline().strip("\n"))
S[A+1] = 10**20
T[B+1] = 10**20

lenS, lenT = len(S), len(T)
Out = ""
for _ in range(Q):
    x = int(F.readline().strip("\n"))
    si, ti = bisect(S, x, 0, lenS), bisect(T, x, 0, lenT)
    sLength0, sLength1 = x - S[si-1], S[si] - x
    tLength0, tLength1 = x - T[ti-1], T[ti] - x
    Ans = min(max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    Out += str(Ans) + "\n"
print(Out)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect
F = sys.stdin
A, B, Q = map(int, F.readline().strip("\n").split(" "))
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(F.readline().strip("\n"))
for i in range(B):
    T[i+1] = int(F.readline().strip("\n"))
S[A+1] = 10**20
T[B+1] = 10**20

lenS, lenT = len(S), len(T)
for _ in range(Q):
    x = int(F.readline().strip("\n"))
    si, ti = bisect(S, x, 0, lenS), bisect(T, x, 0, lenT)
    sLength0, sLength1 = x - S[si-1], S[si] - x
    tLength0, tLength1 = x - T[ti-1], T[ti] - x
    Ans = (max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    print(min(Ans))

Submission status: Accepted

Solution code:
from bisect import bisect
A, B, Q = map(int, input().split())
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S[A+1] = 10**20
T[B+1] = 10**20

lenS, lenT = len(S), len(T)
for _ in range(Q):
    x = int(input())
    si, ti = bisect(S, x, 0, lenS), bisect(T, x, 0, lenT)
    sLength0, sLength1 = x - S[si-1], S[si] - x
    tLength0, tLength1 = x - T[ti-1], T[ti] - x
    Ans = (max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    print(min(Ans))

Submission status: Accepted

Solution code:
import bisect
 
a, b, q = map(int, input().split())
INF = 10 ** 12
S = [-INF] + [int(input()) for i in range(a)] + [INF]
T = [-INF] + [int(input()) for i in range(b)] + [INF]

for i in range(q):
    x = int(input())
    sid = bisect.bisect_left(S, x)
    tid = bisect.bisect_left(T, x)
    ans = INF
    for s in (S[sid - 1], S[sid]):
        for t in (T[tid - 1], T[tid]):
            ans = min(ans, abs(x - s) +abs(s - t))
            ans = min(ans, abs(x - t) + abs(t - s))
    print(ans)

Submission status: Accepted

Solution code:
a,b,q=map(int,input().split())
s=[0]*a
t=[0]*b
x=[0]*q
for i in range(a):
  s[i]=int(input())
for i in range(b):
  t[i]=int(input())
import bisect
for i in range(q):
  x=int(input())
  ids=bisect.bisect_left(s,x)
  idt=bisect.bisect_left(t,x)
  mi=14857363987569617
  if ids < a:
    if idt < b:
      mi=min(mi,min([abs(s[ids]-x)+abs(s[ids]-t[idt]),
                    abs(t[idt]-x)+abs(t[idt]-s[ids])]))
    if idt > 0:
      mi=min(mi,min([abs(s[ids]-x)+abs(s[ids]-t[idt-1]),
                    abs(t[idt-1]-x)+abs(t[idt-1]-s[ids])]))
  if ids > 0:
    if idt < b:
      mi=min(mi,min([abs(s[ids-1]-x)+abs(s[ids-1]-t[idt]),
                    abs(t[idt]-x)+abs(t[idt]-s[ids-1])]))
    if idt > 0:
      mi=min(mi,min([abs(s[ids-1]-x)+abs(s[ids-1]-t[idt-1]),
                    abs(t[idt-1]-x)+abs(t[idt-1]-s[ids-1])]))
  print(mi)

Submission status: Accepted

Solution code:
import bisect

a, b, q = map(int, input().split())
slist = list(sorted(int(input()) for _ in range(a)))
tlist = list(sorted(int(input()) for _ in range(b)))
xlist = [int(input()) for _ in range(q)]
dlist = []

for x in xlist:
    i = bisect.bisect_left(slist, x)
    j = bisect.bisect_left(tlist, x)
    d = float('inf')

    # パターン1: 西に走る場合
    if i >= 1 and j >= 1:
        d = min(d, abs(min(slist[i - 1], tlist[j - 1]) - x))

    # パターン2: 東に走る場合
    if i < a and j < b:
        d = min(d, abs(max(slist[i], tlist[j]) - x))

    # パターン3: それぞれ直近の寺院と神社を訪問する場合
    k = min((v for v in (i - 1, i, i + 1) if 0 <= v < a), key=lambda v : abs(slist[v] - x))
    l = min((v for v in (j - 1, j, j + 1) if 0 <= v < b), key=lambda v : abs(tlist[v] - x))
    d = min(d, min(abs(slist[k] - x), abs(tlist[l] - x)) + abs(slist[k] - tlist[l]))

    dlist.append(d)


for d in dlist:
    print(d)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect_left
INF = 10**18
a,b,q = map(int, input().split())
sl = [-INF] + [int(input()) for _ in range(a)] + [INF]
tl = [-INF] + [int(input()) for _ in range(b)] + [INF]
# print(sl)
# print(tl)
for _ in range(q):
    x = int(input())
    si = bisect_left(sl,x)
    ti = bisect_left(tl,x)
    # print(x,si,ti)
    res = INF
    for s in [sl[si-1], sl[si]]:
        for t in [tl[ti-1], tl[ti]]:
            res = min(
                res,
                abs(s-x)+abs(t-s),
                abs(t-x)+abs(s-t)
            )
    print(res)

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(S-T),abs(T-x)+abs(S-T)
            res=min(res,d1,d2)
    print(res)

Submission status: Accepted

Solution code:
from bisect import *
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')]
Q = [int(input()) for _ in range(q)]
for x in Q:
    i = bisect_left(s, x)
    j = bisect_left(t, x)
    r = max(s[i]-x, t[j]-x)
    k = bisect_right(s, x)-1
    l = bisect_right(t, x)-1
    r = min(r, max(x-s[k], x-t[l]))
    r = min(r, min(x-s[k]+t[j]-s[k], t[j]-x+t[j]-s[k]))
    r = min(r, min(x-t[l]+s[i]-t[l], s[i]-x+s[i]-t[l]))
    print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import sys
input=lambda : sys.stdin.readline().rstrip()
a,b,q=map(int,input().split())
s=sorted([int(input()) for i in range(a)])
t=sorted([int(input()) for i in range(b)])
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Submission status: Accepted

Solution code:
import bisect
import sys
input=lambda : sys.stdin.readline().rstrip()
a,b,q=map(int,input().split())
s=sorted([int(input()) for i in range(a)])
t=sorted([int(input()) for i in range(b)])
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    sys.stdout.write("%d"%ans+'\n')

Submission status: Accepted

Solution code:
import bisect
import sys
input=lambda : sys.stdin.readline().rstrip()
a,b,q=map(int,input().split())
s=sorted([int(input()) for i in range(a)])
t=sorted([int(input()) for i in range(b)])
out=[]
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    out.append(ans)
print(*out,sep='\n')

Submission status: Accepted

Solution code:
import bisect
import sys
def input_function():
    return sys.stdin.readline().rstrip()
input=input_function
a,b,q=map(int,input().split())
s=sorted([int(input()) for i in range(a)])
t=sorted([int(input()) for i in range(b)])
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Submission status: Accepted

Solution code:
import bisect
from sys import stdin
readline=stdin.readline
a,b,q=map(int,input().rstrip().split())
s=sorted([int(readline().rstrip()) for i in range(a)])
t=sorted([int(readline().rstrip()) for i in range(b)])
for i in range(q):
    ans=999999999999
    x=int(readline().rstrip())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Submission status: Accepted

Solution code:
import bisect
from sys import stdin
readline=stdin.readline
a,b,q=map(int,input().split())
s=sorted([int(readline()) for i in range(a)])
t=sorted([int(readline()) for i in range(b)])
for i in range(q):
    ans=999999999999
    x=int(readline())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Submission status: Accepted

Solution code:
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 13
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())
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())
  j=bisect.bisect_left(s,x)
  if j==0:
    kouhoA=[s[j]]
  elif j==A:
    kouhoA=[s[j-1]]
  else:
    kouhoA=[s[j],s[j-1]]
  k=bisect.bisect_left(t,x)
  if k==0:
    kouhoB=[t[k]]
  elif k==B:
    kouhoB=[t[k-1]]
  else:
    kouhoB=[t[k],t[k-1]]
  if len(kouhoA)==1:
    
    if len(kouhoB)==1:
      a=abs(kouhoA[0]-kouhoB[0])
      ans=min([abs(x-kouhoA[0])+a,abs(x-kouhoB[0])+a])
    else:
      u=abs(x-kouhoA[0])
      a=abs(kouhoA[0]-kouhoB[0])
      b=abs(kouhoA[0]-kouhoB[1])
      ans=min([u+a,abs(x-kouhoB[0])+a,u+b,abs(x-kouhoB[1])+b])
  else:
    if len(kouhoB)==1:
      a=abs(kouhoA[0]-kouhoB[0])
      c=abs(kouhoB[0]-kouhoA[1])
      v=abs(x-kouhoB[0])
      ans=min([abs(x-kouhoA[0])+a,v+a,abs(x-kouhoA[1])+c,v+c])
    else:
      u=abs(x-kouhoA[0])
      a=abs(kouhoA[0]-kouhoB[0])
      b=abs(kouhoA[0]-kouhoB[1])
      c=abs(kouhoB[0]-kouhoA[1])
      d=abs(kouhoA[1]-kouhoB[1])
      v=abs(x-kouhoB[0])
      w=abs(x-kouhoA[1])
      y=abs(x-kouhoB[1])
      ans=min([u+a,v+a,w+d,y+d,w+c,v+c,u+b,y+b])
  print(ans)

Submission status: Accepted

Solution code:
a,b,q = map(int,input().split())
s = [-float('inf')]+[int(input()) for i in range(a)]+[float('inf')]
t = [-float('inf')]+[int(input()) for i in range(b)]+[float('inf')]
x = [int(input()) for i in range(q)]
import bisect
for x_ in x:
    s_r = bisect.bisect_right(s,x_)
    t_r = bisect.bisect_right(t,x_)
    res = float('inf')
    for S in [s[s_r-1],s[s_r]]:
        for T in [t[t_r-1],t[t_r]]:
            d1 = abs(S-x_)+abs(T-S)
            d2 = 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 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(T - S)
            res = min(res, d1, d2)
    print(res)

Submission status: Accepted

Solution code:
import bisect

A, B, Q = map(int, input().split())  # 複数数値入力
S = [0] * A
T = [0] * B
X = [0] * Q
for i in range(A):
    S[i] = int(input())
for i in range(B):
    T[i] = int(input())
for i in range(Q):
    X[i] = int(input())

S.insert(0, -float("inf"))
S.append(float("inf"))
T.insert(0, -float("inf"))
T.append(float("inf"))

for i in range(Q):
    b = bisect.bisect_right(S, X[i])
    d = bisect.bisect_right(T, X[i])
    res = float("inf")
    for s in [S[b - 1], S[b]]:
        for t in [T[d - 1], T[d]]:
            d1 = abs(s - X[i]) + abs(t - s)  # 先に神社
            d2 = abs(t - X[i]) + abs(t - s)  # 先に寺
            res = min(res, d1, d2)

    print(res)

Submission status: Accepted

Solution code:
import bisect
a,b,q = input().split()
a = int(a)
b = int(b)
q = int(q)
s = []
for i in range(a):
    s.append(int(input()))
t = []
for i in range(b):
    t.append(int(input()))
x = []
for i in range(q):
    x.append(int(input()))
s = [-10**18] + s + [10**18]
t = [-10**18] + t + [10**18]

for xi in x:
    ans = 10 ** 18
    id1_rs = bisect.bisect_right(s,xi)
    id1_rt = bisect.bisect_right(t,xi)
    id1_ls = id1_rs - 1
    id1_lt = id1_rt - 1
    for i1 in [id1_ls,id1_rs]:
        id2_rt = bisect.bisect_right(t,s[i1])
        id2_lt = id2_rt - 1
        if id2_rt==len(t):
            id2_rt -= 1
        for i2 in [id2_rt,id2_lt]:
            ans_tmp = abs(xi - s[i1])
            ans_tmp += abs(s[i1] - t[i2])
            if ans > ans_tmp:
                ans = ans_tmp
    for i1 in [id1_lt,id1_rt]:
        id2_rs = bisect.bisect_right(s,t[i1])
        if id2_rs==len(s):
            id2_rs -= 1
        id2_ls = id2_rs - 1
        for i2 in [id2_rs,id2_ls]:
            ans_tmp = abs(xi - t[i1]) + abs(t[i1] - s[i2])
            if ans > ans_tmp:
                ans = ans_tmp
    print(ans)

Submission status: Accepted

Solution code:
import bisect

def bl(A,x):
    return bisect.bisect_left(A,x)

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

INF = 10 ** 18
S,T = [-INF],[-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)

O = []
for i in range(Q):
    x = int(input())    
    xs = bl(S,x)
    xt = bl(T,x)
    res = INF
    for s in [S[xs-1],S[xs]]:
        for t in [T[xt-1],T[xt]]:
            d1,d2 = abs(x-s)+abs(s-t),abs(x-t)+abs(t-s)
            res = min(res,d1,d2)
    O.append(res)
    
for o in O:
    print(o)

Submission status: Accepted

Solution code:
from bisect import bisect_left
A , B, Q = map(int,input().split(" "))
s = [0] * A
t = [0] * B
x = [0] * Q
for i in range(A):
    s[i] = int(input())
for i in range(B):
    t[i] = int(input())

for i in range(Q):
    x[i] = int(input())
    shrine1 = bisect_left(s,x[i])
    shrine2 = shrine1 - 1
    temple1 = bisect_left(t,x[i])
    temple2 = temple1 - 1
    a = float('inf')
    if shrine1 == len(s):
        s1,s2 = (x[i] - s[-1]),float('inf')
    elif shrine1 == 0:
        s1,s2 = float('inf'),(s[0] - x[i])
    else:
        s1,s2 = x[i] - s[shrine2],s[shrine1] - x[i]

    if temple1 == len(t):
        t1,t2 = (x[i] - t[-1]),float('inf')

    elif temple1 == 0:
        t1,t2 = float('inf'),(t[0] - x[i])
    else:
        t1,t2 = x[i] - t[temple2],t[temple1] - x[i]
    print(min([ max(s2,t2) , max(t1,s1) , min(s1,t2)*2+max(s1,t2), min(s2,t1)*2+max(s2,t1) ]))

Submission status: Accepted

Solution code:
import bisect
A, B, Q = map(int, input().split())
s = [-10**10 - 1, -10**10] + [int(input()) for _ in range(A)] + [2*10**10]
t = [-10**10 - 1, -10**10] + [int(input()) for _ in range(B)] + [2*10**10]
x = [int(input()) for _ in range(Q)]


def func(c, d, z):
    i = bisect.bisect_left(c, z)
    j = bisect.bisect_left(d, z)
    if c[i - 1] < d[j - 1] and c[i] < d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    elif c[i - 1] < d[j - 1] and c[i] >= d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * d[j] - z - c[i - 1], c[i] - z)
    elif c[i - 1] >= d[j - 1] and c[i] < d[j]:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    else:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * d[j] - z - c[i - 1], c[i] - z)


for z in x:
    print(func(s, t, z))

Submission status: Accepted

Solution code:
import bisect
A,B,Q = map(int,input().split())
a = []
b = []
x = []
for i in range(A):
  a.append(int(input()))
for j in range(B):
  b.append(int(input()))
for k in range(Q):
  x.append(int(input()))
a.insert(0,-(10**15))
a.append(10**15)
b.insert(0,-(10**15))
b.append(10**15)
for k in range(Q):  
  p = bisect.bisect_left(a,x[k])
  q = bisect.bisect_left(b,x[k])
  teraleft_right = [a[p-1],a[p]]
  jinjyaleft_right = [b[q-1],b[q]]
  minlist = []
  for i in range(2):
    for j in range(2):
      minlist.append(abs(x[k]-teraleft_right[i])+abs(teraleft_right[i]-jinjyaleft_right[j]))
      minlist.append(abs(x[k]-jinjyaleft_right[i])+abs(jinjyaleft_right[i]-teraleft_right[j]))
  minlist.sort() 
  print(minlist[0])