Suppose I have Q operations of following kind on a map storing key, value pairs. keys are comparable via operator '<'
Given l, r and a value x. Erase all the keys already present in the range [l, r) and insert pair (r,x).
My question is, Is the naive strategy of just erasing keys in the range and then inserting has a good ammortized complexity (may be O(log n) or something). As I have an intuitive feeling that if an operation does a lot of deletion, future operations will have less time complexity because of reduced size. Any ideas are appreciated.
More detailed description of "naive" algorithm : you simply binary search to find smallest keyl >= l and largest keyr < r and delete all the keys in the range [keyl, keyr] (overall this is O(log n + n log n) worst case)
Your described algorithm does not achieve the complexity that you want.
The problem is that when you erase a range and then insert new data, you can wind up with more or less data than you started. If this happens in the middle of a vector, up to half of your data needs to be moved.
Moving big blocks of data is one of the fastest things that you can do to it. But moving
O(n)data still takesO(n)time - just with good constants.We have many better solutions. Data structures that solve it fully include red-black trees, btrees, skiplists, and leveldb. There are many subtle tradeoffs. But all of them will be amortized time
O(log(n))for this operation.