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:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

a, b, c = map(int, input().split())
jin = []
for i in range(a):
    v = int(input())
    jin.append(v)
tera = []
for i in range(b):
    v = int(input())
    tera.append(v)
q = []
for i in range(c):
    v = int(input())
    q.append(v)
jin.sort()
tera.sort()

i = q[0]
flg = True
ans = 10 ** 99
def solve(current, total, flg, count):
    global ans
    if count == 2:
        ans = min(total, ans)
        return
    mn = 10 ** 99
    ncurrent = 0
    if flg:
        ins = bisect.bisect_left(jin, current)
        if ins >= a:
            ins -= 1
        mn = abs(current - jin[ins])
        ncurrent = jin[ins]
        if ins > 0:
            if abs(current - jin[ins - 1]) < mn:
                mn = abs(current - jin[ins - 1])
                ncurrent = jin[ins - 1]
        if ins < a - 1:
            if abs(current - jin[ins + 1]) < mn:
                mn = abs(current - jin[ins + 1])
                ncurrent = jin[ins + 1]
        count += 1
        solve(ncurrent, mn + total, False, count)
    else:
        ins = bisect.bisect_left(tera, current)
        if ins >= b:
            ins -= 1
        mn = abs(current - tera[ins])
        ncurrent = tera[ins]
        if ins > 0:
            if abs(current - tera[ins - 1]) < mn:
                mn = abs(current - tera[ins - 1])
                ncurrent = tera[ins - 1]
        if ins < b - 1:
            if abs(current - tera[ins + 1]) < mn:
                mn = abs(current - tera[ins + 1])
                ncurrent = tera[ins + 1]
        count += 1
        solve(ncurrent, mn + total, True, count)
for i in range(c):
    ans2 = 10 ** 99
    for j in range(2):
        ans = 10 ** 99
        solve(q[i], 0, j, 0)
        ans2 = min(ans2, ans)
    print(ans2)

Submission status: Wrong Answer

Solution code:
import numpy as np
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()))
s = np.array(s)
t = np.array(t)
for i in range(q):
  x = int(input())
  toS = np.abs(s - x)
  toT = np.abs(t - x)
  toSarg = np.argmin(toS)
  toTarg = np.argmin(toT)
  toStoT = np.abs(t - s[toSarg])
  toTtoS = np.abs(s - t[toTarg])
  toStoTmin = np.min(toStoT)
  toTtoSmin = np.min(toTtoS)
  print(min(abs(s[toSarg]) + toStoTmin, abs(t[toTarg]) + toTtoSmin))

Submission status: Wrong Answer

Solution code:
import bisect
import sys
 
class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self # self.key == keyの場合
        
    def search_lower(self,key,key_lower):
        if self.key == None:
            return key_lower
        if self.key > key:
            if self.left == None:
                return key_lower
            else:
                return self.left.search_lower(key,key_lower)
        if self.key < key:
            key_lower = self.key
            if self.right == None:
                return key_lower
            else:
                return self.right.search_lower(key,key_lower)
        #self.key == keyの場合
        if self.left == None:
            return key_lower
        else:
            if key_lower == None:
                return self.left.end_higher(self.left.key)
            else:
                return max(key_lower,self.left.end_higher(self.left.key))
    
    def search_higher(self,key,key_higher):
        if self.key == None:
            return key_higher
        if self.key > key:
            key_higher = self.key
            if self.left == None:
                return key_higher
            else:
                return self.left.search_higher(key,key_higher)
        if self.key < key:
            if self.right == None:
                return key_higher
            else:
                return self.right.search_higher(key,key_higher)
        #self.key == keyの場合
        if self.right == None:
            return key_higher
        else:
            if key_higher == None:
                return self.right.end_lower(self.right.key)
            else:
                return min(key_higher,self.right.end_lower(self.right.key))
        
    
    def end_lower(self,end_lower_key):
        if self.left == None:
            return end_lower_key
        else:
            end_lower_key = self.left.key
            return self.left.end_lower(end_lower_key)
    def end_higher(self,end_higher_key):
        if self.right == None:
            return end_higher_key
        else:
            end_higher_key = self.right.key
            return self.right.end_higher(end_higher_key)
    
            
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self # selfはそのノード
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): # 親ノードpの下にある自分(self)をvに置き換える。
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): # rootでのみ呼ばれる挿入
        if self.key == None: # rootを含むrotationはしないことにする。
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: # key == self.key:
            pass # do not overwrite
    def insertx(self,p,key): # replaceを呼ぶために一つ上の親を持っているinsert
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): # 左の木が生長しなければ、
                    return False # 成長しない
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True # 成長した
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False # rotationを行うと成長しない
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False # self.key == keyの時は何もしないので成長もしない 
 
    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key
        
