Problem p03112 (python)

Fixed submission:
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        dist1[0]=s[0]-x[k]
        dist1[1]=s[0]-x[k]
        sp1[0]=s[0]
        sp1[1]=s[0]        
    elif p1==a:
        dist1[0]=x[k]-s[a-1]
        dist1[1]=x[k]-s[a-1]
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
    else:
        dist1[0]=x[k]-s[p1-1]
        dist1[1]=s[p1]-x[k]
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]
 
    p11=bisect.bisect_left(t,sp1[0])

    if p11==0:
        dist2[0]=t[0]-sp1[0]
        dist2[1]=t[0]-sp1[0]
    elif p11==b:
        dist2[0]=sp1[0]-t[b-1]
        dist2[1]=sp1[0]-t[b-1]
    else:
        dist2[0]=sp1[0]-t[p11-1]
        dist2[1]=t[p11]-sp1[0]
        
    p11=bisect.bisect_left(t,sp1[1])

    if p11==0:
        dist2[2]=t[0]-sp1[1]
        dist2[3]=t[0]-sp1[1]
    elif p11==b:
        dist2[2]=sp1[1]-t[b-1]
        dist2[3]=sp1[1]-t[b-1]
    else:
        dist2[2]=sp1[1]-t[p11-1]
        dist2[3]=t[p11]-sp1[1]
        
    dist[0]=dist1[0]+dist2[0]
    dist[1]=dist1[0]+dist2[1]
    dist[2]=dist1[1]+dist2[2]
    dist[3]=dist1[1]+dist2[3]
                

    p1=bisect.bisect_left(t,x[k])

    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
        dist1[0]=t[0]-x[k]
        dist1[1]=t[0]-x[k]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
        dist1[0]=x[k]-t[b-1]
        dist1[1]=x[k]-t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]
        dist1[0]=x[k]-t[p1-1]
        dist1[1]=t[p1]-x[k]
        
    p11=bisect.bisect_left(s,sp1[0])

    if p11==0:
        dist2[0]=s[0]-sp1[0]
        dist2[1]=s[0]-sp1[0]
    elif p11==a:
        dist2[0]=sp1[0]-s[a-1]
        dist2[1]=sp1[0]-s[a-1]
    else:
        dist2[0]=sp1[0]-s[p11-1]
        dist2[1]=s[p11]-sp1[0]
    
    p11=bisect.bisect_left(s,sp1[1])

    if p11==0:
        dist2[2]=s[0]-sp1[1]
        dist2[3]=s[0]-sp1[1]       
    elif p11==a:
        dist2[2]=sp1[1]-s[a-1]
        dist2[3]=sp1[1]-s[a-1]
    else:
        dist2[2]=sp1[1]-s[p11-1]
        dist2[3]=s[p11]-sp1[1]

    dist[4]=dist1[0]+dist2[0]
    dist[5]=dist1[0]+dist2[1]
    dist[6]=dist1[1]+dist2[2]
    dist[7]=dist1[1]+dist2[3]
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Buggy submission (verdict: Runtime Error):
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        dist1[0]=s[0]-x[k]
        dist1[1]=s[0]-x[k]
    elif p1==a:
        dist1[0]=x[k]-s[a-1]
        dist1[1]=x[k]-s[a-1]
    else:
        dist1[0]=x[k]-s[p1-1]
        dist1[1]=s[p1]-x[k]
 
    p11=bisect.bisect_left(t,sp1[0])

    if p11==0:
        dist2[0]=t[0]-sp1[0]
        dist2[1]=t[0]-sp1[0]
    elif p11==b:
        dist2[0]=sp1[0]-t[b-1]
        dist2[1]=sp1[0]-t[b-1]
    else:
        dist2[0]=sp1[0]-t[p11-1]
        dist2[1]=t[p11]-sp1[0]
        
    p11=bisect.bisect_left(t,sp1[1])

    if p11==0:
        dist2[2]=t[0]-sp1[1]
        dist2[3]=t[0]-sp1[1]
    elif p11==b:
        dist2[2]=sp1[1]-t[b-1]
        dist2[3]=sp1[1]-t[b-1]
    else:
        dist2[2]=sp1[1]-t[p11-1]
        dist2[3]=t[p11]-sp1[1]
        
    dist[0]=dist1[0]+dist2[0]
    dist[1]=dist1[0]+dist2[1]
    dist[2]=dist1[1]+dist2[2]
    dist[3]=dist1[1]+dist2[3]
                

    p1=bisect.bisect_left(t,x[k])

    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Buggy submission (verdict: Runtime Error):
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        sp1[0]=s[0]
        sp1[1]=s[0]
        dist1[0]=sp1[0]-x[k]
    elif p1==a:
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
        dist1[1]=x[k]-sp1[0]
    else:
        sp1[0]=s[p1-1]
        dist1[1]=x[k]-sp1[0]
        sp1[1]=s[p1]
        dist1[0]=sp1[0]-x[k]
    for i in range(2):
        p11=bisect.bisect_left(t,sp1[i])

        if p11==0:
            tp1[0+2*i]=t[0]
            tp1[1+2*i]=t[0]
        elif p11==b:
            tp1[0+2*i]=t[b-1]
            tp1[1+2*i]=t[b-1]
        else:
            tp1[0+2*i]=t[p11-1]
            tp1[1+2*i]=t[p11]

    dist[0]=dist1[0]+abs(sp1[0]-tp1[0])
    dist[1]=dist1[0]+abs(sp1[0]-tp1[1])
    dist[2]=dist1[1]+abs(sp1[1]-tp1[2])
    dist[3]=dist1[1]+abs(sp1[1]-tp1[3])
                

    p1=bisect.bisect_left(t,x[k])

    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Buggy submission (verdict: Runtime Error):
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
sp1=[0 for i in range(4)]
tp1=[0 for i in range(4)]

