F.R.I.E.N.D.S
Practice
4 (7 votes)
Segment trees
Advanced data structures
Data structures
Problem
69% Success 890 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There are $$N$$ People, $$i$$-th person wants to be friend with all the person between $$[X_i, Y_i]$$.

A friendship is possible between two distinct person if and only if both of them wants to be friends with each other i.e, $$i$$-th and $$j$$-th person can be friend only if $$X_i$$  $$\leq$$  $$j$$  $$\leq$$  $$Y_i$$ and $$X_j$$  $$\leq$$ $$ i$$ $$\leq$$  $$Y_j $$.

Print the total number of possible friendship.

 Input format

  • The first line contains \(T\) denoting the number of test cases.
  • The first line of each test case contains integers \(N\), denoting the number of people.
  • The next $$N$$ lines contain $$X_i$$ and $$Y_i$$.

Output format

For each test case print the total number of possible friendship in a separate line.

Constraints

\(1 \leq T \leq 1000\)
\(1 \leq N \leq 10^5\)
\(1 \leq X_i \leq Y_i \leq N\)

The sum of \(N\) over all test cases does not exceed \(2 \cdot 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
17 votes
Tags:
Segment TreesDFSAdvanced Data StructuresData StructuresPointerMath
Points:50
2 votes
Tags:
Advanced Data StructuresSegment TreesData Structures
Points:50
26 votes
Tags:
Data StructuresHardOpenSegment TreesTrees