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:
from bisect import bisect_left
a,b,q = map(int,input().split())
S = [-float('inf')]+[int(input()) for i in range(a)]+[float('inf')]
T = [-float('inf')]+[int(input()) for i in range(b)]+[float('inf')]

def f(S,T):
    res = 10**15
    s = bisect_left(S,x)

    ans = 0
    t = bisect_left(T,S[s-1])
    ans += abs(x-S[s-1])+min(abs(S[s-1]-T[t-1]),abs(S[s-1]-T[t]))
    if ans <= res:
        res = ans
    
    ans = 0
    t = bisect_left(T,S[s])
    ans += abs(x-S[s])+min(abs(S[s]-T[t-1]),abs(S[s-1]-T[t]))
    if ans <= res:
        res = ans

    return res


for i in range(q):
    x = int(input())
    print(min(f(S,T),f(T,S)))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
a,b,q = map(int,input().split())
S = [-float('inf')]+[int(input()) for i in range(a)]+[float('inf')]
T = [-float('inf')]+[int(input()) for i in range(b)]+[float('inf')]

def f(S,T):
    res = 10**15
    s = bisect_left(S,x)

    ans = 0
    t = bisect_left(T,S[s-1])
    ans += abs(x-S[s-1])+min(abs(S[s-1]-T[t-1]),abs(S[s-1]-T[t]))
    if ans <= res:
        res = ans
    
    ans = 0
    t = bisect_left(T,S[s])
    ans += abs(x-S[s])+min(abs(S[s]-T[t-1]),abs(S[s-1]-T[t]))
    if ans <= res:
        res = ans

    return res


for i in range(q):
    x = int(input())
    print(min(f(S,T),f(T,S)))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left


def solve(string):
    a, b, q, *stx = map(int, string.split())
    s = [-10**10] + stx[:a] + [2 * 10**10]
    t = [-10**10] + stx[a:a + b] + [2 * 10**10]
    # x = dict([])
    ans = []
    i_s = 0
    i_t = 0
    for _x in stx[-q:]:
        """
        while s[i_s] < _x:
            i_s += 1
        while t[i_t] < _x:
            i_t += 1
        """
        i_s, i_t = bisect_left(s, _x), bisect_left(t, _x)
        ls, rs = _x - s[i_s - 1], s[i_s] - _x
        lt, rt = _x - t[i_t - 1], t[i_t] - _x
        r_max = max(rs, rt)
        l_min = _x - min(ls, lt)
        """
        x[_x] = min([
            r_max - _x, _x - l_min, rt - ls + min(rt - _x, _x - ls),
            rs - lt + min(rs - _x, _x - lt)
        ])
        """
        ans.append(str(min([r_max, l_min, rt - ls + min(rt, ls), rs - lt + min(rs, lt)])))
    # return "\n".join([str(x[_x]) for _x in stx[-q:]])
    return "\n".join(ans)


if __name__ == '__main__':
    n, m, l = map(int, input().split())
    print(solve('{} {} {}\n'.format(n, m, l) + '\n'.join([input() for _ in range(n + m + l)])))

Submission status: Wrong Answer

Solution code:
def solve(string):
    a, b, q, *stx = map(int, string.split())
    s = sorted(stx[:a] + [-10**10, 2 * 10**10])
    t = sorted(stx[a:a + b] + [-10**10, 2 * 10**10])
    x = {_q: -1 for _q in stx[-q:]}
    i_s = 0
    i_t = 0
    for _x in sorted(x.keys()):
        while s[i_s] < _x:
            i_s += 1
        while t[i_t] < _x:
            i_t += 1
        l_s, r_s = s[i_s - 1], s[i_s]
        l_t, r_t = t[i_t - 1], t[i_t]
        r_max = max(r_s, r_t)
        r_min = min(r_s, r_t)
        l_max = max(l_s, l_t)
        l_min = min(l_s, l_t)

        x[_x] = min([r_max - _x, _x - l_min, r_min - l_max + min(r_min - _x, _x - l_max)])
    return "\n".join([str(x[_x]) for _x in stx[-q:]])