import bisect
for k in range(q):
    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        sp1[0]=s[0]
        sp1[1]=s[0]
    elif p1==a:
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
    else:
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]

    for i in range(2):
        p11=bisect.bisect_left(t,sp1[i])

        if p11==0:
            tp1[0+2*i]=t[0]
            tp1[1+2*i]=t[0]
        elif p11==b:
            tp1[0+2*i]=t[b-1]
            tp1[1+2*i]=t[b-1]
        else:
            tp1[0+2*i]=t[p11-1]
            tp1[1+2*i]=t[p11]

    dist[0]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[1]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[2]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[3]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])

    p1=bisect.bisect_left(t,x[k])
    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Buggy submission (verdict: Runtime Error):
a,b,q=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
sp1=[0 for i in range(2)]
tp1=[0 for i in range(4)]

import bisect
for k in range(q):
    p1=bisect.bisect_left(s,x[k])

    if p1==0:
        sp1[0]=s[0]
        sp1[1]=s[0]
    elif p1==a:
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
    else:
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]

    for i in range(2):
        p11=bisect.bisect_left(t,sp1[i])

        if p11==0:
            tp1[0+2*i]=t[0]
            tp1[1+2*i]=t[0]
        elif p11==b:
            tp1[0+2*i]=t[b-1]
            tp1[1+2*i]=t[b-1]
        else:
            tp1[0+2*i]=t[p11-1]
            tp1[1+2*i]=t[p11]

    dist[0]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[1]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[2]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[3]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])

    p1=bisect.bisect_left(t,x[k])
    if p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
    else:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]

    for i in range(2):
        p11=bisect.bisect_left(s,sp1[i])

        if p11==0:
            tp1[0+2*i]=s[0]
            tp1[1+2*i]=s[0]
        elif p11==a:
            tp1[0+2*i]=s[a-1]
            tp1[1+2*i]=s[a-1]
        else:
            tp1[0+2*i]=s[p11-1]
            tp1[1+2*i]=s[p11]

    dist[4]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[0])
    dist[5]=abs(x[k]-sp1[0])+abs(sp1[0]-tp1[1])
    dist[6]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[2])
    dist[7]=abs(x[k]-sp1[1])+abs(sp1[1]-tp1[3])
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)