Infinite Inversions(树状数组+离散化)
思路及代码参考: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 ≤ 109, ai ≠ bi) — the arguments of the swap operation.
Output
Print a single integer — the number of inversions in the resulting sequence.
Examples
2
4 2
1 4
4
3
1 6
3 4
2 5
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(树状数组+离散化)的更多相关文章
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- [HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...
- Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6321 Solved: 2628[Su ...
- HDU5196--DZY Loves Inversions 树状数组 逆序数
题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...
随机推荐
- [vue] computed 和 method
计算属性 计算属性只有在它的相关依赖发生改变时才会重新取值 Method method每次渲染的时候都会被执行 举一个栗子 <template>...<div> <p& ...
- Access to XMLHttpRequest at xxxx from origin ‘null‘ has been blocked by CORS policy:
使用前后端分离的方式创建web项目的时候出现问题: 这是因为 ajax 请求的对应的域在本地的一个文件路径,比如在D盘的某个文件夹,这里存放的都是前端文件: 但是对应的服务器是 localhost 的 ...
- 有用的20个Python代码段
Python是一种非BS编程语言.设计简单和易读性是它广受欢迎的两大原因.正如Python的宗旨:美丽胜于丑陋,显式胜于隐式. 记住一些帮助提高编码设计的常用小诀窍是有用的.在必要时刻,这些小诀窍能够 ...
- Python3 网络爬虫:漫画下载,动态加载、反爬虫这都不叫事
一.前言 作者:Jack Cui 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那 ...
- 2020-07-09:mysql如何开启慢查询?
福哥答案2020-07-09: 1.参数说明 slow_query_log 慢查询开启状态slow_query_log_file 慢查询日志存放的位置(这个目录需要MySQL的运行帐号的可写权限,一般 ...
- 2020-04-07:假如你们系统接收十几种报文,用什么方式对应的各自的service,总不能都用if-else判断吧
福哥答案2020-04-08: 策略,工厂.
- 对象原型之__proto__
对象都会有一个__proto__指向构造函数的prototype原型对象,对象之所以能够使用构造函数的prototype原型对象的方法,就是因为有__proto__原型的存在. funct ...
- Go语言入门系列(六)之再探函数
Go语言入门系列前面的文章: Go语言入门系列(三)之数组和切片 Go语言入门系列(四)之map的使用 Go语言入门系列(五)之指针和结构体的使用 在Go语言入门系列(二)之基础语法总结这篇文章中已经 ...
- 【转载】PyChram简单使用教程
原文链接:https://www.cnblogs.com/yamei/p/5519818.html 一.PyChram下载官网:http://www.jetbrains.com/pycharm Win ...
- Android 用versionName判断版本大小(是否进行版本更新)
一般情况下都是用versionCode进行版本大小的判断从而进行判断是否进行app的更新,但是有可能从网站上爬下来的versionCode不准确,有的网站叫做build,所以用versionName进 ...