树状数组30分WA,求助
  • 板块P1908 逆序对
  • 楼主Br00k5xx
  • 当前回复8
  • 已保存回复8
  • 发布时间2021/12/22 17:18
  • 上次更新2023/10/28 13:53:47
查看原帖
树状数组30分WA,求助
529247
Br00k5xx楼主2021/12/22 17:18
#include <algorithm>
#include <cstdio>
using std::stable_sort;

int n, c[500005], lsh[500005], a[500005];
long long ans, sum;

int lowbit(int x)
{
	return x & (-x);
}

void add(int x)
{
	while (x <= n)
		c[x]++, x += lowbit(x);
}

int query(int x)
{
	sum = 0;
	while (x > 0)
		sum += c[x], x -= lowbit(x);
	return sum;
}

bool cmp(int x, int y)
{
	return a[x] > a[y];
}

int main()
{
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		lsh[i] = i;
	}

	stable_sort(lsh + 1, lsh + n + 1, cmp);

	for (int i = 1; i <= n; i++)
	{
		add(lsh[i]);
		ans += query(lsh[i] - 1);
	}

	printf("%lld", ans);
	return 0;
}
2021/12/22 17:18
加载中...