import bisect from typing import List def index(a: List[int], x: int) -> int: # 探索したい数値のindexを探索 'Locate the leftmost value exactly equal to x' i: int = bisect.bisect_left(a, x) if i != len(a) and a[i] == x: return i raise ValueError("value not found") def find_lt(a: List[int], x: int) -> int: # 探索したい数値未満のうち最大の数値を探索 'Find rightmost value less than x' i: int = bisect.bisect_left(a, x) if i: return a[i-1] raise ValueError("no item < x") def find_le(a: List[int], x: int) -> int: # 探索したい数値以下のうち最大の数値を探索 'Find rightmost value less than or equal to x' i: int = bisect.bisect_right(a, x) if i: return a[i-1] raise ValueError("no item <= x") def find_gt(a: List[int], x: int) -> int: # 探索したい数値を超えるもののうち最小の数値を探索 'Find leftmost value greater than x' i: int = bisect.bisect_right(a, x) if i != len(a): return a[i] raise ValueError("no item > x") def find_ge(a: List[int], x: int) -> int: # 探索したい数値以上のうち最小の数値を探索 'Find leftmost item greater than or equal to x' i: int = bisect.bisect_left(a, x) if i != len(a): return a[i] raise ValueError("no item >= x")