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

	
def find_st(start):
	# 最大一回の折り返し。
	# 左右の最初の建物と、そこから最も近い建物との距離ふたつの小さい方を求める。
	sli = bisect.bisect_right(s,start)
	if sli==0:sl = -1
	else:sl = s[sli-1]
	if sli==a: sr = -1
	else: sr = s[sli]
	
	tli = bisect.bisect_right(t,start)
	if tli==0:tl=-1
	else: tl = t[tli-1]
	if tli==b: tr=-1
	else: tr = t[tli]
	return [sl, sr, tl, tr]

def find_md(start):
	min_dist = 100000000000000000
	sl,sr,tl,tr = find_st(start)
	# 左からルート
	if tl==-1 and sl==-1:
		return max(sr-start, tr-start)
	if sr==-1 and tr==-1:
		return max(start-sl, start-tl)
	if sl==-1 and tr==-1:
		return (sr-start) + (start-tl)
	if tl==-1 and sr==-1:
		return (tr-start) + (start-sl)
	
	if sl==-1:
		# 右に両方行くのと折り返すのとを比較
		return min(max(sr-start, tr-start), (sr-start)+(start-tl)*2, (sr-start)*2+(start-tl))
	if tl==-1:
		# 右に両方行くのと折り返すのとを比較
		return min(max(sr-start, tr-start), (tr-start)+(start-sl)*2, (tr-start)*2+(start-sl))
	if sr==-1:
		# 左に両方行くのと折り返すのとを比較
		return min(max(start-sl, start-tl), (tr-start)+(start-sl)*2, (tr-start)*2+(start-sl))
	if tr==-1:
		# 左に両方行くのと折り返すのとを比較
		return min(max(start-sl, start-tl), (sr-start)+(start-tl)*2, (sr-start)*2+(start-tl))

	return min(max(start-sl, start-tl), max(sr-start, tr-start),min(start-sl, start-tl)*2+min(sr-start, tr-start),min(start-sl, start-tl)+min(sr-start, tr-start)*2)			
			
def solve():
	for i in range(q):
		print(find_md(x[i]))
	
solve()

Submission status: Wrong Answer

Solution code:
num=list(map(int,input().split()))
s_list = [int(input()) for i in range(num[0])]
t_list = [int(input()) for i in range(num[1])]
x_list = [int(input()) for i in range(num[2])]

for x_ in x_list:
	min_s=min(list(map(lambda x: abs(x-x_), s_list)))
	min_t=min(list(map(lambda x: abs(x-x_), t_list)))
	
	min_index_t=list(map(lambda x: abs(x-x_), t_list)).index(min_t)
	
	minmin_t=min(list(map(lambda x: abs(x-t_list[min_index_t]), s_list)))+min_t




	min_index_s=list(map(lambda x: abs(x-x_), s_list)).index(min_s)
	minmin_s=min(list(map(lambda x: abs(x-s_list[min_index_s]), t_list)))+min_s
	print(minmin_t,minmin_s)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

lines = sys.stdin.readlines()
A, B, Q = map(int, lines[0].split())
S = sorted([int(n) for n in lines[1:A+1]])
T = sorted([int(n) for n in lines[A+1:A + B + 1]])
X = sorted([int(n) for n in lines[A + B + 1:A + B + Q + 1]])

# print(S)
# print(T)
# print(X)


def find_le(a, x):
    i = bisect.bisect_right(a, x)
    if i:
        return i - 1, a[i-1]
    return -1, - sys.maxsize

def find_ge(a, x):
    i = bisect.bisect_left(a, x)
    if i != len(a):
        return i, a[i]
    return len(a), sys.maxsize
  
x = X[0]
for x in X:
  S_l_i, S_left = find_le(S, x)
  S_r_i, S_right = find_ge(S, x)
  T_l_i, T_left = find_le(T, x)
  T_r_i, T_right = find_ge(T, x)

  # print(S_left, S_right, T_left, T_right)

  dist_S_left = x - S_left
  dist_S_right = S_right - x
  dist_T_left = x - T_left
  dist_T_right = T_right - x

  # print(dist_S_left, dist_S_right, dist_T_left, dist_T_right)


  S_ans = 0
  if dist_S_left <= dist_S_right:
    S_ans = S[S_l_i]
  else:
    S_ans = S[S_r_i]

  T_ans = 0
  if dist_T_left <= dist_T_right:
    T_ans = T[T_l_i]
  else:
    T_ans = T[T_r_i]

  diff_S = abs(S_ans - x)
  diff_T = abs(T_ans - x)

  ans = 0
  if diff_S < diff_T:
    ans = diff_S
  else:
    ans = diff_T

  ans += abs(S_ans - T_ans)

  print(ans)

Submission status: Wrong Answer

Solution code:
def nibuntansaku(l,x):
    n = len(l)
    def hantei(med):
        if l[med] > x:
            return True
        else:
            return False
    left = -1
    right =n
    while right-left >1:
        med = (left+right)//2
        if right == med or left == med:
            break
        if hantei(med):
            right = med
        else:
            left = med
    ret =[0,0]
    if left == -1:
        ret[0] = 10**11
        ret[1] = l[right] -x
    elif right == n:
        ret[0] = x -l[left]
        ret[1] = 10**11
    else:
        ret[0] = x - l[left]
        ret[1] = l[right] -x
    return ret


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

for _ in range(Q):
    x = int(input())
    slef,sri = nibuntansaku(S,x)
    tlef,tri = nibuntansaku(T,x)
    lmax,lmin = max(slef,tlef),min(slef,tlef)
    rmax,rmin = max(sri,tri),min(sri,tri)
    ans = min(lmax,rmax,2*lmin+rmin,2*rmin+lmin)
    print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

abq = list(map(int, input().split()))
s = sorted([int(input()) for i in range(abq[0])])
t = sorted([int(input()) for i in range(abq[1])])
x = [int(input()) for i in range(abq[2])]

for i in range(abq[2]):
    
    s_xindex = bisect_left(s, x[i])
    if s_xindex < len(s):
        t_sindex = bisect_left(t, s[s_xindex])
        if t_sindex<len(t):
            idea1 = 0
            if (x[i] - s[s_xindex])*(x[i] - t[t_sindex]) > 0:
                idea1 = max(abs(x[i] - s[s_xindex]), abs(x[i] - t[t_sindex]))
            else:
                idea1 = min(abs(x[i] - s[s_xindex]), abs(x[i] - t[t_sindex]))*2 + max(abs(x[i] - s[s_xindex]), abs(x[i] - t[t_sindex]))
        else:
            idea1 = 10**10
    else:
        idea1 = 10**10
            
    t_sindex2 = bisect_left(t, s[s_xindex -1])
    if s_xindex -1 >= 0 and t_sindex2 < len(t):
        idea2 = 0
        if (x[i] - s[s_xindex - 1])*(x[i] - t[t_sindex2]) > 0:
            idea2 = max(abs(x[i] - s[s_xindex - 1]), abs(x[i] - t[t_sindex2]))
        else:
            idea2 = min(abs(x[i] - s[s_xindex - 1]), abs(x[i] - t[t_sindex2]))*2 + max(abs(x[i] - s[s_xindex - 1]), abs(x[i] - t[t_sindex2]))
    else:
        idea2 = 10**10

    t_xindex = bisect_left(t, x[i])
    if t_xindex < len(t):
        s_tindex = bisect_left(s, t[t_xindex])
        if s_tindex < len(s):
            idea3 = 0
            if (x[i] - t[t_xindex])*(x[i] - s[s_tindex]) > 0:
                idea3 = max(abs(x[i] - t[t_xindex]), abs(x[i] - s[s_tindex]))
            else:
                idea3 = min(abs(x[i] - t[t_xindex]), abs(x[i] - s[s_tindex]))*2 + max(abs(x[i] - t[t_xindex]), abs(x[i] - s[s_tindex]))
        else:
            idea3 = 10**10
    else:
        idea3 = 10**10
            
    s_tindex2 = bisect_left(s, t[t_xindex - 1])
    if t_xindex - 1 >= 0 and s_tindex2 < len(s):
        idea4 = 0
        if (x[i] - t[t_xindex - 1])*(x[i] - s[s_tindex2]) > 0:
            idea4 = max(abs(x[i] - t[t_xindex -1]), abs(x[i] - s[s_tindex2]))
        else:
            idea4 = min(abs(x[i] - t[t_xindex -1]), abs(x[i] - s[s_tindex2]))*2 + max(abs(x[i] - t[t_xindex -1]), abs(x[i] - s[s_tindex2]))
    else:
        idea4 = 10**10
            
    print(min(idea1,idea2,idea3,idea4))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def bin_search(S, x):
    L = 0
    R = len(S) - 1

    while 1:
        M = (L + R)/2
        if (R - L) <= 1:
            if abs(S[R] - x) < abs(S[L] - x):
                return (R, -1)
            elif abs(S[R] - x) > abs(S[L] - x):
                return (L, -1)
            else:
                if L != R:
                    return (L, R)
                else:
                    return (L, -1)
        if x == S[L]:
            return (L, -1)
        if x == S[R]:
            return (R, -1)
        if S[M] < x:
            L = M
        else:
            R = M



