답안 #789626

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789626 2023-07-21T15:09:35 Z math_rabbit_1028 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 142696 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], sum[303030], xsum[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[cnt[i]]++;

    vector<int> v;
    v.push_back(0);
    for (int i = 0; i <= n; i++) {
        for (int j = 1; j <= cnt2[i]; j++) v.push_back(i);
    }

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

    for (ll i = 1; i <= m; i++) {
        ans += reord[i] * (reord[i] + 1) / 2;
        sum[i] = sum[i - 1] + reord[i];
        xsum[i] = xsum[i - 1] + i * reord[i];
    }

    for (ll i = 1; i <= m; i++) {
        ans += reord[i] * ((i + 1) * sum[i - 1] - xsum[i - 1]);
    }
}

void add(int j) {
    int idx, i;
    idx = upper_bound(cnt2 + 1, cnt2 + m + 1, cnt[arr[j]]) - cnt2 - 1;
    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;
    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 = 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 6 ms 10956 KB Output is correct
2 Correct 7 ms 11084 KB Output is correct
3 Correct 6 ms 10956 KB Output is correct
4 Correct 7 ms 10956 KB Output is correct
5 Correct 8 ms 10956 KB Output is correct
6 Correct 7 ms 11076 KB Output is correct
7 Correct 7 ms 10956 KB Output is correct
8 Correct 6 ms 11064 KB Output is correct
9 Correct 7 ms 11084 KB Output is correct
10 Correct 7 ms 10956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10956 KB Output is correct
2 Correct 7 ms 11076 KB Output is correct
3 Correct 7 ms 11092 KB Output is correct
4 Correct 11 ms 11596 KB Output is correct
5 Correct 15 ms 11988 KB Output is correct
6 Correct 20 ms 12328 KB Output is correct
7 Correct 19 ms 12368 KB Output is correct
8 Correct 19 ms 12364 KB Output is correct
9 Correct 19 ms 12392 KB Output is correct
10 Correct 26 ms 12364 KB Output is correct
11 Correct 20 ms 12308 KB Output is correct
12 Correct 23 ms 12292 KB Output is correct
13 Correct 19 ms 12356 KB Output is correct
14 Correct 20 ms 12284 KB Output is correct
15 Correct 19 ms 12352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10956 KB Output is correct
2 Correct 7 ms 11076 KB Output is correct
3 Correct 7 ms 11092 KB Output is correct
4 Correct 11 ms 11596 KB Output is correct
5 Correct 15 ms 11988 KB Output is correct
6 Correct 20 ms 12328 KB Output is correct
7 Correct 19 ms 12368 KB Output is correct
8 Correct 19 ms 12364 KB Output is correct
9 Correct 19 ms 12392 KB Output is correct
10 Correct 26 ms 12364 KB Output is correct
11 Correct 20 ms 12308 KB Output is correct
12 Correct 23 ms 12292 KB Output is correct
13 Correct 19 ms 12356 KB Output is correct
14 Correct 20 ms 12284 KB Output is correct
15 Correct 19 ms 12352 KB Output is correct
16 Correct 8 ms 10956 KB Output is correct
17 Correct 8 ms 11076 KB Output is correct
18 Correct 9 ms 11088 KB Output is correct
19 Correct 12 ms 11600 KB Output is correct
20 Correct 20 ms 12144 KB Output is correct
21 Correct 20 ms 12380 KB Output is correct
22 Correct 21 ms 12364 KB Output is correct
23 Correct 21 ms 12344 KB Output is correct
24 Correct 21 ms 12400 KB Output is correct
25 Correct 20 ms 12408 KB Output is correct
26 Correct 21 ms 12424 KB Output is correct
27 Correct 20 ms 12416 KB Output is correct
28 Correct 21 ms 12344 KB Output is correct
29 Correct 21 ms 12348 KB Output is correct
30 Correct 20 ms 12324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10956 KB Output is correct
2 Correct 7 ms 11076 KB Output is correct
3 Correct 7 ms 11092 KB Output is correct
4 Correct 11 ms 11596 KB Output is correct
5 Correct 15 ms 11988 KB Output is correct
6 Correct 20 ms 12328 KB Output is correct
7 Correct 19 ms 12368 KB Output is correct
8 Correct 19 ms 12364 KB Output is correct
9 Correct 19 ms 12392 KB Output is correct
10 Correct 26 ms 12364 KB Output is correct
11 Correct 20 ms 12308 KB Output is correct
12 Correct 23 ms 12292 KB Output is correct
13 Correct 19 ms 12356 KB Output is correct
14 Correct 20 ms 12284 KB Output is correct
15 Correct 19 ms 12352 KB Output is correct
16 Correct 8 ms 10956 KB Output is correct
17 Correct 8 ms 11076 KB Output is correct
18 Correct 9 ms 11088 KB Output is correct
19 Correct 12 ms 11600 KB Output is correct
20 Correct 20 ms 12144 KB Output is correct
21 Correct 20 ms 12380 KB Output is correct
22 Correct 21 ms 12364 KB Output is correct
23 Correct 21 ms 12344 KB Output is correct
24 Correct 21 ms 12400 KB Output is correct
25 Correct 20 ms 12408 KB Output is correct
26 Correct 21 ms 12424 KB Output is correct
27 Correct 20 ms 12416 KB Output is correct
28 Correct 21 ms 12344 KB Output is correct
29 Correct 21 ms 12348 KB Output is correct
30 Correct 20 ms 12324 KB Output is correct
31 Correct 6 ms 11084 KB Output is correct
32 Correct 7 ms 11028 KB Output is correct
33 Correct 7 ms 11044 KB Output is correct
34 Correct 7 ms 11084 KB Output is correct
35 Correct 7 ms 11124 KB Output is correct
36 Correct 8 ms 11084 KB Output is correct
37 Correct 9 ms 11340 KB Output is correct
38 Correct 9 ms 11340 KB Output is correct
39 Correct 12 ms 11592 KB Output is correct
40 Correct 19 ms 11980 KB Output is correct
41 Correct 24 ms 12356 KB Output is correct
42 Correct 22 ms 12352 KB Output is correct
43 Correct 21 ms 12364 KB Output is correct
44 Correct 21 ms 12348 KB Output is correct
45 Correct 21 ms 12344 KB Output is correct
46 Correct 22 ms 12280 KB Output is correct
47 Correct 22 ms 12360 KB Output is correct
48 Correct 24 ms 12496 KB Output is correct
49 Correct 22 ms 12304 KB Output is correct
50 Correct 29 ms 12272 KB Output is correct
51 Correct 21 ms 12392 KB Output is correct
52 Correct 22 ms 12372 KB Output is correct
53 Correct 22 ms 12372 KB Output is correct
54 Correct 21 ms 12344 KB Output is correct
55 Correct 22 ms 12300 KB Output is correct
56 Correct 24 ms 12320 KB Output is correct
57 Correct 22 ms 12308 KB Output is correct
58 Correct 22 ms 12308 KB Output is correct
59 Correct 22 ms 12272 KB Output is correct
60 Correct 21 ms 12256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10956 KB Output is correct
2 Correct 7 ms 11084 KB Output is correct
3 Correct 6 ms 10956 KB Output is correct
4 Correct 7 ms 10956 KB Output is correct
5 Correct 8 ms 10956 KB Output is correct
6 Correct 7 ms 11076 KB Output is correct
7 Correct 7 ms 10956 KB Output is correct
8 Correct 6 ms 11064 KB Output is correct
9 Correct 7 ms 11084 KB Output is correct
10 Correct 7 ms 10956 KB Output is correct
11 Correct 7 ms 10956 KB Output is correct
12 Correct 7 ms 11076 KB Output is correct
13 Correct 7 ms 11092 KB Output is correct
14 Correct 11 ms 11596 KB Output is correct
15 Correct 15 ms 11988 KB Output is correct
16 Correct 20 ms 12328 KB Output is correct
17 Correct 19 ms 12368 KB Output is correct
18 Correct 19 ms 12364 KB Output is correct
19 Correct 19 ms 12392 KB Output is correct
20 Correct 26 ms 12364 KB Output is correct
21 Correct 20 ms 12308 KB Output is correct
22 Correct 23 ms 12292 KB Output is correct
23 Correct 19 ms 12356 KB Output is correct
24 Correct 20 ms 12284 KB Output is correct
25 Correct 19 ms 12352 KB Output is correct
26 Correct 8 ms 10956 KB Output is correct
27 Correct 8 ms 11076 KB Output is correct
28 Correct 9 ms 11088 KB Output is correct
29 Correct 12 ms 11600 KB Output is correct
30 Correct 20 ms 12144 KB Output is correct
31 Correct 20 ms 12380 KB Output is correct
32 Correct 21 ms 12364 KB Output is correct
33 Correct 21 ms 12344 KB Output is correct
34 Correct 21 ms 12400 KB Output is correct
35 Correct 20 ms 12408 KB Output is correct
36 Correct 21 ms 12424 KB Output is correct
37 Correct 20 ms 12416 KB Output is correct
38 Correct 21 ms 12344 KB Output is correct
39 Correct 21 ms 12348 KB Output is correct
40 Correct 20 ms 12324 KB Output is correct
41 Correct 6 ms 11084 KB Output is correct
42 Correct 7 ms 11028 KB Output is correct
43 Correct 7 ms 11044 KB Output is correct
44 Correct 7 ms 11084 KB Output is correct
45 Correct 7 ms 11124 KB Output is correct
46 Correct 8 ms 11084 KB Output is correct
47 Correct 9 ms 11340 KB Output is correct
48 Correct 9 ms 11340 KB Output is correct
49 Correct 12 ms 11592 KB Output is correct
50 Correct 19 ms 11980 KB Output is correct
51 Correct 24 ms 12356 KB Output is correct
52 Correct 22 ms 12352 KB Output is correct
53 Correct 21 ms 12364 KB Output is correct
54 Correct 21 ms 12348 KB Output is correct
55 Correct 21 ms 12344 KB Output is correct
56 Correct 22 ms 12280 KB Output is correct
57 Correct 22 ms 12360 KB Output is correct
58 Correct 24 ms 12496 KB Output is correct
59 Correct 22 ms 12304 KB Output is correct
60 Correct 29 ms 12272 KB Output is correct
61 Correct 21 ms 12392 KB Output is correct
62 Correct 22 ms 12372 KB Output is correct
63 Correct 22 ms 12372 KB Output is correct
64 Correct 21 ms 12344 KB Output is correct
65 Correct 22 ms 12300 KB Output is correct
66 Correct 24 ms 12320 KB Output is correct
67 Correct 22 ms 12308 KB Output is correct
68 Correct 22 ms 12308 KB Output is correct
69 Correct 22 ms 12272 KB Output is correct
70 Correct 21 ms 12256 KB Output is correct
71 Correct 9 ms 11340 KB Output is correct
72 Correct 9 ms 11340 KB Output is correct
73 Correct 10 ms 11388 KB Output is correct
74 Correct 10 ms 11396 KB Output is correct
75 Correct 10 ms 11372 KB Output is correct
76 Correct 12 ms 11500 KB Output is correct
77 Correct 14 ms 11596 KB Output is correct
78 Correct 12 ms 11456 KB Output is correct
79 Correct 12 ms 11480 KB Output is correct
80 Correct 12 ms 11596 KB Output is correct
81 Correct 18 ms 11964 KB Output is correct
82 Correct 18 ms 11932 KB Output is correct
83 Correct 22 ms 11860 KB Output is correct
84 Correct 19 ms 11976 KB Output is correct
85 Correct 18 ms 11936 KB Output is correct
86 Correct 19 ms 11952 KB Output is correct
87 Correct 19 ms 11980 KB Output is correct
88 Correct 19 ms 11892 KB Output is correct
89 Correct 19 ms 11980 KB Output is correct
90 Correct 19 ms 11980 KB Output is correct
91 Correct 27 ms 12356 KB Output is correct
92 Correct 24 ms 12244 KB Output is correct
93 Correct 24 ms 12256 KB Output is correct
94 Correct 24 ms 12308 KB Output is correct
95 Correct 24 ms 12296 KB Output is correct
96 Correct 27 ms 12356 KB Output is correct
97 Correct 30 ms 12272 KB Output is correct
98 Correct 27 ms 12272 KB Output is correct
99 Correct 26 ms 12344 KB Output is correct
100 Correct 26 ms 12364 KB Output is correct
101 Correct 27 ms 12292 KB Output is correct
102 Correct 26 ms 12340 KB Output is correct
103 Correct 26 ms 12276 KB Output is correct
104 Correct 27 ms 12308 KB Output is correct
105 Correct 27 ms 12280 KB Output is correct
106 Correct 28 ms 12248 KB Output is correct
107 Correct 27 ms 12328 KB Output is correct
108 Correct 26 ms 12356 KB Output is correct
109 Correct 26 ms 12312 KB Output is correct
110 Correct 27 ms 12352 KB Output is correct
111 Correct 26 ms 12348 KB Output is correct
112 Correct 31 ms 12264 KB Output is correct
113 Correct 26 ms 12316 KB Output is correct
114 Correct 26 ms 12276 KB Output is correct
115 Correct 27 ms 12356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10956 KB Output is correct
2 Correct 7 ms 11084 KB Output is correct
3 Correct 6 ms 10956 KB Output is correct
4 Correct 7 ms 10956 KB Output is correct
5 Correct 8 ms 10956 KB Output is correct
6 Correct 7 ms 11076 KB Output is correct
7 Correct 7 ms 10956 KB Output is correct
8 Correct 6 ms 11064 KB Output is correct
9 Correct 7 ms 11084 KB Output is correct
10 Correct 7 ms 10956 KB Output is correct
11 Correct 7 ms 10956 KB Output is correct
12 Correct 7 ms 11076 KB Output is correct
13 Correct 7 ms 11092 KB Output is correct
14 Correct 11 ms 11596 KB Output is correct
15 Correct 15 ms 11988 KB Output is correct
16 Correct 20 ms 12328 KB Output is correct
17 Correct 19 ms 12368 KB Output is correct
18 Correct 19 ms 12364 KB Output is correct
19 Correct 19 ms 12392 KB Output is correct
20 Correct 26 ms 12364 KB Output is correct
21 Correct 20 ms 12308 KB Output is correct
22 Correct 23 ms 12292 KB Output is correct
23 Correct 19 ms 12356 KB Output is correct
24 Correct 20 ms 12284 KB Output is correct
25 Correct 19 ms 12352 KB Output is correct
26 Correct 8 ms 10956 KB Output is correct
27 Correct 8 ms 11076 KB Output is correct
28 Correct 9 ms 11088 KB Output is correct
29 Correct 12 ms 11600 KB Output is correct
30 Correct 20 ms 12144 KB Output is correct
31 Correct 20 ms 12380 KB Output is correct
32 Correct 21 ms 12364 KB Output is correct
33 Correct 21 ms 12344 KB Output is correct
34 Correct 21 ms 12400 KB Output is correct
35 Correct 20 ms 12408 KB Output is correct
36 Correct 21 ms 12424 KB Output is correct
37 Correct 20 ms 12416 KB Output is correct
38 Correct 21 ms 12344 KB Output is correct
39 Correct 21 ms 12348 KB Output is correct
40 Correct 20 ms 12324 KB Output is correct
41 Correct 6 ms 11084 KB Output is correct
42 Correct 7 ms 11028 KB Output is correct
43 Correct 7 ms 11044 KB Output is correct
44 Correct 7 ms 11084 KB Output is correct
45 Correct 7 ms 11124 KB Output is correct
46 Correct 8 ms 11084 KB Output is correct
47 Correct 9 ms 11340 KB Output is correct
48 Correct 9 ms 11340 KB Output is correct
49 Correct 12 ms 11592 KB Output is correct
50 Correct 19 ms 11980 KB Output is correct
51 Correct 24 ms 12356 KB Output is correct
52 Correct 22 ms 12352 KB Output is correct
53 Correct 21 ms 12364 KB Output is correct
54 Correct 21 ms 12348 KB Output is correct
55 Correct 21 ms 12344 KB Output is correct
56 Correct 22 ms 12280 KB Output is correct
57 Correct 22 ms 12360 KB Output is correct
58 Correct 24 ms 12496 KB Output is correct
59 Correct 22 ms 12304 KB Output is correct
60 Correct 29 ms 12272 KB Output is correct
61 Correct 21 ms 12392 KB Output is correct
62 Correct 22 ms 12372 KB Output is correct
63 Correct 22 ms 12372 KB Output is correct
64 Correct 21 ms 12344 KB Output is correct
65 Correct 22 ms 12300 KB Output is correct
66 Correct 24 ms 12320 KB Output is correct
67 Correct 22 ms 12308 KB Output is correct
68 Correct 22 ms 12308 KB Output is correct
69 Correct 22 ms 12272 KB Output is correct
70 Correct 21 ms 12256 KB Output is correct
71 Correct 9 ms 11340 KB Output is correct
72 Correct 9 ms 11340 KB Output is correct
73 Correct 10 ms 11388 KB Output is correct
74 Correct 10 ms 11396 KB Output is correct
75 Correct 10 ms 11372 KB Output is correct
76 Correct 12 ms 11500 KB Output is correct
77 Correct 14 ms 11596 KB Output is correct
78 Correct 12 ms 11456 KB Output is correct
79 Correct 12 ms 11480 KB Output is correct
80 Correct 12 ms 11596 KB Output is correct
81 Correct 18 ms 11964 KB Output is correct
82 Correct 18 ms 11932 KB Output is correct
83 Correct 22 ms 11860 KB Output is correct
84 Correct 19 ms 11976 KB Output is correct
85 Correct 18 ms 11936 KB Output is correct
86 Correct 19 ms 11952 KB Output is correct
87 Correct 19 ms 11980 KB Output is correct
88 Correct 19 ms 11892 KB Output is correct
89 Correct 19 ms 11980 KB Output is correct
90 Correct 19 ms 11980 KB Output is correct
91 Correct 27 ms 12356 KB Output is correct
92 Correct 24 ms 12244 KB Output is correct
93 Correct 24 ms 12256 KB Output is correct
94 Correct 24 ms 12308 KB Output is correct
95 Correct 24 ms 12296 KB Output is correct
96 Correct 27 ms 12356 KB Output is correct
97 Correct 30 ms 12272 KB Output is correct
98 Correct 27 ms 12272 KB Output is correct
99 Correct 26 ms 12344 KB Output is correct
100 Correct 26 ms 12364 KB Output is correct
101 Correct 27 ms 12292 KB Output is correct
102 Correct 26 ms 12340 KB Output is correct
103 Correct 26 ms 12276 KB Output is correct
104 Correct 27 ms 12308 KB Output is correct
105 Correct 27 ms 12280 KB Output is correct
106 Correct 28 ms 12248 KB Output is correct
107 Correct 27 ms 12328 KB Output is correct
108 Correct 26 ms 12356 KB Output is correct
109 Correct 26 ms 12312 KB Output is correct
110 Correct 27 ms 12352 KB Output is correct
111 Correct 26 ms 12348 KB Output is correct
112 Correct 31 ms 12264 KB Output is correct
113 Correct 26 ms 12316 KB Output is correct
114 Correct 26 ms 12276 KB Output is correct
115 Correct 27 ms 12356 KB Output is correct
116 Execution timed out 7050 ms 142696 KB Time limit exceeded
117 Halted 0 ms 0 KB -