2818.Infinite Inversions

Time Limit: 1s Memory Limit: 256MB

There is an infinite sequence consisting of all positive integers in the increasing order: p={1,2,3,...}. We performed n swap operations with this sequence. A swap(a,b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i,j), that i \lt j and pi \gt pj.

Input Format(From the terminal/stdin)

The first line contains a single integer n (1 \le n \le 105)- the number of swap operations applied to the sequence.

Each of the next n lines contains two integers ai and bi (1 \le ai,bi \le 109, ai \neq bi)- the arguments of the swap operation.

Output Format(To the terminal/stdout)

Print a single integer - the number of inversions in the resulting sequence.

Sample Input 1

Copy
2
4 2
1 4
 \n
 · \n
 · \n

Sample Output 1

Copy
4
 \n

Sample Input 2

Copy
3
1 6
3 4
2 5
 \n
 · \n
 · \n
 · \n

Sample Output 2

Copy
15
  \n

Hints

In the first sample the sequence is being modified as follows: 2818_1.png. It has 4 inversions formed by index pairs (1,4), (2,3), (2,4) and (3,4).

Submit

请先 登录

© 2025 FAQs