传送门

题意:

  给你一个无限大的整数序列  p = {1, 2, 3, ...};

  有 n 次操作,每次操作交换第 ai 个数和第 aj 个数;

  求序列中逆序对的个数;

题解:

  考虑交换完后的序列,存在连续的区间 [ i , j ] 使得 p[ i ] = i , 那么分下一下这种区间的特点

  假设 i 之前有 x 个数大于 p[ i ],那么可知对于第 i 个数有 x 个逆序对,同理,因为 p[ i+1 ] = p[ i ]

  所以 i+1 之前也一定有且只有 x 个数大于 p[ i+1 ],那么第 i+1 个数也有 x 个逆序对,那么对于连续的区间[i,j]

  易的区间 [ i , j ] 与其之前的数可构成的逆序对的个数为 (j-i+1)*x 个,那么可将其映射为一个值 i ,并记录其有 (j-i+1) 个数;

  例如,假设 n 次操作为

  1  4

  1  8

  那么交换完后,前 8 个数的排列状况为:

  1  2  3  4  5  6  7  8

  8  2  3  1  5  6  7  4

  那么可得区间 [2,3] 和区间[5,6]的值未发生改变,当然区间[9,+∞]也为发生改变,但[9,+∞]并不影响答案,所以不用考虑。

  那么根据之前提到的,将为改变的大区间映射到某个值身上,并记录有多少数映射到这个值上了,如下所示:

  1  2  3  4  5

  8    1    4

  映射完后,区间[2,3]合并到了2上,区间[5,7]合并到了5上,接下来就是求映射完后的数组的逆序对总个数,这就变成了经典的

  树状数组求逆序对个数的题了。

  这就完事了么????当然不是~~~

  考虑 4 这个值,其之前的 5 只给 4 提供了一个逆序对,但实际上 6,7 也会提供,那么这就少算了两个逆序对数,这该怎么办呢?

  考虑某个区间 [i , j],一共有 j-i+1 个数,易得区间 [ i , j ] 只会影响其之后且值小于 i 的数,那么就再用一次树状数组,如果来到了某个

  映射的值,那么将 i 之后的数通过树状数组 +(j-i),对于某个在其之后且值小于 i 的数 x ,直接在答案上额外加上 Sum(x+1)即可。

AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
#define lowbit(x) (x&-x)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=1e6+; int n;
map<int,int>f;//记录交换后的序列状况
int diff[maxn];
int sum[maxn];
struct Date
{
int val;
int id;//初始编号
int tot;//如果某个区间映射到值val上,那么tot记录的就是区间的总个数
int newVal;//离散后的值
Date(int val=,int id=,int tot=):val(val),id(id),tot(tot){}
}_date[maxn]; struct BIT
{
int bit[maxn];
void Init()
{
mem(bit,);
}
void Add(int t,int x,int k)
{
while(t <= k)
{
bit[t] += x;
t += lowbit(t);
}
}
int Sum(int t)
{
int sum=;
while(t > )
{
sum += bit[t];
t -= lowbit(t);
}
return sum;
}
}_bit; bool cmp1(Date a,Date b)
{
return a.val < b.val;
}
bool cmp2(Date a,Date b)
{
return a.id < b.id;
} int Trans()
{
map<int,int>::iterator it;
int index=;
int preIndex=f.begin()->first;
int nexIndex; for(it=++f.begin();it != f.end();++it)
{
_date[++index]=Date(f[preIndex],index,);
nexIndex=it->first; //判断是否为未改变的区间
int tot=nexIndex-preIndex-;
if(tot >= )
_date[++index]=Date(preIndex+,index,tot); preIndex=nexIndex;
}
_date[++index]=Date(f[preIndex],index,); sort(_date+,_date+index+,cmp1);//按其val由小到大排序,方便离散化
for(int i=;i <= index;++i)
_date[i].newVal=i;//离散化
sort(_date+,_date+index+,cmp2);//按 id 从小到大复原 return index;
} ll Solve()
{
//离散化
int index=Trans();
_bit.Init();//树状数组初始化
ll ans=; //经典树状数组求逆序对个数
for(int i=;i <= index;++i)
{
int x=_date[i].newVal;
_bit.Add(x,,index);
ans += 1ll*(i-_bit.Sum(x))*_date[i].tot;
} //求解区间少加的逆序对个数
_bit.Init();
for(int i=;i <= index;++i)
{
int x=_date[i].newVal;
_bit.Add(x,_date[i].tot-,index);//只有 tot > 1才会加入到树状数组中 //求解 i 前值 > x 的未改变的区间少加的数的总个数
if(_date[i].tot == )
ans += _bit.Sum(index)-_bit.Sum(x); } return ans;
}
int main()
{
// freopen("C:/Users/hyacinthLJP/Desktop/stdin/contest","r",stdin);
scanf("%d",&n);
for(int i=;i <= n;++i)
{
int a,b;
scanf("%d%d",&a,&b);
int x,y;
x=(!f.count(a) ? a:f[a]);
y=(!f.count(b) ? b:f[b]); f[a]=y;
f[b]=x;
}
printf("%I64d\n",Solve()); return ;
}

