def _bisect_points(a, x, lo=0, hi=None):
    """
    Insert point `p` in list `a`, and keep it sorted assuming `a` is sorted.
    If `p` is already in `a`, insert it to the left of the leftmost `p` if `no_repeat` is `False`.
    Optional args `lo` (default `0`) and `hi` (default `len(a)`) bound the
    slice of `a` to be searched.

    Source: https://github.com/python/cpython/blob/master/Lib/bisect.py
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    a_len = len(a)
    if hi is None:
        hi = a_len
    while lo < hi:
        mid = (lo + hi) // 2
        if a[mid][0] < x: lo = mid + 1
        else: hi = mid
    return lo