Data Structures19 sections · 729 units
Open in Course

Dynamic Frequency Queries

Multiset with BIT

BIT can implement a dynamic multiset supporting:

  • insert(x)\text{insert}(x): add element xx
  • delete(x)\text{delete}(x): remove one occurrence of xx
  • countLess(x)\text{countLess}(x): count elements <x< x
  • kth(k)\text{kth}(k): find kk-th smallest element

Implementation:

def insert(x):
    update(x, +1)

def delete(x):
    update(x, -1)

def countLess(x):
    return query(x - 1)

def kth(k):
    return findFirst(k)

With coordinate compression, this handles any value range in O(logn)O(\log n) per operation.