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):
with open("td") as inputfile:
    open0=inputfile.read()

def zengo(l,n):
    if n<l[0]:
        return 0,l[0]-n
    elif n>l[-1]:
        return n-l[-1],0
    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

near=lambda x,y:min(x,y) if x*y else max(x,y)

jn,tn,qn,*tmp=map(int,open0.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=[]
    if jb*tb:
        ans.append(max(jb,tb))
    if jf*tf:
        ans.append(max(jf,tf))
    jd=near(jb,jf)
    td=near(tb,tf)
    ans.append(jd+td+min(jd,td))
    print(min(ans))

def zengo(l,n):
    if n<l[0]:
        return 0,l[0]-n
    elif n>l[-1]:
        return n-l[-1],0
    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

near=lambda x,y:min(x,y) if x*y else max(x,y)

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=[]
    if jb*tb:
        ans.append(max(jb,tb))
    if jf*tf:
        ans.append(max(jf,tf))
    jd=near(jb,jf)
    td=near(tb,tf)
    ans.append(jd+td+min(jd,td))
    print(min(ans))

def zengo(l,n):
    if n<l[0]:
        return 0,l[0]-n
    elif n>l[-1]:
        return n-l[-1],0
    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

near=lambda x,y:min(x,y) if x*y else max(x,y)

jn,tn,qn,*tmp=map(int,open(0).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=[]
    if jb*tb:
        ans.append(max(jb,tb))
    if jf*tf:
        ans.append(max(jf,tf))
    jd=near(jb,jf)
    td=near(tb,tf)
    ans.append(jd+td+min(jd,td))
    print(min(ans))

def zengo(l,n):
    if n<l[0]:
        return 0,l[0]-n
    elif n>l[-1]:
        return n-l[-1],0
    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

near=lambda x,y:min(x,y) if x*y else max(x,y)

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=[]
    if jb*tb:
        ans.append(max(jb,tb))
    if jf*tf:
        ans.append(max(jf,tf))
    jd=near(jb,jf)
    td=near(tb,tf)
    ans.append(jd+td+min(jd,td))
    print(min(ans))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted