답안 #789655

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789655 2023-07-21T16:08:32 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 15820 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);
}

ll sum[m + 1], xsum[m + 1];

void updatesum(int i, ll x) {
    while (i <= m) {
        sum[i] += x;
        i += (i & -i);
    }
}

void updatexsum(int i, ll x) {
    while (i <= m) {
        xsum[i] += x;
        i += (i & -i);
    }
}

ll getsum(int i) {
    ll ans = 0;
    while (i > 0) {
        ans += sum[i];
        i -= (i & -i);
    }
    return ans;
}

ll getxsum(int i) {
    ll ans = 0;
    while (i > 0) {
        ans += xsum[i];
        i -= (i & -i);
    }
    return ans;
}

int Count[303030], Arr[303030];
void init(int p) {
    for (int i = 1; i <= m; i++) cnt[i] = sum[i] = xsum[i] = 0;
    for (int i = 0; i <= n; i++) Count[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];

    int r = 0;
    for (int i = 1; i <= m; i++) Count[cnt2[i]]++;
    for (int i = 0; i <= n; i++) {
        for (int j = 1; j <= Count[i]; j++) Arr[++r] = i;
    }
    for (int i = 1; i <= m; i++) cnt2[i] = Arr[i];

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

    for (ll i = 1; i <= m; i++) {
        updatesum(i, reord[i]);
        updatexsum(i, i * reord[i]);
    }

    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) * getsum(i - 1) - getxsum(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) * getsum(i - 1) - getxsum(i - 1);
    ans += (getxsum(m) - getxsum(i)) - (i - 1) * (getsum(m) - getsum(i));
    updatesum(i, 1);
    updatexsum(i, i);
}

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) * getsum(i - 1) - getxsum(i - 1);
    ans -= (getxsum(m) - getxsum(i)) - (i - 1) * (getsum(m) - getsum(i));
    updatesum(i, -1);
    updatexsum(i, -i);
}

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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 13268 KB Output is correct
2 Correct 15 ms 13244 KB Output is correct
3 Correct 15 ms 13268 KB Output is correct
4 Correct 15 ms 13268 KB Output is correct
5 Correct 21 ms 13272 KB Output is correct
6 Correct 16 ms 13264 KB Output is correct
7 Correct 18 ms 13268 KB Output is correct
8 Correct 16 ms 13268 KB Output is correct
9 Correct 15 ms 13268 KB Output is correct
10 Correct 14 ms 13268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 13268 KB Output is correct
2 Correct 15 ms 13268 KB Output is correct
3 Correct 15 ms 13336 KB Output is correct
4 Correct 19 ms 14036 KB Output is correct
5 Correct 25 ms 14832 KB Output is correct
6 Correct 29 ms 15548 KB Output is correct
7 Correct 29 ms 15576 KB Output is correct
8 Correct 27 ms 15544 KB Output is correct
9 Correct 34 ms 15564 KB Output is correct
10 Correct 28 ms 15508 KB Output is correct
11 Correct 28 ms 15600 KB Output is correct
12 Correct 28 ms 15616 KB Output is correct
13 Correct 28 ms 15616 KB Output is correct
14 Correct 28 ms 15568 KB Output is correct
15 Correct 30 ms 15596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 13268 KB Output is correct
2 Correct 15 ms 13268 KB Output is correct
3 Correct 15 ms 13336 KB Output is correct
4 Correct 19 ms 14036 KB Output is correct
5 Correct 25 ms 14832 KB Output is correct
6 Correct 29 ms 15548 KB Output is correct
7 Correct 29 ms 15576 KB Output is correct
8 Correct 27 ms 15544 KB Output is correct
9 Correct 34 ms 15564 KB Output is correct
10 Correct 28 ms 15508 KB Output is correct
11 Correct 28 ms 15600 KB Output is correct
12 Correct 28 ms 15616 KB Output is correct
13 Correct 28 ms 15616 KB Output is correct
14 Correct 28 ms 15568 KB Output is correct
15 Correct 30 ms 15596 KB Output is correct
16 Correct 16 ms 13264 KB Output is correct
17 Correct 15 ms 13280 KB Output is correct
18 Correct 15 ms 13268 KB Output is correct
19 Correct 22 ms 14048 KB Output is correct
20 Correct 24 ms 14828 KB Output is correct
21 Correct 29 ms 15592 KB Output is correct
22 Correct 28 ms 15616 KB Output is correct
23 Correct 29 ms 15576 KB Output is correct
24 Correct 29 ms 15608 KB Output is correct
25 Correct 33 ms 15572 KB Output is correct
26 Correct 31 ms 15552 KB Output is correct
27 Correct 31 ms 15600 KB Output is correct
28 Correct 28 ms 15616 KB Output is correct
29 Correct 29 ms 15592 KB Output is correct
30 Correct 29 ms 15552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 13268 KB Output is correct
2 Correct 15 ms 13268 KB Output is correct
3 Correct 15 ms 13336 KB Output is correct
4 Correct 19 ms 14036 KB Output is correct
5 Correct 25 ms 14832 KB Output is correct
6 Correct 29 ms 15548 KB Output is correct
7 Correct 29 ms 15576 KB Output is correct
8 Correct 27 ms 15544 KB Output is correct
9 Correct 34 ms 15564 KB Output is correct
10 Correct 28 ms 15508 KB Output is correct
11 Correct 28 ms 15600 KB Output is correct
12 Correct 28 ms 15616 KB Output is correct
13 Correct 28 ms 15616 KB Output is correct
14 Correct 28 ms 15568 KB Output is correct
15 Correct 30 ms 15596 KB Output is correct
16 Correct 16 ms 13264 KB Output is correct
17 Correct 15 ms 13280 KB Output is correct
18 Correct 15 ms 13268 KB Output is correct
19 Correct 22 ms 14048 KB Output is correct
20 Correct 24 ms 14828 KB Output is correct
21 Correct 29 ms 15592 KB Output is correct
22 Correct 28 ms 15616 KB Output is correct
23 Correct 29 ms 15576 KB Output is correct
24 Correct 29 ms 15608 KB Output is correct
25 Correct 33 ms 15572 KB Output is correct
26 Correct 31 ms 15552 KB Output is correct
27 Correct 31 ms 15600 KB Output is correct
28 Correct 28 ms 15616 KB Output is correct
29 Correct 29 ms 15592 KB Output is correct
30 Correct 29 ms 15552 KB Output is correct
31 Correct 15 ms 13284 KB Output is correct
32 Correct 14 ms 13276 KB Output is correct
33 Correct 15 ms 13268 KB Output is correct
34 Correct 20 ms 13356 KB Output is correct
35 Correct 18 ms 13268 KB Output is correct
36 Correct 19 ms 13352 KB Output is correct
37 Correct 18 ms 13664 KB Output is correct
38 Correct 19 ms 13652 KB Output is correct
39 Correct 23 ms 14004 KB Output is correct
40 Correct 25 ms 14812 KB Output is correct
41 Correct 29 ms 15592 KB Output is correct
42 Correct 30 ms 15528 KB Output is correct
43 Correct 30 ms 15512 KB Output is correct
44 Correct 29 ms 15540 KB Output is correct
45 Correct 35 ms 15572 KB Output is correct
46 Correct 33 ms 15568 KB Output is correct
47 Correct 38 ms 15596 KB Output is correct
48 Correct 31 ms 15596 KB Output is correct
49 Correct 29 ms 15544 KB Output is correct
50 Correct 32 ms 15576 KB Output is correct
51 Correct 34 ms 15580 KB Output is correct
52 Correct 30 ms 15596 KB Output is correct
53 Correct 35 ms 15552 KB Output is correct
54 Correct 30 ms 15572 KB Output is correct
55 Correct 36 ms 15596 KB Output is correct
56 Correct 30 ms 15576 KB Output is correct
57 Correct 29 ms 15500 KB Output is correct
58 Correct 31 ms 15536 KB Output is correct
59 Correct 30 ms 15612 KB Output is correct
60 Correct 34 ms 15576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 13268 KB Output is correct
2 Correct 15 ms 13244 KB Output is correct
3 Correct 15 ms 13268 KB Output is correct
4 Correct 15 ms 13268 KB Output is correct
5 Correct 21 ms 13272 KB Output is correct
6 Correct 16 ms 13264 KB Output is correct
7 Correct 18 ms 13268 KB Output is correct
8 Correct 16 ms 13268 KB Output is correct
9 Correct 15 ms 13268 KB Output is correct
10 Correct 14 ms 13268 KB Output is correct
11 Correct 18 ms 13268 KB Output is correct
12 Correct 15 ms 13268 KB Output is correct
13 Correct 15 ms 13336 KB Output is correct
14 Correct 19 ms 14036 KB Output is correct
15 Correct 25 ms 14832 KB Output is correct
16 Correct 29 ms 15548 KB Output is correct
17 Correct 29 ms 15576 KB Output is correct
18 Correct 27 ms 15544 KB Output is correct
19 Correct 34 ms 15564 KB Output is correct
20 Correct 28 ms 15508 KB Output is correct
21 Correct 28 ms 15600 KB Output is correct
22 Correct 28 ms 15616 KB Output is correct
23 Correct 28 ms 15616 KB Output is correct
24 Correct 28 ms 15568 KB Output is correct
25 Correct 30 ms 15596 KB Output is correct
26 Correct 16 ms 13264 KB Output is correct
27 Correct 15 ms 13280 KB Output is correct
28 Correct 15 ms 13268 KB Output is correct
29 Correct 22 ms 14048 KB Output is correct
30 Correct 24 ms 14828 KB Output is correct
31 Correct 29 ms 15592 KB Output is correct
32 Correct 28 ms 15616 KB Output is correct
33 Correct 29 ms 15576 KB Output is correct
34 Correct 29 ms 15608 KB Output is correct
35 Correct 33 ms 15572 KB Output is correct
36 Correct 31 ms 15552 KB Output is correct
37 Correct 31 ms 15600 KB Output is correct
38 Correct 28 ms 15616 KB Output is correct
39 Correct 29 ms 15592 KB Output is correct
40 Correct 29 ms 15552 KB Output is correct
41 Correct 15 ms 13284 KB Output is correct
42 Correct 14 ms 13276 KB Output is correct
43 Correct 15 ms 13268 KB Output is correct
44 Correct 20 ms 13356 KB Output is correct
45 Correct 18 ms 13268 KB Output is correct
46 Correct 19 ms 13352 KB Output is correct
47 Correct 18 ms 13664 KB Output is correct
48 Correct 19 ms 13652 KB Output is correct
49 Correct 23 ms 14004 KB Output is correct
50 Correct 25 ms 14812 KB Output is correct
51 Correct 29 ms 15592 KB Output is correct
52 Correct 30 ms 15528 KB Output is correct
53 Correct 30 ms 15512 KB Output is correct
54 Correct 29 ms 15540 KB Output is correct
55 Correct 35 ms 15572 KB Output is correct
56 Correct 33 ms 15568 KB Output is correct
57 Correct 38 ms 15596 KB Output is correct
58 Correct 31 ms 15596 KB Output is correct
59 Correct 29 ms 15544 KB Output is correct
60 Correct 32 ms 15576 KB Output is correct
61 Correct 34 ms 15580 KB Output is correct
62 Correct 30 ms 15596 KB Output is correct
63 Correct 35 ms 15552 KB Output is correct
64 Correct 30 ms 15572 KB Output is correct
65 Correct 36 ms 15596 KB Output is correct
66 Correct 30 ms 15576 KB Output is correct
67 Correct 29 ms 15500 KB Output is correct
68 Correct 31 ms 15536 KB Output is correct
69 Correct 30 ms 15612 KB Output is correct
70 Correct 34 ms 15576 KB Output is correct
71 Correct 17 ms 13652 KB Output is correct
72 Correct 19 ms 13652 KB Output is correct
73 Correct 21 ms 13652 KB Output is correct
74 Correct 20 ms 13652 KB Output is correct
75 Correct 18 ms 13652 KB Output is correct
76 Correct 22 ms 14044 KB Output is correct
77 Correct 22 ms 14008 KB Output is correct
78 Correct 22 ms 14036 KB Output is correct
79 Correct 21 ms 13988 KB Output is correct
80 Correct 20 ms 14020 KB Output is correct
81 Correct 27 ms 14788 KB Output is correct
82 Correct 26 ms 14792 KB Output is correct
83 Correct 26 ms 14804 KB Output is correct
84 Correct 26 ms 14784 KB Output is correct
85 Correct 31 ms 14804 KB Output is correct
86 Correct 30 ms 14780 KB Output is correct
87 Correct 27 ms 14804 KB Output is correct
88 Correct 28 ms 14736 KB Output is correct
89 Correct 28 ms 14784 KB Output is correct
90 Correct 27 ms 14804 KB Output is correct
91 Correct 42 ms 15496 KB Output is correct
92 Correct 40 ms 15600 KB Output is correct
93 Correct 36 ms 15528 KB Output is correct
94 Correct 32 ms 15564 KB Output is correct
95 Correct 33 ms 15592 KB Output is correct
96 Correct 35 ms 15484 KB Output is correct
97 Correct 43 ms 15672 KB Output is correct
98 Correct 34 ms 15572 KB Output is correct
99 Correct 34 ms 15500 KB Output is correct
100 Correct 40 ms 15552 KB Output is correct
101 Correct 44 ms 15596 KB Output is correct
102 Correct 35 ms 15564 KB Output is correct
103 Correct 36 ms 15552 KB Output is correct
104 Correct 34 ms 15556 KB Output is correct
105 Correct 36 ms 15596 KB Output is correct
106 Correct 35 ms 15572 KB Output is correct
107 Correct 46 ms 15600 KB Output is correct
108 Correct 35 ms 15572 KB Output is correct
109 Correct 41 ms 15592 KB Output is correct
110 Correct 36 ms 15596 KB Output is correct
111 Correct 37 ms 15560 KB Output is correct
112 Correct 35 ms 15596 KB Output is correct
113 Correct 35 ms 15528 KB Output is correct
114 Correct 50 ms 15564 KB Output is correct
115 Correct 48 ms 15552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 13268 KB Output is correct
2 Correct 15 ms 13244 KB Output is correct
3 Correct 15 ms 13268 KB Output is correct
4 Correct 15 ms 13268 KB Output is correct
5 Correct 21 ms 13272 KB Output is correct
6 Correct 16 ms 13264 KB Output is correct
7 Correct 18 ms 13268 KB Output is correct
8 Correct 16 ms 13268 KB Output is correct
9 Correct 15 ms 13268 KB Output is correct
10 Correct 14 ms 13268 KB Output is correct
11 Correct 18 ms 13268 KB Output is correct
12 Correct 15 ms 13268 KB Output is correct
13 Correct 15 ms 13336 KB Output is correct
14 Correct 19 ms 14036 KB Output is correct
15 Correct 25 ms 14832 KB Output is correct
16 Correct 29 ms 15548 KB Output is correct
17 Correct 29 ms 15576 KB Output is correct
18 Correct 27 ms 15544 KB Output is correct
19 Correct 34 ms 15564 KB Output is correct
20 Correct 28 ms 15508 KB Output is correct
21 Correct 28 ms 15600 KB Output is correct
22 Correct 28 ms 15616 KB Output is correct
23 Correct 28 ms 15616 KB Output is correct
24 Correct 28 ms 15568 KB Output is correct
25 Correct 30 ms 15596 KB Output is correct
26 Correct 16 ms 13264 KB Output is correct
27 Correct 15 ms 13280 KB Output is correct
28 Correct 15 ms 13268 KB Output is correct
29 Correct 22 ms 14048 KB Output is correct
30 Correct 24 ms 14828 KB Output is correct
31 Correct 29 ms 15592 KB Output is correct
32 Correct 28 ms 15616 KB Output is correct
33 Correct 29 ms 15576 KB Output is correct
34 Correct 29 ms 15608 KB Output is correct
35 Correct 33 ms 15572 KB Output is correct
36 Correct 31 ms 15552 KB Output is correct
37 Correct 31 ms 15600 KB Output is correct
38 Correct 28 ms 15616 KB Output is correct
39 Correct 29 ms 15592 KB Output is correct
40 Correct 29 ms 15552 KB Output is correct
41 Correct 15 ms 13284 KB Output is correct
42 Correct 14 ms 13276 KB Output is correct
43 Correct 15 ms 13268 KB Output is correct
44 Correct 20 ms 13356 KB Output is correct
45 Correct 18 ms 13268 KB Output is correct
46 Correct 19 ms 13352 KB Output is correct
47 Correct 18 ms 13664 KB Output is correct
48 Correct 19 ms 13652 KB Output is correct
49 Correct 23 ms 14004 KB Output is correct
50 Correct 25 ms 14812 KB Output is correct
51 Correct 29 ms 15592 KB Output is correct
52 Correct 30 ms 15528 KB Output is correct
53 Correct 30 ms 15512 KB Output is correct
54 Correct 29 ms 15540 KB Output is correct
55 Correct 35 ms 15572 KB Output is correct
56 Correct 33 ms 15568 KB Output is correct
57 Correct 38 ms 15596 KB Output is correct
58 Correct 31 ms 15596 KB Output is correct
59 Correct 29 ms 15544 KB Output is correct
60 Correct 32 ms 15576 KB Output is correct
61 Correct 34 ms 15580 KB Output is correct
62 Correct 30 ms 15596 KB Output is correct
63 Correct 35 ms 15552 KB Output is correct
64 Correct 30 ms 15572 KB Output is correct
65 Correct 36 ms 15596 KB Output is correct
66 Correct 30 ms 15576 KB Output is correct
67 Correct 29 ms 15500 KB Output is correct
68 Correct 31 ms 15536 KB Output is correct
69 Correct 30 ms 15612 KB Output is correct
70 Correct 34 ms 15576 KB Output is correct
71 Correct 17 ms 13652 KB Output is correct
72 Correct 19 ms 13652 KB Output is correct
73 Correct 21 ms 13652 KB Output is correct
74 Correct 20 ms 13652 KB Output is correct
75 Correct 18 ms 13652 KB Output is correct
76 Correct 22 ms 14044 KB Output is correct
77 Correct 22 ms 14008 KB Output is correct
78 Correct 22 ms 14036 KB Output is correct
79 Correct 21 ms 13988 KB Output is correct
80 Correct 20 ms 14020 KB Output is correct
81 Correct 27 ms 14788 KB Output is correct
82 Correct 26 ms 14792 KB Output is correct
83 Correct 26 ms 14804 KB Output is correct
84 Correct 26 ms 14784 KB Output is correct
85 Correct 31 ms 14804 KB Output is correct
86 Correct 30 ms 14780 KB Output is correct
87 Correct 27 ms 14804 KB Output is correct
88 Correct 28 ms 14736 KB Output is correct
89 Correct 28 ms 14784 KB Output is correct
90 Correct 27 ms 14804 KB Output is correct
91 Correct 42 ms 15496 KB Output is correct
92 Correct 40 ms 15600 KB Output is correct
93 Correct 36 ms 15528 KB Output is correct
94 Correct 32 ms 15564 KB Output is correct
95 Correct 33 ms 15592 KB Output is correct
96 Correct 35 ms 15484 KB Output is correct
97 Correct 43 ms 15672 KB Output is correct
98 Correct 34 ms 15572 KB Output is correct
99 Correct 34 ms 15500 KB Output is correct
100 Correct 40 ms 15552 KB Output is correct
101 Correct 44 ms 15596 KB Output is correct
102 Correct 35 ms 15564 KB Output is correct
103 Correct 36 ms 15552 KB Output is correct
104 Correct 34 ms 15556 KB Output is correct
105 Correct 36 ms 15596 KB Output is correct
106 Correct 35 ms 15572 KB Output is correct
107 Correct 46 ms 15600 KB Output is correct
108 Correct 35 ms 15572 KB Output is correct
109 Correct 41 ms 15592 KB Output is correct
110 Correct 36 ms 15596 KB Output is correct
111 Correct 37 ms 15560 KB Output is correct
112 Correct 35 ms 15596 KB Output is correct
113 Correct 35 ms 15528 KB Output is correct
114 Correct 50 ms 15564 KB Output is correct
115 Correct 48 ms 15552 KB Output is correct
116 Correct 1447 ms 14696 KB Output is correct
117 Correct 1434 ms 14692 KB Output is correct
118 Correct 3563 ms 15128 KB Output is correct
119 Correct 3637 ms 15436 KB Output is correct
120 Correct 3708 ms 15272 KB Output is correct
121 Correct 6193 ms 15664 KB Output is correct
122 Correct 6357 ms 15676 KB Output is correct
123 Execution timed out 7056 ms 15820 KB Time limit exceeded
124 Halted 0 ms 0 KB -