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: Runtime Error

Solution code:
a, b, q = map(int, input().split())
s = sorted(int(input()) for _ in range(a))
t = sorted(int(input()) for _ in range(b))

st = []
(i, j) = (0, 0)
while i < len(s) or j < len(t):
    if j == len(t) or (i < len(s) and s[i] < t[j]):
        st.append((s[i], min(abs(s[i] - t[max(j - 1, 0)]),
                             abs(s[i] - t[min(j, len(t) - 1)]))))
        i += 1
    else:
        st.append((t[j], min(abs(t[j] - s[max(i - 1, 0)]),
                             abs(t[j] - s[min(i, len(s) - 1)]))))
        j += 1
    print(i, j, s, t, st)
print(st)

def doit(x):
    lo, hi = 0, len(st) - 1
    while hi - lo > 1:
        mid = (lo + hi) // 2
        if st[mid][0] < x:
            lo = mid
        else:
            hi = mid
    mid = (lo + hi) // 2
    return min(abs(st[mid][0] - x) + st[mid][1], abs(st[mid + 1][0] - x) + st[mid + 1][1])

for _ in range(q):
    print(doit(int(input())))

Submission status: Runtime Error

Solution code:
#ABC119 D - Lazy Faith
from bisect import bisect_left,bisect_right
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

a,b,q = map(int, input().split())
INF = 10**11
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 range(q):
    idx_s = bisect_left(S,X[i])
    idx_t = bisect_left(T,X[i])
    ans = INF
    for s in [S[idx_s-1],S[idx_s]]:
        for t in [T[idx_s-1],T[idx_s]]:
            d1,d2 = abs(s-X[i])+abs(s-t),abs(t-X[i])+abs(t-s)
            ans = min(ans,d1,d2)
    print(ans)

Submission status: Runtime Error

Solution code:
uwaaaaaaaaaaaaaaaaaaaaaa

Submission status: Runtime Error

Solution code:
import bisect
a,b,q=map(int,input().split())
ls=[-10**11]
lg=[-10**11]
for i in range(a):
  ls.append(int(input()))
for i in range(b):
  lg.append(int(input()))
ls.append(10**11)
lg.append(10**11)
ls=sorted(ls)
lg=sorted(lg)
for i in range(q):
  x=int(input())
  i1=bisect.bisect_left(ls,x)
  i2=bisect.bisect_left(lg,x)#xはi-1~iにある
  l1=[i1,i1+1]
  l2=[i2,i2+1]
  ans1=10**10
  ans2=10**10
  for k in l1:
    for j in l2:
      ans3=abs(x-ls[k])+abs(ls[k]-lg[j])
      if ans3<ans1:
        ans1=ans3
      ans4=abs(x-lg[j])+abs(ls[k]-lg[j])
      if ans4<ans2:
        ans2=ans4
  print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:
import bisect
a,b,q=map(int,input().split())
ls=[-10**11]
lg=[-10**11]
for i in range(a):
  ls.append(int(input()))
for i in range(b):
  lg.append(int(input()))
ls.append(10**11)
lg.append(10**11)
ls=sorted(ls)
lg=sorted(lg)
for i in range(q):
  x=int(input())
  i1=bisect.bisect_left(ls,x)
  i2=bisect.bisect_left(lg,x)#xはi-1~iにある
  l1=[i1,i1+1]
  l2=[i2,i2+1]
  ans1=10**10
  ans2=10**10
  for i in l1:
    for j in l2:
      ans3=abs(x-ls[i])+abs(ls[i]-lg[j])
      if ans3<ans1:
        ans1=ans3
      ans4=abs(x-lg[j])+abs(ls[i]-lg[j])
      if ans4<ans2:
        ans2=ans4
  print(min(ans1,ans2))

Submission status: Runtime Error

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

s=[-2*10**10]+[int(input()) for _ in range(a)]+[2*10**10]
t=[-2*10**10]+[int(input()) for _ in range(b)]+[2*10**10]
x=[int(input()) for _ in range(q)]

