fork download
  1. using namespace std;
  2. #include <bits/stdc++.h>
  3. #define mapii map<int, int>
  4. #define debug(a) cout << #a << ": " << a << endl
  5. #define fdto(i, r, l) for(int i = r; i >= l; --i)
  6. #define fto(i, l, r) for(int i = l; i <= r; ++i)
  7. #define forit(it, type, var) for(type::iterator it = var.begin(); it != var.end(); it++)
  8. #define ii pair<int, int>
  9. #define iii pair<int, ii>
  10. #define fi first
  11. #define se second
  12. #define mp make_pair
  13. #define pb push_back
  14. #define ll long long
  15. #define maxN 60005
  16.  
  17. struct FenwickTree {
  18. int y[maxN];
  19. void add(int n, int i, int d) {
  20. for(; i <= n; i += i&(-i)) y[i]+=d;
  21. }
  22. int sum(int j) {
  23. int res = 0;
  24. for(; j > 0; j -= j&(-j)) res+=y[j];
  25. return res;
  26. }
  27. };
  28.  
  29. int n, m, a[maxN];
  30. FenwickTree t;
  31. ll ans;
  32.  
  33. int main () {
  34. #ifndef ONLINE_JUDGE
  35. freopen("input.txt", "r", stdin);
  36. //freopen("output.txt", "w", stdout);
  37. #endif // ONLINE_JUDGE
  38.  
  39. scanf("%d", &n);
  40. fto(i, 1, n) scanf("%d", &a[i]);
  41.  
  42. fdto(i, n, 1) {
  43. ans+=t.sum(a[i]-1);
  44. t.add(n, a[i], 1);
  45. }
  46.  
  47. cout << ans;
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 3932KB
stdin
5
1 2 1 2 1
stdout
3