AcWing 241. 楼兰图腾
原题链接
简单
作者:
阿有
,
2020-10-14 10:15:43
,
所有人可见
,
阅读 522
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int tr[N], num[N];
int n;
int sum1_u[N], sum2_u[N];//v 所有i 之前比yi大的值,所有i 之后比yi大的值
int sum1_d[N], sum2_d[N];//^ 所有i 之前比yi小的值,所有i 之后比yi小的值
int lowbit(int x){
return x & -x;
}
void add(int x, int v){
for(int i = x; i <= N; i += lowbit(i)){
tr[i] += v;
}
}
int query(int x){
int res = 0;
for(int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &num[i]);
}
for(int i = 1; i <= n; i++){
sum1_u[i] = query(N - 1) - query(num[i]);
sum1_d[i] = query(num[i] - 1);
add(num[i], 1);
}
memset(tr, 0, sizeof tr);
for(int i = n; i >= 1; i--){
sum2_u[i] = query(N - 1) - query(num[i]);
sum2_d[i] = query(num[i] - 1);
add(num[i], 1);
}
LL ans1 = 0, ans2 = 0;//V ^
for(int i = 1; i <= n; i++){
ans1 += (LL)sum1_u[i] * (LL)sum2_u[i];
ans2 += (LL)sum1_d[i] * (LL)sum2_d[i];
}
cout << ans1 << " " << ans2 << endl;
return 0;
}