Range intersection is a simple, but non-trivial problem.
Its has been answered twice already:
Edit: It sounds like this solution is more or less an Interval Tree. A more complete implementation of an Interval Tree can be found here.
class TreeNode
{
public:
long pivot;
List leaves; //Any ranges that intersect the pivot
TreeNode left; //Tree nodes that fall to the left of the pivot
TreeNode right; //Tree nodes that fall to the right of the pivot
};
Prep O(n log n):
Search:
Traverse the tree until the pivot > TestRange.Start
2a. Add the leaves to your result.
Example:
Ranges:
Tree:
4
--------------+------------------
3 | 7
| 1-4 |
| 2-4 |
| 0-5 |
| 4-5 |
---------+------ --------+--------
2 | null 6 | null
-----+---- 2-3 ----+---- 3-7
null | null null | null
0-2 2-6
1-2