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):
def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))
    INF = 10 ** 18
    
    jinja_list = [-INF]
    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))
    jinja_list.append(INF)
    
    temple_list = [-INF]
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))
    temple_list.append(INF)

    from bisect import bisect

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]

        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]

        distances = []
        distances.append(pos - min(jinja_left[0], temple_left[0]))
        #distances.append("jinja_left and temple_left")
        distances.append(max(jinja_right[0], temple_right[0]) - pos)
        #distances.append("jinja_right and temple_right")
        left_right = abs(pos - jinja_left[0]) + abs(temple_right[0] - jinja_left[0])
        right_left = abs(pos - temple_right[0]) + abs(jinja_left[0] - temple_right[0])
        distances.append(left_right)
        distances.append(right_left)
        #distances.append("jinja_left and temple_right")
        left_right = abs(pos - temple_left[0]) + abs(jinja_right[0] - temple_left[0])
        right_left = abs(pos - jinja_right[0]) + abs(temple_left[0] - jinja_right[0])
        distances.append(left_right)
        distances.append(right_left)
        #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))
        

def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))
    INF = 10 ** 18
    
    jinja_list = [-INF]
    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))
    jinja_list.append(INF)
    
    temple_list = [-INF]
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))
    temple_list.append(INF)

    from bisect import bisect, bisect_right

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect_right(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1]
        jinja_right = jinja_list[jinja_index]

        temple_index = bisect_right(temple_list, pos)
        temple_left = temple_list[temple_index - 1]
        temple_right = temple_list[temple_index]

        distances = []
        
        
        
        left_right = abs(pos - jinja_left) + abs(temple_left - jinja_left)
        right_left = abs(pos - temple_left) + abs(jinja_left - temple_left)
        distances.append(left_right)
        distances.append(right_left)
        
        left_right = abs(pos - jinja_right) + abs(temple_right - jinja_right)
        right_left = abs(pos - temple_right) + abs(jinja_right - temple_right)
        distances.append(left_right)
        distances.append(right_left)
        
        
        left_right = abs(pos - jinja_left) + abs(temple_right - jinja_left)
        right_left = abs(pos - temple_right) + abs(jinja_left - temple_right)
        distances.append(left_right)
        distances.append(right_left)
        left_right = abs(pos - temple_left) + abs(jinja_right - temple_left)
        right_left = abs(pos - jinja_right) + abs(temple_left - jinja_right)
        distances.append(left_right)
        distances.append(right_left)
        print(min(distances))
        

def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))
 
    jinja_list = []
 
    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))
 
    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))
 
    from bisect import bisect
 
    for i in range(q):
        pos = int(input().rstrip("\n"))
 
        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]
 
        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]
 
        distances = []
        
        if jinja_left and temple_left:
            distances.append(pos - min(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(max(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = (pos - jinja_left[0]) * 2 + (temple_right[0] - pos)
            right_left = (pos - jinja_left[0]) + (temple_right[0] - pos) * 2
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = ((pos - temple_left[0]) * 2) + (jinja_right[0] - pos)
            right_left = (pos - temple_left[0]) + ((jinja_right[0] - pos) * 2)
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))

if __name __ == '__main__':
  main()


def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))

    jinja_list = []

    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))

    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))

    from bisect import bisect

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]

        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]

        distances = []
        
        if jinja_left and temple_left:
            distances.append(pos - min(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(max(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = (pos - jinja_left[0]) * 2 + (temple_right[0] - pos)
            right_left = (pos - jinja_left[0]) + (temple_right[0] - pos) * 2
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = ((pos - temple_left[0]) * 2) + (jinja_right[0] - pos)
            right_left = (pos - temple_left[0]) + ((jinja_right[0] - pos) * 2)
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))
        

def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))

    jinja_list = []

    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))

    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))

    from bisect import bisect

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]

        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]

        distances = []
        
        if jinja_left and temple_left:
            distances.append(pos - min(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(max(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = abs(pos - jinja_left[0]) + abs(temple_right[0] - jinja_left[0])
            right_left = abs(pos - temple_right[0]) + abs(jinja_left[0] - temple_right[0])
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = abs(pos - temple_left[0]) + abs(jinja_right[0] - temple_left[0])
            right_left = abs(pos - jinja_right[0]) + abs(temple_left[0] - jinja_right[0])
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))
        

def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))

    jinja_list = []

    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))

    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))

    from bisect import bisect

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]

        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]

        distances = []
        
        if jinja_left and temple_left:
            distances.append(pos - max(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(min(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = (pos - jinja_left[0]) * 2 + (temple_right[0] - pos)
            right_left = (pos - jinja_left[0]) + (temple_right[0] - pos) * 2
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = ((pos - temple_left[0]) * 2) + (jinja_right[0] - pos)
            right_left = (pos - temple_left[0]) + ((jinja_right[0] - pos) * 2)
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))
        

def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))
    INF = 10 ** 18
    
    jinja_list = [-INF]
    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))
    jinja_list.append(INF)
    
    temple_list = [-INF]
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))
    temple_list.append(INF)

    from bisect import bisect

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1]
        jinja_right = jinja_list[jinja_index]

        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1]
        temple_right = temple_list[temple_index]

        distances = []
        distances.append(pos - min(jinja_left, temple_left))
        #distances.append("jinja_left and temple_left")
        distances.append(max(jinja_right, temple_right) - pos)
        #distances.append("jinja_right and temple_right")
        left_right = abs(pos - jinja_left) + abs(temple_right - jinja_left)
        right_left = abs(pos - temple_right) + abs(jinja_left- temple_right)
        distances.append(left_right)
        distances.append(right_left)
        #distances.append("jinja_left and temple_right")
        left_right = abs(pos - temple_left) + abs(jinja_right - temple_left)
        right_left = abs(pos - jinja_right) + abs(temple_left - jinja_right)
        distances.append(left_right)
        distances.append(right_left)
        #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))
        

def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))

    jinja_list = []

    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))

    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))

    from bisect import bisect

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]

        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]

        distances = []
        
        if jinja_left and temple_left:
            distances.append(pos - min(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(max(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = abs(pos - jinja_left[0]) + abs(temple_right[0] - jinja_left[0])
            right_left = abs(pos - temple_right[0]) + abs(jinja_left[0] - temple_right[0])
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = abs(pos - temple_left[0]) + abs(jinja_right[0] - temple_left[0])
            right_left = abs(pos - jinja_right[0]) + abs(temple_left[0] - jinja_right[0])
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))
        
if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted