codeforces 301 E. Infinite Inversions

题目:

     

 
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Input

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.

Output

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

终于脑补出来了。。,总是看不懂E文题解

 

由于a[i],b[i]很大,但是我们可以分为两部分计算

part1:首先计算只交换的逆序数,这个可以使离散+BIT(树状数组)

part2:未出现的数怎么计算,其实为出现的也只跟 出现的数有关,因为未出现的数的相对顺序不变。

举个例子:交换后:1 9 3 6 7 2 4 8 5

               这里只有  1 3 8 没有交换,且出现

所以先是算9不满足,然后 6 也没有 7也没有(这里指计算为出现与出现部分

然后2:总共有 9 3 6 7四个 未出现的有3,那么怎么计算?

        2在的位置是6那么前面有6-2个大于他的数,减去之前出现的(交换出现 只与2相关)就是答案

可以发现 5 也是如此推算:9 3 6 7 8 --》9-5=4;4-3=1; 

 

如此云云

posted on 2015-05-04 00:35  forgot93  阅读(244)  评论(0编辑  收藏  举报

导航