input = sys.stdin.readline
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)]
lens, lent = len(s), len(t)
treeA, treeB = Avltree(), Avltree()
for i in s: treeA.insert(i) # 木を作る
for i in t: treeB.insert(i)
 
print(1)

Submission status: Wrong Answer

Solution code:
A,B,Q=(int(i) for i in input().split())
slist=[int(input()) for i in range(A)]
tlist=[int(input()) for i in range(B)]
qlist=[int(input()) for i in range(Q)]
for j in range(Q):
    sl=[qlist[j]-slist[i] for i in range(A) if qlist[j]-slist[i]>=0]
    sr=[abs(qlist[j]-slist[i]) for i in range(A) if qlist[j]-slist[i]<0]
    tl=[qlist[j]-tlist[i] for i in range(B) if qlist[j]-tlist[i]>=0]
    tr=[abs(qlist[j]-tlist[i]) for i in range(B) if qlist[j]-tlist[i]<0]

    sl.sort();sr.sort();tl.sort();tr.sort()

    if sl==[]:
        if tl==[]:
            L=[sr[0]+tr[0]]
        elif tr==[]:
            L=[sr[0]*2+tl[0],sr[0]+tl[0]*2]
        else:
            L=[sr[0]+tr[0],sr[0]*2+tl[0],sr[0]+tl[0]*2]
    elif sr==[]:
        if tl==[]:
            L=[sl[0]*2+tr[0],sl[0]+tr[0]*2]
        elif tr==[]:
            L=[sl[0]+tl[0]]
        else:
            L=[sl[0]+tl[0],sl[0]*2+tr[0],sl[0]+tr[0]*2]
    else:
        if tl==[]:
            L=[sl[0]*2+tr[0],sl[0]+tr[0]*2,sr[0]+tr[0]]
        elif tr==[]:
            L=[sr[0]*2+tl[0],sr[0]+tl[0]*2,sl[0]+tl[0]]
        else:
            L=[sl[0]*2+tr[0],sl[0]+tr[0]*2,sr[0]+tr[0],sr[0]*2+tl[0],sr[0]+tl[0]*2,sl[0]+tl[0]]

    print(min(L))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import bisect
A,B,Q = map(int,input().split())
S = []
T = []
X = []
for a in range(A):
    S.append(int(input()))
for b in range(B):
    T.append(int(input()))
for q in range(Q):
    X.append(int(input()))

#print (S,T,X)

def find_nearest(array, point):
    #print ("p",point)
    abs_diff = 10 ** 11
    left_idx = bisect.bisect_left(array,point)
    right_idx = bisect.bisect_right(array,point)
    if right_idx == len(array):
        right_idx -= 1
    if left_idx == len(array):
        left_idx -= 1
    target = [left_idx,right_idx]
    for t in target:
        
        diff = array[t] - point
        temp = abs(diff)
        if abs_diff > temp:
            min_diff = diff
            abs_diff = temp
            
        
    return min_diff

def calc(first, second,point):
    score = 0
    md = find_nearest(first,point)
    #print("m",md)
    score += abs(md)
    point += md
    md = find_nearest(second,point)
    score += abs(md)
    #print ("m",md)
    #print (score)
    return score

for x in X:
    print (min(calc(S,T,x),calc(T,S,x)))
    #break

Submission status: Wrong Answer

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

# 配列の中身は(x座標, 次のポイントへの距離)
shrines = []
temples = []

for _ in range(A):
    s = int(input())
    shrines.append((s, 10**10))