def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [A, B, Q] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2])]

    jinja = []
    for i in range(A):
        input_raw = read_func().strip().split()
        jinja.append(long(input_raw[0]))
    tera = []
    for i in range(B):
        input_raw = read_func().strip().split()
        tera.append(long(input_raw[0]))

    for i in range(Q):
        input_raw = read_func().strip().split()
        x = long(input_raw[0])
        dist1 = [2**63-1 for i in range(2)]
        z = bin_search(jinja, x)
        for j in range(len(z)):
            if z[j] != -1:
                dist1[j] = abs(jinja[z[j]] - x)
                y = bin_search(tera, jinja[z[j]])
                dist1[j]  += abs(tera[y[0]] - jinja[z[j]])

        dist2 = [2**63-1 for i in range(2)]
        z = bin_search(tera, x)
        for j in range(len(z)):
            if z[j] != -1:
                dist2[j] = abs(tera[z[j]] - x)
                y = bin_search(jinja, tera[z[j]])
                dist2[j]  += abs(jinja[y[0]] - tera[z[j]])
        print min(min(dist1), min(dist2))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# input
a, b, q = map(int, input().split())
S = [int(input()) for _ in range(a)]
T = [int(input()) for _ in range(b)]

S_rinsetsu = [0] * a
tmp = 0

for i in range(a):
    for j in range(tmp, b):
        if S[i] <= T[j]:
            S_rinsetsu[i] = j
            tmp = j
            break

for k in range(q):
    x = int(input())
    for i in range(a):
        if x <= S[i]:
            if i == 0:
                ans = max(S[0] - x, T[0] - x)
                print(ans)
                break
            elif x <= T[0]:
                ans = min(max(S[i] - x, T[0] - x), T[0] - x + T[0] - S[i - 1], x - S[i - 1] + T[0] - S[i - 1])
                print(ans)
                break
            else:
                if x > T[S_rinsetsu[i] - 1]:
                    ans1 = min(S[i] - x, x - T[S_rinsetsu[i] - 1]) + S[i] - T[S_rinsetsu[i] - 1]
                else:
                    ans1 = S[i] - x
                ans2 = T[S_rinsetsu[i]] - x
                ans3 = x - T[S_rinsetsu[i - 1] - 1]
                if x < T[S_rinsetsu[i - 1]]:
                    ans4 = min(T[S_rinsetsu[i - 1]] - x, x - S[i - 1]) + T[S_rinsetsu[i - 1]] - S[i - 1]
                else:
                    ans4 = x - S[i - 1]
                ans = min(ans1, ans2, ans3, ans4)
                print(ans)
                break
        if i == a - 1:
            if T[S_rinsetsu[i]] > x:
                ans = min(T[S_rinsetsu[i]] - x + T[S_rinsetsu[i]] - S[i], x - S[i] + T[S_rinsetsu[i]] - S[i], x - T[S_rinsetsu[i] - 1])
                print(ans)
            else:
                ans = x - S[i]
                print(ans)

