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: Wrong Answer

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

a, b, q = map(int, input().split())
al = [-float("INF")] + [int(input()) for _ in range(a)] + [float("INF")]
bl = [-float("INF")] + [int(input()) for _ in range(b)] + [float("INF")]
for _ in range(q):
    res = float("INF")
    x = int(input())
    b, d = bisect.bisect_right(al, x), bisect.bisect_right(bl, x)
    ans = min(abs(al[b-1] - x) + abs(bl[d-1] - al[b-1]),  # 左方向に神社行って寺
              abs(bl[d-1] - x) + abs(al[b-1] - bl[d-1]),  # 左方向に寺行って神社
              abs(al[b-1] - x) * 2 + abs(bl[d] - x),  # 左の神社行って右の寺
              abs(bl[d-1] - x) * 2 + abs(al[b] - x),  # 左の寺行って右の神社
              abs(al[b] - x) * 2 + abs(bl[d-1] - x),  # 右の神社行って左の寺
              abs(bl[d] - x) * 2 + abs(al[b-1] - x),  # 右の寺行って左の神社
              abs(al[b] - x) + abs(al[b] - bl[d]),  # 右方向に神社行って寺
              abs(bl[d] - x) + abs(bl[d] - al[b])  #　右方向に寺行って神社
              )
    print(ans)

Submission status: Wrong Answer

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

a, b, q = map(int, input().split())
al = [-float("INF")] + [int(input()) for _ in range(a)] + [float("INF")]
bl = [-float("INF")] + [int(input()) for _ in range(b)] + [float("INF")]
for _ in range(q):
    res = float("INF")
    x = int(input())
    b, d = bisect.bisect_right(al, x), bisect.bisect_right(bl, x)
    ans = min(abs(al[b-1] - x) + abs(bl[d-1] - al[b-1]),  # 左方向に神社行って寺
              abs(bl[d-1] - x) + abs(al[b-1] - bl[d-1]),  # 左方向に寺行って神社
              abs(al[b-1] - x) * 2 + abs(bl[d] - x),  # 左の神社行って右の寺
              abs(bl[d-1] - x) * 2 + abs(al[b] - x),  # 左の寺行って右の神社
              abs(al[b] - x) * 2 + abs(bl[d-1] - x),  # 右の神社行って左の寺
              abs(bl[d] - x) * 2 + abs(al[b-1] - x),  # 右の寺行って左の神社
              abs(al[b] - x) + abs(al[b] - bl[d]),  # 右方向に神社行って寺
              abs(bl[d] - x) + abs(al[b] - bl[d])  #　右方向に寺行って神社
              )
    print(ans)

Submission status: Wrong Answer

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

a, b, q = map(int, input().split())
al = [-float("INF")] + [int(input()) for _ in range(a)] + [float("INF")]
bl = [-float("INF")] + [int(input()) for _ in range(b)] + [float("INF")]
for _ in range(q):
    res = float("INF")
    x = int(input())
    b, d = bisect.bisect_right(al, x), bisect.bisect_right(bl, x)
    ans = min(abs(al[b-1] - x) + abs(bl[d-1] - al[b-1]),  # 左方向に神社行って寺
              abs(bl[d-1] - x) + abs(al[b-1] - bl[d-1]),  # 左方向に寺行って神社
              abs(al[b-1] - x) + abs(al[b-1] - bl[d]),  # 左の神社行って右の寺
              abs(bl[d-1] - x) + abs(bl[d] - al[b-1]),  # 左の寺行って右の神社
              abs(al[b] - x) + abs(al[b] - bl[d-1]),  # 右の神社行って左の寺
              abs(bl[d] - x) + abs(bl[d] - al[b-1]),  # 右の寺行って左の神社
              abs(al[b] - x) + abs(al[b] - bl[d]),  # 右方向に神社行って寺
              abs(bl[d] - x) + abs(al[b] - bl[d])  #　右方向に寺行って神社
              )
    print(ans)