# jinjas のインデックス
# temples の各点との距離を求めるのに使う
ji = 0
for _ in range(B):
    tp, tc = int(input()), 10**10
    sp, sc = shrines[ji]

    while abs(tp - sp) < tc:
        tc = abs(tp - sp)

        if abs(tp - sp) < sc:
            shrines[ji] = (sp, abs(tp - sp))

        if ji + 1 < A:
            ji += 1
            sp, sc = shrines[ji]
        else:
            break

    temples.append((tp, tc))

road = sorted(temples + shrines, key=lambda x: x[0])


def search(x):
    global A, B

    l, r = 0, A + B
    while r - l > 1:
        m = (l + r) // 2
        mp, mc = road[m]

        if mp < x:
            l = m
        else:
            r = m

    p1, c1 = road[l]
    if l == A + B - 1 or A + B == 1:
        return abs(x - p1) + c1

    p2, c2 = road[l+1]
    return min(abs(x-p1) + c1, abs(x-p2) + c2)


for _ in range(Q):
    q = int(input())

    y = search(q)
    print(y)

Submission status: Wrong Answer

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

# 配列の中身は(x座標, 次のポイントへの距離)
shrines = []
temples = []

for _ in range(A):
    s = int(input())
    shrines.append((s, 10**10))

# jinjas のインデックス
# temples の各点との距離を求めるのに使う
ji = 0
for _ in range(B):
    tp, tc = int(input()), 10**10
    sp, sc = shrines[ji]

    while abs(tp - sp) < tc:
        tc = abs(tp - sp)

        if abs(tp - sp) < sc:
            shrines[ji] = (sp, abs(tp - sp))

        if ji + 1 < A:
            ji += 1
            sp, sc = shrines[ji]
        else:
            break

    temples.append((tp, tc))

road = sorted(temples + shrines, key=lambda x: x[0])


def search(x):
    global A, B

    l, r = 0, A + B
    while r - l > 1:
        m = (l + r) // 2
        mp, mc = road[m]

        if mp < x:
            l = m
        else:
            r = m

    p1, c1 = road[l]
    if l == A + B - 1 or A + B == 1:
        return abs(x - p1) + c1

    p2, c2 = road[l+1]
    return min(abs(x-p1) + c1, abs(x-p2) + c2)


for _ in range(Q):
    q = int(input())

    y = search(q)
    print(q, y)

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)]
print(s, t, x)
for i in x:
    si = bisect.bisect_left(s,i)
    ti = bisect.bisect_left(t,i)
    sl = 100000000000 if si == 0 else i - s[si-1]
    sr = 100000000000 if si == a else s[si] - i
    tl = 100000000000 if ti == 0 else i - t[ti-1]
    tr = 100000000000 if ti == b else t[ti] - i
    print(min(max(sl,tl), max(sr,tr), max(sl,tr) + min(sl, tr)*2, max(sr,tl) + min(sr,tl)*2))

Submission status: Wrong Answer

Solution code:
import numpy as np

A, B, Q = map(int, input().split(' '))
ss, tt, xx = [], [], []
for _ in range(A):
    ss.append(int(input()))
for _ in range(B):
    tt.append(int(input()))
for _ in range(Q):
    xx.append(int(input()))

def solve(x):
    min_xs = ss[np.argmin([abs(s-x) for s in ss])]
    min_ts = tt[np.argmin([abs(t-x) for t in tt])]
    if (min_xs - x) * (min_ts - x) > 0:
        return max(min_ts, min_xs)
    else:
        return min_xs + min_ts + min(min_xs, min_ts)


for i in range(Q):
    print(solve(xx[i]))

Submission status: Wrong Answer

Solution code:

import numpy as np

def searchDL(s, x):
    i = np.searchsorted(s, x, side='left')
    if i == 0:
        dist = x - s[0]
        loc = s[0]
        return dist, loc, dist, loc
    elif i == len(s):
        dist = s[i-1] - x
        loc = s[i-1]
        return dist, loc, dist, loc
    else:
        dist1 = abs(x - s[i-1])
        loc1 = s[i-1]
        dist2 = abs(s[i] - x)
        loc2 = s[i]
        return dist1, loc1, dist2, loc2

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

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

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

