FIX:
Turned the indexed set into an indexed multiset, as there can be multiple equal R ranges. This can be done by changing the template to template <typename T> using indexed_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
(changing less
to less_equal
).
Problem Statement
Given n ranges, your task is to count for each range how many other ranges it contains and how many other ranges contain it.
My approach
Insert all ranges into a vector and sort it. Iterating through it, a given range contains all of those to its left with the same left boundary, since their right boundary will also be smaller than the current's. So we do a lowerbound in the vector and get all of them. The given range also contains all of those to its right (bigger left boundaries) whose right boundary is smaller than the current. So I just iterate from right to left in the sorted vector keeping an indexed set to get how many elements to my right have a smaller right boundary. I add these two results to an answer vector.
For the contained task, the approach is similar; just add the number of ranges with the same left boundary and bigger right boundary, also those to its left with bigger right boundary.
Is my approach wrong? I don't understand why my code nets wrong answer. The tests in which it occurs are too big for me to debug.
Auto comment: topic has been updated by gsscala (previous revision, new revision, compare).