#abc355d. D - Intersecting Intervals

D - Intersecting Intervals

Score : 400400 points

问题陈述

给定 NN 个实数区间。第 ii(1iN)(1 \leq i \leq N) 区间是 [li,ri][l_i, r_i]。找出满足第 ii 个和第 jj 个区间相交的 (i,j)(i, j) 对的数量,其中 (1i<jN)(1 \leq i < j \leq N)

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given NN intervals of real numbers. The ii-th (1iN)(1 \leq i \leq N) interval is [li,ri][l_i, r_i]. Find the number of pairs (i,j)(1i<jN)(i, j)\,(1 \leq i < j \leq N) such that the ii-th and jj-th intervals intersect.

Constraints

  • 2N5×1052 \leq N \leq 5 \times 10^5
  • 0li<ri1090 \leq l_i < r_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

l1l_1 r1r_1

l2l_2 r2r_2

\vdots

lNl_N rNr_N

Output

Print the answer.

Sample Input 1

3
1 5
7 8
3 7

Sample Output 1

2

The given intervals are [1,5],[7,8],[3,7][1,5], [7,8], [3,7]. Among these, the 11-st and 33-rd intervals intersect, as well as the 22-nd and 33-rd intervals, so the answer is 22.

Sample Input 2

3
3 4
2 5
1 6

Sample Output 2

3

Sample Input 3

2
1 2
3 4

Sample Output 3

0