codeforces 540E"Infinite Inversions"的更多相关文章

  1. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

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

                                                                    E. Infinite Inversions               ...

  3. CF #301 E:Infinite Inversions(逆序数,树状数组)

    A-Combination Lock  B-School Marks   C-Ice Cave   D-Bad Luck Island   E-Infinite Inversions E:Infini ...

  4. 线段树 离散化 E. Infinite Inversions E. Physical Education Lessons

    题目一:E. Infinite Inversions 这个题目没什么思维量,还比较简单,就是离散化要加上每一个值的后面一个值,然后每一个值放进去的不是1 ,而是这个值与下一个点的差值. 因为这个数代表 ...

  5. codeforces 301 E. Infinite Inversions

    题目:   time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  6. 【Codeforces Round #301 (Div. 2) E】Infinite Inversions

    [链接] 我是链接,点我呀:) [题意] 给你一个无限长的序列1,2,3,4... 然后给你n个操作. 每个操作ai,bi; 表示调换位置为ai和位置为bi的数的位置. (ai,bi<=10^9 ...

  7. Infinite Inversions(树状数组+离散化)

    思路及代码参考:https://blog.csdn.net/u014800748/article/details/45420085 There is an infinite sequence cons ...

  8. 【codeforces 749E】 Inversions After Shuffle

    http://codeforces.com/problemset/problem/749/E (题目链接) 题意 给出一个1~n的排列,从中等概率的选取一个连续段,设其长度为l.对连续段重新进行等概率 ...

  9. codeforces 622A Infinite Sequence

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. HashMap、HashTable、ConcurrentHashMap、HashSet区别 线程安全类

    HashMap专题:HashMap的实现原理--链表散列 HashTable专题:Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析 Hash,Tree数据结构时 ...

  2. LeetCode & Online Programming Learning Platform

    leetcode LeetCode is the best platform to help you enhance your skills, expand your knowledge and pr ...

  3. Yii2总结

    1. Web访问流程(即在浏览器中输入一个网址至浏览器展现页面结果的过程) a. 将输入的网址提取出域名,在本地hosts文件中查找对应的IP地址(windows为C:/windows/system3 ...

  4. PHP金额工具类之将阿利伯数字转换为大写中文数字

    1.将阿拉伯数字转换为中文大写数字 <?php namespace core\components; class PriceHelper extends \yii\base\Component{ ...

  5. jdbc工具类1.0

    package cn.zhouzhou; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManag ...

  6. php new self()

    php里new self() 一般在类内部使用,作用是对自身类实例化 <?php class test{ public function __construct(){        echo ' ...

  7. 【README.md】Markdown语言常用语法

    转自:http://blog.csdn.net/zhaokaiqiang1992 这里只介绍最常用和最常见的功能,若想查看全部的语法,请移步http://wowubuntu.com/markdown/ ...

  8. 【数学建模】day08-数理统计III

    2. 回归分析 回归分析与曲线拟合区分. 曲线拟合是,根据得到的若干有关变量的一组数据,寻找因变量与(一个或几个)自变量之间的一个函数,使这个函数对那组数据拟合得好.通常,函数的形式可以由经验.先验知 ...

  9. 【数学建模】day04-插值与拟合

    关于插值原理,这篇文章里总结过. 插值,是在有限个数据点的情况下,模拟出更多的点来适应实际问题的需要. 拟合,是在已知数据点基础上,以已知点处最小误差为标准,模拟出近似函数. 二者有似,实则不同,ma ...

  10. Centos虚拟环境工具virtualenvwrapper

    下载安装virtualenvwrapper pip3 install virtualenvwrapper !!!!注意安装virtualenvwrapper必须是在本地环境下!!! 设置Linux的用 ...