Submission #789559

# Submission time Handle Problem Language Result Execution time Memory
789559 2023-07-21T13:45:25 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 26904 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q, b;
int arr[303030];
ll cnt[303030], cnt2[303030], reord[303030], ans = 0, res[50505];

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);
}

struct seg {
    ll sum[4 * m], xsum[4 * m];
    void init(int v, int st, int ed) {
        if (st == ed) {
            sum[v] = reord[st];
            xsum[v] = (ll)st * reord[st];
            return;
        }
        int mid = (st + ed) / 2;
        init(2 * v, st, mid);
        init(2 * v + 1, mid + 1, ed);
        sum[v] = sum[2 * v] + sum[2 * v + 1];
        xsum[v] = xsum[2 * v] + xsum[2 * v + 1];
    }
    void update(int v, int st, int ed, int idx, ll diff) {
        if (st == idx && ed == idx) {
            sum[v] += diff;
            xsum[v] += (ll)st * diff;
            return;
        }
        if (idx < st || idx > ed) return;
        int mid = (st + ed) / 2;
        update(2 * v, st, mid, idx, diff);
        update(2 * v + 1, mid + 1, ed, idx, diff);
        sum[v] = sum[2 * v] + sum[2 * v + 1];
        xsum[v] = xsum[2 * v] + xsum[2 * v + 1];
    }
    ll getsum(int v, int st, int ed, int lt, int rt) {
        if (ed < lt || st > rt) return 0;
        if (lt <= st && ed <= rt) return sum[v];
        int mid = (st + ed) / 2;
        return getsum(2 * v, st, mid, lt, rt) + getsum(2 * v + 1, mid + 1, ed, lt, rt);
    }
    ll getxsum(int v, int st, int ed, int lt, int rt) {
        if (ed < lt || st > rt) return 0;
        if (lt <= st && ed <= rt) return xsum[v];
        int mid = (st + ed) / 2;
        return getxsum(2 * v, st, mid, lt, rt) + getxsum(2 * v + 1, mid + 1, ed, lt, rt);
    }
} s;

void init(int p) {
    for (int i = 1; i <= m; i++) cnt[i] = 0;
    ans = 0;

    for (int i = Q[p].lt; i <= Q[p].rt; i++) cnt[arr[i]]++;
    for (int i = 1; i <= m; i++) cnt2[i] = cnt[i];

    sort(cnt2 + 1, cnt2 + m + 1);

    for (int i = 1; i <= m/2; i++) {
        reord[i] = cnt2[2 * i - 1];
    }
    for (int i = 1; i <= m/2; i++) {
        reord[m - i + 1] = cnt2[2 * i];
    }

    s.init(1, 1, m);

    for (ll i = 1; i <= m; i++) ans += reord[i] * (reord[i] + 1) / 2;

    for (ll i = 1; i <= m; i++) {
        ans += reord[i] * ((i + 1) * s.getsum(1, 1, m, 1, i - 1) - s.getxsum(1, 1, m, 1, i - 1));
    }
}

void add(int j) {
    int idx, i;
    idx = upper_bound(cnt2 + 1, cnt2 + m + 1, cnt[arr[j]]) - cnt2 - 1;
    assert(cnt2[idx] == cnt[arr[j]]);
    cnt[arr[j]]++;
    cnt2[idx]++;
    if (idx % 2 == 0) i = m - idx/2 + 1;
    else i = (idx + 1) / 2;
    reord[i]++;
    ans += reord[i];
    ans += (i + 1) * s.getsum(1, 1, m, 1, i - 1) - s.getxsum(1, 1, m, 1, i - 1);
    ans += s.getxsum(1, 1, m, i + 1, m) - (i - 1) * s.getsum(1, 1, m, i + 1, m);
    s.update(1, 1, m, i, 1);
}