Submission status: Wrong Answer

Solution code:
N = list(map(int, input().split()))
#A = [[0] * (N[0]+N[1]+N[2])]*2
A = [[10**11+1] * 4 for i in range((N[0]+N[1]+N[2]))]
#print(A)
for i in range(N[0]):
    A[i][0]=int(input())
    A[i][1]=0

for i in range(N[0],N[0]+N[1]):
    A[i][0]=int(input())
    A[i][1]=1

r=0
for i in range(N[0]+N[1],N[0]+N[1]+N[2]):
    A[i][0]=int(input())
    A[i][1]=2
    A[i][2]=r
    r+=1

A=sorted(A)
#print(A)

L0=10**11+1
R0=10**11+1

L1=10**11+1
R1=10**11+1

j=0

for i in range(N[0]+N[1]+N[2]):
    k=i
    R0=10**11+1
    R1=10**11+1
    if(A[i][1]==0):L0=A[i][0]
    if(A[i][1]==1):L1=A[i][0]
    if(A[i][1]==2):
        for j in range(k,N[0]+N[1]+N[2]):
            if(A[j][1]==0):
                R0=A[j][0]
            if(A[j][1]==1):
                R1=A[j][0]
            if((R0!=(10**11+1))&(R1!=(10**11+1))):break
        LA01=abs(L0-A[i][0])
        RA01=abs(R0-A[i][0])
        LA11=abs(L1-A[i][0])
        RA11=abs(R1-A[i][0])
        LA02=LA01*2
        RA02=RA01*2
        LA12=LA11*2
        RA12=RA11*2

        #print(min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11)))
        A[i][3]=min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11))

A=sorted(A, key=lambda x: x[2])

for i in range(N[2]):
    print(A[i][3])

Submission status: Wrong Answer

Solution code:
N = list(map(int, input().split()))
#A = [[0] * (N[0]+N[1]+N[2])]*2
A = [[10**10+1] * 4 for i in range((N[0]+N[1]+N[2]))]
#print(A)
for i in range(N[0]):
    A[i][0]=int(input())
    A[i][1]=0

for i in range(N[0],N[0]+N[1]):
    A[i][0]=int(input())
    A[i][1]=1

r=0
for i in range(N[0]+N[1],N[0]+N[1]+N[2]):
    A[i][0]=int(input())
    A[i][1]=2
    A[i][2]=r
    r+=1

A=sorted(A)
#print(A)

L0=10**10+1
R0=10**10+1

L1=10**10+1
R1=10**10+1

j=0

for i in range(N[0]+N[1]+N[2]):
    k=i
    R0=10**10+1
    R1=10**10+1
    if(A[i][1]==0):L0=A[i][0]
    if(A[i][1]==1):L1=A[i][0]
    if(A[i][1]==2):
        for j in range(k,N[0]+N[1]+N[2]):
            if(A[j][1]==0):
                R0=A[j][0]
            if(A[j][1]==1):
                R1=A[j][0]
            if((R0!=(10**10+1))&(R1!=(10**10+1))):break
        LA01=abs(L0-A[i][0])
        RA01=abs(R0-A[i][0])
        LA11=abs(L1-A[i][0])
        RA11=abs(R1-A[i][0])
        LA02=LA01*2
        RA02=RA01*2
        LA12=LA11*2
        RA12=RA11*2

        #print(min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11)))
        A[i][3]=min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11))

A=sorted(A, key=lambda x: x[2])

for i in range(N[2]):
    print(A[i][3])

Submission status: Wrong Answer

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: Wrong Answer

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: Wrong Answer

Solution code:
import numpy as np
import math
import sys
import bisect
sys.setrecursionlimit(10000000)

def find_neighbor(list_, x, left, right):
    if right == len(list_):
        # 右がない
        neighbor = left
    else:
        if abs(list_[left] - x) < abs(list_[right] - x):
            neighbor = left
        else:
            neighbor = right
    return neighbor

