You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1004 B
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define lowbit(x) ((x) & -(x))
const int N = 500005;
int sum[N];
int query(int x)
{
int ans = 0;
for (; x; x -= lowbit(x)) ans += sum[x];
return ans;
}
void update(int x, int y)
{
for (; x <= N; x += lowbit(x)) sum[x] += y;
}
struct abcd
{
int val, pos;
bool operator<(const abcd &rhs) const { return val < rhs.val; }
} nodes[N];
int main()
{
int n;
while (~scanf("%d", &n) && n)
{
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) scanf("%d", &nodes[i].val), nodes[i].pos = i;
std::sort(nodes + 1, nodes + n + 1);
long long ans = 0;
for (int i = 1; i <= n; i++)
{
update(nodes[i].pos, 1);
ans += i - query(nodes[i].pos);
}
printf("%lld\n", ans);
}
return 0;
}