Problem: 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.)

Submission attempts (in order):
import sys
import bisect

_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))
"""
s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = sorted(data[-Q:])

for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)


import sys
import bisect

A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))

s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = data[-Q:]
 
for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)

import sys
import bisect

A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))

s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = sorted(data[-Q:])
 
for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)

import sys
import bisect
 
_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))
"""
s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = data[-Q:]
 
for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)

import sys
import bisect

_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))
"""
s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = data[-Q:]

for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - s_left) + abs(x - t_right) + min(abs(x - s_left), abs(x - t_right))
    d = abs(x - t_left) + abs(x - s_right) + min(abs(x - t_left), abs(x - s_right))
    dis = min(a,
              b,
              c,
              d)
    print(dis)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted