답안 #789757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789757 2023-07-22T00:43:15 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 5848 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);

    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 2644 KB Output is correct
2 Correct 13 ms 2644 KB Output is correct
3 Correct 12 ms 2692 KB Output is correct
4 Correct 13 ms 2696 KB Output is correct
5 Correct 13 ms 2692 KB Output is correct
6 Correct 13 ms 2644 KB Output is correct
7 Correct 13 ms 2700 KB Output is correct
8 Correct 12 ms 2640 KB Output is correct
9 Correct 13 ms 2696 KB Output is correct
10 Correct 12 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2696 KB Output is correct
2 Correct 13 ms 2696 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 18 ms 3092 KB Output is correct
5 Correct 22 ms 3480 KB Output is correct
6 Correct 26 ms 3832 KB Output is correct
7 Correct 25 ms 3868 KB Output is correct
8 Correct 29 ms 3852 KB Output is correct
9 Correct 26 ms 3848 KB Output is correct
10 Correct 31 ms 3828 KB Output is correct
11 Correct 26 ms 3868 KB Output is correct
12 Correct 25 ms 3848 KB Output is correct
13 Correct 26 ms 3872 KB Output is correct
14 Correct 25 ms 3796 KB Output is correct
15 Correct 25 ms 3864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2696 KB Output is correct
2 Correct 13 ms 2696 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 18 ms 3092 KB Output is correct
5 Correct 22 ms 3480 KB Output is correct
6 Correct 26 ms 3832 KB Output is correct
7 Correct 25 ms 3868 KB Output is correct
8 Correct 29 ms 3852 KB Output is correct
9 Correct 26 ms 3848 KB Output is correct
10 Correct 31 ms 3828 KB Output is correct
11 Correct 26 ms 3868 KB Output is correct
12 Correct 25 ms 3848 KB Output is correct
13 Correct 26 ms 3872 KB Output is correct
14 Correct 25 ms 3796 KB Output is correct
15 Correct 25 ms 3864 KB Output is correct
16 Correct 12 ms 2692 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 3028 KB Output is correct
20 Correct 23 ms 3440 KB Output is correct
21 Correct 26 ms 3872 KB Output is correct
22 Correct 26 ms 3856 KB Output is correct
23 Correct 30 ms 3860 KB Output is correct
24 Correct 27 ms 3792 KB Output is correct
25 Correct 28 ms 3760 KB Output is correct
26 Correct 28 ms 3840 KB Output is correct
27 Correct 27 ms 3844 KB Output is correct
28 Correct 27 ms 3848 KB Output is correct
29 Correct 28 ms 3888 KB Output is correct
30 Correct 26 ms 3784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2696 KB Output is correct
2 Correct 13 ms 2696 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Correct 18 ms 3092 KB Output is correct
5 Correct 22 ms 3480 KB Output is correct
6 Correct 26 ms 3832 KB Output is correct
7 Correct 25 ms 3868 KB Output is correct
8 Correct 29 ms 3852 KB Output is correct
9 Correct 26 ms 3848 KB Output is correct
10 Correct 31 ms 3828 KB Output is correct
11 Correct 26 ms 3868 KB Output is correct
12 Correct 25 ms 3848 KB Output is correct
13 Correct 26 ms 3872 KB Output is correct
14 Correct 25 ms 3796 KB Output is correct
15 Correct 25 ms 3864 KB Output is correct
16 Correct 12 ms 2692 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 3028 KB Output is correct
20 Correct 23 ms 3440 KB Output is correct
21 Correct 26 ms 3872 KB Output is correct
22 Correct 26 ms 3856 KB Output is correct
23 Correct 30 ms 3860 KB Output is correct
24 Correct 27 ms 3792 KB Output is correct
25 Correct 28 ms 3760 KB Output is correct
26 Correct 28 ms 3840 KB Output is correct
27 Correct 27 ms 3844 KB Output is correct
28 Correct 27 ms 3848 KB Output is correct
29 Correct 28 ms 3888 KB Output is correct
30 Correct 26 ms 3784 KB Output is correct
31 Correct 13 ms 2696 KB Output is correct
32 Correct 14 ms 2700 KB Output is correct
33 Correct 13 ms 2700 KB Output is correct
34 Correct 15 ms 2768 KB Output is correct
35 Correct 15 ms 2772 KB Output is correct
36 Correct 15 ms 2772 KB Output is correct
37 Correct 16 ms 3088 KB Output is correct
38 Correct 17 ms 3028 KB Output is correct
39 Correct 20 ms 3476 KB Output is correct
40 Correct 24 ms 4248 KB Output is correct
41 Correct 29 ms 4988 KB Output is correct
42 Correct 33 ms 5052 KB Output is correct
43 Correct 29 ms 4944 KB Output is correct
44 Correct 29 ms 4948 KB Output is correct
45 Correct 29 ms 5004 KB Output is correct
46 Correct 29 ms 4948 KB Output is correct
47 Correct 29 ms 4956 KB Output is correct
48 Correct 29 ms 5020 KB Output is correct
49 Correct 29 ms 5004 KB Output is correct
50 Correct 30 ms 4932 KB Output is correct
51 Correct 30 ms 4960 KB Output is correct
52 Correct 29 ms 4952 KB Output is correct
53 Correct 32 ms 5004 KB Output is correct
54 Correct 29 ms 4940 KB Output is correct
55 Correct 28 ms 4928 KB Output is correct
56 Correct 29 ms 5028 KB Output is correct
57 Correct 29 ms 4944 KB Output is correct
58 Correct 28 ms 4924 KB Output is correct
59 Correct 28 ms 5008 KB Output is correct
60 Correct 28 ms 5032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 2644 KB Output is correct
2 Correct 13 ms 2644 KB Output is correct
3 Correct 12 ms 2692 KB Output is correct
4 Correct 13 ms 2696 KB Output is correct
5 Correct 13 ms 2692 KB Output is correct
6 Correct 13 ms 2644 KB Output is correct
7 Correct 13 ms 2700 KB Output is correct
8 Correct 12 ms 2640 KB Output is correct
9 Correct 13 ms 2696 KB Output is correct
10 Correct 12 ms 2644 KB Output is correct
11 Correct 15 ms 2696 KB Output is correct
12 Correct 13 ms 2696 KB Output is correct
13 Correct 13 ms 2644 KB Output is correct
14 Correct 18 ms 3092 KB Output is correct
15 Correct 22 ms 3480 KB Output is correct
16 Correct 26 ms 3832 KB Output is correct
17 Correct 25 ms 3868 KB Output is correct
18 Correct 29 ms 3852 KB Output is correct
19 Correct 26 ms 3848 KB Output is correct
20 Correct 31 ms 3828 KB Output is correct
21 Correct 26 ms 3868 KB Output is correct
22 Correct 25 ms 3848 KB Output is correct
23 Correct 26 ms 3872 KB Output is correct
24 Correct 25 ms 3796 KB Output is correct
25 Correct 25 ms 3864 KB Output is correct
26 Correct 12 ms 2692 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 3028 KB Output is correct
30 Correct 23 ms 3440 KB Output is correct
31 Correct 26 ms 3872 KB Output is correct
32 Correct 26 ms 3856 KB Output is correct
33 Correct 30 ms 3860 KB Output is correct
34 Correct 27 ms 3792 KB Output is correct
35 Correct 28 ms 3760 KB Output is correct
36 Correct 28 ms 3840 KB Output is correct
37 Correct 27 ms 3844 KB Output is correct
38 Correct 27 ms 3848 KB Output is correct
39 Correct 28 ms 3888 KB Output is correct
40 Correct 26 ms 3784 KB Output is correct
41 Correct 13 ms 2696 KB Output is correct
42 Correct 14 ms 2700 KB Output is correct
43 Correct 13 ms 2700 KB Output is correct
44 Correct 15 ms 2768 KB Output is correct
45 Correct 15 ms 2772 KB Output is correct
46 Correct 15 ms 2772 KB Output is correct
47 Correct 16 ms 3088 KB Output is correct
48 Correct 17 ms 3028 KB Output is correct
49 Correct 20 ms 3476 KB Output is correct
50 Correct 24 ms 4248 KB Output is correct
51 Correct 29 ms 4988 KB Output is correct
52 Correct 33 ms 5052 KB Output is correct
53 Correct 29 ms 4944 KB Output is correct
54 Correct 29 ms 4948 KB Output is correct
55 Correct 29 ms 5004 KB Output is correct
56 Correct 29 ms 4948 KB Output is correct
57 Correct 29 ms 4956 KB Output is correct
58 Correct 29 ms 5020 KB Output is correct
59 Correct 29 ms 5004 KB Output is correct
60 Correct 30 ms 4932 KB Output is correct
61 Correct 30 ms 4960 KB Output is correct
62 Correct 29 ms 4952 KB Output is correct
63 Correct 32 ms 5004 KB Output is correct
64 Correct 29 ms 4940 KB Output is correct
65 Correct 28 ms 4928 KB Output is correct
66 Correct 29 ms 5028 KB Output is correct
67 Correct 29 ms 4944 KB Output is correct
68 Correct 28 ms 4924 KB Output is correct
69 Correct 28 ms 5008 KB Output is correct
70 Correct 28 ms 5032 KB Output is correct
71 Correct 17 ms 3088 KB Output is correct
72 Correct 17 ms 3028 KB Output is correct
73 Correct 19 ms 3024 KB Output is correct
74 Correct 19 ms 3232 KB Output is correct
75 Correct 17 ms 3120 KB Output is correct
76 Correct 20 ms 3496 KB Output is correct
77 Correct 19 ms 3540 KB Output is correct
78 Correct 19 ms 3568 KB Output is correct
79 Correct 20 ms 3568 KB Output is correct
80 Correct 19 ms 3540 KB Output is correct
81 Correct 26 ms 4416 KB Output is correct
82 Correct 26 ms 4436 KB Output is correct
83 Correct 25 ms 4332 KB Output is correct
84 Correct 25 ms 4428 KB Output is correct
85 Correct 25 ms 4424 KB Output is correct
86 Correct 27 ms 4436 KB Output is correct
87 Correct 27 ms 4436 KB Output is correct
88 Correct 28 ms 4556 KB Output is correct
89 Correct 28 ms 4496 KB Output is correct
90 Correct 28 ms 4512 KB Output is correct
91 Correct 33 ms 5568 KB Output is correct
92 Correct 32 ms 5528 KB Output is correct
93 Correct 34 ms 5480 KB Output is correct
94 Correct 32 ms 5588 KB Output is correct
95 Correct 32 ms 5460 KB Output is correct
96 Correct 33 ms 5736 KB Output is correct
97 Correct 33 ms 5708 KB Output is correct
98 Correct 34 ms 5708 KB Output is correct
99 Correct 35 ms 5820 KB Output is correct
100 Correct 34 ms 5688 KB Output is correct
101 Correct 34 ms 5736 KB Output is correct
102 Correct 34 ms 5660 KB Output is correct
103 Correct 35 ms 5748 KB Output is correct
104 Correct 34 ms 5644 KB Output is correct
105 Correct 34 ms 5648 KB Output is correct
106 Correct 34 ms 5708 KB Output is correct
107 Correct 33 ms 5772 KB Output is correct
108 Correct 33 ms 5776 KB Output is correct
109 Correct 34 ms 5848 KB Output is correct
110 Correct 33 ms 5716 KB Output is correct
111 Correct 34 ms 5708 KB Output is correct
112 Correct 33 ms 5720 KB Output is correct
113 Correct 33 ms 5788 KB Output is correct
114 Correct 33 ms 5704 KB Output is correct
115 Correct 33 ms 5708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 2644 KB Output is correct
2 Correct 13 ms 2644 KB Output is correct
3 Correct 12 ms 2692 KB Output is correct
4 Correct 13 ms 2696 KB Output is correct
5 Correct 13 ms 2692 KB Output is correct
6 Correct 13 ms 2644 KB Output is correct
7 Correct 13 ms 2700 KB Output is correct
8 Correct 12 ms 2640 KB Output is correct
9 Correct 13 ms 2696 KB Output is correct
10 Correct 12 ms 2644 KB Output is correct
11 Correct 15 ms 2696 KB Output is correct
12 Correct 13 ms 2696 KB Output is correct
13 Correct 13 ms 2644 KB Output is correct
14 Correct 18 ms 3092 KB Output is correct
15 Correct 22 ms 3480 KB Output is correct
16 Correct 26 ms 3832 KB Output is correct
17 Correct 25 ms 3868 KB Output is correct
18 Correct 29 ms 3852 KB Output is correct
19 Correct 26 ms 3848 KB Output is correct
20 Correct 31 ms 3828 KB Output is correct
21 Correct 26 ms 3868 KB Output is correct
22 Correct 25 ms 3848 KB Output is correct
23 Correct 26 ms 3872 KB Output is correct
24 Correct 25 ms 3796 KB Output is correct
25 Correct 25 ms 3864 KB Output is correct
26 Correct 12 ms 2692 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 3028 KB Output is correct
30 Correct 23 ms 3440 KB Output is correct
31 Correct 26 ms 3872 KB Output is correct
32 Correct 26 ms 3856 KB Output is correct
33 Correct 30 ms 3860 KB Output is correct
34 Correct 27 ms 3792 KB Output is correct
35 Correct 28 ms 3760 KB Output is correct
36 Correct 28 ms 3840 KB Output is correct
37 Correct 27 ms 3844 KB Output is correct
38 Correct 27 ms 3848 KB Output is correct
39 Correct 28 ms 3888 KB Output is correct
40 Correct 26 ms 3784 KB Output is correct
41 Correct 13 ms 2696 KB Output is correct
42 Correct 14 ms 2700 KB Output is correct
43 Correct 13 ms 2700 KB Output is correct
44 Correct 15 ms 2768 KB Output is correct
45 Correct 15 ms 2772 KB Output is correct
46 Correct 15 ms 2772 KB Output is correct
47 Correct 16 ms 3088 KB Output is correct
48 Correct 17 ms 3028 KB Output is correct
49 Correct 20 ms 3476 KB Output is correct
50 Correct 24 ms 4248 KB Output is correct
51 Correct 29 ms 4988 KB Output is correct
52 Correct 33 ms 5052 KB Output is correct
53 Correct 29 ms 4944 KB Output is correct
54 Correct 29 ms 4948 KB Output is correct
55 Correct 29 ms 5004 KB Output is correct
56 Correct 29 ms 4948 KB Output is correct
57 Correct 29 ms 4956 KB Output is correct
58 Correct 29 ms 5020 KB Output is correct
59 Correct 29 ms 5004 KB Output is correct
60 Correct 30 ms 4932 KB Output is correct
61 Correct 30 ms 4960 KB Output is correct
62 Correct 29 ms 4952 KB Output is correct
63 Correct 32 ms 5004 KB Output is correct
64 Correct 29 ms 4940 KB Output is correct
65 Correct 28 ms 4928 KB Output is correct
66 Correct 29 ms 5028 KB Output is correct
67 Correct 29 ms 4944 KB Output is correct
68 Correct 28 ms 4924 KB Output is correct
69 Correct 28 ms 5008 KB Output is correct
70 Correct 28 ms 5032 KB Output is correct
71 Correct 17 ms 3088 KB Output is correct
72 Correct 17 ms 3028 KB Output is correct
73 Correct 19 ms 3024 KB Output is correct
74 Correct 19 ms 3232 KB Output is correct
75 Correct 17 ms 3120 KB Output is correct
76 Correct 20 ms 3496 KB Output is correct
77 Correct 19 ms 3540 KB Output is correct
78 Correct 19 ms 3568 KB Output is correct
79 Correct 20 ms 3568 KB Output is correct
80 Correct 19 ms 3540 KB Output is correct
81 Correct 26 ms 4416 KB Output is correct
82 Correct 26 ms 4436 KB Output is correct
83 Correct 25 ms 4332 KB Output is correct
84 Correct 25 ms 4428 KB Output is correct
85 Correct 25 ms 4424 KB Output is correct
86 Correct 27 ms 4436 KB Output is correct
87 Correct 27 ms 4436 KB Output is correct
88 Correct 28 ms 4556 KB Output is correct
89 Correct 28 ms 4496 KB Output is correct
90 Correct 28 ms 4512 KB Output is correct
91 Correct 33 ms 5568 KB Output is correct
92 Correct 32 ms 5528 KB Output is correct
93 Correct 34 ms 5480 KB Output is correct
94 Correct 32 ms 5588 KB Output is correct
95 Correct 32 ms 5460 KB Output is correct
96 Correct 33 ms 5736 KB Output is correct
97 Correct 33 ms 5708 KB Output is correct
98 Correct 34 ms 5708 KB Output is correct
99 Correct 35 ms 5820 KB Output is correct
100 Correct 34 ms 5688 KB Output is correct
101 Correct 34 ms 5736 KB Output is correct
102 Correct 34 ms 5660 KB Output is correct
103 Correct 35 ms 5748 KB Output is correct
104 Correct 34 ms 5644 KB Output is correct
105 Correct 34 ms 5648 KB Output is correct
106 Correct 34 ms 5708 KB Output is correct
107 Correct 33 ms 5772 KB Output is correct
108 Correct 33 ms 5776 KB Output is correct
109 Correct 34 ms 5848 KB Output is correct
110 Correct 33 ms 5716 KB Output is correct
111 Correct 34 ms 5708 KB Output is correct
112 Correct 33 ms 5720 KB Output is correct
113 Correct 33 ms 5788 KB Output is correct
114 Correct 33 ms 5704 KB Output is correct
115 Correct 33 ms 5708 KB Output is correct
116 Correct 1472 ms 4584 KB Output is correct
117 Correct 1518 ms 4672 KB Output is correct
118 Correct 4009 ms 5024 KB Output is correct
119 Correct 4393 ms 5068 KB Output is correct
120 Correct 4232 ms 5132 KB Output is correct
121 Execution timed out 7074 ms 5020 KB Time limit exceeded
122 Halted 0 ms 0 KB -