void rem(int j) {
    int idx, i;
    idx = lower_bound(cnt2 + 1, cnt2 + m + 1, cnt[arr[j]]) - cnt2;
    assert(cnt2[idx] == cnt[arr[j]]);
    cnt[arr[j]]--;
    cnt2[idx]--;
    if (idx % 2 == 0) i = m - idx/2 + 1;
    else i = (idx + 1) / 2;
    reord[i]--;
    ans -= reord[i] + 1;
    ans -= (i + 1) * s.getsum(1, 1, m, 1, i - 1) - s.getxsum(1, 1, m, 1, i - 1);
    ans -= s.getxsum(1, 1, m, i + 1, m) - (i - 1) * s.getsum(1, 1, m, i + 1, m);
    s.update(1, 1, m, i, -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] = ans;

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

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 68 ms 23764 KB Output is correct
2 Correct 73 ms 23804 KB Output is correct
3 Correct 66 ms 23804 KB Output is correct
4 Correct 68 ms 23764 KB Output is correct
5 Correct 67 ms 23704 KB Output is correct
6 Correct 67 ms 23756 KB Output is correct
7 Correct 66 ms 23764 KB Output is correct
8 Correct 67 ms 23800 KB Output is correct
9 Correct 67 ms 23812 KB Output is correct
10 Correct 66 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 23820 KB Output is correct
2 Correct 66 ms 23808 KB Output is correct
3 Correct 66 ms 23968 KB Output is correct
4 Correct 74 ms 24444 KB Output is correct
5 Correct 74 ms 24984 KB Output is correct
6 Correct 82 ms 25564 KB Output is correct
7 Correct 81 ms 25588 KB Output is correct
8 Correct 79 ms 25548 KB Output is correct
9 Correct 79 ms 25548 KB Output is correct
10 Correct 83 ms 25648 KB Output is correct
11 Correct 81 ms 25548 KB Output is correct
12 Correct 91 ms 25644 KB Output is correct
13 Correct 79 ms 25624 KB Output is correct
14 Correct 79 ms 25616 KB Output is correct
15 Correct 79 ms 25608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 23820 KB Output is correct
2 Correct 66 ms 23808 KB Output is correct
3 Correct 66 ms 23968 KB Output is correct
4 Correct 74 ms 24444 KB Output is correct
5 Correct 74 ms 24984 KB Output is correct
6 Correct 82 ms 25564 KB Output is correct
7 Correct 81 ms 25588 KB Output is correct
8 Correct 79 ms 25548 KB Output is correct
9 Correct 79 ms 25548 KB Output is correct
10 Correct 83 ms 25648 KB Output is correct
11 Correct 81 ms 25548 KB Output is correct
12 Correct 91 ms 25644 KB Output is correct
13 Correct 79 ms 25624 KB Output is correct
14 Correct 79 ms 25616 KB Output is correct
15 Correct 79 ms 25608 KB Output is correct
16 Correct 66 ms 23764 KB Output is correct
17 Correct 67 ms 23808 KB Output is correct
18 Correct 68 ms 23864 KB Output is correct
19 Correct 73 ms 24452 KB Output is correct
20 Correct 75 ms 25032 KB Output is correct
21 Correct 84 ms 25920 KB Output is correct
22 Correct 80 ms 25760 KB Output is correct
23 Correct 84 ms 25768 KB Output is correct
24 Correct 79 ms 25732 KB Output is correct
25 Correct 80 ms 25752 KB Output is correct
26 Correct 80 ms 25732 KB Output is correct
27 Correct 79 ms 25752 KB Output is correct
28 Correct 79 ms 25652 KB Output is correct
29 Correct 95 ms 25680 KB Output is correct
30 Correct 80 ms 25708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 23820 KB Output is correct
2 Correct 66 ms 23808 KB Output is correct
3 Correct 66 ms 23968 KB Output is correct
4 Correct 74 ms 24444 KB Output is correct
5 Correct 74 ms 24984 KB Output is correct
6 Correct 82 ms 25564 KB Output is correct
7 Correct 81 ms 25588 KB Output is correct
8 Correct 79 ms 25548 KB Output is correct
9 Correct 79 ms 25548 KB Output is correct
10 Correct 83 ms 25648 KB Output is correct
11 Correct 81 ms 25548 KB Output is correct
12 Correct 91 ms 25644 KB Output is correct
13 Correct 79 ms 25624 KB Output is correct
14 Correct 79 ms 25616 KB Output is correct
15 Correct 79 ms 25608 KB Output is correct
16 Correct 66 ms 23764 KB Output is correct
17 Correct 67 ms 23808 KB Output is correct
18 Correct 68 ms 23864 KB Output is correct
19 Correct 73 ms 24452 KB Output is correct
20 Correct 75 ms 25032 KB Output is correct
21 Correct 84 ms 25920 KB Output is correct
22 Correct 80 ms 25760 KB Output is correct
23 Correct 84 ms 25768 KB Output is correct
24 Correct 79 ms 25732 KB Output is correct
25 Correct 80 ms 25752 KB Output is correct
26 Correct 80 ms 25732 KB Output is correct
27 Correct 79 ms 25752 KB Output is correct
28 Correct 79 ms 25652 KB Output is correct
29 Correct 95 ms 25680 KB Output is correct
30 Correct 80 ms 25708 KB Output is correct
31 Correct 67 ms 23816 KB Output is correct
32 Correct 65 ms 23764 KB Output is correct
33 Correct 66 ms 23808 KB Output is correct
34 Correct 67 ms 23888 KB Output is correct
35 Correct 66 ms 23764 KB Output is correct
36 Correct 67 ms 23892 KB Output is correct
37 Correct 70 ms 24148 KB Output is correct
38 Correct 72 ms 24184 KB Output is correct
39 Correct 73 ms 24536 KB Output is correct
40 Correct 77 ms 25356 KB Output is correct
41 Correct 86 ms 26116 KB Output is correct
42 Correct 84 ms 26124 KB Output is correct
43 Correct 81 ms 26112 KB Output is correct
44 Correct 81 ms 26128 KB Output is correct
45 Correct 80 ms 26032 KB Output is correct
46 Correct 81 ms 26024 KB Output is correct
47 Correct 88 ms 26016 KB Output is correct
48 Correct 82 ms 26112 KB Output is correct
49 Correct 82 ms 26120 KB Output is correct
50 Correct 82 ms 26116 KB Output is correct
51 Correct 80 ms 26060 KB Output is correct
52 Correct 81 ms 26112 KB Output is correct
53 Correct 82 ms 26188 KB Output is correct
54 Correct 82 ms 26048 KB Output is correct
55 Correct 81 ms 26068 KB Output is correct
56 Correct 80 ms 26060 KB Output is correct
57 Correct 82 ms 26120 KB Output is correct
58 Correct 81 ms 26188 KB Output is correct
59 Correct 80 ms 26048 KB Output is correct
60 Correct 81 ms 26108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 23764 KB Output is correct
2 Correct 73 ms 23804 KB Output is correct
3 Correct 66 ms 23804 KB Output is correct
4 Correct 68 ms 23764 KB Output is correct
5 Correct 67 ms 23704 KB Output is correct
6 Correct 67 ms 23756 KB Output is correct
7 Correct 66 ms 23764 KB Output is correct
8 Correct 67 ms 23800 KB Output is correct
9 Correct 67 ms 23812 KB Output is correct
10 Correct 66 ms 23800 KB Output is correct
11 Correct 67 ms 23820 KB Output is correct
12 Correct 66 ms 23808 KB Output is correct
13 Correct 66 ms 23968 KB Output is correct
14 Correct 74 ms 24444 KB Output is correct
15 Correct 74 ms 24984 KB Output is correct
16 Correct 82 ms 25564 KB Output is correct
17 Correct 81 ms 25588 KB Output is correct
18 Correct 79 ms 25548 KB Output is correct
19 Correct 79 ms 25548 KB Output is correct
20 Correct 83 ms 25648 KB Output is correct
21 Correct 81 ms 25548 KB Output is correct
22 Correct 91 ms 25644 KB Output is correct
23 Correct 79 ms 25624 KB Output is correct
24 Correct 79 ms 25616 KB Output is correct
25 Correct 79 ms 25608 KB Output is correct
26 Correct 66 ms 23764 KB Output is correct
27 Correct 67 ms 23808 KB Output is correct
28 Correct 68 ms 23864 KB Output is correct
29 Correct 73 ms 24452 KB Output is correct
30 Correct 75 ms 25032 KB Output is correct
31 Correct 84 ms 25920 KB Output is correct
32 Correct 80 ms 25760 KB Output is correct
33 Correct 84 ms 25768 KB Output is correct
34 Correct 79 ms 25732 KB Output is correct
35 Correct 80 ms 25752 KB Output is correct
36 Correct 80 ms 25732 KB Output is correct
37 Correct 79 ms 25752 KB Output is correct
38 Correct 79 ms 25652 KB Output is correct
39 Correct 95 ms 25680 KB Output is correct
40 Correct 80 ms 25708 KB Output is correct
41 Correct 67 ms 23816 KB Output is correct
42 Correct 65 ms 23764 KB Output is correct
43 Correct 66 ms 23808 KB Output is correct
44 Correct 67 ms 23888 KB Output is correct
45 Correct 66 ms 23764 KB Output is correct
46 Correct 67 ms 23892 KB Output is correct
47 Correct 70 ms 24148 KB Output is correct
48 Correct 72 ms 24184 KB Output is correct
49 Correct 73 ms 24536 KB Output is correct
50 Correct 77 ms 25356 KB Output is correct
51 Correct 86 ms 26116 KB Output is correct
52 Correct 84 ms 26124 KB Output is correct
53 Correct 81 ms 26112 KB Output is correct
54 Correct 81 ms 26128 KB Output is correct
55 Correct 80 ms 26032 KB Output is correct
56 Correct 81 ms 26024 KB Output is correct
57 Correct 88 ms 26016 KB Output is correct
58 Correct 82 ms 26112 KB Output is correct
59 Correct 82 ms 26120 KB Output is correct
60 Correct 82 ms 26116 KB Output is correct
61 Correct 80 ms 26060 KB Output is correct
62 Correct 81 ms 26112 KB Output is correct
63 Correct 82 ms 26188 KB Output is correct
64 Correct 82 ms 26048 KB Output is correct
65 Correct 81 ms 26068 KB Output is correct
66 Correct 80 ms 26060 KB Output is correct
67 Correct 82 ms 26120 KB Output is correct
68 Correct 81 ms 26188 KB Output is correct
69 Correct 80 ms 26048 KB Output is correct
70 Correct 81 ms 26108 KB Output is correct
71 Correct 73 ms 24128 KB Output is correct
72 Correct 69 ms 24216 KB Output is correct
73 Correct 69 ms 24192 KB Output is correct
74 Correct 70 ms 24220 KB Output is correct
75 Correct 68 ms 24148 KB Output is correct
76 Correct 84 ms 24600 KB Output is correct
77 Correct 73 ms 24660 KB Output is correct
78 Correct 72 ms 24660 KB Output is correct
79 Correct 72 ms 24656 KB Output is correct
80 Correct 73 ms 24660 KB Output is correct
81 Correct 87 ms 25540 KB Output is correct
82 Correct 80 ms 25540 KB Output is correct
83 Correct 78 ms 25536 KB Output is correct
84 Correct 77 ms 25460 KB Output is correct
85 Correct 77 ms 25556 KB Output is correct
86 Correct 96 ms 25648 KB Output is correct
87 Correct 79 ms 25644 KB Output is correct
88 Correct 79 ms 25644 KB Output is correct
89 Correct 78 ms 25648 KB Output is correct
90 Correct 80 ms 25560 KB Output is correct
91 Correct 85 ms 26704 KB Output is correct
92 Correct 100 ms 26624 KB Output is correct
93 Correct 85 ms 26632 KB Output is correct
94 Correct 85 ms 26700 KB Output is correct
95 Correct 84 ms 26688 KB Output is correct
96 Correct 101 ms 26868 KB Output is correct
97 Correct 90 ms 26884 KB Output is correct
98 Correct 87 ms 26868 KB Output is correct
99 Correct 86 ms 26836 KB Output is correct
100 Correct 88 ms 26792 KB Output is correct
101 Correct 86 ms 26868 KB Output is correct
102 Correct 88 ms 26884 KB Output is correct
103 Correct 88 ms 26884 KB Output is correct
104 Correct 88 ms 26868 KB Output is correct
105 Correct 89 ms 26868 KB Output is correct
106 Correct 90 ms 26900 KB Output is correct
107 Correct 90 ms 26904 KB Output is correct
108 Correct 85 ms 26904 KB Output is correct
109 Correct 101 ms 26904 KB Output is correct
110 Correct 88 ms 26820 KB Output is correct
111 Correct 100 ms 26896 KB Output is correct
112 Correct 87 ms 26900 KB Output is correct
113 Correct 100 ms 26876 KB Output is correct
114 Correct 86 ms 26804 KB Output is correct
115 Correct 86 ms 26876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 23764 KB Output is correct
2 Correct 73 ms 23804 KB Output is correct
3 Correct 66 ms 23804 KB Output is correct
4 Correct 68 ms 23764 KB Output is correct
5 Correct 67 ms 23704 KB Output is correct
6 Correct 67 ms 23756 KB Output is correct
7 Correct 66 ms 23764 KB Output is correct
8 Correct 67 ms 23800 KB Output is correct
9 Correct 67 ms 23812 KB Output is correct
10 Correct 66 ms 23800 KB Output is correct
11 Correct 67 ms 23820 KB Output is correct
12 Correct 66 ms 23808 KB Output is correct
13 Correct 66 ms 23968 KB Output is correct
14 Correct 74 ms 24444 KB Output is correct
15 Correct 74 ms 24984 KB Output is correct
16 Correct 82 ms 25564 KB Output is correct
17 Correct 81 ms 25588 KB Output is correct
18 Correct 79 ms 25548 KB Output is correct
19 Correct 79 ms 25548 KB Output is correct
20 Correct 83 ms 25648 KB Output is correct
21 Correct 81 ms 25548 KB Output is correct
22 Correct 91 ms 25644 KB Output is correct
23 Correct 79 ms 25624 KB Output is correct
24 Correct 79 ms 25616 KB Output is correct
25 Correct 79 ms 25608 KB Output is correct
26 Correct 66 ms 23764 KB Output is correct
27 Correct 67 ms 23808 KB Output is correct
28 Correct 68 ms 23864 KB Output is correct
29 Correct 73 ms 24452 KB Output is correct
30 Correct 75 ms 25032 KB Output is correct
31 Correct 84 ms 25920 KB Output is correct
32 Correct 80 ms 25760 KB Output is correct
33 Correct 84 ms 25768 KB Output is correct
34 Correct 79 ms 25732 KB Output is correct
35 Correct 80 ms 25752 KB Output is correct
36 Correct 80 ms 25732 KB Output is correct
37 Correct 79 ms 25752 KB Output is correct
38 Correct 79 ms 25652 KB Output is correct
39 Correct 95 ms 25680 KB Output is correct
40 Correct 80 ms 25708 KB Output is correct
41 Correct 67 ms 23816 KB Output is correct
42 Correct 65 ms 23764 KB Output is correct
43 Correct 66 ms 23808 KB Output is correct
44 Correct 67 ms 23888 KB Output is correct
45 Correct 66 ms 23764 KB Output is correct
46 Correct 67 ms 23892 KB Output is correct
47 Correct 70 ms 24148 KB Output is correct
48 Correct 72 ms 24184 KB Output is correct
49 Correct 73 ms 24536 KB Output is correct
50 Correct 77 ms 25356 KB Output is correct
51 Correct 86 ms 26116 KB Output is correct
52 Correct 84 ms 26124 KB Output is correct
53 Correct 81 ms 26112 KB Output is correct
54 Correct 81 ms 26128 KB Output is correct
55 Correct 80 ms 26032 KB Output is correct
56 Correct 81 ms 26024 KB Output is correct
57 Correct 88 ms 26016 KB Output is correct
58 Correct 82 ms 26112 KB Output is correct
59 Correct 82 ms 26120 KB Output is correct
60 Correct 82 ms 26116 KB Output is correct
61 Correct 80 ms 26060 KB Output is correct
62 Correct 81 ms 26112 KB Output is correct
63 Correct 82 ms 26188 KB Output is correct
64 Correct 82 ms 26048 KB Output is correct
65 Correct 81 ms 26068 KB Output is correct
66 Correct 80 ms 26060 KB Output is correct
67 Correct 82 ms 26120 KB Output is correct
68 Correct 81 ms 26188 KB Output is correct
69 Correct 80 ms 26048 KB Output is correct
70 Correct 81 ms 26108 KB Output is correct
71 Correct 73 ms 24128 KB Output is correct
72 Correct 69 ms 24216 KB Output is correct
73 Correct 69 ms 24192 KB Output is correct
74 Correct 70 ms 24220 KB Output is correct
75 Correct 68 ms 24148 KB Output is correct
76 Correct 84 ms 24600 KB Output is correct
77 Correct 73 ms 24660 KB Output is correct
78 Correct 72 ms 24660 KB Output is correct
79 Correct 72 ms 24656 KB Output is correct
80 Correct 73 ms 24660 KB Output is correct
81 Correct 87 ms 25540 KB Output is correct
82 Correct 80 ms 25540 KB Output is correct
83 Correct 78 ms 25536 KB Output is correct
84 Correct 77 ms 25460 KB Output is correct
85 Correct 77 ms 25556 KB Output is correct
86 Correct 96 ms 25648 KB Output is correct
87 Correct 79 ms 25644 KB Output is correct
88 Correct 79 ms 25644 KB Output is correct
89 Correct 78 ms 25648 KB Output is correct
90 Correct 80 ms 25560 KB Output is correct
91 Correct 85 ms 26704 KB Output is correct
92 Correct 100 ms 26624 KB Output is correct
93 Correct 85 ms 26632 KB Output is correct
94 Correct 85 ms 26700 KB Output is correct
95 Correct 84 ms 26688 KB Output is correct
96 Correct 101 ms 26868 KB Output is correct
97 Correct 90 ms 26884 KB Output is correct
98 Correct 87 ms 26868 KB Output is correct
99 Correct 86 ms 26836 KB Output is correct
100 Correct 88 ms 26792 KB Output is correct
101 Correct 86 ms 26868 KB Output is correct
102 Correct 88 ms 26884 KB Output is correct
103 Correct 88 ms 26884 KB Output is correct
104 Correct 88 ms 26868 KB Output is correct
105 Correct 89 ms 26868 KB Output is correct
106 Correct 90 ms 26900 KB Output is correct
107 Correct 90 ms 26904 KB Output is correct
108 Correct 85 ms 26904 KB Output is correct
109 Correct 101 ms 26904 KB Output is correct
110 Correct 88 ms 26820 KB Output is correct
111 Correct 100 ms 26896 KB Output is correct
112 Correct 87 ms 26900 KB Output is correct
113 Correct 100 ms 26876 KB Output is correct
114 Correct 86 ms 26804 KB Output is correct
115 Correct 86 ms 26876 KB Output is correct
116 Correct 6765 ms 25716 KB Output is correct
117 Correct 6881 ms 25720 KB Output is correct
118 Execution timed out 7077 ms 25684 KB Time limit exceeded
119 Halted 0 ms 0 KB -