Problem F. Infinite Inversions

时间限制 2000 ms   内存限制 256 MB

There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed nswap 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 < j and pi > pj.

输入数据

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

Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.

输出数据

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

样例

样例说明

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

提交

请先 登录

© 2025 FAQs Contact About