F.R.I.E.N.D.S

4

7 votes
Segment Trees, Advanced Data Structures, Data Structures
Problem

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

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 Xi    j    Yi and Xj   i   Yj.

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 Xi and Yi.

Output format

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

Constraints

1T1000
1N105
1XiYiN

The sum of N over all test cases does not exceed 2105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Friendship is only possible between (1,2), (1,4) and (3,6).

Editor Image

?