思路及代码参考: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. 数据结构中的树(二叉树、二叉搜索树、AVL树)

    数据结构动图展示网站 树的概念 树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合.它是由n(n>=1)个有限节点组成一个具有 ...

  2. Python高性能HTTP客户端库requests的使用

    Python中有许多HTTP客户端.使用最广泛且最容易的是requests. 持续连接 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很 ...

  3. docker找回构建时被删除的文件

    设想这样一个场景:当一个docker镜像被多次引用构建,在某次构建中某个文件被删除,如何找回被删除的文件? 要想回答这么一个问题,首先得熟悉下docker镜像的分层存储结构,镜像每一层都是只读的: 那 ...

  4. java多线程(三):多线程单例模式,双重检查,volatile关键字

    一.事先准备 首先准备一个运行用的代码: public class Singleton { public static void main(String[] args) { Thread[] thre ...

  5. Azure Application Gateway(一)对后端 Web App 进行负载均衡

    一,引言 今天,我们学习一个新的知识点-----Azure Application Gateway,通过Azure 应用程序网关为我么后端的服务提供负载均衡的功能.我们再文章头中大概先了解一下什么是应 ...

  6. Vue中diff算法的理解

    Vue中diff算法的理解 diff算法用来计算出Virtual DOM中改变的部分,然后针对该部分进行DOM操作,而不用重新渲染整个页面,渲染整个DOM结构的过程中开销是很大的,需要浏览器对DOM结 ...

  7. 在K3s上使用Kong网关插件,开启K3s的无限可能!

    我的工作中很重要的一部分是参加各种各样的技术会议.最近参加的是去年11月的北美KubeCon,在会议的最后一天,所有人都焦头烂额,我也一直机械地向不同的人重复我的自我介绍.后来,我已经十分烦躁,决定逃 ...

  8. Java引用类型之最终引用

    FinalReference类只有一个子类Finalizer,并且Finalizer由关键字final修饰,所以无法继承扩展.类的定义如下: class FinalReference<T> ...

  9. Dataway与SpringBoot集成干掉后台开发

    Dataway与SpringBoot集成干掉后台开发 Dataway让SpringBoot不在需要Controller.Service.DAO.Mapper了. 第一步:引入相关依赖 <depe ...

  10. 面试28k职位,老乡面试官从HashCode到HashMap给我讲了一下午!「回家赶忙整理出1.6万字的面试材料」

    作者:小傅哥 博客:https://bugstack.cn 目录 一.前言 二.HashCode为什么使用31作为乘数 1. 固定乘积31在这用到了 2. 来自stackoverflow的回答 3. ...