Submission #789758

# Submission time Handle Problem Language Result Execution time Memory
789758 2023-07-22T00:54:06 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 4764 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) {
            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;
    }

    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 13 ms 2644 KB Output is correct
2 Correct 12 ms 2692 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 14 ms 2692 KB Output is correct
5 Correct 13 ms 2696 KB Output is correct
6 Correct 13 ms 2696 KB Output is correct
7 Correct 12 ms 2644 KB Output is correct
8 Correct 12 ms 2696 KB Output is correct
9 Correct 12 ms 2692 KB Output is correct
10 Correct 13 ms 2696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 14 ms 2700 KB Output is correct
3 Correct 15 ms 2652 KB Output is correct
4 Correct 17 ms 3280 KB Output is correct
5 Correct 22 ms 3724 KB Output is correct
6 Correct 26 ms 4116 KB Output is correct
7 Correct 26 ms 4128 KB Output is correct
8 Correct 27 ms 4088 KB Output is correct
9 Correct 28 ms 4088 KB Output is correct
10 Correct 27 ms 4100 KB Output is correct
11 Correct 26 ms 4104 KB Output is correct
12 Correct 26 ms 4024 KB Output is correct
13 Correct 27 ms 4124 KB Output is correct
14 Correct 27 ms 4040 KB Output is correct
15 Correct 27 ms 4272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 14 ms 2700 KB Output is correct
3 Correct 15 ms 2652 KB Output is correct
4 Correct 17 ms 3280 KB Output is correct
5 Correct 22 ms 3724 KB Output is correct
6 Correct 26 ms 4116 KB Output is correct
7 Correct 26 ms 4128 KB Output is correct
8 Correct 27 ms 4088 KB Output is correct
9 Correct 28 ms 4088 KB Output is correct
10 Correct 27 ms 4100 KB Output is correct
11 Correct 26 ms 4104 KB Output is correct
12 Correct 26 ms 4024 KB Output is correct
13 Correct 27 ms 4124 KB Output is correct
14 Correct 27 ms 4040 KB Output is correct
15 Correct 27 ms 4272 KB Output is correct
16 Correct 17 ms 2644 KB Output is correct
17 Correct 14 ms 2704 KB Output is correct
18 Correct 15 ms 2656 KB Output is correct
19 Correct 18 ms 3344 KB Output is correct
20 Correct 24 ms 3672 KB Output is correct
21 Correct 28 ms 4108 KB Output is correct
22 Correct 28 ms 4084 KB Output is correct
23 Correct 27 ms 4000 KB Output is correct
24 Correct 28 ms 4020 KB Output is correct
25 Correct 28 ms 4052 KB Output is correct
26 Correct 28 ms 4208 KB Output is correct
27 Correct 29 ms 4044 KB Output is correct
28 Correct 27 ms 3984 KB Output is correct
29 Correct 28 ms 4052 KB Output is correct
30 Correct 28 ms 4044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 14 ms 2700 KB Output is correct
3 Correct 15 ms 2652 KB Output is correct
4 Correct 17 ms 3280 KB Output is correct
5 Correct 22 ms 3724 KB Output is correct
6 Correct 26 ms 4116 KB Output is correct
7 Correct 26 ms 4128 KB Output is correct
8 Correct 27 ms 4088 KB Output is correct
9 Correct 28 ms 4088 KB Output is correct
10 Correct 27 ms 4100 KB Output is correct
11 Correct 26 ms 4104 KB Output is correct
12 Correct 26 ms 4024 KB Output is correct
13 Correct 27 ms 4124 KB Output is correct
14 Correct 27 ms 4040 KB Output is correct
15 Correct 27 ms 4272 KB Output is correct
16 Correct 17 ms 2644 KB Output is correct
17 Correct 14 ms 2704 KB Output is correct
18 Correct 15 ms 2656 KB Output is correct
19 Correct 18 ms 3344 KB Output is correct
20 Correct 24 ms 3672 KB Output is correct
21 Correct 28 ms 4108 KB Output is correct
22 Correct 28 ms 4084 KB Output is correct
23 Correct 27 ms 4000 KB Output is correct
24 Correct 28 ms 4020 KB Output is correct
25 Correct 28 ms 4052 KB Output is correct
26 Correct 28 ms 4208 KB Output is correct
27 Correct 29 ms 4044 KB Output is correct
28 Correct 27 ms 3984 KB Output is correct
29 Correct 28 ms 4052 KB Output is correct
30 Correct 28 ms 4044 KB Output is correct
31 Correct 14 ms 2704 KB Output is correct
32 Correct 14 ms 2644 KB Output is correct
33 Correct 14 ms 2704 KB Output is correct
34 Correct 14 ms 2768 KB Output is correct
35 Correct 14 ms 2776 KB Output is correct
36 Correct 14 ms 2780 KB Output is correct
37 Correct 16 ms 3028 KB Output is correct
38 Correct 21 ms 3164 KB Output is correct
39 Correct 20 ms 3356 KB Output is correct
40 Correct 25 ms 3752 KB Output is correct
41 Correct 29 ms 4148 KB Output is correct
42 Correct 28 ms 4088 KB Output is correct
43 Correct 29 ms 4148 KB Output is correct
44 Correct 29 ms 4148 KB Output is correct
45 Correct 28 ms 4152 KB Output is correct
46 Correct 28 ms 4048 KB Output is correct
47 Correct 29 ms 4092 KB Output is correct
48 Correct 29 ms 4084 KB Output is correct
49 Correct 28 ms 4140 KB Output is correct
50 Correct 28 ms 4116 KB Output is correct
51 Correct 29 ms 4152 KB Output is correct
52 Correct 28 ms 4060 KB Output is correct
53 Correct 30 ms 4116 KB Output is correct
54 Correct 30 ms 4064 KB Output is correct
55 Correct 28 ms 4148 KB Output is correct
56 Correct 29 ms 4152 KB Output is correct
57 Correct 30 ms 4156 KB Output is correct
58 Correct 29 ms 4180 KB Output is correct
59 Correct 29 ms 4044 KB Output is correct
60 Correct 28 ms 4132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 12 ms 2692 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 14 ms 2692 KB Output is correct
5 Correct 13 ms 2696 KB Output is correct
6 Correct 13 ms 2696 KB Output is correct
7 Correct 12 ms 2644 KB Output is correct
8 Correct 12 ms 2696 KB Output is correct
9 Correct 12 ms 2692 KB Output is correct
10 Correct 13 ms 2696 KB Output is correct
11 Correct 13 ms 2644 KB Output is correct
12 Correct 14 ms 2700 KB Output is correct
13 Correct 15 ms 2652 KB Output is correct
14 Correct 17 ms 3280 KB Output is correct
15 Correct 22 ms 3724 KB Output is correct
16 Correct 26 ms 4116 KB Output is correct
17 Correct 26 ms 4128 KB Output is correct
18 Correct 27 ms 4088 KB Output is correct
19 Correct 28 ms 4088 KB Output is correct
20 Correct 27 ms 4100 KB Output is correct
21 Correct 26 ms 4104 KB Output is correct
22 Correct 26 ms 4024 KB Output is correct
23 Correct 27 ms 4124 KB Output is correct
24 Correct 27 ms 4040 KB Output is correct
25 Correct 27 ms 4272 KB Output is correct
26 Correct 17 ms 2644 KB Output is correct
27 Correct 14 ms 2704 KB Output is correct
28 Correct 15 ms 2656 KB Output is correct
29 Correct 18 ms 3344 KB Output is correct
30 Correct 24 ms 3672 KB Output is correct
31 Correct 28 ms 4108 KB Output is correct
32 Correct 28 ms 4084 KB Output is correct
33 Correct 27 ms 4000 KB Output is correct
34 Correct 28 ms 4020 KB Output is correct
35 Correct 28 ms 4052 KB Output is correct
36 Correct 28 ms 4208 KB Output is correct
37 Correct 29 ms 4044 KB Output is correct
38 Correct 27 ms 3984 KB Output is correct
39 Correct 28 ms 4052 KB Output is correct
40 Correct 28 ms 4044 KB Output is correct
41 Correct 14 ms 2704 KB Output is correct
42 Correct 14 ms 2644 KB Output is correct
43 Correct 14 ms 2704 KB Output is correct
44 Correct 14 ms 2768 KB Output is correct
45 Correct 14 ms 2776 KB Output is correct
46 Correct 14 ms 2780 KB Output is correct
47 Correct 16 ms 3028 KB Output is correct
48 Correct 21 ms 3164 KB Output is correct
49 Correct 20 ms 3356 KB Output is correct
50 Correct 25 ms 3752 KB Output is correct
51 Correct 29 ms 4148 KB Output is correct
52 Correct 28 ms 4088 KB Output is correct
53 Correct 29 ms 4148 KB Output is correct
54 Correct 29 ms 4148 KB Output is correct
55 Correct 28 ms 4152 KB Output is correct
56 Correct 28 ms 4048 KB Output is correct
57 Correct 29 ms 4092 KB Output is correct
58 Correct 29 ms 4084 KB Output is correct
59 Correct 28 ms 4140 KB Output is correct
60 Correct 28 ms 4116 KB Output is correct
61 Correct 29 ms 4152 KB Output is correct
62 Correct 28 ms 4060 KB Output is correct
63 Correct 30 ms 4116 KB Output is correct
64 Correct 30 ms 4064 KB Output is correct
65 Correct 28 ms 4148 KB Output is correct
66 Correct 29 ms 4152 KB Output is correct
67 Correct 30 ms 4156 KB Output is correct
68 Correct 29 ms 4180 KB Output is correct
69 Correct 29 ms 4044 KB Output is correct
70 Correct 28 ms 4132 KB Output is correct
71 Correct 22 ms 3032 KB Output is correct
72 Correct 21 ms 3020 KB Output is correct
73 Correct 17 ms 3028 KB Output is correct
74 Correct 18 ms 3052 KB Output is correct
75 Correct 19 ms 3036 KB Output is correct
76 Correct 19 ms 3284 KB Output is correct
77 Correct 19 ms 3300 KB Output is correct
78 Correct 20 ms 3340 KB Output is correct
79 Correct 20 ms 3348 KB Output is correct
80 Correct 20 ms 3236 KB Output is correct
81 Correct 25 ms 3668 KB Output is correct
82 Correct 25 ms 3644 KB Output is correct
83 Correct 27 ms 3788 KB Output is correct
84 Correct 26 ms 3724 KB Output is correct
85 Correct 25 ms 3632 KB Output is correct
86 Correct 27 ms 3672 KB Output is correct
87 Correct 27 ms 3668 KB Output is correct
88 Correct 27 ms 3728 KB Output is correct
89 Correct 27 ms 3612 KB Output is correct
90 Correct 27 ms 3672 KB Output is correct
91 Correct 32 ms 4060 KB Output is correct
92 Correct 32 ms 4052 KB Output is correct
93 Correct 32 ms 4004 KB Output is correct
94 Correct 36 ms 4036 KB Output is correct
95 Correct 32 ms 4016 KB Output is correct
96 Correct 34 ms 4000 KB Output is correct
97 Correct 34 ms 4048 KB Output is correct
98 Correct 33 ms 4044 KB Output is correct
99 Correct 34 ms 4104 KB Output is correct
100 Correct 33 ms 4024 KB Output is correct
101 Correct 34 ms 4108 KB Output is correct
102 Correct 34 ms 4104 KB Output is correct
103 Correct 35 ms 4284 KB Output is correct
104 Correct 33 ms 4060 KB Output is correct
105 Correct 33 ms 4192 KB Output is correct
106 Correct 33 ms 4044 KB Output is correct
107 Correct 33 ms 4044 KB Output is correct
108 Correct 32 ms 4032 KB Output is correct
109 Correct 32 ms 4080 KB Output is correct
110 Correct 32 ms 4060 KB Output is correct
111 Correct 33 ms 3980 KB Output is correct
112 Correct 33 ms 4116 KB Output is correct
113 Correct 33 ms 4104 KB Output is correct
114 Correct 33 ms 3988 KB Output is correct
115 Correct 33 ms 4108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 12 ms 2692 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 14 ms 2692 KB Output is correct
5 Correct 13 ms 2696 KB Output is correct
6 Correct 13 ms 2696 KB Output is correct
7 Correct 12 ms 2644 KB Output is correct
8 Correct 12 ms 2696 KB Output is correct
9 Correct 12 ms 2692 KB Output is correct
10 Correct 13 ms 2696 KB Output is correct
11 Correct 13 ms 2644 KB Output is correct
12 Correct 14 ms 2700 KB Output is correct
13 Correct 15 ms 2652 KB Output is correct
14 Correct 17 ms 3280 KB Output is correct
15 Correct 22 ms 3724 KB Output is correct
16 Correct 26 ms 4116 KB Output is correct
17 Correct 26 ms 4128 KB Output is correct
18 Correct 27 ms 4088 KB Output is correct
19 Correct 28 ms 4088 KB Output is correct
20 Correct 27 ms 4100 KB Output is correct
21 Correct 26 ms 4104 KB Output is correct
22 Correct 26 ms 4024 KB Output is correct
23 Correct 27 ms 4124 KB Output is correct
24 Correct 27 ms 4040 KB Output is correct
25 Correct 27 ms 4272 KB Output is correct
26 Correct 17 ms 2644 KB Output is correct
27 Correct 14 ms 2704 KB Output is correct
28 Correct 15 ms 2656 KB Output is correct
29 Correct 18 ms 3344 KB Output is correct
30 Correct 24 ms 3672 KB Output is correct
31 Correct 28 ms 4108 KB Output is correct
32 Correct 28 ms 4084 KB Output is correct
33 Correct 27 ms 4000 KB Output is correct
34 Correct 28 ms 4020 KB Output is correct
35 Correct 28 ms 4052 KB Output is correct
36 Correct 28 ms 4208 KB Output is correct
37 Correct 29 ms 4044 KB Output is correct
38 Correct 27 ms 3984 KB Output is correct
39 Correct 28 ms 4052 KB Output is correct
40 Correct 28 ms 4044 KB Output is correct
41 Correct 14 ms 2704 KB Output is correct
42 Correct 14 ms 2644 KB Output is correct
43 Correct 14 ms 2704 KB Output is correct
44 Correct 14 ms 2768 KB Output is correct
45 Correct 14 ms 2776 KB Output is correct
46 Correct 14 ms 2780 KB Output is correct
47 Correct 16 ms 3028 KB Output is correct
48 Correct 21 ms 3164 KB Output is correct
49 Correct 20 ms 3356 KB Output is correct
50 Correct 25 ms 3752 KB Output is correct
51 Correct 29 ms 4148 KB Output is correct
52 Correct 28 ms 4088 KB Output is correct
53 Correct 29 ms 4148 KB Output is correct
54 Correct 29 ms 4148 KB Output is correct
55 Correct 28 ms 4152 KB Output is correct
56 Correct 28 ms 4048 KB Output is correct
57 Correct 29 ms 4092 KB Output is correct
58 Correct 29 ms 4084 KB Output is correct
59 Correct 28 ms 4140 KB Output is correct
60 Correct 28 ms 4116 KB Output is correct
61 Correct 29 ms 4152 KB Output is correct
62 Correct 28 ms 4060 KB Output is correct
63 Correct 30 ms 4116 KB Output is correct
64 Correct 30 ms 4064 KB Output is correct
65 Correct 28 ms 4148 KB Output is correct
66 Correct 29 ms 4152 KB Output is correct
67 Correct 30 ms 4156 KB Output is correct
68 Correct 29 ms 4180 KB Output is correct
69 Correct 29 ms 4044 KB Output is correct
70 Correct 28 ms 4132 KB Output is correct
71 Correct 22 ms 3032 KB Output is correct
72 Correct 21 ms 3020 KB Output is correct
73 Correct 17 ms 3028 KB Output is correct
74 Correct 18 ms 3052 KB Output is correct
75 Correct 19 ms 3036 KB Output is correct
76 Correct 19 ms 3284 KB Output is correct
77 Correct 19 ms 3300 KB Output is correct
78 Correct 20 ms 3340 KB Output is correct
79 Correct 20 ms 3348 KB Output is correct
80 Correct 20 ms 3236 KB Output is correct
81 Correct 25 ms 3668 KB Output is correct
82 Correct 25 ms 3644 KB Output is correct
83 Correct 27 ms 3788 KB Output is correct
84 Correct 26 ms 3724 KB Output is correct
85 Correct 25 ms 3632 KB Output is correct
86 Correct 27 ms 3672 KB Output is correct
87 Correct 27 ms 3668 KB Output is correct
88 Correct 27 ms 3728 KB Output is correct
89 Correct 27 ms 3612 KB Output is correct
90 Correct 27 ms 3672 KB Output is correct
91 Correct 32 ms 4060 KB Output is correct
92 Correct 32 ms 4052 KB Output is correct
93 Correct 32 ms 4004 KB Output is correct
94 Correct 36 ms 4036 KB Output is correct
95 Correct 32 ms 4016 KB Output is correct
96 Correct 34 ms 4000 KB Output is correct
97 Correct 34 ms 4048 KB Output is correct
98 Correct 33 ms 4044 KB Output is correct
99 Correct 34 ms 4104 KB Output is correct
100 Correct 33 ms 4024 KB Output is correct
101 Correct 34 ms 4108 KB Output is correct
102 Correct 34 ms 4104 KB Output is correct
103 Correct 35 ms 4284 KB Output is correct
104 Correct 33 ms 4060 KB Output is correct
105 Correct 33 ms 4192 KB Output is correct
106 Correct 33 ms 4044 KB Output is correct
107 Correct 33 ms 4044 KB Output is correct
108 Correct 32 ms 4032 KB Output is correct
109 Correct 32 ms 4080 KB Output is correct
110 Correct 32 ms 4060 KB Output is correct
111 Correct 33 ms 3980 KB Output is correct
112 Correct 33 ms 4116 KB Output is correct
113 Correct 33 ms 4104 KB Output is correct
114 Correct 33 ms 3988 KB Output is correct
115 Correct 33 ms 4108 KB Output is correct
116 Correct 412 ms 4336 KB Output is correct
117 Correct 516 ms 4416 KB Output is correct
118 Correct 2192 ms 4624 KB Output is correct
119 Correct 2924 ms 4764 KB Output is correct
120 Correct 2724 ms 4628 KB Output is correct
121 Execution timed out 7061 ms 4376 KB Time limit exceeded
122 Halted 0 ms 0 KB -