Codeforces Round 301 (Div. 2)
E
Infinite Inversions
Codeforces
2100
原题
← D→
翻译中…

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* < *j*$ and $*p*_{*i*} > *p*_{*j*}$.

翻译中…

Input

翻译中…

The first line contains a single integer $*n*$ ($1 ≤ *n* ≤ 10^{5}$) — the number of swap operations applied to the sequence.

翻译中…

Each of the next $*n*$ lines contains two integers $*a*_{*i*}$ and $*b*_{*i*}$ ($1 ≤ *a*_{*i*}, *b*_{*i*} ≤ 10^{9}$, $*a*_{*i*} ≠ *b*_{*i*}$) — the arguments of the swap operation.

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

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

翻译中…

样例

Input #1
2
4 2
1 4
Output #1
4
InputCopy OutputCopy
Input #2
3
1 6
3 4
2 5
Output #2
15
InputCopy OutputCopy
题目信息
题号
540E
时间限制
2 s
内存限制
256 MB
标签
二分
数据结构
模拟
排序
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。