I have std::set<std::pair<int, int>> intervals which it's values are some intervals, it is somthing similar to this:
{ 0, 5 },
{ 5, 8 },
{ 8, 10 }
and I have a number x can I find in which interval it is using lower_bound or upper_bound?
I need a solution with O(log(N)) complexity.
Because I remember that I saw someone doing it someway like this:
int x;
std::set<std::pair<int, int>> intervals;
cin >> x;
auto ans = intervals.upper_bound({ x, INF })
and I don't remember what was the value of INF
Note: the intervals aren't intersecting.
First of all, if you want to have a set of continuous ranges, you should simply represent them as a set that keeps only the beginning of each range.
If you really want to use a structure that keeps both ends of a range, the following will allow you to search in
O(log(N)). In the case of keeping both ends, it is possible to express other than continuous ranges set, so this example intentionally omits [6, 8). If the structure keeps both ends, it is also possible to express overlapping ranges set. In that case, we would have to search for more than one range. I left that example out.stdout is here.