for i in range(q):
  j = bisect.bisect_left(s,x[i])
  al = x[i] - s[j-1]
  ar = s[j] - x[i]
  j = bisect.bisect_left(t,x[i])
  bl = x[i] - t[j-1]
  br = t[j] - x[i]
  l = min(max(al, bl), max(ar, br), 2*al+br, 2*ar+bl, 2*bl+ar, 2*br+al)
  print(l)

Submission status: Runtime Error

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())
    si=bisect.bisect_left(s,x)
    ti=bisect.bisect_left(t,x)
    if si==a:
        sk=[s[a-1]]
    elif si==0:
        sk=[s[0]]
    else:
        sk=[s[si-1],s[si]]
    if ti==0:
        tk=[t[0]]
    else:
        tk=[t[ti-1],t[ti]]
    ans=1000000000000
    for f in sk:
        for g in tk:
            ans=min(ans,abs(x-f)+abs(f-g),abs(x-g)+abs(g-f))
print(ans)

Submission status: Runtime Error

Solution code:
import bisect
A,B,Q = map(int,f.readline().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
for _ in range(Q):
    x = int(input())
    indS = bisect.bisect_right(S,x)
    indT = bisect.bisect_right(T,x)
    if indS==0 and indT==0:
        print(max(S[0],T[0])-x)
    elif indS==0 and indT>0:
        if indT<len(T) and T[indT]<S[indS]:
            print(S[0]-x)
        elif indT<len(T) and T[indT]>S[indS]:
            r1 = T[indT]-x
            r2 = 2*(x-T[indT-1])+S[indS]-x
            r3 = x-T[ind-1]+2*(S[indS]-x)
            print(min(r1,r2,r3))
        elif indT==len(T):
            r1 = 2*(x-T[indT-1])+S[indS]-x
            r2 = x-T[indT-1]+2*(S[indS]-x)
            print(min(r1,r2))
    elif indT==0 and indS>0:
        if indS<len(S) and S[indS]<T[indT]:
            print(T[0]-x)
        elif indS<len(S) and S[indS]>T[indT]:
            r1 = S[indS]-x
            r2 = 2*(x-S[indS-1])+T[indT]-x
            r3 = x-S[indS-1]+2*(T[indT]-x)
            print(min(r1,r2,r3))
        elif indS==len(S):
            r1 = 2*(x-S[indS-1])+T[indT]-x
            r2 = x-S[indS-1]+2*(T[indT]-x)
            print(min(r1,r2))
    elif indS==len(S) and indT==len(T):
        print(x-min(S[-1],T[-1]))
    elif indS==len(S) and indT<len(T):
        if indT==0:
            r1 = 2*(x-S[-1])+T[0]-x
            r2 = x-S[-1]+2*(T[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if T[indT-1]>S[-1]:
                print(x-S[-1])
            elif T[indT-1]<S[-1]:
                r1 = 2*(T[indT]-x)+x-S[-1]
                r3 = T[indT]-x+2*(x-S[-1])
                r2 = x-T[indT-1]
                print(min(r1,r2,r3))
    elif indS<len(S) and indT==len(T):
        if indS==0:
            r1 = 2*(x-T[-1])+S[0]-x
            r2 = x-T[-1]+2*(S[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if S[indS-1]>T[-1]:
                print(x-T[-1])
            elif S[indS-1]<T[-1]:
                r1 = 2*(S[indS]-x)+x-T[-1]
                r3 = S[indS]-x+2*(x-T[-1])
                r2 = x-S[indt-1]
                print(min(r1,r2,r3))
    else:
        r1 = max(S[indS],T[indT])-x
        r2 = x-min(S[indS-1],T[indT-1])
        r3 = 2*(S[indS]-x)+x-T[indT-1]
        r4 = S[indS]-x+2*(x-T[indT-1])
        r5 = 2*(x-S[indS-1])+T[indT]-x
        r6 = x-S[indS-1]+2*(T[indT]-x)
        print(min(r1,r2,r3,r4,r5,r6))

Submission status: Runtime Error

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())
    indS = bisect.bisect_right(S,x)
    indT = bisect.bisect_right(T,x)
    if indS==0 and indT==0:
        print(max(S[0],T[0])-x)
    elif indS==0 and indT>0:
        if indT<len(T) and T[indT]<S[indS]:
            print(S[0]-x)
        elif indT<len(T) and T[indT]>S[indS]:
            r1 = T[indT]-x
            r2 = 2*(x-T[indT-1])+S[indS]-x
            print(min(r1,r2))
        elif indT==len(T):
            r1 = 2*(x-T[indT-1])+S[indS]-x
            r2 = x-T[indT-1]+2*(S[indS]-x)
            print(min(r1,r2))
    elif indT==0 and indS>0:
        if indS<len(S) and S[indS]<T[indT]:
            print(T[0]-x)
        elif indS<len(S) and S[indS]>T[indT]:
            r1 = S[indS]-x
            r2 = 2*(x-S[indS-1])+T[indT]-x
            print(min(r1,r2))
        elif indS==len(S):
            r1 = 2*(x-S[indS-1])+T[indT]-x
            r2 = x-S[indS-1]+2*(T[indT]-x)
            print(min(r1,r2))
    elif indS==len(S) and indT==len(T):
        print(x-min(S[-1],T[-1]))
    elif indS==len(S) and indT<len(T):
        if indT==0:
            r1 = 2*(x-S[-1])+T[0]-x
            r2 = x-S[-1]+2*(T[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if T[indT-1]>S[-1]:
                print(x-S[-1])
            elif T[indT-1]<S[-1]:
                r1 = 2*(T[indT]-x)+x-S[-1]
                r2 = x-T[indT-1]
                print(min(r1,r2))
    elif indS<len(S) and indT==len(T):
        if indS==0:
            r1 = 2*(x-T[-1])+S[0]-x
            r2 = x-T[-1]+2*(S[0]-x)
            print(min(r1,r2))
        elif indT>0:
            if S[indS-1]>T[-1]:
                print(x-T[-1])
            elif S[indS-1]<T[-1]:
                r1 = 2*(S[indS]-x)+x-T[-1]
                r2 = x-S[indt-1]
                print(min(r1,r2))
    else:
        r1 = max(S[indS],T[indT])-x
        r2 = x-min(S[indS-1],T[indT-1])
        print(min(r1,r2))

Submission status: Runtime Error

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 i 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[b-1], t[b]]:
      d1, d2 = abs(S-x) + abs(T-S), abs(T-x) + abs(S-T)
      res = min(d1, d2, res)
  print(res)

Submission status: Runtime Error

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

Submission status: Runtime Error

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)]
v=10**18
w=[-10**18]
s.append(v)
s=w+s
t.append(v)
t=w+t
print(s)
print(t)
print(x)

for i in range(n):
    
    index1 = bisect.bisect_left(s, x[i])    
    index2 = bisect.bisect_left(t, x[i])  
    ans=abs(x[i]-)

Submission status: Runtime Error

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

for i in range(Q):
  x = int(input())
  ai = bisect_right(x,als)
  bi = bisect_right(x,bls)
  res = INF
  for S in [als[ai], als[ai-1]]:
    for T in [bls[bi], bls[bi-1]]:
      d1, d2 = abs(S-x)+abs(T-S), abs(T-x)+abs(S-T)
      res = min(res,d1,d2)
  print(res)

Submission status: Runtime Error

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

for i in range(Q):
  x = int(input())
  ai = bisect_left(x,als)
  bi = bisect_left(x,bls)
  res = INF
  for S in [als[ai], als[ai-1]]:
    for T in [bls[bi], bls[bi-1]]:
      d1, d2 = abs(S-x)+abs(T-S), abs(T-x)+abs(S-T)
      res = min(res,d1,d2)
  print(res)

Submission status: Runtime Error

Solution code:
import bisect
A,B,Q = map(int, input().split())
S,T,X = [],[],[]
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())

for i in range(Q):
  ans = []
  sin = bisect.bisect_left(S, X[i])
  if sin == 0:
    s1,s2 = S[sin], -(10**9)
  elif sin == A:
    s1,s2 = (10**9),S[sin-1]
  else:
    s1,s2 = S[sin],S[sin-1]
  tin = bisect.bisect_left(T, X[i])
  if tin == 0:
    t1,t2 = T[tin], -(10**9)
  elif tin == B:
    t1,t2 = (10**9),T[tin-1]
  else:
    t1,t2 = T[tin],T[tin-1]
  for j in [s1,s2]:
    for k in [t1,t2]:
      for l in [0,1]:
        if l ==0:
          ans.append(abs(j-X[i])+abs(k-j))
        else:
          ans.append(abs(k-X[i])+abs(j-k)) 
          
  print(min(ans))

Submission status: Runtime Error

Solution code:
import bisect
A,B,Q = map(int, input().split())
S,T,X = [],[],[]
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())
 for i in range(Q):
  ans = []
  sin = bisect.bisect_left(S, X[i])
  if sin == 0:
    s1,s2 = S[sin], -(10**9)
  elif sin == A:
    s1,s2 = (10**9),S[sin-1]
  else:
    s1,s2 = S[sin],S[sin-1]
  tin = bisect.bisect_left(T, X[i])
  if tin == 0:
    t1,t2 = T[tin], -(10**9)
  elif tin == B:
    t1,t2 = (10**9),T[tin-1]
  else:
    t1,t2 = T[tin],T[tin-1]
  for j in [s1,s2]:
    for k in [t1,t2]:
      for l in [0,1]:
        if l ==0:
          ans.append(abs(j-X[i])+abs(k-j))
        else:
          ans.append(abs(k-X[i])+abs(j-k)) 
          
  print(min(ans))

Submission status: Runtime Error

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: Runtime Error

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: Runtime Error

Solution code:
# ABC 119 D

import bisect

def resolve():
    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):
        i = int(input())
        s1 = -1 if bisect.bisect_left(s, i) == 0 else s[bisect.bisect_left(s, i)-1]
        s2 = -1 if bisect.bisect_right(s, i) >= A else s[bisect.bisect_right(s, i)]
        t1 = -1 if bisect.bisect_left(t, i) == 0 else t[bisect.bisect_left(t, i)-1]
        t2 = -1 if bisect.bisect_left(t, i) >= A else t[bisect.bisect_right(t, i)]
        tmp = []

        if s1 != -1 and t1 != -1:
            tmp.append(max(i-s1, i-t1))
        if s2 != -1 and t2 != -1:
            tmp.append(max(s2-i, t2-i))
        if s1 != -1 and t2 != -1:
            tmp.append(min(i-s1, t2-i) + t2-s1)
        if t1 != -1 and s2 != -1:
            tmp.append(min(i-t1, s2-i) + s2-t1)

        print(min(tmp))


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

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: Runtime Error