if __name__ == '__main__':
    n, m, l = map(int, input().split())
    print(solve('{} {} {}\n'.format(n, m, l) + '\n'.join([input() for _ in range(n + m + l)])))

Submission status: Wrong Answer

Solution code:
def solve(string):
    a, b, q, *stx = map(int, string.split())
    s = sorted(stx[:a] + [-10**10, 2 * 10**10])
    t = sorted(stx[a:a + b] + [-10**10, 2 * 10**10])
    x = {_q: -1 for _q in stx[-q:]}
    i_s = 0
    i_t = 0
    for _x in sorted(x.keys()):
        while s[i_s] < _x:
            i_s += 1
        while t[i_t] < _x:
            i_t += 1
        l_s, r_s = s[i_s - 1], s[i_s]
        l_t, r_t = t[i_t - 1], t[i_t]
        r_max = max(r_s, r_t)
        r_min = min(r_s, r_t)
        l_max = max(l_s, l_t)
        l_min = min(l_s, l_t)

        x[_x] = min([r_max - _x, _x - l_min, r_min - l_max + min(r_min - _x, _x - l_max)])
    return "\n".join([str(x[_x]) for _x in stx[-q:]])


if __name__ == '__main__':
    n, m, l = map(int, input().split())
    print(solve('{} {}\n'.format(n, m, l) + '\n'.join([input() for _ in range(n + m + l)])))

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]
print(s,t)
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    print(b,d)
    print([s[b - 1], s[b]])
    print([t[d - 1], t[d]])
    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:
read = input
# read = Read(False, """
# 1 1 3
# 1
# 10000000000
# 2
# 9999999999
# 5000000000
# """).read
A,B,Q = list(map(int, read().split(' ')))
arrA = list(map(int, [read() for i in range(A)]))
arrB = list(map(int, [read() for i in range(B)]))
arrQ = list(map(int, [read() for i in range(Q)]))
arrA.sort()
arrB.sort()
import bisect
def cs(x, arr):
    al = bisect.bisect_left(arr, x)
    return list(map(lambda i: arr[i], filter(lambda i:i>=0 and i< len(arr), range(al, al + 2))))

def same_s(da,db):
    da = da / abs(da)
    db = db / abs(db)
    return da*db>0
    
def t(q,a,b):
    da = q - a
    db = q - b
    if(min(abs(da), abs(db)) == 0):
        return max(abs(da), abs(db))
    if same_s(da, db):
        return max(abs(da), abs(db))
    else :
        return 2*min(abs(da), abs(db)) + max(abs(da), abs(db))
for q in arrQ:
    optA = cs(q, arrA)
    optB = cs(q, arrB)
    ret = 10 ** 11
    for a in optA:
        for b in optB:
            ret = min(ret, t(q, a,b))
    print(ret)

Submission status: Wrong Answer

Solution code:
import bisect


def solve(a_list, b_list, position):
    # time O(log(A))
    a_right_key = bisect.bisect_left(a_list, position)
    a_left_key = a_right_key - 1
    # time O(log(B))
    b_right_key = bisect.bisect_left(b_list, position)
    b_left_key = b_right_key - 1

    a_left, a_right = a_list[a_left_key], a_list[a_right_key]
    b_left, b_right = b_list[b_left_key], b_list[b_right_key]

    return min(
        position - min(a_left, b_left),
        max(a_right, b_right) - position,
        position - max(a_left, b_left) + (b_right - a_left) if (a_left > b_left) else (a_right - b_left),
        min(a_right, b_right) - position + (b_right - a_left) if (a_right > b_right) else (a_right - b_left)
    )


def main():
    inf = 10 ** 20
    A, B, Q = map(int, input().split())

    a_list, b_list = [-inf], [-inf]
    # time O(A)
    for _ in range(A):
        a_list.append(int(input()))
    a_list.append(inf)
    # time O(B)
    for _ in range(B):
        b_list.append(int(input()))
    b_list.append(inf)

    # time O(Q (log(A) + log(B))
    for _ in range(Q):
        position = int(input())
        ans = solve(a_list, b_list, position)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
 
 
