struct Fenwick {
    vector<ll> t;
    int n;
    Fenwick(int n_) {
        n = n_;
        t.assign(n + 1, 0);
    }

    void change(int x, int y) {
        for (; x <= n; x += x & -x) {
            t[x] += y;
        }
    }

    ll query(int x) {
        ll res = 0;
        for (; x > 0; x -= x & -x) {
            res += t[x];
        }
        return res;
    }

    ll query(int l, int r) {
        return query(r) - query(l - 1);
    }
};
void solve() {
    int n; cin >> n;
    const int N = 1e5;
    Fenwick t(N + 1);

    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    ll res = 0;
    for (int i = 1; i <= n; i++) {
        res += t.query(a[i] + 1, N);
        t.change(a[i], 1);
    }
    cout << res << '\n';
}

0 条评论

目前还没有评论...