C - Team Matching Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は、プログラミング合宿の運営スタッフとして、参加者のチーム分けを担当することになりました。

この合宿では、参加者同士でペアを組んで課題に取り組みます。N 人の参加者がおり、各参加者 i(1 \leq i \leq N)には、事前テストで測定された「アルゴリズム力」A_i と「実装力」B_i の2つのスコアが割り当てられています。

運営の方針として、能力の偏り方が同じ参加者同士でペアを組ませることが重要視されています。そこで、各参加者 i について D_i = A_i - B_i を「能力バランス値」と定義します。なお、D_i は負の値になることもあります。2人の参加者 i, j(i < j)について、

D_i = D_j

すなわち A_i - B_i = A_j - B_j が成り立つとき、この2人を「相性が良い」と呼びます。

高橋君は、チーム分けの参考にするため、「相性が良いペアが全部でいくつ存在するか」を知りたいと思っています。

N 人の参加者のアルゴリズム力と実装力が与えられたとき、相性が良いペアの総数を求めてください。ただし、ペアは順序を区別しません。すなわち、参加者 i と参加者 j の組と、参加者 j と参加者 i の組は同一とみなします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力はすべて整数
  • 答えは 2 \times 10^{10} 以下になることが保証される

入力

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • 1 行目には、参加者の人数を表す整数 N が与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、参加者 i のアルゴリズム力 A_i と実装力 B_i が、スペース区切りで与えられる。

出力

相性が良いペアの総数を1行で出力してください。


入力例 1

4
3 1
5 3
2 4
4 2

出力例 1

3

入力例 2

6
10 5
8 3
12 7
6 2
9 4
7 3

出力例 2

7

入力例 3

10
1000000000 1
1 1000000000
500000000 500000000
100 100
200 200
300 300
999999999 1000000000
1000000000 999999999
50 51
150 151

出力例 3

9

Score : 333 pts

Problem Statement

Takahashi has been assigned to handle team formation for participants as a staff member organizing a programming camp.

In this camp, participants work on tasks in pairs. There are N participants, and each participant i (1 \leq i \leq N) has been assigned two scores measured by a preliminary test: "algorithm skill" A_i and "implementation skill" B_i.

As an organizational policy, it is considered important to pair participants who have the same type of skill bias. Therefore, for each participant i, we define D_i = A_i - B_i as their "skill balance value". Note that D_i can be negative. For two participants i, j (i < j), if

D_i = D_j

that is, A_i - B_i = A_j - B_j holds, we call these two participants "compatible".

Takahashi wants to know "how many compatible pairs exist in total" as a reference for team formation.

Given the algorithm skill and implementation skill of N participants, find the total number of compatible pairs. Note that pairs are unordered. That is, the pair of participant i and participant j is considered the same as the pair of participant j and participant i.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All inputs are integers
  • It is guaranteed that the answer is at most 2 \times 10^{10}

Input

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • The first line contains an integer N representing the number of participants.
  • The i-th line (1 \leq i \leq N) of the following N lines contains participant i's algorithm skill A_i and implementation skill B_i, separated by a space.

Output

Output the total number of compatible pairs in a single line.


Sample Input 1

4
3 1
5 3
2 4
4 2

Sample Output 1

3

Sample Input 2

6
10 5
8 3
12 7
6 2
9 4
7 3

Sample Output 2

7

Sample Input 3

10
1000000000 1
1 1000000000
500000000 500000000
100 100
200 200
300 300
999999999 1000000000
1000000000 999999999
50 51
150 151

Sample Output 3

9