fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. // #define ll long long
  6.  
  7. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  8.  
  9. int rand(int L, int R) {
  10. return uniform_int_distribution<int>(L, R)(rng);
  11. }
  12.  
  13. const int N = 100000+5;
  14. int bit[N], a[N];
  15. int n;
  16.  
  17. void update(int x) {
  18. while (x < N) {
  19. bit[x]++;
  20. x += (x & (-x));
  21. }
  22. }
  23.  
  24. int get(int x) {
  25. int res = 0;
  26. while (x > 0) {
  27. res += bit[x];
  28. x -= (x & (-x));
  29. }
  30. return res;
  31. }
  32.  
  33. void solve() {
  34. cin >> n;
  35. for (int i = 1; i <= n; i++) cin >> a[i];
  36. int answer = 0;
  37. for (int i = n; i >= 1; i--) {
  38. answer += get(a[i]-1);
  39. update(a[i]);
  40. }
  41. cout << answer << '\n';
  42. }
  43.  
  44. signed main() {
  45. ios_base::sync_with_stdio(false);
  46. cin.tie(0);
  47. // int t; cin >> t;
  48. // while (t--) solve();
  49. solve();
  50. }
Success #stdin #stdout 0.01s 5280KB
stdin
4
2 4 1 3
stdout
3