I have a massive (~109) set of events characterized by a start and end time. Given a time, I want to find how many on those events were ongoing at that time.
What sort of data structure would be helpful in this situation? The operations I need to be fast are:
- Inserting new events, e.g.,
{start: 100000 milliseconds, end: 100010 milliseconds}. - Querying the number of concurrent events at a given time.
Update: Someone put a computational geometry flag on this, so I figure I should rephrase this in terms of computational geometry. I have a set of 1-dimensional intervals and I want to calculate how many of those intervals intersect with a given point. Insertion of new intervals must be fast.
You’re looking for an interval tree.
O(n log n), wherenis the number of intervalsO(m+log n), wheremis the number of query results andnis the number of intervalsO(n)