Submission status: Wrong Answer

Solution code:
#A,B,Q=[int(x) for x in input().split()]
LA=[]#神社
LB=[]#寺
"""
for i in range(A):
    LA.append(int(input()))
for i in range(B):
    LB.append(int(input()))
"""
import random
A=int(random.random()*100000)
B=int(random.random()*100000)
Q=int(random.random()*100000)
for i in range(A):
    LA.append(int(random.random()*100000))
for i in range(B):
    LB.append(int(random.random()*100000))
mx=200000000001

for i in range(Q):
    x=int(random.random()*1000000)#int(input())
    if LA[-1]<x:
        sa=mx
        sb=x-LA[-1]
    else:
        if x<LA[int(len(LA)/2)]:        
            for js,s in enumerate(LA):
                if x<s:
                    if js==0:
                        sb=mx
                    else:
                        sb=x-LA[js-1]
                    sa=s-x
                    break
        else:
            LA.reverse()
            for js,s in enumerate(LA):
                if s<x:
                    sa=x-LA[js-1]
                    sb=s-x
                    break
    if LB[-1]<x:
        ta=mx
        tb=x-LB[-1]
    else:
        if x<LB[int(len(LB)/2)]:
            for jt,t in enumerate(LB):
                if x<t:
                    if jt==0:
                        tb=mx
                    else:
                        tb=x-LB[jt-1]
                    ta=t-x
                    break
        else:
            LB.reverse()
            for jt,t in enumerate(LB):
                if t<x:
                    sa=x-LA[jt-1]
                    tb=t-x
                    break
    
    print(min(max(ta,sa),max(tb,sb),tb+sa+min(tb,sa),ta+sb+min(ta,sb)))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ST=[]
for i in range(a):
    k=int(input())
    ST.append([k,0,0])
for i in range(b):
    k=int(input())
    ST.append([k,1,0])

ST.sort()

le0=-1
le1=-1
ri0=1
ri1=1

for i in range(a+b):
    if ST[i][1]==0 :
        pa=1
        
        moyori=np.inf

        if le1>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le1][0]))
        if ri1<=i :
            ri1=i+1
            while ri1<=a+b-1 and ST[ri1][1]==0 :
                ri1+=1
        if ri1<=a+b-1:
            moyori=min(moyori,abs(ST[i][0]-ST[ri1][0]))
        ST[i][2]=moyori
        le0=i
    else:
        pa=0
        
        moyori=np.inf

        if le0>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le0][0]))
        if ri0<=i :
            ri0=i+1
            while ri0<=a+b-1 and ST[ri0][1]==1 :
                ri0+=1
        if ri0<=a+b-1:
            moyori=min(moyori,abs(ST[i][0]-ST[ri0][0]))
        ST[i][2]=moyori
        le1=i



#print(ST)

for i in range(q):
    k=int(input())
    ST.append([k,2,0,i])

ST.sort()

