思路及代码参考: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 ;
}

Infinite Inversions(树状数组+离散化)的更多相关文章

  1. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  2. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...

  6. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  7. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  8. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

  9. HDU5196--DZY Loves Inversions 树状数组 逆序数

    题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...

随机推荐

  1. [vue] computed 和 method

    计算属性 计算属性只有在它的相关依赖发生改变时才会重新取值 Method method每次渲染的时候都会被执行 举一个栗子 <template>...<div>  <p& ...

  2. 用Spark进行实时流计算

    Spark Streaming VS Structured Streaming Spark Streaming是Spark最初的流处理框架,使用了微批的形式来进行流处理. 提供了基于RDDs的Dstr ...

  3. Android——对话框的全部内容。(课堂总结)

    前面的总结是写过对话框的,但是那只是冰山一角,简单的创建和使用罢了. 今天具体讲下AlertDialog. 首先对话框不需要在布局里写,在活动里new出来的. AlertDialog.Builder ...

  4. PHP 之 Composer 新手入门指南

    自2012年3月1日发布以来,Composer因提供了PHP迫切需要的东西:依赖项管理而广受欢迎.实际上,Composer是将所有第三方软件(例如CSS框架,jQuery插件等)引入你的项目的一种方法 ...

  5. 有哪些开源的 Python 库让你相见恨晚?

    Arrow 我们知道 Python 已经内置了好几个处理时间相关的库,但是对于时间以及时区间的转换并不清晰,操作起来略繁琐,而 Arrow 可以弥补这个问题,它提供了更友好的方法,方便我们对时间,日期 ...

  6. C#设计模式之17-中介者模式

    中介者模式(Mediator Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/419 访问. 中介者模式 ...

  7. C#LeetCode刷题之#88-合并两个有序数组(Merge Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3686 访问. 给定两个有序整数数组 nums1 和 nums2, ...

  8. 免费深度学习GPU,Google Yes!

    深度学习越加火热,但是,很多实验室并没有配套的硬件设备,让贫穷的学生党头大 经过网上大量的搜罗,我整理了适合学生党的深度学习解决方案.利用Colab + Kaggle两大免费的GPU环境,让深度学习变 ...

  9. [Kong 与 Konga与postgres数据库] 之 Kuberneres 部署

    1.Kong的概述 Kong是一个clould-native.快速的.可扩展的.分布式的微服务抽象层(也称为API网关.API中间件或在某些情况下称为服务网格)框架.Kong作为开源项目在2015年推 ...

  10. day7 地址 名片管理系统

    1 无限循环  (while  True)   break 退出     人为设计的 ,并且有退除的出口      死循环 bug  错误 2.引用   数字型