codeforces 540E Infinite Inversions

 

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 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 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.

Examples

input

Copy

2
4 2
1 4

output

Copy

4

input

Copy

3
1 6
3 4
2 5

output

Copy

15

Note

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

思路很简单: 虽然a,b 很大,但是中间有好多数字既没有用上,也没有改变相对位置关系,所以我们在离散化的时候就直接给这些点(线段) 离散化成一个点就可以了,点权为线段的长度。这样就是一个裸的的逆序对了。

注意数组大小呀呀呀呀!!!!!!!!!

代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int ,int > pii;
const int N =5e5+5;

pii a[N];
int n;
int dis[N];
int tmp[N];
int num[N];
ll cc[N];
ll C[N];

int lowbit(int x)
{
    return x&(-x);
}

void update(int x,ll val)
{
    for(;x>0;x-=lowbit(x)){
        C[x]+=val;
    }
}

ll query(int x)
{
    ll sum=0;
    for(;x<N;x+=lowbit(x)){
        sum+=C[x];
    }
    return sum;
}

int main()
{
    scanf("%d",&n);
    int tot=0;
    for(int i=1;i<=n;i++){
        scanf("%d %d",&a[i].first,&a[i].second);
        dis[++tot]=a[i].first; dis[++tot]=a[i].second;
    }
    sort(dis+1,dis+tot+1);
    tot=unique(dis+1,dis+tot+1)-(dis+1);
    dis[0]=0;
    int id=0;
    for(int i=1;i<=tot;i++){
        if(dis[i]==dis[i-1]+1){
            tmp[++id]=dis[i];
            cc[id]=1;
        }
        else{
            tmp[++id]=dis[i]-1;
            cc[id]=1ll*(dis[i]-dis[i-1]-1);
            tmp[++id]=dis[i];
            cc[id]=1;
        }
    }

    for(int i=1;i<=n;i++){
        int inde=lower_bound(tmp+1,tmp+id+1,a[i].first)-tmp;
        a[i].first=inde;
        inde=lower_bound(tmp+1,tmp+id+1,a[i].second)-tmp;
        a[i].second=inde;
    }
    for(int i=1;i<=id;i++){
        num[i]=i;
    }
    for(int i=1;i<=n;i++){
        swap(num[a[i].first],num[a[i].second]);
    }
    /*
    for(int i=1;i<=id;i++) cout<<num[i]<<" ";
    cout<<endl;
    for(int i=1;i<=id;i++) cout<<cc[num[i]]<<" ";
    cout<<endl;
    */

    ll Ans=0;
    for(int i=1;i<=id;i++){
        Ans+=cc[num[i]]*query(num[i]);
        update(num[i],cc[num[i]]);
        //cout<<"Ans "<<Ans<<endl;
    }
    printf("%lld\n",Ans);


    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值