def main():
    # 入力処理
    A, B, Q = [int(c) for c in input().split()]
    S = []
    T = []
    X = []
    for i in range(A):
        S.append(int(input()))
    for i in range(B):
        T.append(int(input()))
    for i in range(Q):
        X.append(int(input()))

    for i, x in enumerate(X):
        # s -> t
        right = bisect.bisect_left(S, x)
        left = right - 1
        
        s_neighbor = find_neighbor(S, x, left, right)

        right = bisect.bisect_left(T, S[s_neighbor])
        left = right - 1
        t_neighbor = find_neighbor(T, S[s_neighbor], left, right)

        distance = abs(x - S[s_neighbor]) + abs(T[t_neighbor] - S[s_neighbor])

        # s -> t
        right = bisect.bisect_left(T, x)
        left = right - 1
        
        t_neighbor = find_neighbor(T, x, left, right)

        right = bisect.bisect_left(S, T[t_neighbor])
        left = right - 1
        s_neighbor = find_neighbor(S, T[t_neighbor], left, right)

        distance = min(abs(x - T[t_neighbor]) + abs(S[s_neighbor] - T[t_neighbor]), distance)
        print(distance)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

ans = 10**100

s = [-ans] + [int(input()) for i in range(a)] + [ans]
t = [-ans] + [int(input()) for i in range(b)] + [ans]
x = [int(input()) for i in range(q)]


ans = 10**100

for i in range(q):
    ans = 10**100
    s_idx = bisect.bisect_left(s,x[i])
    t_idx = bisect.bisect_left(t,x[i])
    s_idx -= 1
    t_idx -= 1
    #print(x[i],s_idx,t_idx)
    for j in range(2):
        for k in range(2):
            ans = min(ans,abs(x[i] - s[s_idx+j]) + abs(s[s_idx+j] - t[t_idx+k]),abs(x[i] - t[t_idx+k]) + abs(s[s_idx] - t[t_idx+k]))
            
    print(ans)

Submission status: Wrong Answer

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 np(x,l):
   i = bisect.bisect_left(l,x)
   if i==0:
      return l[0]
   if i==len(l):
      return l[i-1]
   else:
      return l[i] if l[i]-x < x-l[i-1] else l[i-1]

for i in range(Q):
   p1 = np(x[i], s)
   p2 = np(p1, t)
   p3 = np(x[i], t)
   p4 = np(p3, s)
   print(min(abs(p1-x[i])+abs(p2-p1), abs(p3-x[i])+abs(p4-p3)))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

A, B, Q = LI()
s_list = []
t_list = []
for i in range(A):
  s_list.append(II())
for j in range(B):
  t_list.append(II())
x_list = []
for q in range(Q):
  x_list.append(II())
  
import bisect
for x in x_list:
  # 神を先に訪れる場合
  spos = bisect.bisect_left(s_list, x)
  spos = min(max(0, spos), len(s_list)-1)
  if spos >= 1:
    if abs(x - s_list[spos]) > abs(x - s_list[spos-1]):
      spos = spos -1
  sd = abs(x-s_list[spos])
  # 先に訪れた一番近い神から近い寺へ
  tpos = bisect.bisect_left(t_list, s_list[spos])
  tpos = min(max(0, tpos), len(t_list)-1)
  if tpos >= 1:
    if abs(s_list[spos] - t_list[tpos]) > abs(s_list[spos] - t_list[tpos-1]):
      tpos = tpos -1
  td = abs(t_list[tpos] - s_list[spos])
  ans1 = sd + td
  
  # 寺を先に訪れる場合
  tpos = bisect.bisect_left(t_list, x)
  tpos = min(max(0, tpos), len(t_list)-1)
  if tpos > 1 and abs(x - t_list[tpos]) > abs(x - t_list[tpos-1]):
    tpos = tpos -1
  td = abs(x-t_list[tpos])
  # 先に訪れた一番近い神から近い寺へ
  spos = bisect.bisect_left(s_list, t_list[tpos])
  spos = min(max(0, spos), len(s_list)-1)
  if spos > 1 and abs(t_list[tpos] - s_list[spos]) > abs(t_list[tpos] - s_list[spos-1]):
    spos = spos -1
  sd = abs(s_list[spos] - t_list[tpos])
  ans2 = sd + td
  
  print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

