P1637 三元上升子序列-树状数组的应用

点我呀!!!!!!!!!!!!!!!!!!!!!!
在这里插入图片描述
思路:这个题目需要离散化。。。。。
h数组就是离散化的结果。当a[i]非常大的时候需要去离散化处理

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <vector>
#include <set>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
using namespace std;
const int N = 30100;
int a[N]; ll n, ans;
ll le[N], ri[N], c[N];
int h[N];
struct node
{
	int v, pos;
}no[N];
int cmp(node a, node b)
{
	return a.v < b.v;
}
int lowbit(int x) { return x & (-x); }
void add(int i, int x)
{
	while (i <= N)
	{
		c[i] += x;
		i += lowbit(i);
	}
}
int getsum(int i)
{
	int ans = 0;
	while (i > 0)
	{
		ans += c[i];
		i -= lowbit(i);
	}
	return ans;
}
int main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; i++)
	{
		no[i].pos = i; cin >> no[i].v;
	}
	sort(no + 1, no + 1 + n, cmp);
	for (int i = 1; i <= n; i++)
	{
		if (i == 1 || no[i].v != no[i - 1].v)
			h[no[i].pos] = i;
		else
			h[no[i].pos] = h[no[i - 1].pos];
	}
	for (int i = 1; i <= n; i++)
	{
		add(h[i], 1);
		le[i] = getsum(h[i] - 1);
	}
	memset(c, 0, sizeof c);
	for (int i = n; i >= 1; i--)
	{
		add(h[i], 1);
		ri[i] = n - i + 1 - getsum(h[i]);
	}
	for (int i = 2; i < n; i++)
	{
		ans += le[i] * ri[i];
	}
	cout << ans << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值