Problem p03557 (python)

Fixed submission:
N = int(input())
A = list(int(i) for i in input().split())
B = list(int(i) for i in input().split())
C = list(int(i) for i in input().split())
A.sort()
B.sort()
C.sort()
sum = 0
for j in range(N):
    low = low2 = 0
    high = high2 = N
    b = B[j]
    mid = mid2 = 0
    #print(b)
    while(high>low):
        mid = (high + low)//2
        if b > A[mid]:
            low = mid +1
        else:
            high = mid
    acount = low
    while (high2>low2):
        mid2 = (high2 + low2)//2
        if b < C[mid2]:
            high2 = mid2
        else:
            low2 = mid2 + 1
    ccount=N-low2
    sum += acount*ccount
print(sum)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(int(i) for i in input().split())
B = list(int(i) for i in input().split())
C = list(int(i) for i in input().split())
A.sort()
B.sort()
C.sort()
sum = 0
for j in range(N):
    low = low2 = 0
    high = high2 = N
    b = B[j]
    #print(b)
    while(high>low):
        mid = (high + low)//2
        if b > A[mid]:
            low = mid +1
        else:
            high = mid
    acount = mid
    while (high2>low2):
        mid2 = (high + low)//2
        if b < C[mid2]:
            high2 = mid2
        else:
            low2 = mid2 + 1
    ccount=N-mid2
    sum += acount*ccount
print(sum)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(int(i) for i in input().split())
B = list(int(i) for i in input().split())
C = list(int(i) for i in input().split())
A.sort()
B.sort()
C.sort()
sum = 0
for j in range(N):
    low = low2 = 0
    high = high2 = N
    b = B[j]
    #print(b)
    while(high>low):
        mid = (high + low)//2
        if b > A[mid]:
            low = mid +1
        else:
            high = mid
    acount = mid
    while (high2>low2):
        mid2 = (high + low)//2
        if b < C[mid2]:
            high2 = mid2
        else:
            low = mid2 + 1
    ccount=N-mid2
    sum += acount*ccount
print(sum)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(int(i) for i in input().split())
B = list(int(i) for i in input().split())
C = list(int(i) for i in input().split())
A.sort()
B.sort()
C.sort()
sum = 0
#print(A,B,C)
for j in range(N):
    low = low2 = 0
    high = high2 = N - 1
    mid =mid2 = (N - 1) // 2
    b = B[j]
    if A[0] > b:
        acount = 0
    else:
        while high > low:
            if A[mid] < b:
                if low == mid:
                    if A[mid+1] < b:
                            low += 1
                    if A[mid+1] >= b:
                            high += - 1
                else:
                    low = mid
            if A[mid] >= b:
                high = mid-1
            mid = (high + low) // 2
        acount = mid + 1


    while high2 > low2:
        if C[mid2] > b:
            high2 = mid2
        if C[mid2] <= b:
            if low2 == mid2:
                if C[low2+1] > b:
                    low2 += 1
                if C[low2+1] <=b:
                    high2 -= 1
            else:
                low2 = mid2 + 1
        mid2 = (high2 + low2)//2
    ccount = N-mid2
    #print(acount,ccount)
    sum += acount*ccount
print(sum)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A2=sorted(A)
B2=sorted(B)
C2=sorted(C)
ans = 0
sum = 0
t =0
b = 0
for i in range(N):
    a = A2[i]
    low = 0
    high = N-1
    while True:
        t = (low + high) // 2
        if B2[t] <= a:
            if t == N-1:
                b = False
                break
            elif B2[t] <= a <B2[t+1]:

                b = B2[t+1]
                t = t + 1
                break
            else:
                low = t+1
                continue
        elif B2[t] > a:
            if t == 0:
                b = B2[0]
                break
            elif B2[t] > a >= B2[t-1]:
                b = B2[t]
                break
            else:
                high = t - 1
                continue
    if b != False:
        for j in range(t,N):
            b = B2[j]
            low = 0
            high = N - 1
            while True:
                print(a, b)
                t = (low + high) // 2
                if C2[t] <= b:
                    if C2[t] <= b < C2[t + 1]:
                        c = C2[t + 1]
                        ans = N - t - 1
                        break
                    else:
                        low = t + 1
                        continue
                elif C2[t] > b:
                    if t == 0:
                        if C2[t] > b:
                            ans = N
                            break
                        else:
                            ans = 0
                            break
                    elif C2[t] > b >= C2[t - 1]:
                        c = C2[t]
                        ans = N - t
                        break
                    else:
                        high = t - 1
                        continue
                print("ans={}".format(ans))
            sum += ans

print(sum)