A , B , Q = map(int,input().split())
INF = 10**20

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 = [INF] + s + [INF]
t = [INF] + t + [INF]

for p in x :
    rightS = bisect_right(s,p)
    rightT = bisect_right(t,p)

    dist = INF

    for i in [s[rightS - 1], s[rightS]] :
        for j in [t[rightT -1], t[rightT]] :
            dist1 = abs(i - p) + abs(i - j)
            dist2 = abs(j - p) + abs(i - j)
            dist = min(dist, dist1, dist2)

    print(dist)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11 + 9
Sh = [int(input()) for _ in range(A)] + [inf]
Te = [int(input()) for _ in range(B)] + [inf]

def move(x, s, t, d):
  ress = inf
  rest = inf
  if s >= 1:
    if t >= 1:
      #print(d, "dです")
      return d
  mins = inf
  tempi = 0
  if s == 0:
    ress = 0
    tempi = bl(Sh, x)
    #print(tempi ,ress, "ress")
    if tempi > 0:
      mins = min(abs(x - Sh[tempi - 1]), abs(x - Sh[tempi]))
      if mins != abs(x - Sh[tempi]):
        tempi = tempi - 1
      ress += move(Sh[tempi], s + 1, t + 0, d + mins)
    else:
      ress += move(Sh[tempi], s + 1, t + 0, d + abs(x - Sh[tempi]))
    #print(Sh[tempi], ress, "ress")
  
  tempi = 0
  mint = inf
  if t == 0:
    rest = 0
    tempi = bl(Te, x)
    #print(tempi ,rest, "rest")
    if tempi > 0:
      mint = min(abs(x - Te[tempi - 1]), abs(x - Te[tempi]))
      if mint != abs(x - Te[tempi]):
        tempi = tempi - 1 

      rest += move(Te[tempi], s + 0, t + 1, d + mint)
    else:
      rest += move(Te[tempi], s + 0, t + 1, d + abs(x - Te[tempi]))
    #print(Te[tempi], rest, "rest")
  #print(ress, rest)
  return min(ress, rest)
for _ in range(Q):
  print(move(int(input()), 0, 0, 0))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11 + 9
Sh = [int(input()) for _ in range(A)] + [inf]
Te = [int(input()) for _ in range(B)] + [inf]

def move(x, s, t, d):
  ress = inf
  rest = inf
  if s >= 1:
    if t >= 1:
      #print(d, "dです")
      return d
  mins = inf
  tempi = 0
  if s == 0:
    ress = 0
    tempi = bl(Sh, x)
    #print(tempi ,ress, "ress")
    if tempi > 0:
      if len(Sh) >= 2:
        mins = min(abs(x - Sh[tempi - 1]), abs(x - Sh[tempi]))
        if mins != abs(x - Sh[tempi]):
          tempi = tempi - 1
      else:
        mins = abs(x - Sh[tempi - 1])
        tempi = tempi - 1
      ress += move(Sh[tempi], s + 1, t + 0, d + mins)
    else:
      ress += move(Sh[tempi], s + 1, t + 0, d + abs(x - Sh[tempi]))
    #print(Sh[tempi], ress, "ress")
  
  tempi = 0
  mint = inf
  if t == 0:
    rest = 0
    tempi = bl(Te, x)
    #print(tempi ,rest, "rest")
    if tempi > 0:
      if len(Te) >= 2:
        mint = min(abs(x - Te[tempi - 1]), abs(x - Te[tempi]))
        if mint != abs(x - Te[tempi]):
          tempi = tempi - 1 
      else:
        mint = abs(x - Te[tempi - 1])
        tempi = tempi - 1
      rest += move(Te[tempi], s + 0, t + 1, d + mint)
    else:
      rest += move(Te[tempi], s + 0, t + 1, d + abs(x - Te[tempi]))
    #print(Te[tempi], rest, "rest")
  #print(ress, rest)
  return min(ress, rest)
for _ in range(Q):
  print(move(int(input()), 0, 0, 0))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def cal(x):
  s_pos=bisect.bisect(s,x)
  t_pos=bisect.bisect(t,x)
  s_near=[]
  t_near=[]
  if s_pos==0:
    s_near.append(-1000000000001)
  else:
    s_near.append(s[s_pos-1])
  if s_pos==a:
    s_near.append(1000000000001)
  else:
    s_near.append(s[s_pos])
  if t_pos==0:
    t_near.append(-1000000000001)
  else:
    t_near.append(t[t_pos-1])
  if t_pos==b:
    t_near.append(1000000000001)
  else:
    t_near.append(t[t_pos])
  if s_near[0]<t_near[0] and s_near[1]>t_near[1]:
    return min(abs(x-s_near[0]),abs(x-s_near[1]))
  elif s_near[0]>t_near[0] and s_near[1]<t_near[1]:
    return min(abs(x-t_near[0]),abs(x-t_near[1]))
  elif s_near[0]<t_near[0]:
    return min(abs(x-s_near[0]),abs(x-t_near[1]),min(s_near[1]-x,x-t_near[0])+s_near[1]-t_near[0])
  else:
    return min(s_near[1]-x,x-t_near[0],min(x-s_near[0],t_near[1]-x)+t_near[1]-s_near[0])

for k in range(q):
  print(cal(X[k]))

Submission status: Wrong Answer

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

def cal(x):
  s_pos=bisect.bisect_left(s,x)
  t_pos=bisect.bisect_left(t,x)
  s_near=[]
  t_near=[]
  if s_pos==0:
    s_near.append(-1000000000001)
  else:
    s_near.append(s[s_pos-1])
  if s_pos==a:
    s_near.append(1000000000001)
  else:
    s_near.append(s[s_pos])
  if t_pos==0:
    t_near.append(-1000000000001)
  else:
    t_near.append(t[t_pos-1])
  if t_pos==b:
    t_near.append(1000000000001)
  else:
    t_near.append(t[t_pos])
  if s_near[0]<t_near[0] and s_near[1]>t_near[1]:
    return min(abs(x-s_near[0]),abs(x-s_near[1]))
  elif s_near[0]>t_near[0] and s_near[1]<t_near[1]:
    return min(abs(x-t_near[0]),abs(x-t_near[1]))
  elif s_near[0]<t_near[0]:
    return min(abs(x-s_near[0]),abs(x-t_near[1]),min(s_near[1]-x,x-t_near[0])+s_near[1]-t_near[0])
  else:
    return min(s_near[1]-x,x-t_near[0],min(x-s_near[0],t_near[1]-x)+t_near[1]-s_near[0])

for k in range(q):
  print(cal(X[k]))

Submission status: Wrong Answer

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

def cal(x):
  s_pos=bisect.bisect_left(s,x)
  t_pos=bisect.bisect_left(t,x)
  s_near=[]
  t_near=[]
  if s_pos==0:
    s_near.append(-100000000001)
  else:
    s_near.append(s[s_pos-1])
  if s_pos==a:
    s_near.append(100000000001)
  else:
    s_near.append(s[s_pos])
  if t_pos==0:
    t_near.append(-100000000001)
  else:
    t_near.append(t[t_pos-1])
  if t_pos==b:
    t_near.append(100000000001)
  else:
    t_near.append(t[t_pos])
  if s_near[0]<t_near[0] and s_near[1]>t_near[1]:
    return min(abs(x-s_near[0]),abs(x-s_near[1]))
  elif s_near[0]>t_near[0] and s_near[1]<t_near[1]:
    return min(abs(x-t_near[0]),abs(x-t_near[1]))
  elif s_near[0]<t_near[0]:
    return min(abs(x-s_near[0]),abs(x-t_near[1]),min(s_near[1]-x,x-t_near[0])+s_near[1]-t_near[0])
  else:
    return min(s_near[1]-x,x-t_near[0],min(x-s_near[0],t_near[1]-x)+t_near[1]-s_near[0])

