- 树状数组专题
楼兰图腾
- @ 2025-8-24 10:58:31
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 20;
typedef long long LL;
int n;
int a[N], t[N];
int L[N], G[N]; //L[i]左边比a[i]小的个数,G[i],左边比它大的个数
int lowbit(int x)
{
return x & -x;
}
void add(int x, int k)
{
for(int i = x; i <= n; i += lowbit(i))
t[i] += k;
}
int ask(int x)
{
int sum = 0;
for(int i = x; i; i -= lowbit(i) )
sum += t[i];
return sum;
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++)
{
int y = a[i];
L[i] = ask(y - 1);
G[i] = ask(n) - ask(y);
add(y, 1);
}
memset(t, 0, sizeof t);
LL resA = 0, resB = 0;
for(int i = n; i >= 1; i--)
{
int y = a[i];
resA += (LL)L[i] * ask(y - 1);
resB += (LL)G[i] * (ask(n) - ask(y));
add(y, 1);
}
cout << resB << " " << resA << endl;
return 0;
}
0 条评论
目前还没有评论...