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.

46 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 200010;
int n, a[N]; // n个元素a[i]代表原始值
LL big[N], small[N]; // big[i]:比a[i]大的元素个数small[i]:比a[i]小的元素个数
LL res1, res2; // V的个数∧的个数
// 树状数组模板
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;
}
// 桶计数+前缀和
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) { // 枚举每个点,由左到右一个个下场
int x = a[i];
small[i] = sum(x - 1); // 在我前面出场,并且,值比我小, 用于方便统计铁锹(∧)
big[i] = sum(n) - sum(x - 1); // 在我前面出场,并且,值比我大, 用于方便统计尖刀(V)
add(x, 1); // 以值为桶的标识,记录此桶中数量+1
}
memset(c, 0, sizeof c);
for (int i = n; i; i--) {
int x = a[i];
res1 += small[i] * sum(x - 1);
res2 += big[i] * (sum(n) - sum(x - 1));
add(x, 1);
}
printf("%lld %lld\n", res2, res1);
return 0;
}