Problem p03557 (python)

Fixed submission:
import sys

readline = sys.stdin.buffer.readline


def binary_search(A, n, p):
    if p == "+":
        n += 0.5
    else:
        n -= 0.5

    left = 0
    right = len(A) - 1

    if n < A[0]:
        return 0
    else:
        while left <= right:
            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            else:
                right = mid - 1

        return left


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

    A, B, C = sorted(A), sorted(B), sorted(C)

    cnt = 0
    for i, b in enumerate(B):
        ab = binary_search(A, b, "-")
        bc = N - binary_search(C, b, "+")
        cnt += ab * bc

    print(cnt)


main()

Buggy submission (verdict: Runtime Error):
import sys

import numpy as np

readline = sys.stdin.buffer.readline


# 同じ数値が複数あるとおかしくなる
def binary_search(A, n):
    n -= 1
    left = 0
    right = len(A)-1

    if n < A[0]:
        return -1
    else:
        while left < right:
            # if right - left == 1:
            #     break

            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            elif n < A[mid]:
                right = mid - 1
            else:
                left = len(A) - mid//2
                break
        if A[left] > n:
            left -= 1

        return left


def main():
    N = int(readline())
    A = np.array(list(map(int, readline().split())))
    B = np.array(list(map(int, readline().split())))
    C = np.array(list(map(int, readline().split())))

    A, B, C = np.sort(A), np.sort(B), np.sort(C)

    aa = np.zeros([N], dtype=np.int32)
    for i, b in enumerate(B):
        aa[i] = binary_search(A, b) + 1

    bb = np.cumsum(aa)

    cnt = 0
    for i, c in enumerate(C):
        m = binary_search(B, c)
        if m != -1:
            cnt += bb[m]

    print(cnt)


main()

Buggy submission (verdict: Runtime Error):
import sys

import numpy as np

readline = sys.stdin.buffer.readline


# 同じ数値が複数あるとおかしくなる
def binary_search(A, n):
    n -= 1
    left = 0
    right = len(A)-1

    if n < A[0]:
        return -1
    else:
        while left < right:
            # if right - left == 1:
            #     break

            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            elif n < A[mid]:
                right = mid - 1
            else:
                left = len(A) - mid//2
                break
        if A[left] > n:
            left -= 1

        return left


def main():
    N = int(readline())
    A = np.array(list(map(int, readline().split())))
    B = np.array(list(map(int, readline().split())))
    C = np.array(list(map(int, readline().split())))

    A, B, C = np.sort(A), np.sort(B), np.sort(C)

    aa = np.zeros([N], dtype=np.int32)
    for i, b in enumerate(B):
        aa[i] = binary_search(A, b) + 1

    bb = np.cumsum(aa)

    cnt = 0
    for i, c in enumerate(C):
        m = binary_search(B, c)
        if m != -1:
            cnt += bb[m]

    print(cnt)


main()

Buggy submission (verdict: Runtime Error):
import sys

import numpy as np

readline = sys.stdin.buffer.readline


# 同じ数値が複数あるとおかしくなる
def binary_search(A, n):
    n -= 1
    left = 0
    right = len(A)-1

    if n < A[0]:
        return -1
    else:
        while left < right:
            # if right - left == 1:
            #     break

            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            elif n < A[mid]:
                right = mid - 1
            else:
                left = len(A) - mid + left
                break
        if A[left] > n:
            left -= 1

        return left


def main():
    N = int(readline())
    A = np.array(list(map(int, readline().split())))
    B = np.array(list(map(int, readline().split())))
    C = np.array(list(map(int, readline().split())))

    A, B, C = np.sort(A), np.sort(B), np.sort(C)

    aa = np.zeros([N], dtype=np.int32)
    for i, b in enumerate(B):
        aa[i] = binary_search(A, b) + 1

    bb = np.cumsum(aa)

    cnt = 0
    for i, c in enumerate(C):
        m = binary_search(B, c)
        if m != -1:
            cnt += bb[m]

    print(cnt)


main()