树状数组求逆序对RE0分
  • 板块P1908 逆序对
  • 楼主Candycar
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/11/26 19:03
  • 上次更新2023/11/3 23:32:56
查看原帖
树状数组求逆序对RE0分
398818
Candycar楼主2021/11/26 19:03

树状数组求逆序对RE了0分,烦请各路巨佬帮忙检查一下,谢谢。

#include<bits/stdc++.h>
#define int long long
#define mem(a) memset(a,0,sizeof(a))
#define set(a,b) memset(a,b,sizeof(a))
using namespace std;
inline int in()       //in()
{
	int x = 0, f = 1;
	char c = getchar();
	while (!isdigit(c))
	{
		if (c == '-')
			f =- 1;
		c = getchar();
	}
	while (isdigit(c))
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}
int n;
int c[1000005];
int lowbit(int x)
{
	return x & (-x);
}

int add(int x, int val)
{
	while (x <= n)
	{
		c[x] += val;
		x += lowbit(x);
	}
}
int sum(int x)
{
	int res = 0;
	while (x > 0)
	{
		res += c[x];
		x -= lowbit(x);
	}
	return res;
}
signed main()
{
//	freopen("nxd.in", "r", stdout);
//	freopen("nxd.out", "w", stdout);
	cin >> n;
	int a[n + 1];
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	int ans = 0;
	for (int i = n; i >= 1; i--)
	{
		int j = a[i];
		add(j, 1);
		j = a[i] - 1;
		ans += sum(j);
	}
	cout << ans << endl;
    return 0;
}

2021/11/26 19:03
加载中...