Files
python/TangDou/AcWing/BIT/241_BIT.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

46 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}