ANS=[]
le0=-1
le1=-1
ri0=1
ri1=1
for i in range(a+b+q):
    if ST[i][1]==0 :
        le0=i
    elif ST[i][1]==1 :
        le1=i
    elif ST[i][1]==2 :
        moyori=np.inf
    
            
        if le0>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le0][0])+ST[le0][2])
            #print("L"+str(le[typ]))
            #print(moyori)
        if ri0<=i :
            ri0=i+1
            while ri0<=a+b+q-1 and (ST[ri0][1]==1 or ST[ri0][1]==2):
                ri0+=1
        if ri0<=a+b+q-1 :
            moyori=min(moyori,abs(ST[i][0]-ST[ri0][0])+ST[ri0][2])
            #print("R"+str(ri[typ]))
            #print(moyori)

        if le1>=0:
            moyori=min(moyori,abs(ST[i][0]-ST[le1][0])+ST[le1][2])
            #print("L"+str(le[typ]))
            #print(moyori)
        if ri1<=i :
            ri1=i+1
            while ri1<=a+b+q-1 and (ST[ri1][1]==0 or ST[ri1][1]==2):
                ri1+=1
        if ri1<=a+b+q-1 :
            moyori=min(moyori,abs(ST[i][0]-ST[ri1][0])+ST[ri1][2])
            #print("R"+str(ri[typ]))
            #print(moyori)



        ST[i][2]=moyori
        ANS.append([ST[i][3],moyori])

ANS.sort()

#print(ST)

for i in range(q):
    print(ANS[i][1])

Submission status: Wrong Answer

Solution code:
a,b,q = map(int,input().split())
s,t,x = [0]*a,[0]*b,[0]*q
metera,meterb,meters,sposition,tposition = [0] * q,[0] * q,[0] * q,[0] * q,[0] * q
for i in range(a):
  s[i] = int(input())
i = 0
for i in range(b):
  t[i] = int(input())