for i in range(Q):
    x = int(input())
    d1_1a, l1_1a, d1_1b, l1_1b = searchDL(s, x)
    d1_2a, l1_2a, d1_2b, l1_2b = searchDL(t, l1_1a)
    d1_3a, l1_3a, d1_3b, l1_3b = searchDL(t, l1_1b)
    
    d2_1a, l2_1a, d2_1b, l2_1b = searchDL(t, x)
    d2_2a, l2_2a, d2_2b, l2_2b = searchDL(s, l2_1a)
    d2_3a, l2_3a, d2_3b, l2_3b = searchDL(s, l2_1b)
    
    print(min(d1_1a + d1_2a, d1_1a + d1_3a, d2_1a + d2_2a, d2_1a + d2_3a))

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

for i in range(q):
    id = bisect.bisect_left(S, X[i])
    if id == 0:
        s = S[id]
    elif id == len(S):
        s = S[id - 1]
    else:
        if abs(X[i] - S[id]) < abs(X[i] - S[id - 1]):
            s = S[id]
        else:
            s = S[id - 1]
    
    id2 = bisect.bisect_left(T, X[i])
    if id2 == 0:
        t = T[id2]
    elif id2 == len(T):
        t = T[id2 - 1]
    else:
        if abs(X[i] - T[id2]) < abs(X[i] - T[id2 - 1]):
            t = T[id2]
        else:
            t = T[id2 - 1]
        
    if abs(X[i] - s) < abs(X[i] - t):
        ans = abs(X[i] - s)
        id3 = bisect.bisect_left(T, s)
        if id3 == 0:
            ans += abs(s - T[id3])
        elif id3 == len(T):
            ans += abs(s - T[id3 -1])
        else:
            if abs(s - T[id3]) < abs(s - T[id3 - 1]):
                ans += abs(s - T[id3])
            else:
                ans += abs(s - T[id3 -1])
                
    elif abs(X[i] - s) > abs(X[i] - t):
        ans = abs(X[i] - t)
        id4 = bisect.bisect_left(S, t)
        if id4 == 0:
            ans += abs(t - S[id4])
        elif id4 == len(S):
            ans += abs(t - S[id4 -1])
        else:
            if abs(t - S[id4]) < abs(t - S[id4 - 1]):
                ans += abs(t - S[id4])
            else:
                ans += abs(t - S[id4 -1])
    
    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)]
q=[int(input()) for _ in range(Q)]

def solve(start):
    ans=[]
    x=bisect.bisect_left(s,start,lo=0,hi=len(s))

    t1=bisect.bisect_left(t,x,lo=0,hi=len(t))
    if x<len(s) and t1<len(t):
        ans.append(min(abs(start-s[x])+abs(t[t1]-s[x]),abs(start-s[x])+abs(t[t1-1]-s[x])))

    if x>=1:
        t2=bisect.bisect_left(t,x-1,lo=0,hi=len(t))
        ans.append(min(abs(start-s[x-1])+abs(t[t2]-s[x-1]),abs(start-s[x-1])+abs(t[t2-1]-s[x-1])))




    y=bisect.bisect_left(t,start,lo=0,hi=len(t))
    s1=bisect.bisect_left(s,x,lo=0,hi=len(s))
    if y < len(t) and s1 < len(s):
        ans.append(min(abs(start - t[y]) + abs(s[s1] - t[y]), abs(start - t[y]) + abs(s[s1 - 1] - t[y])))
    if y>=1:
        s2=bisect.bisect_left(s,y-1,lo=0,hi=len(s))
        ans.append(min(abs(start-t[y-1])+abs(s[s2]-t[y-1]),abs(start-t[y-1])+abs(t[s2-1]-t[y-1])))
    
    print(min(ans))

for e in q:
    solve(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 j in range(b) ]
res = []
for i in range(q):
  start = int(input())
  near_s_in = min(zip([ abs(j - i) for j in s], range(len(s))))[1]
  near_t_in = min(zip([ abs(k - i) for k in t], range(len(t))))[1]

  print(near_s_in)
  print(near_t_in)
  if start <= s[near_s_in] and start <= t[near_t_in]:
    res.append(min(s[near_s_in], t[near_t_in]) - start + abs(t[near_t_in] - s[near_s_in]))
  elif start >= s[near_s_in] and start >= t[near_t_in]:
    res.append(start - max(s[near_s_in], t[near_t_in]) + abs(t[near_t_in] - s[near_s_in]))
  else:
    res.append(min(abs(s[near_s_in] - start), abs(t[near_t_in] - start)) + abs(t[near_t_in] - s[near_s_in]))