def solve(n: int, m: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"):
 
    def check_pos(target, a, x):
        ret = {}
        a_x = a + x
        a_x.sort()
        idx = 0
        while idx < len(a_x) and a_x[idx] < target[0]:
            dis = target[0] - a_x[idx]
            ret[a_x[idx]] = [dis, [target[0], dis]] # [pos, dis]
            idx += 1
        for i in range(len(target) - 1):
            while idx < len(a_x) and target[i] <= a_x[idx] <= target[i + 1]:
                pos = a_x[idx]
                l = a_x[idx] - target[i]
                r = target[i + 1] - a_x[idx]
                ret[pos] = [
                    min(l, r),
                    [target[i],     l],
                    [target[i + 1], r]
                ]
                idx += 1
        while idx < len(a_x):
            dis = a_x[idx] - target[-1]
            ret[a_x[idx]] = [dis, [target[-1], dis]] # [pos, dis]
            idx += 1
        return ret
 
    nearest_s = check_pos(s, t, x)
    nearest_t = check_pos(t, s, x)
    return
 
    for q in x:
        ret = min(
                min([(pos_dis[1] + nearest_t[pos_dis[0]][0]) for pos_dis in nearest_s[q][1:]]),
                min([(pos_dis[1] + nearest_s[pos_dis[0]][0]) for pos_dis in nearest_t[q][1:]]))
        print(ret)
    return
 
 
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    s = [ int(next(tokens)) for _ in range(A) ]  # type: "List[int]"
    t = [ int(next(tokens)) for _ in range(B) ]  # type: "List[int]"
    x = [ int(next(tokens)) for _ in range(Q) ]  # type: "List[int]"
    solve(A, B, Q, s, t, x)
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, m: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"):

    def check_pos(target, a, x):
        ret = {}
        a_x = a + x
        a_x.sort()
        idx = 0
        while idx < len(a_x) and a_x[idx] < target[0]:
            pos = a_x[idx]
            ret[pos] = {
                'dis': target[0] - a_x[idx],
                'pos': [target[0]]
            }
            idx += 1
        for i in range(len(target) - 1):
            while idx < len(a_x) and target[i] <= a_x[idx] <= target[i + 1]:
                pos = a_x[idx]
                ret[pos] = {'dis': min(a_x[idx] - target[i], target[i + 1] - a_x[idx])}
                if a_x[idx] - target[i] == target[i + 1] - a_x[idx]:
                    ret[pos]['pos'] = [target[i], target[i + 1]]
                elif a_x[idx] - target[i] < target[i + 1] - a_x[idx]:
                    ret[pos]['pos'] = [target[i]]
                else:
                    ret[pos]['pos'] = [target[i + 1]]
                idx += 1
        while idx < len(a_x):
            pos = a_x[idx]
            ret[pos] = {
                'dis': a_x[idx] - target[-1],
                'pos': [target[-1]]
            }
            idx += 1
        return ret

    nearest_s = check_pos(s, t, x)
    nearest_t = check_pos(t, s, x)

    print('hoge')
    return

    for q in x:
        ret = min(nearest_s[q]['dis'] + min([nearest_t[pos]['dis'] for pos in nearest_s[q]['pos']]),
                  nearest_t[q]['dis'] + min([nearest_s[pos]['dis'] for pos in nearest_t[q]['pos']]))
        print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    s = [ int(next(tokens)) for _ in range(A) ]  # type: "List[int]"
    t = [ int(next(tokens)) for _ in range(B) ]  # type: "List[int]"
    x = [ int(next(tokens)) for _ in range(Q) ]  # type: "List[int]"
    solve(A, B, Q, s, t, x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, m: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"):

    def check_pos(target, a, x):
        ret = {}
        a_x = a + x
        a_x.sort()
        idx = 0
        while idx < len(a_x) and a_x[idx] < target[0]:
            pos = a_x[idx]
            ret[pos] = {
                'dis': target[0] - a_x[idx],
                'pos': [target[0]]
            }
            idx += 1
        for i in range(len(target) - 1):
            while idx < len(a_x) and target[i] <= a_x[idx] <= target[i + 1]:
                pos = a_x[idx]
                ret[pos] = {'dis': min(a_x[idx] - target[i], target[i + 1] - a_x[idx])}
                if a_x[idx] - target[i] == target[i + 1] - a_x[idx]:
                    ret[pos]['pos'] = [target[i], target[i + 1]]
                elif a_x[idx] - target[i] < target[i + 1] - a_x[idx]:
                    ret[pos]['pos'] = [target[i]]
                else:
                    ret[pos]['pos'] = [target[i + 1]]
                idx += 1
        while idx < len(a_x):
            pos = a_x[idx]
            ret[pos] = {
                'dis': a_x[idx] - target[-1],
                'pos': [target[-1]]
            }
            idx += 1
        return ret

    nearest_s = check_pos(s, t, x)
    nearest_t = check_pos(t, s, x)

    for q in x:
        ret = min(nearest_s[q]['dis'] + min([nearest_t[pos]['dis'] for pos in nearest_s[q]['pos']]),
                  nearest_t[q]['dis'] + min([nearest_s[pos]['dis'] for pos in nearest_t[q]['pos']]))
        print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    s = [ int(next(tokens)) for _ in range(A) ]  # type: "List[int]"
    t = [ int(next(tokens)) for _ in range(B) ]  # type: "List[int]"
    x = [ int(next(tokens)) for _ in range(Q) ]  # type: "List[int]"
    solve(A, B, Q, s, t, x)

if __name__ == '__main__':
    main()

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)]
st={}
ts={}
k=0
for i in range(A):
    for j in range(k,B):
        if s[i]<t[0]:
            st[s[i]]=abs(s[i]-t[0])
            break
        if t[B-1]<s[i]:
            st[s[i]]=abs(s[i]-t[B-1])
            break
        if t[j]<s[i] and s[i]<t[j+1]:
            st[s[i]]=min(abs(s[i]-t[j]),abs(s[i]-t[j+1]))
            k=j
            break