i = 0
for i in range(q):
  x[i] = int(input())
  metera[i] = abs(x[i]-s[0])
  j = 0
  for j in range(a):
    if abs(x[i]-s[j]) <= metera[i]:
      metera[i] = abs(x[i]-s[j])
      sposition[i] = s[j]
  j = 0
  meterb[i] = abs(x[i]-t[0])
  for j in range(b-1):
    if abs(x[i]-t[j]) <= meterb[i]:
        meterb[i] = abs(x[i]-t[j])
        tposition[i] = t[j]
  meters[i] = metera[i] + meterb[i]
  print(meters[i])
  if (x[i] > sposition[i]) and (x[i] > tposition[i]):
    if(metera[i] < metera[i]):
      meters[i] -= metera[i]
    else:
      meters[i] -= meterb[i]
  else:
    if (x[i] < sposition[i]) and (x[i] < tposition[i]):
      if(metera[i] < metera[i]):
        meters[i] -= metera[i]
      else:
        meters[i] -= meterb[i]
    else:
      if abs(sposition[i]-x[i]) < abs(tposition[i]-x[i]):
        meters[i] += abs(sposition[i]-x[i])
      else:
        meters[i] += abs(tposition[i]-x[i])
  print(meters[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 24 21:50:59 2019

@author: rubug
"""

import numpy as np
A, B, Q = map(int, input().split())
s = np.zeros(A)
t = np.zeros(B)
x = np.zeros(Q)
for i in range(A):
    s[i] = input()
for j in range(B):
    t[j] = input()
for k in range(Q):
    x[k] = input()
    D_E_shrine = 1.5*10**10
    D_W_shrine = 1.5*10**10
    for i in range(A):
        if (x[k] <= s[i]): ##神社が現在地以東
            if D_E_shrine >= (s[i] - x[k]):
                D_E_shrine = s[i] - x[k]
        elif (s[i] < x[k]):
            if D_W_shrine >= (x[k] - s[i]):
                D_W_shrine = x[k] - s[i]
    D_E_temple = 1.5*10**10
    D_W_temple = 1.5*10**10
    for j in range(B):
        if (x[k] <= t[j]): ##寺が現在地以東
            if D_E_temple >= (t[j] - x[k]):
                D_E_temple = t[j] - x[k]
        elif (t[j] < x[k]):
            if D_W_temple >= (x[k] - t[j]):
                D_W_temple = x[k] - t[j]
    D_E_E = max(D_E_shrine,D_E_temple)
    D_W_W = max(D_W_shrine,D_W_temple)
    D_E_W = min(D_E_shrine,D_W_temple) * 2 + max(D_E_shrine,D_W_temple)
    D_W_E = min(D_W_shrine,D_E_temple) * 2 + max(D_W_shrine,D_E_temple)
    print(min(D_E_E, D_W_W, D_E_W, D_W_E))

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

for i in range(Q):
    min=100000000000
    for p in range(A):
        sx=abs(x[i]-s[p])
        if min>sx:
            min=sx
            min_i=p
    min=100000000000
    for q in range(B):
        tx=abs(x[i]-t[q])
        if min>tx:
            min=tx
            min_i=q
    if (x[i]-s[p])*(x[i]-t[q])>=0:
        R=max(abs(x[i]-s[p]),abs(x[i]-t[q]))
    else:
        R=abs(x[i]-s[p])+abs(x[i]-t[q])
    print (R)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
a, b, q = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
q = [int(input()) for _ in range(q)]
for qi in q:
    qans = 10**18
    ai = bisect_left(s, qi)
    bi = bisect_left(t, qi)
    ac = []
    bc = []
    for i in range(1):
        if (0 <= ai + i < a):
            ac.append(ai + i);
        if (0 <= ai - i < a):
            ac.append(ai - i);
    for i in range(1):
        if (0 <= bi + i < b):
            bc.append(bi + i);
        if (0 <= bi - i < b):
            bc.append(bi - i);
    for i in ac:
        for j in bc:
            aa = s[i];
            bb = t[j];
            if (max(aa, bb) <= qi):
                qans = min(qans, qi - min(aa, bb))
            elif (qi <= min(aa, bb)):
                qans = min(qans, max(aa, bb) - qi)
            else:
                qans = min(qans, min(abs(aa - qi), abs(bb - qi)) * 2 + max(abs(aa - qi), abs(bb - qi)))
    print(qans)

Submission status: Wrong Answer

Solution code:
a, b, q = [int(i) for i in input().split() ]
s = list([])
t = list([])
z = list([])

for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
for i in range(q):
    z.append([i,int(input())])

sd=list([])
td=list([])
if len(s) == 1:
    sd.append([s[0],0, 10**10])
else:
    for j in range(len(s)):
        if j == 0:
            a = 0
            b = s[j] + int((s[j+1] - s[j])/2)
        elif j == len(s) - 1:
            a = sd[-1][2] + 1
            #a = s[j] - int((s[j] - s[j-1])/2)
            b = 10**10
        else:
            a = sd[-1][2] + 1
            b = s[j] + int((s[j+1] - s[j])/2)
        sd.append([s[j],a,b])
if len(t) == 1:
    td.append([t[0],0, 10**10])
else:
    for j in range(len(t)):
        if j == 0:
            a = 0
            b = t[j] + int((t[j+1] - t[j])/2)
        elif j == len(t) - 1:
            a = td[-1][2] + 1
            b = 10**10
        else:
            a = td[-1][2] + 1
            b = t[j] + int((t[j+1] - t[j])/2)
        td.append([t[j],a,b])

z.sort(key= lambda x:x[1])
s_idx1=0
s_idx2=0
t_idx1=0
t_idx2=0
ans=list([])
for zz in z:
    x = zz[1]
    ans_s = 0
    ans_t = 0
    for idx_a in range(s_idx1,len(sd)):
        ss = sd[idx_a]
        #if ((ss[1] > x) or (ss[2] < x)):
        if (ss[2] < x):
            continue
        else:
            s_idx1 = idx_a
            ans_s += abs(x - ss[0])
            y = ss[0]
            for idx_b in range(t_idx1,len(td)):
                tt = td[idx_b]
                if (tt[2] < y):
                    continue
                else:
                    t_idx1 = idx_b
                    ans_s += abs(y - tt[0])
                    break
            break

    for idx_a in range(t_idx2,len(td)):
        tt = td[idx_a]
        if (tt[2] < x):
            continue
        else:
            t_idx2 = idx_a
            ans_t += abs(x - tt[0])
            y = tt[0]
            for idx_b in range(s_idx2,len(sd)):
                ss = sd[idx_b]
                if (ss[2] < y):
                    continue
                else:
                    s_idx2 = idx_b
                    ans_t += abs(y - ss[0])
                    break
            break
    ans.append([zz[0], min(ans_s, ans_t)])
ans.sort(key=lambda x: x[0])
for a in ans:
    print(a[1])

Submission status: Wrong Answer

Solution code:
import numpy as np
A, B, Q = map(int, input().split())

s_list = [int(input()) for a in range(A)]
t_list = [int(input()) for b in range(B)]
x_list = [int(input()) for q in range(Q)]

for x in x_list:
    near_s = np.abs(np.array(s_list) - x).argmin()
    near_t = np.abs(np.array(t_list) - x).argmin()

    if s_list[near_s] < t_list[near_t]:
        next_x = s_list[near_s]
        near_t2 = np.abs(np.array(t_list) - next_x).argmin()
        print(abs(x - s_list[near_s] + abs(next_x - t_list[near_t2])))

    elif s_list[near_s] > t_list[near_t]:
        next_x = t_list[near_t]
        near_s2 = np.abs(np.array(s_list) - next_x).argmin()
        print(abs(x - t_list[near_t] + abs(next_x - s_list[near_s2])))
    else:
        next_x1 = s_list[near_s]
        next_x2 = t_list[near_t]
        near_t2 = np.abs(np.array(t_list) - next_x).argmin()
        near_s2 = np.abs(np.array(s_list) - next_x).argmin()
        if abs(x - s_list[near_s] + abs(next_x - t_list[near_t2])) < abs(x - t_list[near_t] + abs(next_x - s_list[near_s2])):
            print(abs(x - s_list[near_s] + abs(next_x - t_list[near_t2])))
        else:
            print(abs(x - t_list[near_t] + abs(next_x - s_list[near_s2])))

Submission status: Wrong Answer

Solution code:
import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])+min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[a-1]),abs(cc - bb[0])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1])
		print(aaaa)

Submission status: Wrong Answer

Solution code:
# 2019/02/24
# AtCoder Beginner Contest 119 - D

# Input
a, b, q = map(int,input().split())
s = list()
t = list()
x = list()

# Input(Loop)
# Shrine
for idx_s in range(a):
    s.append(int(input()))

# Temple    
for idx_t in range(b):
    t.append(int(input()))

# Question
for idx_x in range(q):
    wx = int(input())
    x.append(wx)

    s_min = abs(s[0] - wx)
    s_min_idx = 0
    for idx_s in range(1, a):
        if s_min > abs(s[idx_s] - wx):
            s_min = abs(s[idx_s] - wx)
            s_min_idx = idx_s
    
    t_min = abs(t[0] - wx)
    t_min_idx = 0
    for idx_t in range(1, b):
        if t_min > abs(t[idx_t] - wx):
            t_min = abs(t[idx_t] - wx)
            t_min_idx = idx_t

    smt_min = abs(t[0] - s_min)
    smt_min_idx = 0

    for idx_smt in range(1, b):
        if smt_min > abs(t[idx_smt] - s_min):
            smt_min = abs(t[idx_smt] - s_min)
            smt_min_idx = idx_smt
    
    tms_min = abs(s[0] - t_min)
    tms_min_idx = 0
    for idx_tms in range(1, a):
        if tms_min > abs(s[idx_tms] - t_min):
            tms_min = abs(s[idx_tms] - t_min)
            tms_min_idx = idx_tms

    ans_min = s_min * 2 + t_min
    if ans_min > t_min * 2 + s_min:
        ans_min = t_min * 2 + s_min

    if ans_min > s_min + smt_min:
        ans_min = s_min + smt_min

    if ans_min > t_min + tms_min:
        ans_min = t_min + tms_min

    # Output
    print(ans_min)

#    print("s_min=", s_min, "s_min_idx=", s_min_idx, "t_min=", t_min, "t_min_idx=", t_min_idx)

#print("s=", s, "t=", t, "q=", x)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
a, b ,q = map(int, input().split())
a_list = [int(input()) for i in range(a)]
b_list = [int(input()) for i in range(b)]
for n in range(q):
    q_pt = int(input())
    l = max(a_list) + max(b_list)
    for a_pt in a_list:
        for b_pt in b_list:
            l = min(l, abs(q_pt-a_pt)+abs(a_pt-b_pt), abs(q_pt-b_pt)+abs(b_pt-a_pt))
    print(l)

Submission status: Wrong Answer

Solution code:
import sys
A,B,Q = [int(n) for n in input().split()]
# N = int(input())
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 = str(input())
# T = str(input())
def binary_search(arry, target):
    result = None
    left = 0
    right = len(arry) - 1

    while left <= right:
        mid = (left + right) // 2
        if arry[mid] == target:
            result = mid
            break
        elif arry[mid] > target:
            right = mid - 1
        else:
            left = mid + 1

    if result == None:
        result = mid
        if(mid+1 < len(arry)):
            result = mid+1 if (abs(arry[mid+1] - target) < abs(arry[mid] - target)) else result
        if(mid-1 >= 0):
            result = mid-1 if (abs(arry[result] - target) > abs(arry[mid-1] - target)) else result
        return result
    else:
        return result+1

for q in x:
    #てらから
    ans0 = 0
    tera = s[binary_search(s,q)]
    ans0 += abs(q - tera)
    zin = t[binary_search(t,tera)]
    ans0 += abs(tera - zin)
    ans1 = 0
    zin = t[binary_search(t,q)]
    ans1 += abs(q - zin)
    tera = s[binary_search(s,zin)]
    ans1 += abs(zin - tera)
    print(min(ans0,ans1))

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A

#B

#C
"""
n,a,b,c = LI()
l = IR(n)
f = [True for i in range(3)]
ans = 0
for i in range(n):
    for d in range(3):
        if abs(l[i]-[a,b,c][d]) <= 10 and f[d]:
            ans += abs(l[i]-[a,b,c][d])
            f[d] = False
            l[i] = -float("inf")
k = False
for i in f:
    if i:
        k = True
        break
if not k:
    print(ans)
    quit()
"""
#D
a,b,q = LI()
s = IR(a)
t = IR(b)
s.insert(0,-float("inf"))
t.insert(0,-float("inf"))
s.append(float("inf"))
t.append(float("inf"))
for i in range(q):
    x = I()
    c = bisect.bisect_left(s,x)
    ans = float("inf")
    k = abs(x-s[c-1])
    y = s[c-1]
    d = bisect.bisect_left(t,y)
    if abs(y-t[d-1]) < abs(y-t[d]):
        k += abs(y-t[d-1])
    else:
        k += abs(y-t[d])
    ans = min(k,ans)
    k = abs(x-s[c])
    y = s[c]
    d = bisect.bisect_left(t,y)
    if abs(y-t[d-1]) < abs(y-t[d]):
        k += abs(y-t[d-1])
    else:
        k += abs(y-t[d])
    ans = min(k,ans)
    c = bisect.bisect_left(t,x)
    k = abs(x-t[c-1])
    y = t[c-1]
    d = bisect.bisect_left(s,y)
    if abs(y-s[d-1]) < abs(y-s[d]):
        k += abs(y-s[d-1])
    else:
        k += abs(y-s[d])
    ans = min(k,ans)
    k = abs(x-t[c])
    y = t[c]
    d = bisect.bisect_left(s,y)
    if abs(y-s[d-1]) < abs(y-s[d]):
        k += abs(y-s[d-1])
    else:
        k += abs(y-s[d])
    ans = min(k,ans)
    print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T