"""Binary search helpers returning insertion positions. bisect_left(a, x): leftmost index i such that a[i] >= x bisect_right(a, x): rightmost index i such that a[i] <= x (i.e. insertion point after equals) Both assume a sorted ascending. """ from __future__ import annotations def bisect_left(a, x): lo, hi = 0, len(a) while lo < hi: mid = (lo + hi) // 2 if a[mid] < x: lo = mid + 1 else: hi = mid return lo def bisect_right(a, x): lo, hi = 0, len(a) while lo < hi: mid = (lo + hi) // 2 if a[mid] <= x: lo = mid + 1 else: hi = mid return lo def contains(a, x): i = bisect_left(a, x) return i < len(a) and a[i] == x