k=0
for i in range(B):
    for j in range(k,A):
        if t[i]<s[0]:
            ts[t[i]]=abs(t[i]-s[0])
            break
        if s[A-1]<t[i]:
            ts[t[i]]=abs(t[i]-s[A-1])
            break
        if s[j]<t[i] and t[i]<s[j+1]:
            ts[t[i]]=min(abs(t[i]-s[j]),abs(t[i]-s[j+1]))
            k=j
            break
l=s+t
l.sort()
st.update(ts)
k=0
for i in range(Q):
    for j in range(k,A+B):
        if x[i]<=l[0]:
            print(l[0]-x[i]+st[l[0]])
            break
        if l[A+B-1]<=x[i]:
            print(x[i]-l[A+B-1]+st[l[A+B-1]])
            break
        if l[j]<=x[i] and x[i]<=l[j+1]:
            print(min((x[i]-l[j]+st[l[j]]),(l[j+1]-x[i]+st[l[j+1]])))
            k=j
            break

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)]
st={}
ts={}
k=0
for i in range(A):
    for j in range(k,B):
        if s[i]<t[0]:
            st[s[i]]=abs(s[i]-t[0])
            break
        if t[B-1]<s[i]:
            st[s[i]]=abs(s[i]-t[B-1])
            break
        if t[j]<s[i] and s[i]<t[j+1]:
            st[s[i]]=min(abs(s[i]-t[j]),abs(s[i]-t[j+1]))
            k=j
            break
k=0
for i in range(B):
    for j in range(k,A):
        if t[i]<s[0]:
            ts[t[i]]=abs(t[i]-s[0])
            break
        if s[A-1]<t[i]:
            ts[t[i]]=abs(t[i]-s[A-1])
            break
        if s[j]<t[i] and t[i]<s[j+1]:
            ts[t[i]]=min(abs(t[i]-s[j]),abs(t[i]-s[j+1]))
            k=j
            break
