Submission #789645

# Submission time Handle Problem Language Result Execution time Memory
789645 2023-07-21T15:51:04 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
0 / 100
0 ms 340 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q, b;
int arr[303030];
ll cnt[303030], cnt2[303030], reord[303030], ans = 0, res[50505];

const int m = 300000;

struct query{
    int idx, lt, rt;
} Q[50505];

bool compare(query p, query q) {
    if (p.lt/b == q.lt/b) return p.rt < q.rt;
    return (p.lt/b) < (q.lt/b);
}

ll sum[m + 1], xsum[m + 1];

void updatesum(int i, ll x) {
    while (i <= m) {
        sum[i] += x;
        i += (i & -i);
    }
}

void updatexsum(int i, ll x) {
    while (i <= m) {
        xsum[i] += x;
        i += (i & -i);
    }
}

ll getsum(int i) {
    ll ans = 0;
    while (i > 0) {
        ans += sum[i];
        i -= (i & -i);
    }
    return ans;
}

ll getxsum(int i) {
    ll ans = 0;
    while (i > 0) {
        ans += xsum[i];
        i -= (i & -i);
    }
    return ans;
}

vector<ll> v;
void init(int p) {
    for (int i = 1; i <= m; i++) cnt[i] = sum[i] = xsum[i] = 0;
    for (int i = 0; i <= n; i++) cnt2[i] = 0;
    ans = 0;

    for (int i = Q[p].lt; i <= Q[p].rt; i++) cnt[arr[i]]++;
    for (int i = 1; i <= m; i++) cnt2[cnt[i]]++;

    v.clear();
    v.push_back(0);
    for (int i = 0; i <= n; i++) {
        for (int j = 1; j <= cnt2[i]; j++) v.push_back(i);
    }

    for (int i = 1; i <= m/2; i++) {
        reord[i] = v[2 * i - 1];
    }
    for (int i = 1; i <= m/2; i++) {
        reord[m - i + 1] = v[2 * i];
    }

    for (ll i = 1; i <= m; i++) {
        updatesum(i, reord[i]);
        updatexsum(i, i * reord[i]);
    }

    for (ll i = 1; i <= m; i++) ans += reord[i] * (reord[i] + 1) / 2;

    for (ll i = 1; i <= m; i++) {
        ans += reord[i] * ((i + 1) * getsum(i - 1) - getxsum(i - 1));
    }
}

void add(int j) {
    int idx, i;
    idx = upper_bound(v.begin() + 1, v.begin() + m + 1, cnt[arr[j]]) - v.begin() - 1;
    cnt[arr[j]]++;
    cnt2[idx]++;
    if (idx % 2 == 0) i = m - idx/2 + 1;
    else i = (idx + 1) / 2;
    reord[i]++;
    ans += reord[i];
    ans += (i + 1) * getsum(i - 1) - getxsum(i - 1);
    ans += (getxsum(m) - getxsum(i)) - (i - 1) * (getsum(m) - getsum(i));
    updatesum(i, 1);
    updatexsum(i, i);
}

void rem(int j) {
    int idx, i;
    idx = lower_bound(v.begin() + 1, v.begin() + m + 1, cnt[arr[j]]) - v.begin();
    assert(cnt2[idx] == cnt[arr[j]]);
    cnt[arr[j]]--;
    cnt2[idx]--;
    if (idx % 2 == 0) i = m - idx/2 + 1;
    else i = (idx + 1) / 2;
    reord[i]--;
    ans -= reord[i] + 1;
    ans -= (i + 1) * getsum(i - 1) - getxsum(i - 1);
    ans -= (getxsum(m) - getxsum(i)) - (i - 1) * (getsum(m) - getsum(i));
    updatesum(i, -1);
    updatexsum(i, -i);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> q; b = (int)sqrt(n);
    for (int i = 1; i <= n; i++) cin >> arr[i];

    for (int i = 1; i <= q; i++) cin >> Q[i].lt >> Q[i].rt;
    for (int i = 1; i <= q; i++) Q[i].idx = i;

    sort(Q + 1, Q + q + 1, compare);

    int p = 1;
    for (int i = 0; i <= n/b; i++) {
        if (Q[p].lt/b != i) continue;

        //init(p);
        res[Q[p].idx] = ans;

        p++;
        while (p <= q && Q[p].lt/b == i) {
            for (int j = Q[p - 1].rt + 1; j <= Q[p].rt; j++) add(j);
            if (Q[p - 1].lt > Q[p].lt) for (int j = Q[p].lt; j < Q[p - 1].lt; j++) add(j);
            else for (int j = Q[p - 1].lt; j < Q[p].lt; j++) rem(j);
            res[Q[p].idx] = ans;
            p++;
        }
    }

    for (int i = 1; i <= q; i++) cout << res[i] << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -