PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000165. Đếm cặp đoạn giao nhau (Intersecting Intervals)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm cặp đoạn giao nhau (Intersecting Intervals)

Intersecting Intervals

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given NN closed real intervals. Interval ii is [li,ri][l_i,r_i] with li<ril_i<r_i. Two intervals intersect if they share at least one real point, including the case when they only touch at an endpoint. Count pairs (i,j)(i,j) satisfying 1≤i<j≤N1\le i<j\le N whose intervals intersect.

Input

  • The first line contains integer NN.
  • Each of the next NN lines contains integers lil_i and rir_i, the endpoints of interval ii.

Output

Print one integer: the number of pairs of intervals sharing at least one point.

Subtasks

  • Subtask 1 (20 points): 2≤N≤502\le N\le 50; 0≤li<ri≤1000\le l_i<r_i\le 100 for all ii.
  • Subtask 2 (30 points): 2≤N≤50002\le N\le 5000; 0≤li<ri≤10000000000\le l_i<r_i\le 1000000000 for all ii.
  • Subtask 3 (50 points): 2≤N≤5000002\le N\le 500000; 0≤li<ri≤10000000000\le l_i<r_i\le 1000000000 for all ii.

Examples

Input

3
1 5
7 8
3 7

Output

2

Explanation

The three intervals are [1,5][1,5], [7,8][7,8] and [3,7][3,7]. Pair (1,2)(1,2) is disjoint since 5<75<7. Pair (1,3)(1,3) intersects on [3,5][3,5]. Pair (2,3)(2,3) shares the endpoint 77, which counts because both intervals are closed. Thus there are two intersecting pairs; print 2.