Solution code:
def search(sta, end, limit, tgt, list_x):
  
  mid = sta + (end - sta) // 2
  
  if(mid == 0):
    if(list_x[mid] > tgt):
      left  = -float('inf')
      right = list_x[mid]
    elif((tgt > list_x[mid]) & (list_x[mid+1] > tgt)):
      left  = list_x[mid]
      right = list_x[mid+1]
    else:
      [left, right] = search(mid+1, end, limit, tgt, list_x)
  elif(mid+1 == limit):
    if(list_x[mid] > tgt):
      [left, right] = search(sta, mid, limit, tgt, list_x)
    elif((tgt > list_x[mid]) & (list_x[mid+1] > tgt)):
      left  = list_x[mid]
      right = list_x[mid+1]
    else:
      left  = list_x[mid+1]
      right = float('inf')
  elif(mid == limit):
    if(list_x[mid] > tgt):
      [left, right] = search(sta, mid, limit, tgt, list_x)
    else:
      left  = list_x[mid]
      right = float('inf')
  else:
    if(list_x[mid] > tgt):
      [left, right] = search(sta, mid, limit, tgt, list_x)
    elif((tgt > list_x[mid]) & (list_x[mid+1] > tgt)):
      left  = list_x[mid]
      right = list_x[mid+1]
    else:
      [left, right] = search(mid+1, end, limit, tgt, list_x)
  
  return [left, right]


