Submission #789755

# Submission time Handle Problem Language Result Execution time Memory
789755 2023-07-22T00:32:55 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
18 / 100
29 ms 4692 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q, b;
int arr[303030];
ll cnt[303030], ans = 0, res[50505];
pair<ll, ll> reord[303030];
map<int, ll> M;

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 init(int p) {
    for (int i = 1; i <= m; i++) cnt[i] = 0;
    for (int i = Q[p].lt; i <= Q[p].rt; i++) cnt[arr[i]]++;
    M.clear();
    for (int i = 1; i <= m; i++) {
        auto iter = M.find(cnt[i]); int val = iter->second;
        if (iter == M.end()) M.insert({cnt[i], 1});
        else {
            M.erase(iter);
            M.insert({cnt[i], val + 1});
        }
    }
}

ll cal() {
    for (auto iter = M.rbegin(); iter != M.rend(); iter++) {
        //cout << iter->first << " " << iter->second << "\n";
    }

    ll ret = 0, sum = 0, xsum = 0, s = 0;
    int sz = M.size(), p = sz + 1, q = sz, tot = 0;
    for (auto iter = M.rbegin(); iter != M.rend(); iter++) {
        ll sm = (iter->second)/2, la = iter->second - sm;
        if (tot % 2 == 0) {
            if (sm > 0) reord[--p] = make_pair(iter->first, sm);
            reord[++q] = make_pair(iter->first, la);
        }
        else {
            reord[--p] = make_pair(iter->first, la);
            if (sm > 0) reord[++q] = make_pair(iter->first, sm);
        }
        tot += iter->second;
    }
    for (int i = p; i <= q; i++) {
        ll a = reord[i].first, k = reord[i].second;
        ret += k * a * (a + 1) / 2;
        ret += a * a * k * (k + 1) * (k + 2) / 6 - a * a * k;
        ret += a * k * (2 * s + k + 3) / 2 * sum;
        ret -= a * k * xsum;
        sum += a * k;
        xsum += a * (2 * s + k + 1) / 2;
        s += k;
    }
    return ret;
}

void add(int i) {
    auto iter = M.find(cnt[i]); int val = iter->second;
    M.erase(iter);
    if (val > 1) M.insert({cnt[i], val - 1});

    cnt[i]++;

    iter = M.find(cnt[i]); val = iter->second;
    if (iter == M.end()) M.insert({cnt[i], 1});
    else {
        M.erase(iter);
        M.insert({cnt[i], val + 1});
    }
}

void rem(int i) {
    auto iter = M.find(cnt[i]); int val = iter->second;
    M.erase(iter);
    if (val > 1) M.insert({cnt[i], val - 1});

    cnt[i]--;

    iter = M.find(cnt[i]); val = iter->second;
    if (iter == M.end()) M.insert({cnt[i], 1});
    else {
        M.erase(iter);
        M.insert({cnt[i], val + 1});
    }
}

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] = cal();

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

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2696 KB Output is correct
2 Correct 12 ms 2700 KB Output is correct
3 Correct 12 ms 2696 KB Output is correct
4 Correct 18 ms 2696 KB Output is correct
5 Correct 13 ms 2644 KB Output is correct
6 Incorrect 13 ms 2700 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2700 KB Output is correct
2 Correct 13 ms 2708 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 17 ms 3156 KB Output is correct
5 Correct 21 ms 3828 KB Output is correct
6 Correct 26 ms 4436 KB Output is correct
7 Correct 25 ms 4352 KB Output is correct
8 Correct 25 ms 4432 KB Output is correct
9 Correct 25 ms 4436 KB Output is correct
10 Correct 26 ms 4512 KB Output is correct
11 Correct 27 ms 4400 KB Output is correct
12 Correct 25 ms 4452 KB Output is correct
13 Correct 25 ms 4436 KB Output is correct
14 Correct 26 ms 4436 KB Output is correct
15 Correct 25 ms 4508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2700 KB Output is correct
2 Correct 13 ms 2708 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 17 ms 3156 KB Output is correct
5 Correct 21 ms 3828 KB Output is correct
6 Correct 26 ms 4436 KB Output is correct
7 Correct 25 ms 4352 KB Output is correct
8 Correct 25 ms 4432 KB Output is correct
9 Correct 25 ms 4436 KB Output is correct
10 Correct 26 ms 4512 KB Output is correct
11 Correct 27 ms 4400 KB Output is correct
12 Correct 25 ms 4452 KB Output is correct
13 Correct 25 ms 4436 KB Output is correct
14 Correct 26 ms 4436 KB Output is correct
15 Correct 25 ms 4508 KB Output is correct
16 Correct 13 ms 2640 KB Output is correct
17 Correct 13 ms 2644 KB Output is correct
18 Correct 14 ms 2764 KB Output is correct
19 Correct 18 ms 3288 KB Output is correct
20 Correct 22 ms 3924 KB Output is correct
21 Correct 27 ms 4692 KB Output is correct
22 Correct 27 ms 4628 KB Output is correct
23 Correct 27 ms 4568 KB Output is correct
24 Correct 27 ms 4636 KB Output is correct
25 Correct 27 ms 4652 KB Output is correct
26 Correct 27 ms 4556 KB Output is correct
27 Correct 27 ms 4548 KB Output is correct
28 Correct 27 ms 4564 KB Output is correct
29 Correct 29 ms 4540 KB Output is correct
30 Correct 27 ms 4556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2700 KB Output is correct
2 Correct 13 ms 2708 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 17 ms 3156 KB Output is correct
5 Correct 21 ms 3828 KB Output is correct
6 Correct 26 ms 4436 KB Output is correct
7 Correct 25 ms 4352 KB Output is correct
8 Correct 25 ms 4432 KB Output is correct
9 Correct 25 ms 4436 KB Output is correct
10 Correct 26 ms 4512 KB Output is correct
11 Correct 27 ms 4400 KB Output is correct
12 Correct 25 ms 4452 KB Output is correct
13 Correct 25 ms 4436 KB Output is correct
14 Correct 26 ms 4436 KB Output is correct
15 Correct 25 ms 4508 KB Output is correct
16 Correct 13 ms 2640 KB Output is correct
17 Correct 13 ms 2644 KB Output is correct
18 Correct 14 ms 2764 KB Output is correct
19 Correct 18 ms 3288 KB Output is correct
20 Correct 22 ms 3924 KB Output is correct
21 Correct 27 ms 4692 KB Output is correct
22 Correct 27 ms 4628 KB Output is correct
23 Correct 27 ms 4568 KB Output is correct
24 Correct 27 ms 4636 KB Output is correct
25 Correct 27 ms 4652 KB Output is correct
26 Correct 27 ms 4556 KB Output is correct
27 Correct 27 ms 4548 KB Output is correct
28 Correct 27 ms 4564 KB Output is correct
29 Correct 29 ms 4540 KB Output is correct
30 Correct 27 ms 4556 KB Output is correct
31 Incorrect 13 ms 2712 KB Output isn't correct
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2696 KB Output is correct
2 Correct 12 ms 2700 KB Output is correct
3 Correct 12 ms 2696 KB Output is correct
4 Correct 18 ms 2696 KB Output is correct
5 Correct 13 ms 2644 KB Output is correct
6 Incorrect 13 ms 2700 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2696 KB Output is correct
2 Correct 12 ms 2700 KB Output is correct
3 Correct 12 ms 2696 KB Output is correct
4 Correct 18 ms 2696 KB Output is correct
5 Correct 13 ms 2644 KB Output is correct
6 Incorrect 13 ms 2700 KB Output isn't correct
7 Halted 0 ms 0 KB -