NC14522 珂朵莉的数列 ( 树状数组 + 离散化 )

本文介绍了如何运用树状数组高效地解决逆序对问题,通过离散化和坐标压缩技术来处理大数据量,避免数组过大。详细阐述了思路、代码实现以及关键的贡献计算方式。
题目链接:点击进入
题目

在这里插入图片描述
在这里插入图片描述

思路

若 a [ i ] 和 a [ j ] 是一对逆序对,那么它对答案贡献是 i * ( n - j + 1 ) ( 也就是对所有区间左边界在 [ 1 , i ] 范围内,区间右边界在 [ j , n ] 的范围的区间都有贡献 ) 。
那么,我们可以枚举 j ,看前面有多少比它大的数,并求它们的坐标和,然后更新答案。
求前面有多少个比它大的数,这个可以用树状数组实现 ( 例如此题 ) ,但是这里还要求坐标和,正常的操作我们是修改值为 1 ,最后求前缀的答案就是有多少个数存在,而这里我们可以修改值为下标,那么我们得到的就是下标的前缀和了。
同时,这个题给的范围为 1e9 ,正常的话是将数作为下标并修改对应值,但是这里要是将数当作下标肯定不行,因为开不了 1e9 那么大的数组,因此我们对其离散化一下 ( 也就是排序,按照大小赋予编号 ),这样我们用编号进行树状数组的修改,数组最大只需要开1e6 即可。( 仔细想一下,离散化其实就是将原来中间没有赋值的那些位置除去了,留下只会被用到的位置,虽然给这些位置重新编号了,但是实际上相对位置是没变的 )
( 注:答案爆long long,开__int128 )

代码
// Problem: 珂朵莉的数列
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/problem/14522
// Memory Limit: 262144 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(3)//O3
//#pragma GCC optimize(2)//O2
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
#define int long long
//#define double long double
//#define rep(i,x,y) for(register int i = x; i <= y;++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e6+10;
const int mod=1e9+7;
const double eps=1e-9;
const int N=5e3+10;
/*--------------------------------------------*/
// inline int read()
// {
    // int k = 0, f = 1 ;
    // char c = getchar() ;
    // while(!isdigit(c)){if(c == '-') f = -1 ;c = getchar() ;}
    // while(isdigit(c)) k = (k << 1) + (k << 3) + c - 48 ,c = getchar() ;
    // return k * f ;
// }
/*--------------------------------------------*/

inline __int128 read()
{
    __int128 x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline void write(__int128 x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}


int n,c[maxn],mp[maxn];
__int128 ans;
struct node
{
	int pos;
	int num;
	bool operator < (const node&t)const
	{
		return num<t.num;
	}
}p[maxn];

void add(int x,int d)
{
	for(int i=x;i<=n;i+=lowbit(i)) c[i]+=d;
}
int getsum(int x)
{
	int sum=0;
	for(int i=x;i>=1;i-=lowbit(i)) sum+=c[i];
	return sum;
}

signed main() 
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p[i].num;
		p[i].pos=i;
	}
	sort(p+1,p+n+1);
	int cnt=1;
	mp[p[1].pos]=cnt;
	for(int i=2;i<=n;i++) 
	{
		if(p[i].num==p[i-1].num)
			mp[p[i].pos]=cnt;
		else
			mp[p[i].pos]=++cnt;
	}
	for(int i=1;i<=n;i++)
	{
		ans=ans+(getsum(n)-getsum(mp[i]))*(n-i+1);
		add(mp[i],i);
	}
	write(ans);
    return 0;
} 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值