l=s+t
l.sort()
st.update(ts)
k=0
for i in range(Q):
    for j in range(k,A+B):
        if x[i]<l[0]:
            print(l[0]-x[i]+st[l[0]])
            break
        if l[A+B-1]<x[i]:
            print(x[i]-l[A+B-1]+st[l[A+B-1]])
            break
        if l[j]<x[i] and x[i]<l[j+1]:
            print(min((x[i]-l[j]+st[l[j]]),(l[j+1]-x[i]+st[l[j+1]])))
            k=j
            break

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)]
st={}
ts={}
k=0
for i in range(A):
    for j in range(k,B):
        if s[i]<t[0]:
            st[s[i]]=abs(s[i]-t[0])
            break
        if t[B-1]<s[i]:
            st[s[i]]=abs(s[i]-t[B-1])
            break
        if t[j]<s[i] and s[i]<t[j+1]:
            st[s[i]]=min(abs(s[i]-t[j]),abs(s[i]-t[j+1]))
            k=j
            break
k=0
for i in range(B):
    for j in range(k,A):
        if t[i]<s[0]:
            ts[t[i]]=abs(t[i]-s[0])
            break
        if s[A-1]<t[i]:
            ts[t[i]]=abs(t[i]-s[A-1])
            break
        if s[j]<t[i] and t[i]<s[j+1]:
            ts[t[i]]=min(abs(t[i]-s[j]),abs(t[i]-s[j+1]))
            k=j
            break
l=s+t
l.sort()
st.update(ts)
k=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)]
st={}
ts={}
k=0
for i in range(A):
    for j in range(k,B):
        if s[i]<t[0]:
            st[s[i]]=abs(s[i]-t[0])
            break
        if t[B-1]<s[i]:
            st[s[i]]=abs(s[i]-t[B-1])
            break
        if t[j]<s[i] and s[i]<t[j+1]:
            st[s[i]]=min(abs(s[i]-t[j]),abs(s[i]-t[j+1]))
            k=j
            break
k=0
for i in range(B):
    for j in range(k,A):
        if t[i]<s[0]:
            ts[t[i]]=abs(t[i]-s[0])
            break
        if s[A-1]<t[i]:
            ts[t[i]]=abs(t[i]-s[A-1])
            break
        if s[j]<t[i] and t[i]<s[j+1]:
            ts[t[i]]=min(abs(t[i]-s[j]),abs(t[i]-s[j+1]))
            k=j
            break
l=s+t
l.sort()
st.update(ts)
k=0
for i in range(Q):
    for j in range(k,A+B):
        if x[i]<l[0]:
            print(l[j]+st[l[0]])
            break
        if l[A+B-1]<x[i]:
            print(x[i]-l[A+B-1]+st[l[A+B-1]])
            break
        if l[j]<x[i] and x[i]<l[j+1]:
            print(min((x[i]-l[j]+st[l[j]]),(l[j+1]-x[i]+st[l[j+1]])))
            k=j
            break

Submission status: Wrong Answer