for k in range(q):
  print(cal(X[k]))

Submission status: Wrong Answer

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 dist > min_dist:
      break

    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: Wrong Answer

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 dist > min_dist:
      break
    
    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: Wrong Answer

Solution code:
import bisect

if __name__ == "__main__":
    A, B, Q = map(int, input().split())
    s = [int(input()) for _ in range(A)]
    t = [int(input()) for _ in range(B)]
    s.append(float("inf"))
    t.append(float("inf"))
    s.sort()
    t.sort()
    for i in range(Q):
        x = int(input())
        s_zahyo = bisect.bisect_left(s, x)
        t_zahyo = bisect.bisect_left(t, x)
        s1 = s_zahyo - 1
        s2 = s_zahyo
        t1 = t_zahyo - 1
        t2 = t_zahyo

        p1 = abs(abs(x - s[s1]) + abs(s[s1] - t[t1]))
        p2 = abs(abs(x - t[t1]) + abs(t[t1] - s[s1]))

        p3 = abs(abs(x - s[s2]) + abs(s[s2] - t[t1]))
        p4 = abs(abs(x - t[t1]) + abs(t[t1] - s[s2]))

        p5 = abs(abs(x - s[s1]) + abs(s[s1] - t[t2]))
        p6 = abs(abs(x - t[t2]) + abs(t[t2] - s[s1]))

        p7 = abs(abs(x - s[s2]) + abs(s[s2] - t[t2]))
        p8 = abs(abs(x - t[t2]) + abs(t[t2] - s[s2]))

        ans = min(p1, p2, p3, p4, p5, p6, p7, p8)
        print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

if __name__ == "__main__":
    A, B, Q = map(int, input().split())
    s = [int(input()) for _ in range(A)]
    t = [int(input()) for _ in range(B)]
    s.append(float("inf"))
    t.append(float("inf"))
    s.sort()
    t.sort()
    for i in range(Q):
        x = int(input())
        s_zahyo = bisect.bisect_left(s, x)
        t_zahyo = bisect.bisect_left(t, x)
        s1 = s_zahyo - 1
        s2 = s_zahyo
        t1 = t_zahyo - 1
        t2 = t_zahyo

        p1 = abs(x - max(s[s1], t[t1])) + abs(max(s[s1], t[t1]) - min(s[s1], t[t1]))
        p2 = min(abs(x - s[s1]), abs(x - t[t2])) * 2 + max(abs(x - s[s1]), abs(x - t[t2]))
        p3 = min(abs(x - s[s2]), abs(x - t[t1])) * 2 + max(abs(x - s[s2]), abs(x - t[t1]))
        p4 = abs(x - min(s[s2], t[t2])) + abs(min(s[s2], t[t2]) - max(s[s2], t[t2]))
        ans = min(p1, p2, p3, p4)
        print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect as br
A, B, Q = map(int, input().split())
inf = 10**11
S = [-inf] + [int(sys.stdin.readline()) for _ in range(A)] + [inf]
T = [-inf] + [int(sys.stdin.readline()) for _ in range(B)] + [inf]
X = [int(sys.stdin.readline()) for _ in range(Q)]

StoT = [0]*A
for i, s in enumerate(S[1:-1]):
    x = br(T, s)
    StoT[i] = (s, min(s - T[x-1], T[x] - s))

TtoS = [0]*B
for i, t in enumerate(T[1:-1]):
    x = br(S, t)
    TtoS[i] = (t, min(t - S[x-1], S[x] - t))