str_in = input().split()
num_a = int(str_in[0])
num_b = int(str_in[1])
num_q = int(str_in[2])

list_a = [int(input()) for i in range(num_a)]
list_b = [int(input()) for i in range(num_b)]
list_q = [int(input()) for i in range(num_q)]

for i in range(num_q):
  near_a = search(0, num_a-1, num_a-1, list_q[i], list_a)
  near_b = search(0, num_b-1, num_b-1, list_q[i], list_b)
  
  len_ll =   list_q[i] - min([near_a[0], near_b[0]])
  len_rr = - list_q[i] + max([near_a[1], near_b[1]])
  len_lr =   min(list_q[i] - near_a[0],  near_b[1] - list_q[i]) * 2 + max(list_q[i] - near_a[0],  near_b[1] - list_q[i])
  len_rl =   min(list_q[i] - near_b[0],  near_a[1] - list_q[i]) * 2 + max(list_q[i] - near_b[0],  near_a[1] - list_q[i])
  
  ans = min([len_ll, len_rr, len_lr, len_rl])
  print(ans)

Submission status: Runtime Error

Solution code:
import bisect

INF = 10 ** 9
A, B, Q = map(int, input().split())
shrine_list = [-INF] + [int(input()) for i in range(A)] + [INF]
temple_list = [-INF] + [int(input()) for i in range(B)] + [INF]
question_list = [int(input()) for i in range(Q)]

