思路及代码参考:https://blog.csdn.net/u014800748/article/details/45420085

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 aand 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 swapoperations applied to the sequence.

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

Output

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

Examples

Input
2
4 2
1 4
Output
4
Input
3
1 6
3 4
2 5
Output
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).

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<cmath>
const int maxn=2e5+;
typedef long long ll;
using namespace std; ll s[maxn],sum[maxn];
int ss[maxn];
int a[maxn],b[maxn],pos[maxn];
int lowbit(int x)
{
return x&(-x);
}
int n;
void update(int pos,int ad)
{
while(pos<=maxn)
{
s[pos]+=ad;
pos+=lowbit(pos);
}
}
ll getnum(int pos)
{
ll res=;
while(pos>)
{
res+=s[pos];
pos-=lowbit(pos);
}
return res;
}
int main()
{
int n;
while (~scanf("%d", &n))
{
for (int i = ; i <= n; i++)
{
scanf("%d%d", &a[i], &b[i]);
ss[i] = a[i];
ss[i + n] = b[i];
pos[i] = i;
pos[i + n] = i + n;
}
sort(ss + , ss + * n + );
ss[] = ;
int cnt = ;
for (int i = ; i <= * n;i++)
if (i == || ss[i] != ss[i - ])
ss[++cnt] = ss[i];
sum[] = ;
for (int i = ; i <= cnt; i++)
sum[i] = sum[i - ] + ss[i] - ss[i - ] - ;
for (int i = ; i <= n; i++)
{
int aa = lower_bound(ss + , ss + cnt + , a[i]) - ss;
int bb = lower_bound(ss + ,ss + cnt + , b[i]) - ss;
swap(pos[aa], pos[bb]);
}
memset(s, , sizeof(s));
ll ans = ;
for (int i = cnt; i; i--)
{
ans += getnum(pos[i]);
ans += abs(sum[i]-sum[pos[i]]);
update(pos[i], );
}
printf("%lld\n", ans);
} return ;
}

最新文章

  1. [.net 面向对象编程基础] (1) 开篇
  2. Redis初级介绍
  3. webForm中的验证控件
  4. 混合式APP开发中中间件方案Rexsee
  5. Python学习(19)正则表达式
  6. 【转载】Asp.net Mvc 入门视频教程
  7. 腾讯QQ企业邮箱POP3/SMTP设置
  8. Android发展的一个重要方面Makefile分析
  9. CentOS环境搭建(JDK安装、mysql安装、hadoop安装等)
  10. 2017-07-04(sudo wc sort)
  11. 原创!!jquery简单tips和dialog
  12. Tornado介绍及自定义组件
  13. Easyui 实现点击不同树节点打开不同tab页展示不同datagrid表数据设计
  14. Asp.net 性能监控之压测接口“卡住” 分析
  15. python +百度语音识别+图灵对话
  16. 解决/bin/sh: 1: syntax error: &quot;(&quot; unexpected错误,以及更换bash仍然无法解决的问题
  17. maven windows 环境变量
  18. 【优化】如何检测移动端 CPU 以及内存占用率
  19. WebViewJavascriptBridge详细使用 iOS与H5交互的方案
  20. 【Java多线程】线程池学习

热门文章

  1. 【模式识别与机器学习】——4.3离散K-L变换
  2. 2020-04-14:mysql原子性和持久性怎么保证
  3. element-ui 格式化树形数组在table组件中展示(单元格合并)
  4. hdfs学习(三)
  5. Mybatis 和 Solon 在一起的升级版
  6. Ubuntu18.04 安装 Fabric &amp; 使用 Fabric 测试网络
  7. windows下Nacos集群搭建与nginx集成
  8. [PyTorch 学习笔记] 1.1 PyTorch 简介与安装
  9. (转)文件上传org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
  10. 基于官方Drone-CI 的alpine版本asia亚洲时区构建支持. Drone-CI based alpine Timezone Build