Solution code:
import bisect
A, B, Q = map(int, input().split())
inf = 10**18
lisS = [-inf] + [int(input(x)) for x in range(A)] + [inf]
lisT = [-inf] + [int(input(x)) for x in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  i, j = bisect.bisect_right(lisS, x), bisect.bisect_right(lisT, x)
  res = inf
  for S in [lisS[i - 1], lisS[i]]:
    for T in [lisT[j - 1], lisT[j]]:
      r1, r2 = abs(S - x) + abs(S - T), abs(T - x) + abs(S - T)
      res = min(res, r1, r2)
  print(res)

Submission status: Wrong Answer

Solution code:
import bisect
A, B, Q = map(int, input().split())
inf = 10**18
lisS = [-inf] + [int(input(x)) for x in range(A)] + [inf]
lisT = [-inf] + [int(input(x)) for x in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  i = bisect.bisect_right(lisS, x)
  j = bisect.bisect_right(lisT, x)
  res = inf
  for S in [lisS[i - 1], lisS[i]]:
    for T in [lisT[j - 1], lisT[j]]:
      r1 = abs(S - x) + abs(S - T)
      r2 = abs(T - x) + abs(S - T)
      res = min(res, r1, r2)
  print(res)

Submission status: Wrong Answer

Solution code:
import bisect
A, B, Q = map(int, input().split())
lisS = [int(input(x)) for x in range(A)]
lisT = [int(input(x)) for x in range(B)]
lisQ = [int(input(x)) for x in range(Q)]
for x in lisQ:
  i = bisect.bisect_right(lisS, x)
  j = bisect.bisect_right(lisT, x)
  if i >= A:
    SR = 10**12
  else:
    SR = lisS[i] - x
  SL = x - lisS[i - 1]  
  if j >= B:
    TR = 10**12
  else:
    TR = lisT[j] - x
  TL = x - lisT[j - 1]
  r1 = SR + abs(SR - TR)
  r2 = 2*SR + TL
  r3 = SL + abs(SL - TL)
  r4 = 2*SL + TR
  r5 = TR + abs(TR - SR)
  r6 = 2*TR + SL
  r7 = TL + abs(TL - SL)
  r8 = 2*TL + SR
  print(min(r1, r2, r3, r4, r5, r6, r7, r8))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

A,B,Q = map(int,input().split(" "))
s,t,q = [],[],[]
for paramlist, N in zip([s,t,q],[A,B,Q]):
    for n in range(N):
        paramlist.append(int(input()))   

s,t,q = [np.array(l,dtype=np.int64) for l in [s,t,q]]
def least_neighbor_t(s):
    sw = 10**5 if t[0] > s else s - np.max(t[t<s])
    se = 10**5 if t[-1] < s else np.min(t[t>s]) - s
    return min(sw,se)
    
def least_neighbor_s(t):
    tw = 10**5 if s[0] > t else t - np.max(s[s<t])
    te = 10**5 if s[-1] < t else np.min(s[s>t]) - t
    return min(tw,te)

s2t = np.frompyfunc(least_neighbor_t,1,1)(s)
t2s = np.frompyfunc(least_neighbor_s,1,1)(t)

def getdist(x):
    return min(np.min(np.abs(s - x) + s2t,axis=0),np.min(np.abs(t - x) + t2s,axis=0))

result = np.frompyfunc(getdist,1,1)(q)
print('\n'.join(list(map(str,result))))

Submission status: Wrong Answer

Solution code:
def zengo(l,n):
    if n<l[0]:
        return 100000000000,l[0]-n
    elif n>l[-1]:
        return n-l[-1],100000000000
    else:
        st=0
        ed=len(l)
        while ed-st>1:
            md=(st+ed)//2
            if n<l[md]:
                ed=md
            else:
                st=md
        return n-l[st],l[ed]-n

jn,tn,qn,*tmp=map(int,open(0).read().split())
jz=tmp[:jn]
tz=tmp[jn:jn+tn]
q=tmp[jn+tn:]
for i in q:
    jb,jf=zengo(jz,i)
    tb,tf=zengo(tz,i)
    ans=[max(jb,tb),max(jf,tf),min(jb,tb)+min(jf,tf)+min(jb,jf,tb,tf)]
    print(min(ans))

Submission status: Wrong Answer

Solution code:
import bisect
import sys
A,B,Q=map(int, input().split())
s=[int(input()) for _ in range(A)]
t=[int(input()) for _ in range(B)]
x=[int(input()) for _ in range(Q)]
s.sort()
t.sort()
ls = len(s)
lt = len(t)
for xi in x:
    s1 = 0
    s2 = bisect.bisect_left(s,xi)#誰の左に入るかが出力される
    if s2!=0:
        s1 = s2-1
    if s2 >= ls:
        s2 = ls-1
        s1 = s2
    t1 = 0
    t2 = bisect.bisect_left(t,xi)
    if t2!=0:
        t1 = t2-1
    if t2 >= lt:
        t2 = lt-1
        t1 = t2
    l1 = l2 = l3 = l4 = sys.maxsize
    if s2!=0 and t2 != 0:
        l1 = max(abs(s[s1]-xi),abs(t[t1]-xi))
    if s1!=s2 and t1 != t2:
        l2 = max(abs(s[s2]-xi),abs(t[t2]-xi))
    if abs(s[s1]-xi)<abs(t[t1]-xi):
        l3 = 2*abs(s[s1]-xi) + abs(t[t2]-xi)
    else:
        l3 = 2*abs(t[t1]-xi) + abs(s[s2]-xi)
    if abs(s[s2]-xi)<abs(t[t2]-xi):
        l4 = 2*abs(s[s2]-xi) + abs(t[t1]-xi)
    else:
        l4 = 2*abs(t[t2]-xi) + abs(s[s1]-xi)
    l = [l1,l2,l3,l4]
    print(min(l))

Submission status: Wrong Answer

Solution code:
def bisearch(li,target):
    l=0
    r=len(li)-1
    while(l<=r):
        m=int((l+r)/2)

        if(m==len(li)-1):
            return target-li[m][0]+li[m][1]
        if(li[m][0]<target and target<li[m+1][0]):
            return min(target-li[m][0]+li[m][1],li[m+1][0]-target+li[m+1][1])
        if(m==0):
            return li[m][0]-target+li[m][1]
        if(target<li[m][0]):
            r = m-1
        else:
            l = m+1


inf = 10**15
A,B,Q = [int(x) for x in input().split()]
s = [0]*(A+1)
t = [0]*(B+1)
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[-1] = inf
t[-1] = inf

li = []
a = b = 0
while(a<A or b<B):
    if(s[a]<t[b]):
        li.append([s[a],min(abs(t[b]-s[a]),abs(t[b-1]-s[a]))])
        a+=1
    else:
        li.append([t[b],min(abs(t[b]-s[a]),abs(t[b]-s[a-1]))])
        b+=1

for i in x:
    print(bisearch(li,i))

Submission status: Wrong Answer

Solution code:
import sys
def calc_dist(xi,s,t):
    s_dif = []
    t_dif = []
    min_s_dif = sys.maxsize
    min_t_dif = sys.maxsize
    min_s = -1
    min_t = -1
    ls = len(s)
    lt = len(t)
    l = ls
    if ls<lt:
        l=lt
    temp_min_s_dif = 0
    temp_min_t_dif = 0
    result=0
    for i in range(l):
        if i < ls:
            temp_min_s_dif = abs(s[i]-xi)
            if min_s_dif > temp_min_s_dif:
                min_s_dif = temp_min_s_dif
                min_s = i
        if i < lt:
            temp_min_t_dif = abs(t[i]-xi)
            if min_t_dif > temp_min_t_dif:
                min_t_dif = temp_min_t_dif
                min_t = i
    #print(min_s_dif,min_t_dif,s[min_s],t[min_t])
    if min_s_dif < min_t_dif:  
        if (s[min_s]<xi and xi<t[min_t]) or (t[min_t]<xi and xi<s[min_s]):
            result = 2*min_s_dif + min_t_dif
        else:
            result = min_t_dif
    else:
        if (s[min_s]<xi and xi<t[min_t]) or (t[min_t]<xi and xi<s[min_s]):
            result = 2*min_t_dif + min_s_dif
        else:
            result = min_s_dif
    return result

"""
A,B,Q = 1,1,3
s=[1]
t=[10000000000]
x=[2,9999999999,5000000000]"""
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)]
for xi in x:
    print(calc_dist(xi,s,t))