for h in res:
  print(h)

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s = []
t = []
x = []

for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
for _ in range(Q):
    x.append(int(input()))

for startP in x:
    sp_place = startP
    Ldist = -10**10
    Rdist = 10**10
    nextP = None
    archi = None
    move = 0
    for shp in s:
        tmpdist = shp - sp_place
        if tmpdist >= 0:
            if tmpdist < Rdist:
                Rdist = tmpdist
        else:
            if tmpdist > Ldist:
                Ldist = tmpdist
    L1dist = -10**10
    R1dist = 10**10
    for tp in t:
        tmpdist = tp - sp_place
        if tmpdist >= 0:
            if tmpdist < R1dist:
                R1dist = tmpdist
        else:
            if tmpdist > Ldist:
                L1dist = tmpdist
    print(min(max(R1dist,Rdist), max(L1dist,Ldist)))

Submission status: Wrong Answer

Solution code:
import bisect
A,B,Q = map(int,input().split())
a = []
b = []
x = []
for i in range(A):
  a.append(int(input()))
for j in range(B):
  b.append(int(input()))
for k in range(Q):
  x.append(int(input()))
for k in range(Q):  
  terafirst = 0
  jinjyafirst = 0
  p = bisect.bisect_left(a,x[k])
  q = bisect.bisect_left(b,x[k])
  if A > p >= 1:
    terafirst = min(abs(x[k]-a[p-1]),abs(x[k]-a[p]))
    if abs(x[k]-a[p-1])<abs(x[k]-a[p]):
      r = bisect.bisect_left(b,a[p-1])
      if B > r >= 1: 
        terafirst += min(abs(b[r-1]-a[p-1]),abs(b[r]-a[p-1]))
      elif r == 0:
        terafirst += abs(b[0]-a[p-1])
      elif r == B:
        terafirst += abs(b[B-1]-a[p-1])
    else:
      s = bisect.bisect_left(b,a[p])
      if B > s >= 1:
        terafirst += min(abs(b[s-1]-a[p]),abs(b[s]-a[p]))
      elif s == 0:
        terafirst += abs(b[0]-a[p])
      elif s == B:
        terafirst += abs(b[B-1]-a[p])
  elif p == 0:
    terafirst = abs(x[k]-a[0])
    tikai1 = bisect.bisect_left(b,a[0])
    if B > tikai1 >= 1:
      terafirst += min(abs(b[tikai1-1]-a[0]),abs(b[tikai1]-a[0]))
    elif tikai1 == 0:
      terafirst += abs(b[0]-a[0])
    elif tikai1 == B:
      terafirst += abs(b[B-1]-a[0])
  else:
    terafirst = abs(x[k]-a[p-1])
    tikai2 = bisect.bisect_left(b,a[p-1])
    if B > tikai2 >= 1:
      terafirst += min(abs(b[tikai2-1]-a[p-1]),abs(b[tikai2]-a[p-1]))
    elif tikai2 == 0:
      terafirst += abs(b[0]-a[p-1])
    elif tikai2 == B:
      terafirst += abs(b[B-1]-a[p-1])
      
  if B > q >= 1:
    jinjyafirst = min(abs(x[k]-b[q-1]),abs(x[k]-b[q]))
    if abs(x[k]-b[q-1])<abs(x[k]-b[q]):
      t = bisect.bisect_left(a,b[p-1])
      if A > t >= 1: 
        jinjyafirst += min(abs(a[t-1]-b[q-1]),abs(a[t]-b[q-1]))
      elif t == 0:
        jinjyafirst += abs(a[0]-b[q-1])
      elif t == B:
        jinjyafirst += abs(a[A-1]-b[q-1])
    else:
      u = bisect.bisect_left(a,b[q])
      if A > u >= 1:
        jinjyafirst += min(abs(a[u-1]-b[q]),abs(a[u]-b[q]))
      elif u == 0:
        jinjyafirst += abs(a[0]-b[q])
      elif u == B:
        jinjyafirst += abs(a[A-1]-b[q])
  elif q == 0:
    jinjyafirst = abs(x[k]-b[0])
    tikai3 = bisect.bisect_left(a,b[0])
    if A > tikai3 >= 1:
      jinjyafirst += min(abs(a[tikai3-1]-b[0]),abs(a[tikai3]-b[0]))
    elif tikai3 == 0:
      jinjyafirst += abs(a[0]-b[0])
    elif tikai3 == A:
      jinjyafirst += abs(a[A-1]-b[0])
  else:
    jinjyafirst = abs(x[k]-b[q-1])
    tikai4 = bisect.bisect_left(a,b[q-1])
    if A > tikai4 >= 1:
      jinjyafirst += min(abs(a[tikai4-1]-b[q-1]),abs(a[tikai4]-b[q-1]))
    elif tikai4 == 0:
      jinjyafirst += abs(a[0]-b[q-1])
    elif tikai4 == A:
      jinjyafirst += abs(a[A-1]-b[q-1])
  print(min(terafirst,jinjyafirst))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right

