fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6. #define ll long long
  7.  
  8. typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  9.  
  10. ll solve(vector<int>a, int n) {
  11. int key;
  12. ordered_set set1;
  13. set1.insert(a[0]);
  14. ll cnt = 0;
  15.  
  16. for(int i = 1; i < n; i++) {
  17. set1.insert(a[i]);
  18. key = set1.order_of_key(a[i] + 1);
  19. cnt += set1.size() - key;
  20. }
  21. return cnt;
  22. }
  23.  
  24. int main() {
  25. int n;
  26. cin>>n;
  27.  
  28. vector<int>a(n);
  29. for(int i = 0; i < n; i++) {
  30. cin>>a[i];
  31. }
  32. cout<<solve(a, n)<<endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5392KB
stdin
5
6 3 4 5 8
stdout
3