Suki's Overlapping Park Visits
Suki, the curly black dog with the grey beard and white paws, loves the park because it is full of
friends. Today she sniffed her way through the park's visitor log, which lists n dog visits. Visit
i began at minute l_i and ended at minute r_i, and the dog was in the park during every moment
from l_i to r_i, both ends included. One of the visits is Suki's own.
Two visits met if both dogs were in the park at some common moment. A visit that ends at the exact minute another one begins counts as meeting it too: the two dogs had a quick sniff at the gate.
Suki wants to know how many pairs of visits met. Count the unordered pairs of different visits
{i, j} for which some moment t satisfies l_i ≤ t ≤ r_i and l_j ≤ t ≤ r_j.
Input
The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of visits.
Each of the next n lines contains two integers l_i and r_i (0 ≤ l_i ≤ r_i ≤ 10^9), the start
and end minute of visit i. Different visits may be identical.
Output
Print one integer: the number of pairs of visits that met. It can exceed 2^31, so use a 64-bit integer.
Notes
In the first example, the pairs that met are visits 1 and 2 (minutes 3 to 5), visits 2 and 3 (they touch at minute 7) and visits 3 and 4 (minutes 9 to 10). Visit 1 meets neither visit 3 nor visit 4, and visit 2 does not meet visit 4.
In the second example, every visit is a single minute: the two visits at minute 5 met, and so did each pair of the three visits at minute 8, for 1 + 3 = 4 pairs.