def reverse_bisect_left(a, x, lo=0, hi=None):
    """
    same as python bisect.bisect_left but for
    lists with reversed order
    """
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo + hi) // 2
        if x > a[mid]:
            hi = mid
        else:
            lo = mid + 1
    return lo