A,B,Q = map(int,input().split())
S = sorted(list(int(input()) for i in range(A)))
T = sorted(list(int(input()) for i in range(B)))
X = list(int(input()) for i in range(Q))


for i in range(len(X)) :
    calc1 = float("inf")
    calc2 = float("inf")
    calc3 = float("inf")
    calc4 = float("inf")
    
    ind_s_left  = bisect_left(S,X[i])
    ind_t_left  = bisect_left(T,X[i])
    ind_s_right = bisect_right(S,X[i])
    ind_t_right = bisect_right(T,X[i])
    
    
    res = []
    
    try :
        c1 = X[i] - min(S[ind_s_right], T[ind_t_right])
        res.append(c1)
    except IndexError :
        continue
    
    try :
        c2 = max(S[ind_s_left], T[ind_t_right]) - X[i]
        res.append(c2)
    except IndexError :
        continue

    try :
        c3 = abs(S[ind_s_left] - T[ind_t_right])
        c3 += min(abs(X[i] - S[ind_s_left]), abs(X[i] - T[ind_t_right]))
        
    except IndexError :
        continue
    
    
    try :
        c4 = abs(T[ind_t_left] - S[ind_s_right])
        c4 += min(abs(X[i] - T[ind_t_left]), abs(X[i] - S[ind_s_right]))
    except IndexError :
        continue
    print(min(res))

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],2*bb[bbb] - cc - aa[aaa-1],bb[bbb] + cc - 2 * aa[aaa-1])
		print(aaaa)

Submission status: Wrong Answer

Solution code:
import numpy as np
A, B, C = map(int, input().split())
s = np.array([int(input()) for _ in range(A)])
t = np.array([int(input()) for _ in range(B)])
x = np.array([int(input()) for _ in range(C)])
for i in x:
    si = i-s
    asi = list(abs(si))
    min_si = min(asi)
    s_idx = asi.index(min_si)
    ti = i-t
    ati = list(abs(ti))
    min_ti = min(ati)
    t_idx = ati.index(min_ti)
    if si[s_idx] * ti[t_idx] < 0:
        if min_si <= min_ti:
            print(min_si*2+min_ti)
        else:
            print(min_ti*2+min_si)
    else:
        print(max(min_si, min_ti))

Submission status: Wrong Answer

Solution code:
A,B,Q = map(int,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 in range(len(x)):
    mins = 10000000000
    mint = 10000000000
    minv = 10000000000
    for j in range(len(s)):
        if abs(x[i] - s[j]) < mins:
            mins = abs(x[i] - s[j])
            idxs = j
    for j in range(len(t)):
        if abs(x[i] - t[j]) < mint:
            mint = abs(x[i] - t[j])
            idxt = j
    #print(mins)
    #print(mint)
    if mins < mint:
        x[i] -= x[i] - s[idxs]
        for j in range(len(t)):
            if abs(x[i] - t[j]) < minv:
                minv = abs(x[i] - t[j])
        #print(mins)
        #print(minv)
        print(mins + minv)
    else:
        x[i] -= x[i] - t[idxt]
        for j in range(len(s)):
            if abs(x[i] - s[j]) < minv:
                minv = abs(x[i] - s[j])
        #print(mins)
        #print(minv)
        print(mint + minv)