Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define debug(l) cerr<<#l<<' '<<l<<'\n';
- #include "bits/stdc++.h"
- using namespace std;
- #define all(pref) pref.begin(), pref.end()
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef long double ld;
- struct Fenwick {
- vector<ll> f;
- Fenwick(ll n) {
- f.resize(n + 1);
- }
- void add(ll ind, ll ad) {
- for (; ind < f.size(); ind += ind & -ind)f[ind] += ad;
- }
- ll get(ll i) {
- ll res = 0;
- for (; i > 0; i -= i & -i) {
- res += f[i];
- }
- return res;
- }
- };
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- ll t;
- cin >> t;
- while (t--) {
- ll n;
- cin >> n;
- vector<ll>a(n);
- Fenwick T(n);
- for (ll i = 0; i < n; i++) {
- cin >> a[i];
- T.add(a[i], +1);
- }
- ll ans = 0;
- for (ll i = 0; i < n; i++) {
- T.add(a[i], -1);
- ans += T.get(a[i]);
- }
- cout << ans << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement