You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.5 KiB
60 lines
1.5 KiB
#include <iostream>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <queue>
|
|
#include <algorithm>
|
|
#include <math.h>
|
|
#include <cstdio>
|
|
using namespace std;
|
|
typedef long long LL;
|
|
|
|
const int N = 500010;
|
|
|
|
int a[N], q[N], ql;
|
|
|
|
int n;
|
|
// 树状数组模板
|
|
typedef long long LL;
|
|
#define lowbit(x) (x & -x)
|
|
int c[N];
|
|
void add(int x, int d) {
|
|
for (int i = x; i < N; i += lowbit(i)) c[i] += d;
|
|
}
|
|
LL sum(int x) {
|
|
LL res = 0;
|
|
for (int i = x; i; i -= lowbit(i)) res += c[i];
|
|
return res;
|
|
}
|
|
// 二分模板 lower_bound (左闭右开)
|
|
int find(int x) {
|
|
return lower_bound(q + 1, q + 1 + ql, x) - q;
|
|
}
|
|
int main() {
|
|
#ifndef ONLINE_JUDGE
|
|
freopen("POJ2299.in", "r", stdin);
|
|
#endif
|
|
while (~scanf("%d", &n) && n) {
|
|
// 清空树状数组
|
|
memset(c, 0, sizeof c);
|
|
// 读入原始数组
|
|
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), q[i] = a[i];
|
|
|
|
// 由小到大排序+离散化
|
|
sort(q + 1, q + 1 + n);
|
|
ql = 1;
|
|
for (int i = 2; i <= n; i++)
|
|
if (q[ql] != q[i]) q[++ql] = q[i];
|
|
|
|
// 利用树状数组动态维护个数,动态获取前缀和(权值)
|
|
LL ans = 0;
|
|
for (int i = 1; i <= n; i++) {
|
|
// 在我前面进来,比我大的有多少个?
|
|
ans += sum(ql) - sum(find(a[i]));
|
|
add(find(a[i]), 1);
|
|
}
|
|
printf("%lld\n", ans);
|
|
}
|
|
return 0;
|
|
} |