Submission #789760

# Submission time Handle Problem Language Result Execution time Memory
789760 2023-07-22T00:58:43 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 4384 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[2020];
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";
    }
    */

    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) {
            reord[--p] = make_pair(iter->first, sm);
            reord[++q] = make_pair(iter->first, la);
        }
        else {
            reord[--p] = make_pair(iter->first, la);
            reord[++q] = make_pair(iter->first, sm);
        }
        tot += iter->second;
    }

    ll ret = 0, sum = 0, xsum = 0, s = 0;
    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 * k * (2 * s + k + 1) / 2;
        s += k;
        //cout << a << " " << k << " " << ret << "\n";
    }
    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);

    init(1);
    res[Q[1].idx] = cal();

    for (int i = 2; i <= q; i++) {
        if (Q[i - 1].rt < Q[i].rt) for (int j = Q[i - 1].rt + 1; j <= Q[i].rt; j++) add(arr[j]);
        else for (int j = Q[i].rt + 1; j <= Q[i - 1].rt; j++) rem(arr[j]);

        if (Q[i - 1].lt > Q[i].lt) for (int j = Q[i].lt; j < Q[i - 1].lt; j++) add(arr[j]);
        else for (int j = Q[i - 1].lt; j < Q[i].lt; j++) rem(arr[j]);

        res[Q[i].idx] = cal();
    }

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2644 KB Output is correct
2 Correct 12 ms 2644 KB Output is correct
3 Correct 13 ms 2688 KB Output is correct
4 Correct 15 ms 2700 KB Output is correct
5 Correct 13 ms 2644 KB Output is correct
6 Correct 13 ms 2696 KB Output is correct
7 Correct 11 ms 2644 KB Output is correct
8 Correct 12 ms 2688 KB Output is correct
9 Correct 13 ms 2644 KB Output is correct
10 Correct 12 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2696 KB Output is correct
2 Correct 14 ms 2696 KB Output is correct
3 Correct 14 ms 2732 KB Output is correct
4 Correct 18 ms 3028 KB Output is correct
5 Correct 21 ms 3480 KB Output is correct
6 Correct 26 ms 3868 KB Output is correct
7 Correct 26 ms 3864 KB Output is correct
8 Correct 26 ms 3820 KB Output is correct
9 Correct 26 ms 3832 KB Output is correct
10 Correct 29 ms 3968 KB Output is correct
11 Correct 26 ms 3796 KB Output is correct
12 Correct 26 ms 3792 KB Output is correct
13 Correct 26 ms 3744 KB Output is correct
14 Correct 26 ms 3864 KB Output is correct
15 Correct 26 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2696 KB Output is correct
2 Correct 14 ms 2696 KB Output is correct
3 Correct 14 ms 2732 KB Output is correct
4 Correct 18 ms 3028 KB Output is correct
5 Correct 21 ms 3480 KB Output is correct
6 Correct 26 ms 3868 KB Output is correct
7 Correct 26 ms 3864 KB Output is correct
8 Correct 26 ms 3820 KB Output is correct
9 Correct 26 ms 3832 KB Output is correct
10 Correct 29 ms 3968 KB Output is correct
11 Correct 26 ms 3796 KB Output is correct
12 Correct 26 ms 3792 KB Output is correct
13 Correct 26 ms 3744 KB Output is correct
14 Correct 26 ms 3864 KB Output is correct
15 Correct 26 ms 3796 KB Output is correct
16 Correct 13 ms 2688 KB Output is correct
17 Correct 14 ms 2700 KB Output is correct
18 Correct 14 ms 2644 KB Output is correct
19 Correct 18 ms 3092 KB Output is correct
20 Correct 22 ms 3424 KB Output is correct
21 Correct 27 ms 3868 KB Output is correct
22 Correct 26 ms 3796 KB Output is correct
23 Correct 29 ms 3824 KB Output is correct
24 Correct 27 ms 3796 KB Output is correct
25 Correct 26 ms 3832 KB Output is correct
26 Correct 27 ms 3800 KB Output is correct
27 Correct 27 ms 3832 KB Output is correct
28 Correct 27 ms 3796 KB Output is correct
29 Correct 27 ms 3844 KB Output is correct
30 Correct 27 ms 3864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2696 KB Output is correct
2 Correct 14 ms 2696 KB Output is correct
3 Correct 14 ms 2732 KB Output is correct
4 Correct 18 ms 3028 KB Output is correct
5 Correct 21 ms 3480 KB Output is correct
6 Correct 26 ms 3868 KB Output is correct
7 Correct 26 ms 3864 KB Output is correct
8 Correct 26 ms 3820 KB Output is correct
9 Correct 26 ms 3832 KB Output is correct
10 Correct 29 ms 3968 KB Output is correct
11 Correct 26 ms 3796 KB Output is correct
12 Correct 26 ms 3792 KB Output is correct
13 Correct 26 ms 3744 KB Output is correct
14 Correct 26 ms 3864 KB Output is correct
15 Correct 26 ms 3796 KB Output is correct
16 Correct 13 ms 2688 KB Output is correct
17 Correct 14 ms 2700 KB Output is correct
18 Correct 14 ms 2644 KB Output is correct
19 Correct 18 ms 3092 KB Output is correct
20 Correct 22 ms 3424 KB Output is correct
21 Correct 27 ms 3868 KB Output is correct
22 Correct 26 ms 3796 KB Output is correct
23 Correct 29 ms 3824 KB Output is correct
24 Correct 27 ms 3796 KB Output is correct
25 Correct 26 ms 3832 KB Output is correct
26 Correct 27 ms 3800 KB Output is correct
27 Correct 27 ms 3832 KB Output is correct
28 Correct 27 ms 3796 KB Output is correct
29 Correct 27 ms 3844 KB Output is correct
30 Correct 27 ms 3864 KB Output is correct
31 Correct 13 ms 2644 KB Output is correct
32 Correct 14 ms 2644 KB Output is correct
33 Correct 14 ms 2644 KB Output is correct
34 Correct 13 ms 2740 KB Output is correct
35 Correct 13 ms 2644 KB Output is correct
36 Correct 13 ms 2736 KB Output is correct
37 Correct 18 ms 2892 KB Output is correct
38 Correct 18 ms 2900 KB Output is correct
39 Correct 19 ms 3100 KB Output is correct
40 Correct 24 ms 3412 KB Output is correct
41 Correct 29 ms 3900 KB Output is correct
42 Correct 29 ms 3836 KB Output is correct
43 Correct 29 ms 3888 KB Output is correct
44 Correct 28 ms 3900 KB Output is correct
45 Correct 28 ms 3896 KB Output is correct
46 Correct 28 ms 3796 KB Output is correct
47 Correct 28 ms 3828 KB Output is correct
48 Correct 28 ms 3880 KB Output is correct
49 Correct 29 ms 3804 KB Output is correct
50 Correct 33 ms 3860 KB Output is correct
51 Correct 28 ms 3792 KB Output is correct
52 Correct 28 ms 3880 KB Output is correct
53 Correct 28 ms 3796 KB Output is correct
54 Correct 28 ms 3916 KB Output is correct
55 Correct 28 ms 3816 KB Output is correct
56 Correct 28 ms 3900 KB Output is correct
57 Correct 30 ms 3908 KB Output is correct
58 Correct 28 ms 3904 KB Output is correct
59 Correct 29 ms 3888 KB Output is correct
60 Correct 28 ms 3852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2644 KB Output is correct
2 Correct 12 ms 2644 KB Output is correct
3 Correct 13 ms 2688 KB Output is correct
4 Correct 15 ms 2700 KB Output is correct
5 Correct 13 ms 2644 KB Output is correct
6 Correct 13 ms 2696 KB Output is correct
7 Correct 11 ms 2644 KB Output is correct
8 Correct 12 ms 2688 KB Output is correct
9 Correct 13 ms 2644 KB Output is correct
10 Correct 12 ms 2644 KB Output is correct
11 Correct 13 ms 2696 KB Output is correct
12 Correct 14 ms 2696 KB Output is correct
13 Correct 14 ms 2732 KB Output is correct
14 Correct 18 ms 3028 KB Output is correct
15 Correct 21 ms 3480 KB Output is correct
16 Correct 26 ms 3868 KB Output is correct
17 Correct 26 ms 3864 KB Output is correct
18 Correct 26 ms 3820 KB Output is correct
19 Correct 26 ms 3832 KB Output is correct
20 Correct 29 ms 3968 KB Output is correct
21 Correct 26 ms 3796 KB Output is correct
22 Correct 26 ms 3792 KB Output is correct
23 Correct 26 ms 3744 KB Output is correct
24 Correct 26 ms 3864 KB Output is correct
25 Correct 26 ms 3796 KB Output is correct
26 Correct 13 ms 2688 KB Output is correct
27 Correct 14 ms 2700 KB Output is correct
28 Correct 14 ms 2644 KB Output is correct
29 Correct 18 ms 3092 KB Output is correct
30 Correct 22 ms 3424 KB Output is correct
31 Correct 27 ms 3868 KB Output is correct
32 Correct 26 ms 3796 KB Output is correct
33 Correct 29 ms 3824 KB Output is correct
34 Correct 27 ms 3796 KB Output is correct
35 Correct 26 ms 3832 KB Output is correct
36 Correct 27 ms 3800 KB Output is correct
37 Correct 27 ms 3832 KB Output is correct
38 Correct 27 ms 3796 KB Output is correct
39 Correct 27 ms 3844 KB Output is correct
40 Correct 27 ms 3864 KB Output is correct
41 Correct 13 ms 2644 KB Output is correct
42 Correct 14 ms 2644 KB Output is correct
43 Correct 14 ms 2644 KB Output is correct
44 Correct 13 ms 2740 KB Output is correct
45 Correct 13 ms 2644 KB Output is correct
46 Correct 13 ms 2736 KB Output is correct
47 Correct 18 ms 2892 KB Output is correct
48 Correct 18 ms 2900 KB Output is correct
49 Correct 19 ms 3100 KB Output is correct
50 Correct 24 ms 3412 KB Output is correct
51 Correct 29 ms 3900 KB Output is correct
52 Correct 29 ms 3836 KB Output is correct
53 Correct 29 ms 3888 KB Output is correct
54 Correct 28 ms 3900 KB Output is correct
55 Correct 28 ms 3896 KB Output is correct
56 Correct 28 ms 3796 KB Output is correct
57 Correct 28 ms 3828 KB Output is correct
58 Correct 28 ms 3880 KB Output is correct
59 Correct 29 ms 3804 KB Output is correct
60 Correct 33 ms 3860 KB Output is correct
61 Correct 28 ms 3792 KB Output is correct
62 Correct 28 ms 3880 KB Output is correct
63 Correct 28 ms 3796 KB Output is correct
64 Correct 28 ms 3916 KB Output is correct
65 Correct 28 ms 3816 KB Output is correct
66 Correct 28 ms 3900 KB Output is correct
67 Correct 30 ms 3908 KB Output is correct
68 Correct 28 ms 3904 KB Output is correct
69 Correct 29 ms 3888 KB Output is correct
70 Correct 28 ms 3852 KB Output is correct
71 Correct 17 ms 2900 KB Output is correct
72 Correct 17 ms 2900 KB Output is correct
73 Correct 16 ms 2900 KB Output is correct
74 Correct 17 ms 2772 KB Output is correct
75 Correct 17 ms 2900 KB Output is correct
76 Correct 22 ms 3092 KB Output is correct
77 Correct 19 ms 3092 KB Output is correct
78 Correct 19 ms 3096 KB Output is correct
79 Correct 20 ms 3008 KB Output is correct
80 Correct 20 ms 3064 KB Output is correct
81 Correct 24 ms 3412 KB Output is correct
82 Correct 25 ms 3480 KB Output is correct
83 Correct 24 ms 3396 KB Output is correct
84 Correct 25 ms 3412 KB Output is correct
85 Correct 27 ms 3412 KB Output is correct
86 Correct 27 ms 3448 KB Output is correct
87 Correct 26 ms 3480 KB Output is correct
88 Correct 26 ms 3468 KB Output is correct
89 Correct 27 ms 3412 KB Output is correct
90 Correct 26 ms 3476 KB Output is correct
91 Correct 32 ms 3796 KB Output is correct
92 Correct 32 ms 3852 KB Output is correct
93 Correct 32 ms 3792 KB Output is correct
94 Correct 31 ms 3772 KB Output is correct
95 Correct 32 ms 3788 KB Output is correct
96 Correct 33 ms 3752 KB Output is correct
97 Correct 34 ms 3796 KB Output is correct
98 Correct 36 ms 3856 KB Output is correct
99 Correct 33 ms 3772 KB Output is correct
100 Correct 33 ms 3752 KB Output is correct
101 Correct 33 ms 3752 KB Output is correct
102 Correct 33 ms 3796 KB Output is correct
103 Correct 34 ms 3764 KB Output is correct
104 Correct 33 ms 3796 KB Output is correct
105 Correct 34 ms 3824 KB Output is correct
106 Correct 32 ms 3796 KB Output is correct
107 Correct 31 ms 3868 KB Output is correct
108 Correct 32 ms 3796 KB Output is correct
109 Correct 32 ms 3796 KB Output is correct
110 Correct 32 ms 3832 KB Output is correct
111 Correct 37 ms 3788 KB Output is correct
112 Correct 32 ms 3796 KB Output is correct
113 Correct 32 ms 3812 KB Output is correct
114 Correct 32 ms 3848 KB Output is correct
115 Correct 32 ms 3772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2644 KB Output is correct
2 Correct 12 ms 2644 KB Output is correct
3 Correct 13 ms 2688 KB Output is correct
4 Correct 15 ms 2700 KB Output is correct
5 Correct 13 ms 2644 KB Output is correct
6 Correct 13 ms 2696 KB Output is correct
7 Correct 11 ms 2644 KB Output is correct
8 Correct 12 ms 2688 KB Output is correct
9 Correct 13 ms 2644 KB Output is correct
10 Correct 12 ms 2644 KB Output is correct
11 Correct 13 ms 2696 KB Output is correct
12 Correct 14 ms 2696 KB Output is correct
13 Correct 14 ms 2732 KB Output is correct
14 Correct 18 ms 3028 KB Output is correct
15 Correct 21 ms 3480 KB Output is correct
16 Correct 26 ms 3868 KB Output is correct
17 Correct 26 ms 3864 KB Output is correct
18 Correct 26 ms 3820 KB Output is correct
19 Correct 26 ms 3832 KB Output is correct
20 Correct 29 ms 3968 KB Output is correct
21 Correct 26 ms 3796 KB Output is correct
22 Correct 26 ms 3792 KB Output is correct
23 Correct 26 ms 3744 KB Output is correct
24 Correct 26 ms 3864 KB Output is correct
25 Correct 26 ms 3796 KB Output is correct
26 Correct 13 ms 2688 KB Output is correct
27 Correct 14 ms 2700 KB Output is correct
28 Correct 14 ms 2644 KB Output is correct
29 Correct 18 ms 3092 KB Output is correct
30 Correct 22 ms 3424 KB Output is correct
31 Correct 27 ms 3868 KB Output is correct
32 Correct 26 ms 3796 KB Output is correct
33 Correct 29 ms 3824 KB Output is correct
34 Correct 27 ms 3796 KB Output is correct
35 Correct 26 ms 3832 KB Output is correct
36 Correct 27 ms 3800 KB Output is correct
37 Correct 27 ms 3832 KB Output is correct
38 Correct 27 ms 3796 KB Output is correct
39 Correct 27 ms 3844 KB Output is correct
40 Correct 27 ms 3864 KB Output is correct
41 Correct 13 ms 2644 KB Output is correct
42 Correct 14 ms 2644 KB Output is correct
43 Correct 14 ms 2644 KB Output is correct
44 Correct 13 ms 2740 KB Output is correct
45 Correct 13 ms 2644 KB Output is correct
46 Correct 13 ms 2736 KB Output is correct
47 Correct 18 ms 2892 KB Output is correct
48 Correct 18 ms 2900 KB Output is correct
49 Correct 19 ms 3100 KB Output is correct
50 Correct 24 ms 3412 KB Output is correct
51 Correct 29 ms 3900 KB Output is correct
52 Correct 29 ms 3836 KB Output is correct
53 Correct 29 ms 3888 KB Output is correct
54 Correct 28 ms 3900 KB Output is correct
55 Correct 28 ms 3896 KB Output is correct
56 Correct 28 ms 3796 KB Output is correct
57 Correct 28 ms 3828 KB Output is correct
58 Correct 28 ms 3880 KB Output is correct
59 Correct 29 ms 3804 KB Output is correct
60 Correct 33 ms 3860 KB Output is correct
61 Correct 28 ms 3792 KB Output is correct
62 Correct 28 ms 3880 KB Output is correct
63 Correct 28 ms 3796 KB Output is correct
64 Correct 28 ms 3916 KB Output is correct
65 Correct 28 ms 3816 KB Output is correct
66 Correct 28 ms 3900 KB Output is correct
67 Correct 30 ms 3908 KB Output is correct
68 Correct 28 ms 3904 KB Output is correct
69 Correct 29 ms 3888 KB Output is correct
70 Correct 28 ms 3852 KB Output is correct
71 Correct 17 ms 2900 KB Output is correct
72 Correct 17 ms 2900 KB Output is correct
73 Correct 16 ms 2900 KB Output is correct
74 Correct 17 ms 2772 KB Output is correct
75 Correct 17 ms 2900 KB Output is correct
76 Correct 22 ms 3092 KB Output is correct
77 Correct 19 ms 3092 KB Output is correct
78 Correct 19 ms 3096 KB Output is correct
79 Correct 20 ms 3008 KB Output is correct
80 Correct 20 ms 3064 KB Output is correct
81 Correct 24 ms 3412 KB Output is correct
82 Correct 25 ms 3480 KB Output is correct
83 Correct 24 ms 3396 KB Output is correct
84 Correct 25 ms 3412 KB Output is correct
85 Correct 27 ms 3412 KB Output is correct
86 Correct 27 ms 3448 KB Output is correct
87 Correct 26 ms 3480 KB Output is correct
88 Correct 26 ms 3468 KB Output is correct
89 Correct 27 ms 3412 KB Output is correct
90 Correct 26 ms 3476 KB Output is correct
91 Correct 32 ms 3796 KB Output is correct
92 Correct 32 ms 3852 KB Output is correct
93 Correct 32 ms 3792 KB Output is correct
94 Correct 31 ms 3772 KB Output is correct
95 Correct 32 ms 3788 KB Output is correct
96 Correct 33 ms 3752 KB Output is correct
97 Correct 34 ms 3796 KB Output is correct
98 Correct 36 ms 3856 KB Output is correct
99 Correct 33 ms 3772 KB Output is correct
100 Correct 33 ms 3752 KB Output is correct
101 Correct 33 ms 3752 KB Output is correct
102 Correct 33 ms 3796 KB Output is correct
103 Correct 34 ms 3764 KB Output is correct
104 Correct 33 ms 3796 KB Output is correct
105 Correct 34 ms 3824 KB Output is correct
106 Correct 32 ms 3796 KB Output is correct
107 Correct 31 ms 3868 KB Output is correct
108 Correct 32 ms 3796 KB Output is correct
109 Correct 32 ms 3796 KB Output is correct
110 Correct 32 ms 3832 KB Output is correct
111 Correct 37 ms 3788 KB Output is correct
112 Correct 32 ms 3796 KB Output is correct
113 Correct 32 ms 3812 KB Output is correct
114 Correct 32 ms 3848 KB Output is correct
115 Correct 32 ms 3772 KB Output is correct
116 Correct 421 ms 4080 KB Output is correct
117 Correct 518 ms 4164 KB Output is correct
118 Correct 2202 ms 4372 KB Output is correct
119 Correct 2914 ms 4384 KB Output is correct
120 Correct 2743 ms 4376 KB Output is correct
121 Execution timed out 7019 ms 4044 KB Time limit exceeded
122 Halted 0 ms 0 KB -