Advertisement
ivnikkk

Untitled

Jul 13th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS  
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(pref) pref.begin(), pref.end()
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8. typedef long double ld;
  9. struct Fenwick {
  10.     vector<ll> f;
  11.     Fenwick(ll n) {
  12.         f.resize(n + 1);
  13.     }
  14.     void add(ll ind, ll ad) {
  15.         for (; ind < f.size(); ind += ind & -ind)f[ind] += ad;
  16.     }
  17.     ll get(ll i) {
  18.         ll res = 0;
  19.         for (; i > 0; i -= i & -i) {
  20.             res += f[i];
  21.         }
  22.         return res;
  23.     }
  24. };
  25. signed main() {
  26. #ifdef _DEBUG
  27.     freopen("input.txt", "r", stdin);
  28.     freopen("output.txt", "w", stdout);
  29. #endif
  30.     ios_base::sync_with_stdio(false);
  31.     cin.tie(nullptr);
  32.     cout.tie(nullptr);
  33.     ll t;
  34.     cin >> t;
  35.     while (t--) {
  36.         ll n;
  37.         cin >> n;
  38.         vector<ll>a(n);
  39.         Fenwick T(n);
  40.         for (ll i = 0; i < n; i++) {
  41.             cin >> a[i];
  42.             T.add(a[i], +1);
  43.         }
  44.         ll ans = 0;
  45.         for (ll i = 0; i < n; i++) {
  46.             T.add(a[i], -1);
  47.             ans += T.get(a[i]);
  48.         }
  49.         cout << ans << '\n';
  50.     }
  51.  
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement