[CSES] Nested Ranges Count

Revision en1, by gsscala, 2025-01-03 06:02:55

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.

Code
Tags cses, pbds, set, range query

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English gsscala 2025-01-03 15:58:14 320
en1 English gsscala 2025-01-03 06:02:55 3977 Initial revision (published)