Submission status: Wrong Answer

Solution code:
A,B,Q = [int(i) for i in input().split()]
s = [int(input())for i in range(A)]
t = [int(input())for i in range(B)]
x = [int(input()) for n in range(Q)]
for x_i in x:
  min_val = 100000000
  min_s = 0
  for s_i in s:
    if abs(x_i-s_i) < min_val:
      min_val = abs(x_i-s_i)
      min_s = s_i
  min_1 = (min([abs(min_s-t_i) for t_i in t]) + min_val)
  min_val = 100000000
  min_s = 0
  for s_i in t:
    if abs(x_i-s_i) < min_val:
      min_val = abs(x_i-s_i)
      min_s = s_i
  min_2 = (min([abs(min_s-t_i) for t_i in s]) + min_val)
  print (min(min_1, min_2))

Submission status: Wrong Answer

Solution code:
import bisect


def get_near(tera_num, tera_data, start_point):
    # tera_pre, tera_after = float('inf'), 0
    # for i in range(tera_num):
    #     if tera_data[i] >= start_point:
    #         tera_after = tera_data[i]
    #         break
    #     tera_pre = tera_data[i]
    index_right = bisect.bisect_right(tera_data, start_point)
    tera_pre, tera_after = tera_data[index_right], tera_data[index_right - 1]
    return tera_pre, tera_after

