from bisect import bisect_left, bisect_right def count_lengths(sorted_lengths, threshold): """Return counts at or above the threshold, then strictly above it.""" n = len(sorted_lengths) return n - bisect_left(sorted_lengths, threshold), n - bisect_right(sorted_lengths, threshold) if __name__ == "__main__": lengths = [3, 5, 5, 9] # 昇順に並べる at_least, above = count_lengths(lengths, 5) print("5cm以上", at_least, "本") print("5cmを超える", above, "本")