extends Node


func insort_right(a: Array, x, lo: int = 0, hi=null):
	lo = bisect_right(a, x, lo, hi)
	a.insert(lo, x)

func bisect_right(a: Array, x, lo: int = 0, hi=null):
	if lo < 0:
		return
	if hi == null:
		hi = a.size()
	var mid: int = 0
	while lo < hi:
		mid = (lo + hi) / 2
		if x < a[mid]:
			hi = mid
		else:
			lo = mid + 1
	return lo

func insort_left(a: Array, x, lo:int = 0, hi=null):
	lo = bisect_left(a, x, lo, hi)
	a.insert(lo, x)

func bisect_left(a: Array, x, lo:int = 0, hi=null):
	if lo < 0:
		return
	if hi == null:
		hi = a.size()
	var mid: int = 0
	while lo < hi:
		mid = (lo + hi) / 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

func bisect(a: Array, x, lo: int = 0, hi=null):
	return bisect_right(a, x, lo, hi)

func insort(a: Array, x, lo: int = 0, hi=null):
	return insort_right(a, x, lo, hi)
