Problem p03112 (python)

Fixed submission:
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()))


import bisect

mas = max(s)
mis = min(s)
mat = max(t)
mit = min(t)

for i in range(Q):
    ans = 10**20

    if mis <= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        s_index -= 1
        if mat >= s[s_index]:
            t_index = bisect.bisect_left(t,s[s_index])
            ans = min(ans, abs(x[i] - s[s_index]) + abs(t[t_index] - s[s_index]))

    if mit <= x[i]:
        t_index = bisect.bisect_left(t,x[i])
        t_index -= 1
        if mas >= t[t_index]:
            s_index = bisect.bisect_left(s,t[t_index])
            ans = min(ans, abs(x[i] - t[t_index]) + abs(t[t_index] - s[s_index]))

    if mas >= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        if mit <= s[s_index]:
            t_index = bisect.bisect_left(t,s[s_index])
            t_index -= 1
            ans = min(ans, abs(x[i] - s[s_index]) + abs(t[t_index] - s[s_index]))

    if mat >= x[i]:
        t_index = bisect.bisect_left(t,x[i])
        if mis <= t[t_index]:
            s_index = bisect.bisect_left(s,t[t_index])
            s_index -= 1
            ans = min(ans, abs(x[i] - t[t_index]) + abs(t[t_index] - s[s_index]))

    if mas >= x[i] and mat >= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        t_index = bisect.bisect_left(t,x[i])
        ans = min(ans,abs(x[i] - max(s[s_index],t[t_index])))

    if mis <= x[i] and mit <= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        s_index -= 1
        t_index = bisect.bisect_left(t,x[i])
        t_index -= 1
        ans = min(ans,abs(x[i] - min(s[s_index],t[t_index])))
    print(ans)

Buggy submission (verdict: Runtime Error):
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()))


import bisect

mas = max(s)
mis = min(s)
mat = max(t)
mit = min(t)

for i in range(Q):
    ans = 10**20

    if mis <= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        s_index -= 1
        if mat >= s[s_index]:
            t_index = bisect.bisect_left(t,s[s_index])
            ans = min(ans, abs(x[i] - s[s_index]) + abs(t[t_index] - s[s_index]))

    if mit <= x[i]:
        t_index = bisect.bisect_left(t,x[i])
        t_index -= 1
        if mas >= t[t_index]:
            s_index = bisect.bisect_left(s,t[t_index])
            ans = min(ans, abs(x[i] - t[t_index]) + abs(t[t_index] - s[s_index]))

    if mas >= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        if mit <= s[s_index]:
            t_index = bisect.bisect_left(t,s[s_index])
            t_index -= 1
            ans = min(ans, abs(x[i] - s[s_index]) + abs(t[t_index] - s[s_index]))

    if mat >= x[i]:
        t_index = bisect.bisect_left(t,x[i])
        if mis <= t[t_index]:
            s_index = bisect.bisect_left(s,t[t_index])
            s_index -= 1
            ans = min(ans, abs(x[i] - t[t_index]) + abs(t[t_index] - s[s_index]))

    if mas >= x[i] and mat >= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        t_index = bisect.bisect_left(t,x[i])
        ans = min(ans,abs(x[i] - max(s[s_index],t[t_index])))

    if mis <= x[i] and mit <= x[i]:
 　　　　s_index = bisect.bisect_left(s,x[i])
        s_index -= 1
        t_index = bisect.bisect_left(t,x[i])
        t_index -= 1
        ans = min(ans,abs(x[i] - min(s[s_index],t[t_index])))
    print(ans)

Buggy submission (verdict: Runtime Error):
for i in range(Q):
    x.append(int(input()))


import bisect

mas = max(s)
mis = min(s)
mat = max(t)
mit = min(t)

for i in range(Q):
    ans = 10**20

    if mis <= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        s_index -= 1
        if mat >= s[s_index]:
            t_index = bisect.bisect_left(t,s[s_index])
            ans = min(ans, abs(x[i] - s[s_index]) + abs(t[t_index] - s[s_index]))

    if mit <= x[i]:
        t_index = bisect.bisect_left(t,x[i])
        t_index -= 1
        if mas >= t[t_index]:
            s_index = bisect.bisect_left(s,t[t_index])
            ans = min(ans, abs(x[i] - t[t_index]) + abs(t[t_index] - s[s_index]))

    if mas >= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        if mit <= s[s_index]:
            t_index = bisect.bisect_left(t,s[s_index])
            t_index -= 1
            ans = min(ans, abs(x[i] - s[s_index]) + abs(t[t_index] - s[s_index]))

    if mat >= x[i]:
        t_index = bisect.bisect_left(t,x[i])
        if mis <= t[t_index]:
            s_index = bisect.bisect_left(s,t[t_index])
            s_index -= 1
            ans = min(ans, abs(x[i] - t[t_index]) + abs(t[t_index] - s[s_index]))

    if mas >= x[i] and mat >= x[i]:
        s_index = bisect.bisect_left(s,x[i])
        t_index = bisect.bisect_left(t,x[i])
        ans = min(ans,abs(x[i] - max(s[s_index],t[t_index])))

    if mis <= x[i] and mit <= x[i]:
 　　　　s_index = bisect.bisect_left(s,x[i])
        s_index -= 1
        t_index = bisect.bisect_left(t,x[i])
        t_index -= 1
        ans = min(ans,abs(x[i] - min(s[s_index],t[t_index])))
    print(ans)