template<class T>
struct Fen {
    int n;
    vector<T> t;
    Fen (int n_) {
        init(n_);
    }
    Fen () {};
    void init(int n_) {
        n = n_;
        t.assign(n + 1, {});
    }
    void change(int u, T x) {
        for(; u <= n; u += u & -u) t[u] += x;
    }
    T sum(int u) {
        T res = 0;
        for(; u; u -= u & -u) res += t[u];
        return res;
    }
    T sum(int l, int r) {
        return sum(r) - sum(l - 1);
    }

    int kth(T sum) {
        int i = 0;
        T tot = {};
        for (int j = 1 << __lg(n); j > 0; j /= 2) {
            if (i + j <= n && t[i + j] + tot < sum) {
                i += j;
                tot += t[i];
            }
        }
        return i + 1;
    }
};

void solve() {
    int n;
    cin >> n;

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

    Fen<int> t(n + 2);
    ll res = 0;
    for (int i = 1; i <= n; i++) {
        res += t.sum(a[i] + 1, n + 2);
        t.change(a[i], 1);
    }

    t.init(n + 2);
    vector<ll> d(n + 2, 0);
    d[0] += res;

    for (int i = 1; i <= n; i++) {
        int cnt = t.sum(a[i] + 1, n + 2);
        t.change(a[i], 1);
        d[0] -= cnt, d[a[i] - 1 + 1] += cnt;
    }

    for (int i = 1; i <= n; i++) d[i] += d[i - 1];
    for (int i = 0; i < n; i++) cout << d[i] << '\n';
}

0 条评论

目前还没有评论...

信息

ID
460
时间
ms
内存
MiB
难度
9
标签
(无)
递交数
9
已通过
5
上传者