Hdu--2492(树状数组)
2014-09-08 15:14:35
思路:参考大白书的思路。。熟悉了下树状数组。
1 /************************************************************************* 2 > File Name: hdu2492.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Mon 08 Sep 2014 01:21:55 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 typedef long long ll; 16 const int INF = 1 << 29; 17 18 int T,n,a[100005]; 19 int c[100005]; 20 int pre[100005],suf[100005]; 21 22 int Lowbit(int x){ return x & (-x); } 23 void Update(int x,int d){ while(x <= 100000){ c[x] += d,x += Lowbit(x);} } 24 int Getsum(int x){ 25 int res = 0; 26 while(x > 0){ 27 res += c[x]; 28 x -= Lowbit(x); 29 } 30 return res; 31 } 32 33 int main(){ 34 cin >> T; 35 while(T--){ 36 cin >> n; 37 memset(c,0,sizeof(c)); 38 for(int i = 1; i <= n; ++i){ 39 cin >> a[i]; 40 Update(a[i],1); 41 pre[i] = Getsum(a[i]) - 1; 42 } 43 memset(c,0,sizeof(c)); 44 for(int i = n; i >= 1; --i){ 45 Update(a[i],1); 46 suf[i] = Getsum(a[i]) - 1; 47 } 48 ll ans = 0; 49 for(int i = 1; i <= n; ++i){ 50 //printf("pre : %d , suf : %d\n",pre[i],suf[i]); 51 ans += pre[i] * (n - i - suf[i]) + (i - 1 - pre[i]) * suf[i]; 52 } 53 cout << ans << endl; 54 } 55 return 0; 56 }