Data Structures19 sections · 729 units
Open in Course

Range Module Solution

Implement your solution

The trickiest part is handling partial overlaps during add and remove. For addRange:

1.1. Find all intervals overlapping [left,right][\text{left}, \text{right}]

2.2. Compute the union (extend left and right as needed)

3.3. Remove old intervals, insert the union For removeRange:

1.1. Find all intervals overlapping [left,right][\text{left}, \text{right}]

2.2. For each, trim or split as needed

3.3. Update the map

Time per operation: O(logn+k)O(\log n + k) where kk is affected intervals. Amortized O(logn)O(\log n) since each interval is added/removed once. Time: O(n)O(n). Space: O(n)O(n).