M = [(-inf, inf)] + TtoS + StoT + [(inf, inf)]
M.sort()
print(M)
Mk , Mv = list(map(list, zip(*M)))
for x in X:
    idx = br(Mk, x)
    print(min(Mk[idx] - x + Mv[idx], x - Mk[idx-1] + Mv[idx-1]))

Submission status: Wrong Answer

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)]
inf = 10 ** 18

s = [-inf] + s + [inf]
t = [-inf] + t + [inf]

def binsearch_l(seq, l, r, tar):
    if r - l < 2: return l
    #
    m = l + (r - l) // 2
    # print("##", l, r, (m, seq[m]), tar)
    if seq[m] <= tar:
        return binsearch_l(seq, m, r, tar)
    else:
        return binsearch_l(seq, l, m - 1, tar)

for x_tmp in x:
    i_l = binsearch_l(s, 0, A + 2, x_tmp)
    j_l = binsearch_l(t, 0, B + 2, x_tmp)
    #
    ans = inf
    for i, j in ((i_l, j_l), (i_l + 1, j_l), (i_l, j_l + 1), (i_l + 1, j_l + 1)):
        if i < 1 or A < i: continue
        if j < 1 or B < j: continue
        ans_tmp_s = abs(x_tmp - s[i]) + abs(s[i] - t[j])
        ans_tmp_t = abs(x_tmp - t[j]) + abs(s[i] - t[j])
        ans_tmp = min(ans_tmp_s, ans_tmp_t)
        # print("#", x_tmp, s[i], t[j], ans_tmp)
        if ans > ans_tmp:
            ans = ans_tmp
    #
    print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect, bisect_left
inf = float('inf')

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

def solve(x):
    si = bisect(S,x)
    sl,sr = S[si-1],S[si]
    ti = bisect(T,x)
    tl,tr = T[ti-1],T[ti]
    ans = inf
    for s in [sl,sr]:
        for t in [tl,tr]:
            res = min(abs(s-x),abs(t-x)) + abs(s-t)
            ans = min(res,ans)
    return ans

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

Submission status: Wrong Answer

Solution code:
""" 入力受ける速度テスト """
import sys
A, B, Q = map(int, input().split())
jinja = list(map(int, (input() for _ in range(A))))
tera = list(map(int, (input() for _ in range(B))))
queries = list(map(int, (input() for _ in range(Q))))
print(0)

Submission status: Wrong Answer

Solution code:
""" 入力受ける速度テスト """
import sys
A, B, Q = map(int, input().split())
jinja = list(map(int, (input() for _ in [0]*A)))
tera = list(map(int, (input() for _ in [0]*B)))
queries = list(map(int, (input() for _ in [0]*Q)))
print(0)

Submission status: Wrong Answer

Solution code:
""" 入力受ける速度テスト """
import sys
A, B, Q = map(int, input().split())
jinja = list(map(int, (input() for _ in [0]*A)))
tera = list(map(int, (input() for _ in [0]*B)))
queries = list(map(int, (input() for _ in [0]*Q)))
print(0)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
a, b, q = list(map(int, input().split()))
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
for i in range(q):
  x = int(input())
  ss = bisect_left(s, x)
  tt = bisect_left(t, x)
  d1, d2, d3, d4 = 1e9, 1e9, 1e9, 1e9
  if ss-1>=0 and tt-1>=0:
    d1 = max((x-s[ss-1]), (x-t[tt-1]))
  if ss-1>=0 and tt<b:
    d2 = (x-s[ss-1])+(t[tt]-x)+min((x-s[ss-1]), (t[tt]-x))
  if ss<a and tt-1>=0:
    d3 = (s[ss]-x)+abs(x-t[tt-1])+min((s[ss]-x), abs(x-t[tt-1]))
  if ss<a and tt<b:
    d4 = max((s[ss]-x), (t[tt]-x))
  ans = min(d1, d2, d3, d4)
  print(ans)