fork download
  1. #include <bits/stdc++.h>
  2. #include <functional>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/pb_ds/tree_policy.hpp>
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. #define PI 2 * acos(0)
  8. #define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  9. #ifndef ONLINE_JUDGE
  10. #define debug(x) cerr << #x << " = " << x << '\n';
  11. #else
  12. #define debug(x)
  13. #endif
  14. #define all(x) (x).begin(), (x).end()
  15. #define allr(x) (x).rbegin(), (x).rend()
  16. #define ordered_multiset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  17. #define ll long long
  18.  
  19. int main()
  20. {
  21. int t;
  22. cin >> t;
  23. while (t--)
  24. {
  25. int n;
  26. cin >> n;
  27. ordered_multiset s;
  28. ll c = 0;
  29. for (int i = 0; i < n; i++)
  30. {
  31. int x;
  32. cin >> x;
  33. if (i + 1 > x)
  34. {
  35. c += s.order_of_key(x);
  36.  
  37. }
  38. if (i + 1 > x)
  39. s.insert(i+1);
  40. }
  41. cout << c << endl;
  42. }
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5460KB
stdin
5
8
1 1 2 3 8 2 1 4
2
1 2
10
0 2 1 6 3 4 1 2 8 3
2
1 1000000000
3
0 1000000000 2
stdout
3
0
10
0
1