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):
N = list(map(int, input().split()))
#A = [[0] * (N[0]+N[1]+N[2])]*2
A = [[10**10+1] * 4 for i in range((N[0]+N[1]+N[2]))]
#print(A)
for i in range(N[0]):
    A[i][0]=int(input())
    A[i][1]=0

for i in range(N[0],N[0]+N[1]):
    A[i][0]=int(input())
    A[i][1]=1

r=0
for i in range(N[0]+N[1],N[0]+N[1]+N[2]):
    A[i][0]=int(input())
    A[i][1]=2
    A[i][2]=r
    r+=1

A=sorted(A)
#print(A)

L0=10**10+1
R0=10**10+1

L1=10**10+1
R1=10**10+1

j=0

for i in range(N[0]+N[1]+N[2]):
    k=i
    R0=10**10+1
    R1=10**10+1
    if(A[i][1]==0):L0=A[i][0]
    if(A[i][1]==1):L1=A[i][0]
    if(A[i][1]==2):
        for j in range(k,N[0]+N[1]+N[2]):
            if(A[j][1]==0):
                R0=A[j][0]
            if(A[j][1]==1):
                R1=A[j][0]
            if((R0!=(10**10+1))&(R1!=(10**10+1))):break
        LA01=abs(L0-A[i][0])
        RA01=abs(R0-A[i][0])
        LA11=abs(L1-A[i][0])
        RA11=abs(R1-A[i][0])
        LA02=LA01*2
        RA02=RA01*2
        LA12=LA11*2
        RA12=RA11*2

        #print(min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11)))
        A[i][3]=min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11))

A=sorted(A, key=lambda x: x[2])

for i in range(N[2]):
    print(A[i][3])



N = list(map(int, input().split()))
#A = [[0] * (N[0]+N[1]+N[2])]*2
A = [[10**11+1] * 4 for i in range((N[0]+N[1]+N[2]))]
#print(A)
for i in range(N[0]):
    A[i][0]=int(input())
    A[i][1]=0

for i in range(N[0],N[0]+N[1]):
    A[i][0]=int(input())
    A[i][1]=1

r=0
for i in range(N[0]+N[1],N[0]+N[1]+N[2]):
    A[i][0]=int(input())
    A[i][1]=2
    A[i][2]=r
    r+=1

A=sorted(A)
#print(A)

L0=10**11+1
R0=10**11+1

L1=10**11+1
R1=10**11+1

j=0

for i in range(N[0]+N[1]+N[2]):
    k=i
    R0=10**11+1
    R1=10**11+1
    if(A[i][1]==0):L0=A[i][0]
    if(A[i][1]==1):L1=A[i][0]
    if(A[i][1]==2):
        for j in range(k,N[0]+N[1]+N[2]):
            if(A[j][1]==0):
                R0=A[j][0]
            if(A[j][1]==1):
                R1=A[j][0]
            if((R0!=(10**11+1))&(R1!=(10**11+1))):break
        LA01=abs(L0-A[i][0])
        RA01=abs(R0-A[i][0])
        LA11=abs(L1-A[i][0])
        RA11=abs(R1-A[i][0])
        LA02=LA01*2
        RA02=RA01*2
        LA12=LA11*2
        RA12=RA11*2

        #print(min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11)))
        A[i][3]=min(max(LA01,LA11),LA01+RA12,RA01+LA12,max(RA01,RA11),max(LA01,LA11),LA02+RA11,RA02+LA11,max(RA01,RA11))

A=sorted(A, key=lambda x: x[2])

for i in range(N[2]):
    print(A[i][3])



import math
import bisect
A,B,Q = list(map(int,input().split(" ")))
Ss = [int(input())for _ in range(A)]
Ts = [int(input())for _ in range(B)]
Xs = [int(input())for _ in range(Q)]
Ss.sort()
Ts.sort()
ss = len(Ss)
ts = len(Ts)
for x in Xs:
    si = bisect.bisect_right(Ss,x)
    ti = bisect.bisect_right(Ts,x)

    cands = [[],[]]
    if si > 0:
        cands[0].append(Ss[si-1])
    if si < ss:
        cands[0].append(Ss[si])
    if ti > 0:
        cands[1].append(Ts[ti-1])
    if ti < ts:
        cands[1].append(Ts[ti])
 
    anss = []
    temp = 0
    for i in [0,1]:
        for t1 in cands[i]: 
            for t2 in cands[1-i]: 
                temp = abs(x-t1) +  abs(t2-t1)
                anss.append(temp)
    print(min(anss))

Verdicts: Wrong Answer

Wrong Answer

Accepted