def calc_data(start_point, point_a, point_b, min_point):
    a = abs(start_point - point_a) + abs(point_a - point_b)
    b = abs(start_point - point_b) + abs(point_b - point_a)
    min_point = min(a, b, min_point)
    return min_point

def calc_ans(start_point, tera_pre, tera_after, jinja_pre, jinja_after):
    min_point = float('inf')
    min_point = calc_data(start_point, tera_pre, jinja_pre, min_point)
    min_point = calc_data(start_point, tera_pre, jinja_after, min_point)
    min_point = calc_data(start_point, tera_after, jinja_pre, min_point)
    min_point = calc_data(start_point, tera_after, jinja_after, min_point)
    return min_point


def start(tera_num, jinja_num, q_num, tera_data, jinja_data, q_data):
    for start_point in q_data:
        # start_point = q_data[i]
        tera_pre, tera_after = get_near(tera_num, tera_data, start_point)
        jinja_pre, jinja_after = get_near(jinja_num, jinja_data, start_point)
        ans = calc_ans(start_point, tera_pre, tera_after, jinja_pre, jinja_after)
        print(ans)

def main():
    tera_num, jinja_num, q_num = list(map(int, input().split()))
    tera_data = [-float('inf')] + [int(input()) for i in range(tera_num)] + [float('inf')]
    jinja_data = [-float('inf')] + [int(input()) for i in range(jinja_num)] + [float('inf')]
    q_data = [int(input()) for i in range(q_num)]

    start(tera_num, jinja_num, q_num, tera_data, jinja_data, q_data)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from bisect import*
a,b,q,*l=map(int,open(0).read().split())
if a==2and b==3and q==4:
  print('hoge')
f=10**18
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i=bisect(s,x)
  j=bisect(t,x)
  for k in[i,i-1]:
    for l in[j,j-1]:
      m=min(m,abs(x-s[k])+abs(s[k]-t[l]),abs(x-t[l])+abs(t[l]-s[k]))
  print(m)

Submission status: Wrong Answer

Solution code:
from bisect import*
a,b,q,*l=map(int,open(0).read().split())
if a==2and b==3and q==4:
  print('hoge')
f=10**18
s,t=[-f]+l[:a]+[f],[-f]+l[a:a+b]+[f]
for x in l[a+b:]:
  m=f
  i=bisect(s,x)
  j=bisect(t,s[i])
  k=bisect(t,s[i-1])
  v=abs(x-s[i])+abs(s[i]-t[k])
  if v<m:m=v
  v=abs(x-t[k])+abs(t[k]-s[i])
  if v<m:m=v
  v=abs(x-s[i])+abs(s[i]-t[k-1])
  if v<m:m=v
  v=abs(x-t[k-1])+abs(t[k-1]-s[i])
  if v<m:m=v 
  v=abs(x-s[i-1])+abs(s[i-1]-t[k])
  if v<m:m=v
  v=abs(x-t[k])+abs(t[k]-s[i-1])
  if v<m:m=v
  v=abs(x-s[i-1])+abs(s[i-1]-t[k-1])
  if v<m:m=v
  v=abs(x-t[k-1])+abs(t[k-1]-s[i-1])
  if v<m:m=v
  i=bisect(t,x)
  j=bisect(s,t[i])
  k=bisect(s,t[i-1])
  v=abs(x-t[i])+abs(t[i]-s[k])
  if v<m:m=v
  v=abs(x-s[k])+abs(s[k]-t[i])
  if v<m:m=v 
  v=abs(x-t[i])+abs(t[i]-s[k-1])
  if v<m:m=v
  v=abs(x-s[k-1])+abs(s[k-1]-t[i])
  if v<m:m=v 
  v=abs(x-t[i-1])+abs(t[i-1]-s[k])
  if v<m:m=v
  v=abs(x-s[k])+abs(s[k]-t[i-1])
  if v<m:m=v
  v=abs(x-t[i-1])+abs(t[i-1]-s[k-1])
  if v<m:m=v
  v=abs(x-s[k-1])+abs(s[k-1]-t[i-1])
  if v<m:m=v
  print(m)