真的越来越发觉树状数组比起线段树来说,无论是时间复习度还是空间复习度都明显强大得多,并且代码实现也简单得多!!!! #include<stdio.h> #include<string.h> #define N 100024 int tree1[N]; int tree2[N]; int count[N]; int count1[N]; int arr[N]; int lowbit(int x) { return x&(x^(x-1)); } int get_sum(int *p,int k) { int sum = 0; while(k >= 1){ sum += *(p+k); k -= lowbit(k); } return sum; } void change(int *p,int k) { while(k <= N){//又是这个问题,这里一定要是tree的长度,不为输入量n *(p+k) += 1; k += lowbit(k); } } int main() { int i; int tcase; int n; __int64 ans; scanf("%d",&tcase); while(tcase--){ ans = 0; scanf("%d",&n); memset(tree1,0,sizeof(tree1)); for(i = 1;i <= n;i++){ scanf("%d",&arr[i]); count[i] = get_sum(tree1,arr[i]-1); change(tree1,arr[i]); } memset(tree2,0,sizeof(tree2)); for(i = n;i >= 1;i--){ count1[i] = get_sum(tree2,arr[i]-1); change(tree2,arr[i]); } for(i = 1;i <= n;i++){ ans += (__int64)count[i]*(__int64)(n-i-count1[i]); ans += (__int64)(i-1-count[i])*(__int64)count1[i]; } printf("%I64d/n",ans); } return 0; }