#P2448. 无尽的生命

无尽的生命

Description

Time flows on like this, never stopping day or night!

Ye Liangchen (叶良辰) believes his lifespan is infinite, and he improves every day.

On the first day of Ye Liangchen’s life, he has 11 point of ability. On the second day, he has 22 points. On day nn, he has nn points. That is, Si=iS_i = i.

However, the mischievous Xiao A (小 A) uses a time machine and tells him that on day xx and day yy, he can arbitrarily swap the ability values of any two days, i.e., SxSyS_x \leftrightarrow S_y.

Xiao A plays and plays, and finally gets bored.

Ye Liangchen: Xiao A, just you wait. I have 100100 ways to make you wish you were dead. Unless you can tell me within 11 second how many pairs are “abnormal pairs.” That is, in the final ability sequence, how many pairs of days x,yx, y with x<yx < y satisfy Sx>SyS_x > S_y?

Xiao A: I’m so scared.

So he comes to you.

Input Format

The first line contains an integer kk, indicating how many times Xiao A used the time machine.

The next kk lines each contain xi,yix_i, y_i, meaning SxiS_{x_i} and SyiS_{y_i} are swapped.

Output Format

Output a single line: the number of “abnormal pairs.”

2
4 2
1 4
4

Hint

Sample explanation

  • Initially it is 1,2,3,4,5,61,2,3,4,5,6\cdots.
  • Then it becomes 1,4,3,2,5,61,4,3,2,5,6\cdots.
  • Then it becomes 2,4,3,1,5,62,4,3,1,5,6\cdots.

The matching pairs are (1,4)(1,4), (2,3)(2,3), (2,4)(2,4), (3,4)(3,4).

Constraints

  • For 30%30\% of the testdata, xi,yi2×103x_i, y_i \le 2 \times 10^3.
  • For 70%70\% of the testdata, xi,yi105x_i, y_i \le 10^5.
  • For 100%100\% of the testdata, xi,yi2311x_i, y_i \le 2^{31} - 1, k105k \le 10^5.

Translated by ChatGPT 5