1 介绍
python3
中有序列表SortedList
数据结构支持下标取值,然后查找、删除、插入的时间复杂度都是
O
(
l
o
g
N
)
O(logN)
O(logN)。
它在sortedcontainers
,经常这样导入from sortedcontainers import SortedList
。
2 基本操作
(1)初始化:sl = SortedList()
。
(2)增加元素x
:sl.add(x)
。
(3)删除元素x
:sl.remove(x)
。
(4)查找元素值大于等于x
的第1个元素的下标idx
:idx = bisect.bisect_left(sl, x)
。
(5)访问下标为0
的元素:sl[0]
。