#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 200100 ;
int a[ N] , b[ N] , num[ N] ;
int m, n, tn, tmp[ N] , T[ N] ;
LL ans;
int find( int x) { int ret = 0 ; for ( ; x; x - = ( x & - x) ) ret + = T[ x] ; return ret; }
void plus( int x) { for ( ; x <= n; x + = ( x & - x) ) T[ x] ++ ; }
int main( )
{
scanf ( "%d" , & m) ;
for ( int i = 1 ; i <= m; i++ ) scanf ( "%d%d" , & a[ i] , & b[ i] ) , tmp[ ++ tn] = a[ i] , tmp[ ++ tn] = b[ i] ;
sort( tmp + 1 , tmp + 1 + tn) ;
tn = unique( tmp + 1 , tmp + 1 + tn) - tmp - 1 ;
n = tn;
for ( int i = 1 ; i <= n; i++ ) num[ i] = i;
for ( int i = 1 ; i <= m; i++ )
{
a[ i] = lower_bound( tmp + 1 , tmp + 1 + tn, a[ i] ) - tmp;
b[ i] = lower_bound( tmp + 1 , tmp + 1 + tn, b[ i] ) - tmp;
swap( num[ a[ i] ] , num[ b[ i] ] ) ;
}
ans = 0 ;
for ( int i = n; i >= 1 ; i-- )
{
ans = ans + find( num[ i] - 1 ) ;
plus( num[ i] ) ;
}
for ( int i = 1 ; i <= n; i++ )
if ( num[ i] ! = i)
{
if ( num[ i] > i)
{
ans = ans + ( tmp[ num[ i] ] - tmp[ i] ) - ( num[ i] - i) ;
} else
{
ans = ans + ( tmp[ i] - tmp[ num[ i] ] ) - ( i - num[ i] ) ;
}
}
printf ( "%I64d\n " , ans) ;
return 0 ;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIExMOwoKY29uc3QgaW50IE4gPSAyMDAxMDA7CmludCBhW05dLCBiW05dLCBudW1bTl07CmludCBtLCBuLCB0biwgdG1wW05dLCBUW05dOwpMTCBhbnM7Cg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CWFucyA9IGFucyArICh0bXBbaV0gLSB0bXBbbnVtW2ldXSkgLSAoaSAtIG51bVtpXSk7CgkJCX0KCQl9CgoJcHJpbnRmKCIlSTY0ZFxuIiwgYW5zKTsKCglyZXR1cm4gMDsKfQo=
compilation info
prog.cpp: In function 'int main()':
prog.cpp:37:3: error: reference to 'plus' is ambiguous
plus(num[i]);
^
prog.cpp:13:6: note: candidates are: void plus(int)
void plus(int x) {for (; x <= n; x += (x & -x)) T[x]++;}
^
In file included from /usr/include/c++/5/string:48:0,
from /usr/include/c++/5/random:40,
from /usr/include/c++/5/bits/stl_algo.h:66,
from /usr/include/c++/5/algorithm:62,
from prog.cpp:3:
/usr/include/c++/5/bits/stl_function.h:147:12: note: template<class _Tp> struct std::plus
struct plus;
^
stdout