思路:
参考了http://blog.csdn.net/wr132/article/details/43856905
用树状数组统计每个数前面比它大的数的个数和它后面比他小的数的个数。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 typedef long long ll; 6 7 const int N = 100000; 8 const int H = 1000000; 9 int bit[H + 5], a[N + 5], cnt[N + 5]; 10 ll u[N + 5]; 11 12 void add(int i, int x) 13 { 14 while (i <= H) 15 { 16 bit[i] += x; 17 i += i & -i; 18 } 19 } 20 21 int sum(int i) 22 { 23 int s = 0; 24 while (i) 25 { 26 s += bit[i]; 27 i -= i & -i; 28 } 29 return s; 30 } 31 32 void init() 33 { 34 for (int i = 1; i <= N; i++) 35 { 36 u[i] = u[i - 1] + (ll)i; 37 } 38 } 39 40 int main() 41 { 42 int n, tmp; 43 init(); 44 cin >> n; 45 for (int i = 0; i < n; i++) 46 { 47 scanf("%d", &a[i]); 48 a[i]++; 49 add(a[i], 1); 50 cnt[i] = i + 1 - sum(a[i]); 51 } 52 memset(bit, 0, sizeof(bit)); 53 for (int i = n - 1; i >= 0; i--) 54 { 55 add(a[i], 1); 56 cnt[i] += sum(a[i] - 1); 57 } 58 ll ans = 0; 59 for (int i = 0; i < n; i++) 60 { 61 ans += u[cnt[i]]; 62 } 63 cout << ans << endl; 64 return 0; 65 }