Insertion and Deletion on segment tree or BIT tree?

299 Views Asked by At

I am having a hard time in solving a variant of Leetcode 307 Range Sum Query - Mutable and Leetcode 528: Random Pick with Index such that to design a data structure so that I can add elements and each element will has a weight and pop elements based proportional to weights. I am thinking about the optimal complexity of add and pop operation to be o(log n ) where n is the existing data size. However, I could find a good algorithm/data structure to solve it.

1

There are 1 best solutions below

0
Chad On

Splay Tree(or other Self-Balancing BST) would supports insertion/deletion at arbitrary position, range sum query, and also lower_bound/upper_bound in amortized O(lgn).