Submission #789759

# Submission time Handle Problem Language Result Execution time Memory
789759 2023-07-22T00:55:57 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 4640 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 13 ms 2700 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 13 ms 2692 KB Output is correct
5 Correct 13 ms 2692 KB Output is correct
6 Correct 14 ms 2692 KB Output is correct
7 Correct 13 ms 2692 KB Output is correct
8 Correct 12 ms 2644 KB Output is correct
9 Correct 13 ms 2644 KB Output is correct
10 Correct 13 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 13 ms 2700 KB Output is correct
3 Correct 14 ms 2756 KB Output is correct
4 Correct 17 ms 3216 KB Output is correct
5 Correct 22 ms 3696 KB Output is correct
6 Correct 28 ms 4052 KB Output is correct
7 Correct 26 ms 4060 KB Output is correct
8 Correct 27 ms 4060 KB Output is correct
9 Correct 27 ms 4112 KB Output is correct
10 Correct 27 ms 4036 KB Output is correct
11 Correct 26 ms 4044 KB Output is correct
12 Correct 26 ms 4040 KB Output is correct
13 Correct 27 ms 4052 KB Output is correct
14 Correct 26 ms 4052 KB Output is correct
15 Correct 27 ms 4044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 13 ms 2700 KB Output is correct
3 Correct 14 ms 2756 KB Output is correct
4 Correct 17 ms 3216 KB Output is correct
5 Correct 22 ms 3696 KB Output is correct
6 Correct 28 ms 4052 KB Output is correct
7 Correct 26 ms 4060 KB Output is correct
8 Correct 27 ms 4060 KB Output is correct
9 Correct 27 ms 4112 KB Output is correct
10 Correct 27 ms 4036 KB Output is correct
11 Correct 26 ms 4044 KB Output is correct
12 Correct 26 ms 4040 KB Output is correct
13 Correct 27 ms 4052 KB Output is correct
14 Correct 26 ms 4052 KB Output is correct
15 Correct 27 ms 4044 KB Output is correct
16 Correct 13 ms 2700 KB Output is correct
17 Correct 14 ms 2640 KB Output is correct
18 Correct 14 ms 2768 KB Output is correct
19 Correct 19 ms 3236 KB Output is correct
20 Correct 29 ms 3716 KB Output is correct
21 Correct 27 ms 4036 KB Output is correct
22 Correct 27 ms 4024 KB Output is correct
23 Correct 27 ms 4128 KB Output is correct
24 Correct 27 ms 4068 KB Output is correct
25 Correct 28 ms 4052 KB Output is correct
26 Correct 27 ms 4044 KB Output is correct
27 Correct 27 ms 4064 KB Output is correct
28 Correct 28 ms 4116 KB Output is correct
29 Correct 28 ms 3996 KB Output is correct
30 Correct 27 ms 4044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 13 ms 2700 KB Output is correct
3 Correct 14 ms 2756 KB Output is correct
4 Correct 17 ms 3216 KB Output is correct
5 Correct 22 ms 3696 KB Output is correct
6 Correct 28 ms 4052 KB Output is correct
7 Correct 26 ms 4060 KB Output is correct
8 Correct 27 ms 4060 KB Output is correct
9 Correct 27 ms 4112 KB Output is correct
10 Correct 27 ms 4036 KB Output is correct
11 Correct 26 ms 4044 KB Output is correct
12 Correct 26 ms 4040 KB Output is correct
13 Correct 27 ms 4052 KB Output is correct
14 Correct 26 ms 4052 KB Output is correct
15 Correct 27 ms 4044 KB Output is correct
16 Correct 13 ms 2700 KB Output is correct
17 Correct 14 ms 2640 KB Output is correct
18 Correct 14 ms 2768 KB Output is correct
19 Correct 19 ms 3236 KB Output is correct
20 Correct 29 ms 3716 KB Output is correct
21 Correct 27 ms 4036 KB Output is correct
22 Correct 27 ms 4024 KB Output is correct
23 Correct 27 ms 4128 KB Output is correct
24 Correct 27 ms 4068 KB Output is correct
25 Correct 28 ms 4052 KB Output is correct
26 Correct 27 ms 4044 KB Output is correct
27 Correct 27 ms 4064 KB Output is correct
28 Correct 28 ms 4116 KB Output is correct
29 Correct 28 ms 3996 KB Output is correct
30 Correct 27 ms 4044 KB Output is correct
31 Correct 13 ms 2644 KB Output is correct
32 Correct 14 ms 2700 KB Output is correct
33 Correct 14 ms 2704 KB Output is correct
34 Correct 14 ms 2776 KB Output is correct
35 Correct 14 ms 2780 KB Output is correct
36 Correct 14 ms 2772 KB Output is correct
37 Correct 17 ms 3036 KB Output is correct
38 Correct 16 ms 3028 KB Output is correct
39 Correct 20 ms 3284 KB Output is correct
40 Correct 24 ms 3648 KB Output is correct
41 Correct 29 ms 4044 KB Output is correct
42 Correct 29 ms 4052 KB Output is correct
43 Correct 30 ms 4056 KB Output is correct
44 Correct 29 ms 4156 KB Output is correct
45 Correct 34 ms 4176 KB Output is correct
46 Correct 29 ms 4148 KB Output is correct
47 Correct 29 ms 4156 KB Output is correct
48 Correct 29 ms 4184 KB Output is correct
49 Correct 29 ms 4112 KB Output is correct
50 Correct 29 ms 4052 KB Output is correct
51 Correct 29 ms 4152 KB Output is correct
52 Correct 28 ms 4044 KB Output is correct
53 Correct 29 ms 4156 KB Output is correct
54 Correct 29 ms 4136 KB Output is correct
55 Correct 32 ms 4172 KB Output is correct
56 Correct 28 ms 4040 KB Output is correct
57 Correct 29 ms 4044 KB Output is correct
58 Correct 29 ms 4156 KB Output is correct
59 Correct 28 ms 4144 KB Output is correct
60 Correct 29 ms 4044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2644 KB Output is correct
2 Correct 13 ms 2700 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 13 ms 2692 KB Output is correct
5 Correct 13 ms 2692 KB Output is correct
6 Correct 14 ms 2692 KB Output is correct
7 Correct 13 ms 2692 KB Output is correct
8 Correct 12 ms 2644 KB Output is correct
9 Correct 13 ms 2644 KB Output is correct
10 Correct 13 ms 2644 KB Output is correct
11 Correct 13 ms 2644 KB Output is correct
12 Correct 13 ms 2700 KB Output is correct
13 Correct 14 ms 2756 KB Output is correct
14 Correct 17 ms 3216 KB Output is correct
15 Correct 22 ms 3696 KB Output is correct
16 Correct 28 ms 4052 KB Output is correct
17 Correct 26 ms 4060 KB Output is correct
18 Correct 27 ms 4060 KB Output is correct
19 Correct 27 ms 4112 KB Output is correct
20 Correct 27 ms 4036 KB Output is correct
21 Correct 26 ms 4044 KB Output is correct
22 Correct 26 ms 4040 KB Output is correct
23 Correct 27 ms 4052 KB Output is correct
24 Correct 26 ms 4052 KB Output is correct
25 Correct 27 ms 4044 KB Output is correct
26 Correct 13 ms 2700 KB Output is correct
27 Correct 14 ms 2640 KB Output is correct
28 Correct 14 ms 2768 KB Output is correct
29 Correct 19 ms 3236 KB Output is correct
30 Correct 29 ms 3716 KB Output is correct
31 Correct 27 ms 4036 KB Output is correct
32 Correct 27 ms 4024 KB Output is correct
33 Correct 27 ms 4128 KB Output is correct
34 Correct 27 ms 4068 KB Output is correct
35 Correct 28 ms 4052 KB Output is correct
36 Correct 27 ms 4044 KB Output is correct
37 Correct 27 ms 4064 KB Output is correct
38 Correct 28 ms 4116 KB Output is correct
39 Correct 28 ms 3996 KB Output is correct
40 Correct 27 ms 4044 KB Output is correct
41 Correct 13 ms 2644 KB Output is correct
42 Correct 14 ms 2700 KB Output is correct
43 Correct 14 ms 2704 KB Output is correct
44 Correct 14 ms 2776 KB Output is correct
45 Correct 14 ms 2780 KB Output is correct
46 Correct 14 ms 2772 KB Output is correct
47 Correct 17 ms 3036 KB Output is correct
48 Correct 16 ms 3028 KB Output is correct
49 Correct 20 ms 3284 KB Output is correct
50 Correct 24 ms 3648 KB Output is correct
51 Correct 29 ms 4044 KB Output is correct
52 Correct 29 ms 4052 KB Output is correct
53 Correct 30 ms 4056 KB Output is correct
54 Correct 29 ms 4156 KB Output is correct
55 Correct 34 ms 4176 KB Output is correct
56 Correct 29 ms 4148 KB Output is correct
57 Correct 29 ms 4156 KB Output is correct
58 Correct 29 ms 4184 KB Output is correct
59 Correct 29 ms 4112 KB Output is correct
60 Correct 29 ms 4052 KB Output is correct
61 Correct 29 ms 4152 KB Output is correct
62 Correct 28 ms 4044 KB Output is correct
63 Correct 29 ms 4156 KB Output is correct
64 Correct 29 ms 4136 KB Output is correct
65 Correct 32 ms 4172 KB Output is correct
66 Correct 28 ms 4040 KB Output is correct
67 Correct 29 ms 4044 KB Output is correct
68 Correct 29 ms 4156 KB Output is correct
69 Correct 28 ms 4144 KB Output is correct
70 Correct 29 ms 4044 KB Output is correct
71 Correct 17 ms 2900 KB Output is correct
72 Correct 17 ms 3016 KB Output is correct
73 Correct 17 ms 3028 KB Output is correct
74 Correct 17 ms 3024 KB Output is correct
75 Correct 17 ms 3028 KB Output is correct
76 Correct 19 ms 3292 KB Output is correct
77 Correct 20 ms 3224 KB Output is correct
78 Correct 24 ms 3340 KB Output is correct
79 Correct 20 ms 3284 KB Output is correct
80 Correct 21 ms 3316 KB Output is correct
81 Correct 26 ms 3732 KB Output is correct
82 Correct 25 ms 3716 KB Output is correct
83 Correct 26 ms 3740 KB Output is correct
84 Correct 26 ms 3640 KB Output is correct
85 Correct 27 ms 3668 KB Output is correct
86 Correct 28 ms 3668 KB Output is correct
87 Correct 27 ms 3664 KB Output is correct
88 Correct 27 ms 3700 KB Output is correct
89 Correct 27 ms 3704 KB Output is correct
90 Correct 27 ms 3660 KB Output is correct
91 Correct 32 ms 4052 KB Output is correct
92 Correct 32 ms 4104 KB Output is correct
93 Correct 32 ms 4096 KB Output is correct
94 Correct 32 ms 4040 KB Output is correct
95 Correct 33 ms 4084 KB Output is correct
96 Correct 33 ms 4040 KB Output is correct
97 Correct 33 ms 4052 KB Output is correct
98 Correct 34 ms 4044 KB Output is correct
99 Correct 34 ms 4056 KB Output is correct
100 Correct 33 ms 4080 KB Output is correct
101 Correct 34 ms 4104 KB Output is correct
102 Correct 36 ms 4044 KB Output is correct
103 Correct 33 ms 4016 KB Output is correct
104 Correct 34 ms 4104 KB Output is correct
105 Correct 34 ms 4000 KB Output is correct
106 Correct 36 ms 4008 KB Output is correct
107 Correct 32 ms 4032 KB Output is correct
108 Correct 34 ms 4140 KB Output is correct
109 Correct 33 ms 4092 KB Output is correct
110 Correct 32 ms 4104 KB Output is correct
111 Correct 39 ms 4108 KB Output is correct
112 Correct 33 ms 4020 KB Output is correct
113 Correct 33 ms 4104 KB Output is correct
114 Correct 32 ms 4016 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 13 ms 2700 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 13 ms 2692 KB Output is correct
5 Correct 13 ms 2692 KB Output is correct
6 Correct 14 ms 2692 KB Output is correct
7 Correct 13 ms 2692 KB Output is correct
8 Correct 12 ms 2644 KB Output is correct
9 Correct 13 ms 2644 KB Output is correct
10 Correct 13 ms 2644 KB Output is correct
11 Correct 13 ms 2644 KB Output is correct
12 Correct 13 ms 2700 KB Output is correct
13 Correct 14 ms 2756 KB Output is correct
14 Correct 17 ms 3216 KB Output is correct
15 Correct 22 ms 3696 KB Output is correct
16 Correct 28 ms 4052 KB Output is correct
17 Correct 26 ms 4060 KB Output is correct
18 Correct 27 ms 4060 KB Output is correct
19 Correct 27 ms 4112 KB Output is correct
20 Correct 27 ms 4036 KB Output is correct
21 Correct 26 ms 4044 KB Output is correct
22 Correct 26 ms 4040 KB Output is correct
23 Correct 27 ms 4052 KB Output is correct
24 Correct 26 ms 4052 KB Output is correct
25 Correct 27 ms 4044 KB Output is correct
26 Correct 13 ms 2700 KB Output is correct
27 Correct 14 ms 2640 KB Output is correct
28 Correct 14 ms 2768 KB Output is correct
29 Correct 19 ms 3236 KB Output is correct
30 Correct 29 ms 3716 KB Output is correct
31 Correct 27 ms 4036 KB Output is correct
32 Correct 27 ms 4024 KB Output is correct
33 Correct 27 ms 4128 KB Output is correct
34 Correct 27 ms 4068 KB Output is correct
35 Correct 28 ms 4052 KB Output is correct
36 Correct 27 ms 4044 KB Output is correct
37 Correct 27 ms 4064 KB Output is correct
38 Correct 28 ms 4116 KB Output is correct
39 Correct 28 ms 3996 KB Output is correct
40 Correct 27 ms 4044 KB Output is correct
41 Correct 13 ms 2644 KB Output is correct
42 Correct 14 ms 2700 KB Output is correct
43 Correct 14 ms 2704 KB Output is correct
44 Correct 14 ms 2776 KB Output is correct
45 Correct 14 ms 2780 KB Output is correct
46 Correct 14 ms 2772 KB Output is correct
47 Correct 17 ms 3036 KB Output is correct
48 Correct 16 ms 3028 KB Output is correct
49 Correct 20 ms 3284 KB Output is correct
50 Correct 24 ms 3648 KB Output is correct
51 Correct 29 ms 4044 KB Output is correct
52 Correct 29 ms 4052 KB Output is correct
53 Correct 30 ms 4056 KB Output is correct
54 Correct 29 ms 4156 KB Output is correct
55 Correct 34 ms 4176 KB Output is correct
56 Correct 29 ms 4148 KB Output is correct
57 Correct 29 ms 4156 KB Output is correct
58 Correct 29 ms 4184 KB Output is correct
59 Correct 29 ms 4112 KB Output is correct
60 Correct 29 ms 4052 KB Output is correct
61 Correct 29 ms 4152 KB Output is correct
62 Correct 28 ms 4044 KB Output is correct
63 Correct 29 ms 4156 KB Output is correct
64 Correct 29 ms 4136 KB Output is correct
65 Correct 32 ms 4172 KB Output is correct
66 Correct 28 ms 4040 KB Output is correct
67 Correct 29 ms 4044 KB Output is correct
68 Correct 29 ms 4156 KB Output is correct
69 Correct 28 ms 4144 KB Output is correct
70 Correct 29 ms 4044 KB Output is correct
71 Correct 17 ms 2900 KB Output is correct
72 Correct 17 ms 3016 KB Output is correct
73 Correct 17 ms 3028 KB Output is correct
74 Correct 17 ms 3024 KB Output is correct
75 Correct 17 ms 3028 KB Output is correct
76 Correct 19 ms 3292 KB Output is correct
77 Correct 20 ms 3224 KB Output is correct
78 Correct 24 ms 3340 KB Output is correct
79 Correct 20 ms 3284 KB Output is correct
80 Correct 21 ms 3316 KB Output is correct
81 Correct 26 ms 3732 KB Output is correct
82 Correct 25 ms 3716 KB Output is correct
83 Correct 26 ms 3740 KB Output is correct
84 Correct 26 ms 3640 KB Output is correct
85 Correct 27 ms 3668 KB Output is correct
86 Correct 28 ms 3668 KB Output is correct
87 Correct 27 ms 3664 KB Output is correct
88 Correct 27 ms 3700 KB Output is correct
89 Correct 27 ms 3704 KB Output is correct
90 Correct 27 ms 3660 KB Output is correct
91 Correct 32 ms 4052 KB Output is correct
92 Correct 32 ms 4104 KB Output is correct
93 Correct 32 ms 4096 KB Output is correct
94 Correct 32 ms 4040 KB Output is correct
95 Correct 33 ms 4084 KB Output is correct
96 Correct 33 ms 4040 KB Output is correct
97 Correct 33 ms 4052 KB Output is correct
98 Correct 34 ms 4044 KB Output is correct
99 Correct 34 ms 4056 KB Output is correct
100 Correct 33 ms 4080 KB Output is correct
101 Correct 34 ms 4104 KB Output is correct
102 Correct 36 ms 4044 KB Output is correct
103 Correct 33 ms 4016 KB Output is correct
104 Correct 34 ms 4104 KB Output is correct
105 Correct 34 ms 4000 KB Output is correct
106 Correct 36 ms 4008 KB Output is correct
107 Correct 32 ms 4032 KB Output is correct
108 Correct 34 ms 4140 KB Output is correct
109 Correct 33 ms 4092 KB Output is correct
110 Correct 32 ms 4104 KB Output is correct
111 Correct 39 ms 4108 KB Output is correct
112 Correct 33 ms 4020 KB Output is correct
113 Correct 33 ms 4104 KB Output is correct
114 Correct 32 ms 4016 KB Output is correct
115 Correct 33 ms 4108 KB Output is correct
116 Correct 413 ms 4336 KB Output is correct
117 Correct 513 ms 4332 KB Output is correct
118 Correct 2205 ms 4620 KB Output is correct
119 Correct 2916 ms 4636 KB Output is correct
120 Correct 2769 ms 4640 KB Output is correct
121 Execution timed out 7065 ms 4304 KB Time limit exceeded
122 Halted 0 ms 0 KB -