传送门:https://codeforces.com/contest/540/problem/E

题意:

有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆序对个数为多少

题解:

因为值域范围为1e9,而n的范围只有1e5,所以我们肯定是不能直接交换的,对n次操作离散化,离散化后的数组最大为2e5,这里需要用到一些离散化的技巧。

将每次交换的u,v两个点放到map里面,键为pos,值为0

然后对于map映射,值就是离散化后的下标

离散化后我们应该做什么呢?

首先,我们将求逆序对数分为两个部分

PART1:位置为u的数与位置为v 的数交换产生的逆序对数 cnt1

PART2: 位置为u的数是被交换过的,他和没有被交换过的数产生的逆序对数 cnt2

f[i]表示交换序列中,离散化后,处于第i个位置上面的值(也是离散化之前的位置)

p[i]表示现在在i位置上的数初始是在p[i]位置(位置是离散化后的从小到大的顺序,因此是对于交换序列的(因为只有交换序列才离散化了))

对于处于交换序列中的某个数,其初始位置和最终位置之间有多少个数,答案为:abs(f[p[i]]-f[i])-1

但是这两个位置之间,处于交换序列中的数在之前的第一部分已经算过了(也就是两个数都是被交换过的数)

所以我们需要减去 两个位置之间,已经处于交换序列的个数(不包含两个位置)

结果为abs(i-p[i])-1 (交换序列中 i和p[i]位置之间,有abs(i-p[i])-1个数)

\[cnt_1= i-get\_sum(p\[i]);\\
cnt_2= abs(f[p[i]]-f[i]) – abs(i-p[i])
\]

代码:

//线段树版本
/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n" const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f; int n;
int u[maxn];
int v[maxn];
int f[maxn], p[maxn];
map<int, int>mp;
LL sum[maxn << 2];
void PushUp(int rt) {
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void update( int p, LL sc, int l, int r, int rt) {
if (l == r) {
sum[rt] += sc;
return ;
}
int mid = (l + r) >> 1;
if (p <= mid) update(p, sc, lson);
else update(p, sc, rson);
PushUp(rt);
}
LL query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return sum[rt];
int mid = (l + r) >> 1;
LL ret = 0LL;
if (L <= mid) ret += query(L, R, lson);
if (R > mid) ret += query(L, R, rson);
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d", &n);
mp.clear();
memset(p, 0, sizeof(p));
for ( int i = 1 ; i <= n ; i++) {
scanf("%d%d", &u[i], &v[i]);
mp[u[i]] = 0 ;
mp[v[i]] = 0;
}
int cnt = 0 ;
for ( auto it = mp.begin() ; it != mp.end() ; it++) {
it->second = ++cnt;
f[cnt] = it->first;
p[cnt] = cnt;
}
for ( int i = 1 ; i <= n ; i++) u[i] = mp[u[i]], v[i] = mp[v[i]];
for ( int i = 1 ; i <= n ; i++) swap(p[u[i]], p[v[i]]);
LL ans = 0LL;
for ( LL i = 1; i <= cnt ; i++) {
update(p[i], 1, 1, cnt, 1);
ans += i - query(1, p[i], 1, cnt, 1);
ans += abs(f[i] - f[p[i]]) - abs(i - p[i]);
}
printf("%lld\n", ans);
return 0;
}
//树状数组版本

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n" const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f; int n;
int u[maxn];
int v[maxn];
int f[maxn], p[maxn];
map<int, int>mp;
int cnt;
int bit[maxn]; int lowbit(int x) {
return x & -x;
}
void add(int pos, int x) {
while(pos <= cnt) {
bit[pos] += x;
pos += lowbit(pos);
}
}
LL sum(int pos) {
LL ans = 0;
while(pos) {
ans += bit[pos];
pos -= lowbit(pos);
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d", &n);
mp.clear();
memset(p, 0, sizeof(p));
for ( int i = 1 ; i <= n ; i++) {
scanf("%d%d", &u[i], &v[i]);
mp[u[i]] = 0 ;
mp[v[i]] = 0;
}
cnt = 0;
for ( auto it = mp.begin() ; it != mp.end() ; it++) {
it->second = ++cnt;
f[cnt] = it->first;
p[cnt] = cnt;
}
for ( int i = 1 ; i <= n ; i++) u[i] = mp[u[i]], v[i] = mp[v[i]];
for ( int i = 1 ; i <= n ; i++) swap(p[u[i]], p[v[i]]);
LL ans = 0LL;
for ( LL i = 1; i <= cnt ; i++) {
add(p[i], 1);
ans += i - sum(p[i]);
ans += abs(f[i] - f[p[i]]) - abs(i - p[i]);
}
printf("%lld\n", ans);
return 0;
}

codeforces 540E 离散化技巧+线段树/树状数组求逆序对的更多相关文章

  1. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  2. NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)

    对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...

  3. [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)

    [NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...

  4. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  5. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...

  6. “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))

    传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...

  7. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  8. codeforces540E-树状数组求逆序对

    1-1e9的数据范围 但有1e5个区间 所以可以考虑把没有涉及到的区间缩成一个点 然后树状数组求逆序对 #include<bits/stdc++.h> #define inf 0x3f3f ...

  9. codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)

    题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...

  10. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

随机推荐

  1. Symmetric Tree 深度优先搜索

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. 【JZOJ4841】【NOIP2016提高A组集训第4场11.1】平衡的子集

    题目描述 夏令营有N个人,每个人的力气为M(i).请大家从这N个人中选出若干人,如果这些人可以分成两组且两组力气之和完全相等,则称为一个合法的选法,问有多少种合法的选法? 数据范围 40%的数据满足: ...

  3. n!素因子p的幂 swjtuOJ 2090【数论】

    原文地址:http://blog.csdn.net/u012717411/article/details/47334969(感谢作者) 素因子分解写的非常好!数论一道好题:给以两个大整数n,s(n&l ...

  4. java中URLEncode和URLDecode

    URLEncode和URLDecode用于完成普通字符串和 application/x-www-from-urlencoded MIME字符串之间的相互转化 如果传递的字符串中包含非西欧字符的字符串, ...

  5. 2019-10-26-dotnet-core-发布只有一个-exe-的方法

    title author date CreateTime categories dotnet core 发布只有一个 exe 的方法 lindexi 2019-10-26 8:42:7 +0800 2 ...

  6. 【datagrid】动态加载列 2016-01-03 16:32 2013人阅读 评论(19) 收藏

    之前我们的项目在前台显示只需要把数据从数据库读出来进行显示就可以,datagrid的表头字段都是写死的,把数据往表里一扔,就基本没什么事儿了,结果客户前几天要求,其中一个字段不能是死的,应该是有多少项 ...

  7. @codeforces - 455E@ Function

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 已知 a 序列,并给定以下关系: \[\begin{cases} ...

  8. oralce函数 next_day(d1[,c1])

    [功能]:返回日期d1在下周,星期几(参数c1)的日期 [参数]:d1日期型,c1为字符型(参数),c1默认为j(即当前日期) [参数表]:c1对应:星期一,星期二,星期三……星期日 [返回]:日期 ...

  9. @noi.ac - 493@ trade

    目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @part - 4@ @accepted code@ @details@ @d ...

  10. 上传图片保存到MySql数据库并显示--经验证有效

    以下方法仅供参考,只是介绍下这一种方法而已.欢迎指正!! 前台(image.html):  1<html> 2<head> 3<title>上传图片</tit ...