for q in question_list:
    b, d = bisect.bisect_right(shrine_list, q), bisect.bisect_right(temple_list, q)
    ans = INF
    for s in [shrine_list[b - 1], shrine_list[b]]:
        for t in [temple_list[d - 1], temple_list[d]]:
            ans = min(abs(q - s) + abs(t - s), abs(q - t) + abs(s - t), ans)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math

sys.setrecursionlimit(5000)

A, B, Q = map(int, input().split())
LIMIT = 2*(10**10)
shrines = [-LIMIT] + [int(input()) for i in range(A)] + [LIMIT]
temples = [-LIMIT] + [int(input()) for j in range(B)] + [LIMIT]

def b_temp_search(num, idx):
    if num >= temples[idx] and num <= temples[idx+1]:
        return idx
    elif num < temples[idx]:
        if idx == 1:
            return 0
        return b_temp_search(num, math.ceil(idx/2))
    else:
        if idx == B:
            return B + 1
        return b_temp_search(num, math.ceil((B+idx+1)/2))

def b_sh_search(num, idx):
    if num >= shrines[idx] and num <= shrines[idx+1]:
        return idx
    elif num < shrines[idx]:
        if idx == 1:
            return 0
        return b_sh_search(num, math.ceil(idx//2))
    else:
        if idx == A:
            return A + 1
        return b_sh_search(num, math.ceil((A+idx+1)/2))

for k in range(Q):
    x = int(input())
    sh_idx = b_sh_search(x, math.ceil((A+2)//2))
    temp_idx = b_temp_search(x, math.ceil((B+2)//2))
    ptn1 = max(abs(x-shrines[sh_idx]), abs(x-temples[temp_idx]))
    ptn2 = max(abs(x-shrines[sh_idx+1]), abs(x-temples[temp_idx+1]))
    ptn3 = min((temples[temp_idx+1] - x)*2 + x - shrines[sh_idx], temples[temp_idx+1] - x + (x - shrines[sh_idx])*2)
    ptn4 = min((shrines[sh_idx+1] - x)*2 + x - temples[temp_idx], shrines[sh_idx+1] - x + (x - temples[temp_idx])*2)
    ans = min(ptn1, ptn2, ptn3, ptn4)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math

sys.setrecursionlimit(5000)

A, B, Q = map(int, input().split())
LIMIT = 2*(10**10)
shrines = [-LIMIT] + [int(input()) for i in range(A)] + [LIMIT]
temples = [-LIMIT] + [int(input()) for j in range(B)] + [LIMIT]

def b_temp_search(num, idx):
    if num >= temples[idx] and num <= temples[idx+1]:
        return idx
    elif num < temples[idx]:
        if idx == 1:
            return 0
        return b_temp_search(num, math.ceil(idx/2))
    else:
        if idx == B:
            return B + 1
        return b_temp_search(num, math.ceil((B+idx)/2))

def b_sh_search(num, idx):
    if num >= shrines[idx] and num <= shrines[idx+1]:
        return idx
    elif num < shrines[idx]:
        if idx == 1:
            return 0
        return b_sh_search(num, math.ceil(idx//2))
    else:
        if idx == A:
            return A + 1
        return b_sh_search(num, math.ceil((A+idx)/2))

for k in range(Q):
    x = int(input())
    sh_idx = b_sh_search(x, math.ceil((A+2)//2))
    temp_idx = b_temp_search(x, math.ceil((B+2)//2))
    ptn1 = max(abs(x-shrines[sh_idx]), abs(x-temples[temp_idx]))
    ptn2 = max(abs(x-shrines[sh_idx+1]), abs(x-temples[temp_idx+1]))
    ptn3 = min((temples[temp_idx+1] - x)*2 + x - shrines[sh_idx], temples[temp_idx+1] - x + (x - shrines[sh_idx])*2)
    ptn4 = min((shrines[sh_idx+1] - x)*2 + x - temples[temp_idx], shrines[sh_idx+1] - x + (x - temples[temp_idx])*2)
    ans = min(ptn1, ptn2, ptn3, ptn4)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math

sys.setrecursionlimit(5000)

A, B, Q = map(int, input().split())
LIMIT = 2*(10**10)
shrines = [-LIMIT] + [int(input()) for i in range(A)] + [LIMIT]
temples = [-LIMIT] + [int(input()) for j in range(B)] + [LIMIT]

def b_temp_search(num, idx):
    if num >= temples[idx] and num <= temples[idx+1]:
        return idx
    elif num < temples[idx]:
        if idx == 1:
            return 0
        return b_temp_search(num, math.ceil(idx/2))
    else:
        if idx == B:
            return B
        return b_temp_search(num, math.ceil((B+idx)/2))

def b_sh_search(num, idx):
    if num >= shrines[idx] and num <= shrines[idx+1]:
        return idx
    elif num < shrines[idx]:
        if idx == 1:
            return 0
        return b_sh_search(num, math.ceil(idx//2))
    else:
        if idx == A:
            return A
        return b_sh_search(num, math.ceil((A+idx)/2))

for k in range(Q):
    x = int(input())
    sh_idx = b_sh_search(x, A//2)
    temp_idx = b_temp_search(x, B//2)
    ptn1 = max(abs(x-shrines[sh_idx]), abs(x-temples[temp_idx]))
    ptn2 = max(abs(x-shrines[sh_idx+1]), abs(x-temples[temp_idx+1]))
    ptn3 = min((temples[temp_idx+1] - x)*2 + x - shrines[sh_idx], temples[temp_idx+1] - x + (x - shrines[sh_idx])*2)
    ptn4 = min((shrines[sh_idx+1] - x)*2 + x - temples[temp_idx], shrines[sh_idx+1] - x + (x - temples[temp_idx])*2)
    ans = min(ptn1, ptn2, ptn3, ptn4)
    print(ans)

Submission status: Runtime Error

Solution code:
import bisect


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

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)

    tl = bisect.bisect(tlist,s)
    if tl == 0:
      dist += abs(s-tlist[0])
    elif tl == len(tlist):
      dist += abs(s-tlist[len(tlist)-1])
    else:
      dist += min(abs(s-tlist[tl-1]),abs(s-tlist[tl]))
    
    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)

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

  print(min_dist)

Submission status: Runtime Error

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)

    tl = bisect.bisect(tlist,s)
    if tl == 0:
      dist += abs(s-tlist[0])
    elif tl == len(tlist):
      dist += abs(s-tlist[len(tlist)-1])
    else:
      dist += min(abs(s-tlist[tl-1]),abs(s-tlist[tl]))
    
    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]

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

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

  print(min_dist)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque


def bfs(s):
    flag_list[s] = 1
    color_list[s] = 0
    q.append(s)
    while len(q)>0:
        u = q.popleft()
        for v, w in enumerate(m_list[u]):
            if w > 0 and flag_list[v] == 0:
                flag_list[v] = 1
                if w%2 == 0:
                    color_list[v] = color_list[u]
                else:
                    color_list[v] = (color_list[u]+1)%2
                q.append(v)
        flag_list[u] = 2


if __name__ == "__main__":
    input = sys.stdin.readline
    n = int(input())
    m_list = [[0]*n for _ in range(n)]
    color_list = [0]*n
    flag_list = [0]*n
    q = deque([])
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        m_list[u-1][v-1] = w
        m_list[v-1][u-1] = w
    bfs(0)
    for color in color_list:
        print(color)

Submission status: Runtime Error

Solution code:
a,b,q=map(int,input().split())
ls=[]
for i in range(a):
  ls.append(int(input()))
lt=[]
for i in range(b):
  lt.append(int(input()))
def nibutan2(n,l):
  left=0
  right=len(l)-1
  mid=(right+left)//2
  while right>left:
    if l[mid]==n:
      return [0,mid]
    elif l[mid]>n:
      right=mid-1
      mid=(right+left)//2
    else:
      left=mid+1
      mid=(right+left)//2
  if min(abs(l[mid]-n),abs(l[mid-1]-n),abs(l[mid+1]-n))==abs(l[mid]-n):
    return [abs(l[mid]-n),mid]
  elif min(abs(l[mid]-n),abs(l[mid-1]-n),abs(l[mid+1]-n))==abs(l[mid-1]-n):
    return [abs(l[mid-1]-n),mid-1]
  else: 
    return [abs(l[mid+1]-n),mid+1]
for i in range(q):
  n=int(input())
  llt=0
  lls=0
  lltl=nibutann(n,lt)
  llt1=lltl[0]
  llts=lltl[1]
  llt2=nibutann(llts,ls)[0]
  llt=llt1+llt2
  lltl=nibutann(n,ls)
  llt1=lltl[0]
  llts=lltl[1]
  llt2=nibutann(llts,lt)[0]
  lls=llt1+llt2
  print(min(llt,lls))

Submission status: Runtime Error

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

#二分探索関数
def bin_search(x,seq):
    a=0
    b=len(seq)-1
    for 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=int(input())
    Nb_s0,Nb_s1=bin_search(x,s)
    Nb_t0,Nb_t1=bin_search(x,t)
    k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
    k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
    k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
    k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
    k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
    k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
    k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
    k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
    print(min(k1,k2,k3,k4,k5,k6,k7,k8))

Submission status: Runtime Error

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

#二分探索関数
def bin_search(x,seq):
    width=[0,len(seq)-1]
    while(width[1]-width[0]>1):
        center=(sum(width)//2)
        if x>=seq[center]:
            width=[center,width[1]]
        else:
            width=[width[0],center]
if x<=seq[width[0]]:
    return [width[0],width[0]]
elif x>=seq[width[1]]:
    return [width[1],width[1]]
else:
    return width

for i in range(Q):
    x=int(input())
    Nb_s=bin_search(x,s)
    Nb_t=bin_search(x,t)
    Nb_s0=s[Nb_s[0]]
    Nb_s1=s[Nb_s[1]]
    Nb_t0=t[Nb_t[0]]
    Nb_t1=t[Nb_t[1]]

    k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
    k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
    k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
    k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
    k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
    k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
    k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
    k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
    print(min(k1,k2,k3,k4,k5,k6,k7,k8))