B - Trapezoid Sum Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

何も書かれていない黒板があります。 高橋くんは N 回の操作を行い、黒板に整数を書きます。

i 回目の操作では、 A_i 以上 B_i 以下の整数すべてを 1 個ずつ、合計 B_i - A_i + 1 個の整数を書きます。

N 回の操作を終えたときの、黒板に書かれた整数の合計を求めてください。

制約

  • 入力はすべて整数
  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq B_i \leq 10^6

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 B_1
\vdots
A_N B_N

出力

N 回の操作を終えたときの、黒板に書かれた整数の合計を出力せよ。


入力例 1

2
1 3
3 5

出力例 1

18

1 回目の操作では、黒板に 1, 2, 3 を書きます。

2 回目の操作では、黒板に 3, 4, 5 を書きます。

黒板に書かれた整数の合計は 1+2+3+3+4+5=18 です。


入力例 2

3
11 13
17 47
359 44683

出力例 2

998244353

入力例 3

1
1 1000000

出力例 3

500000500000

Score : 200 points

Problem Statement

We have a blackboard with nothing written on it. Takahashi will do N operations to write integers on it.

In the i-th operation, he will write each integer from A_i through B_i once, for a total of B_i - A_i + 1 integers.

Find the sum of the integers written on the blackboard after the N operations.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq B_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
\vdots
A_N B_N

Output

Print the sum of the integers written on the blackboard after the N operations.


Sample Input 1

2
1 3
3 5

Sample Output 1

18

In the 1-st operation, he will write 1, 2, and 3 on the blackboard.

In the 2-nd operation, he will write 3, 4, and 5 on the blackboard.

The sum of the integers written is 1+2+3+3+4+5=18.


Sample Input 2

3
11 13
17 47
359 44683

Sample Output 2

998244353

Sample Input 3

1
1 1000000

Sample Output 3

500000500000