답안 #589901

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589901 2022-07-05T11:33:30 Z alextodoran Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 4344 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 300000;
const int A_MAX = 300000;
const int Q_MAX = 50000;

int N, Q;
int A[N_MAX + 2];

int bucketSize;

struct Query {
    int l, r;
    int id;
};
bool operator < (const Query &a, const Query &b) {
    return make_pair((a.l - 1) / bucketSize, a.r) < make_pair((b.l - 1) / bucketSize, b.r);
}

Query queries[Q_MAX + 2];

int occ[A_MAX + 2];
map <int, int> mp;

void update (int val, int sgn) {
    if (--mp[occ[val]] == 0) {
        mp.erase(occ[val]);
    }
    occ[val] += sgn;
    mp[occ[val]]++;
}

ll solve () {
    ll sum = 0, cnt = 0;
    ll sumLR = 0, sumRL = 0;
    ll X = 0, Y = 0;
    for (map <int, int> :: reverse_iterator it = mp.rbegin(); it != mp.rend(); it++) {
        ll len, occ; tie(len, occ) = *it;

        ll u = sumRL - sumLR + sum * occ; ll v = (2 * sum);
        ll L = (v == 0 ? 0 : (u >= 0 ? u / v : -(-u / v)));
        L = max(L, (ll) 0); L = min(L, occ - 1);
        ll R = (occ - 1) - L;

        ll aux;
        // place L to the left
        Y += len * (sumRL * L + len * cnt * L * (L - 1) / 2 + len * (L * (L - 1) * (2 * L - 1) / 6 - L * (L - 1) / 2) / 2);
        aux = 0;
        aux += sum * sum * L;
        aux += 2 * sum * len * L * (L - 1) / 2;
        aux += len * len * L * (L - 1) * (2 * L - 1) / 6;
        aux -= X * L + len * len * L * (L - 1) / 2;
        aux /= 2;
        Y += aux;
        X += len * len * L;
        sumLR += sum * L + len * L * (L - 1) / 2;
        sumRL += len * cnt * L + len * L * (L - 1) / 2;
        sum += len * L;
        cnt += L;
        // place R to the right
        Y += len * (sumLR * R + len * cnt * R * (R - 1) / 2 + len * (R * (R - 1) * (2 * R - 1) / 6 - R * (R - 1) / 2) / 2);
        aux = 0;
        aux += sum * sum * R;
        aux += 2 * sum * len * R * (R - 1) / 2;
        aux += len * len * R * (R - 1) * (2 * R - 1) / 6;
        aux -= X * R + len * len * R * (R - 1) / 2;
        aux /= 2;
        Y += aux;
        X += len * len * R;
        sumRL += sum * R + len * R * (R - 1) / 2;
        sumLR += len * cnt * R + len * R * (R - 1) / 2;
        sum += len * R;
        cnt += R;

        if (sumLR < sumRL) {
            Y += sumRL * len + (sum * sum - X) / 2;
            X += len * len;
            sumLR += sum;
            sumRL += len * cnt;
            sum += len;
            cnt += 1;
        } else {
            Y += sumLR * len + (sum * sum - X) / 2;
            X += len * len;
            sumRL += sum;
            sumLR += len * cnt;
            sum += len;
            cnt += 1;
        }
    }
    ll ret = (cnt - 1) * X + 2 * Y;
    ret += (ll) sum * (cnt - 1);
    ret /= 2;
    ret = cnt * sum * (sum + 1) / 2 - ret;
    return ret;
}

ll answer[Q_MAX + 2];

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> N >> Q;
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
    }
    bucketSize = max(1, (int) (N / sqrt(Q)));
    for (int i = 1; i <= Q; i++) {
        cin >> queries[i].l >> queries[i].r;
        queries[i].id = i;
    }
    sort(queries + 1, queries + Q + 1);

    int l = 1, r = 0;
    for (int i = 1; i <= Q; i++) {
        while (queries[i].l < l) {
            update(A[--l], +1);
        }
        while (r < queries[i].r) {
            update(A[++r], +1);
        }
        while (l < queries[i].l) {
            update(A[l++], -1);
        }
        while (queries[i].r < r) {
            update(A[r--], -1);
        }
        answer[queries[i].id] = solve();
    }
    for (int i = 1; i <= Q; i++) {
        cout << answer[i] << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 13 ms 732 KB Output is correct
5 Correct 29 ms 1084 KB Output is correct
6 Correct 42 ms 1436 KB Output is correct
7 Correct 36 ms 1492 KB Output is correct
8 Correct 41 ms 1488 KB Output is correct
9 Correct 41 ms 1492 KB Output is correct
10 Correct 42 ms 1388 KB Output is correct
11 Correct 54 ms 1496 KB Output is correct
12 Correct 55 ms 1488 KB Output is correct
13 Correct 41 ms 1492 KB Output is correct
14 Correct 43 ms 1500 KB Output is correct
15 Correct 42 ms 1504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 13 ms 732 KB Output is correct
5 Correct 29 ms 1084 KB Output is correct
6 Correct 42 ms 1436 KB Output is correct
7 Correct 36 ms 1492 KB Output is correct
8 Correct 41 ms 1488 KB Output is correct
9 Correct 41 ms 1492 KB Output is correct
10 Correct 42 ms 1388 KB Output is correct
11 Correct 54 ms 1496 KB Output is correct
12 Correct 55 ms 1488 KB Output is correct
13 Correct 41 ms 1492 KB Output is correct
14 Correct 43 ms 1500 KB Output is correct
15 Correct 42 ms 1504 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 17 ms 860 KB Output is correct
20 Correct 35 ms 1108 KB Output is correct
21 Correct 51 ms 1612 KB Output is correct
22 Correct 49 ms 1488 KB Output is correct
23 Correct 48 ms 1448 KB Output is correct
24 Correct 48 ms 1440 KB Output is correct
25 Correct 51 ms 1492 KB Output is correct
26 Correct 58 ms 1492 KB Output is correct
27 Correct 53 ms 1492 KB Output is correct
28 Correct 48 ms 1492 KB Output is correct
29 Correct 50 ms 1492 KB Output is correct
30 Correct 51 ms 1468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 13 ms 732 KB Output is correct
5 Correct 29 ms 1084 KB Output is correct
6 Correct 42 ms 1436 KB Output is correct
7 Correct 36 ms 1492 KB Output is correct
8 Correct 41 ms 1488 KB Output is correct
9 Correct 41 ms 1492 KB Output is correct
10 Correct 42 ms 1388 KB Output is correct
11 Correct 54 ms 1496 KB Output is correct
12 Correct 55 ms 1488 KB Output is correct
13 Correct 41 ms 1492 KB Output is correct
14 Correct 43 ms 1500 KB Output is correct
15 Correct 42 ms 1504 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 17 ms 860 KB Output is correct
20 Correct 35 ms 1108 KB Output is correct
21 Correct 51 ms 1612 KB Output is correct
22 Correct 49 ms 1488 KB Output is correct
23 Correct 48 ms 1448 KB Output is correct
24 Correct 48 ms 1440 KB Output is correct
25 Correct 51 ms 1492 KB Output is correct
26 Correct 58 ms 1492 KB Output is correct
27 Correct 53 ms 1492 KB Output is correct
28 Correct 48 ms 1492 KB Output is correct
29 Correct 50 ms 1492 KB Output is correct
30 Correct 51 ms 1468 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 9 ms 548 KB Output is correct
38 Correct 7 ms 468 KB Output is correct
39 Correct 18 ms 744 KB Output is correct
40 Correct 35 ms 1104 KB Output is correct
41 Correct 57 ms 1512 KB Output is correct
42 Correct 58 ms 1492 KB Output is correct
43 Correct 63 ms 1416 KB Output is correct
44 Correct 59 ms 1492 KB Output is correct
45 Correct 56 ms 1520 KB Output is correct
46 Correct 56 ms 1492 KB Output is correct
47 Correct 58 ms 1460 KB Output is correct
48 Correct 63 ms 1624 KB Output is correct
49 Correct 58 ms 1516 KB Output is correct
50 Correct 57 ms 1524 KB Output is correct
51 Correct 60 ms 1508 KB Output is correct
52 Correct 70 ms 1516 KB Output is correct
53 Correct 68 ms 1516 KB Output is correct
54 Correct 59 ms 1492 KB Output is correct
55 Correct 56 ms 1516 KB Output is correct
56 Correct 62 ms 1516 KB Output is correct
57 Correct 57 ms 1524 KB Output is correct
58 Correct 69 ms 1560 KB Output is correct
59 Correct 58 ms 1512 KB Output is correct
60 Correct 59 ms 1516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 13 ms 732 KB Output is correct
15 Correct 29 ms 1084 KB Output is correct
16 Correct 42 ms 1436 KB Output is correct
17 Correct 36 ms 1492 KB Output is correct
18 Correct 41 ms 1488 KB Output is correct
19 Correct 41 ms 1492 KB Output is correct
20 Correct 42 ms 1388 KB Output is correct
21 Correct 54 ms 1496 KB Output is correct
22 Correct 55 ms 1488 KB Output is correct
23 Correct 41 ms 1492 KB Output is correct
24 Correct 43 ms 1500 KB Output is correct
25 Correct 42 ms 1504 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 17 ms 860 KB Output is correct
30 Correct 35 ms 1108 KB Output is correct
31 Correct 51 ms 1612 KB Output is correct
32 Correct 49 ms 1488 KB Output is correct
33 Correct 48 ms 1448 KB Output is correct
34 Correct 48 ms 1440 KB Output is correct
35 Correct 51 ms 1492 KB Output is correct
36 Correct 58 ms 1492 KB Output is correct
37 Correct 53 ms 1492 KB Output is correct
38 Correct 48 ms 1492 KB Output is correct
39 Correct 50 ms 1492 KB Output is correct
40 Correct 51 ms 1468 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 9 ms 548 KB Output is correct
48 Correct 7 ms 468 KB Output is correct
49 Correct 18 ms 744 KB Output is correct
50 Correct 35 ms 1104 KB Output is correct
51 Correct 57 ms 1512 KB Output is correct
52 Correct 58 ms 1492 KB Output is correct
53 Correct 63 ms 1416 KB Output is correct
54 Correct 59 ms 1492 KB Output is correct
55 Correct 56 ms 1520 KB Output is correct
56 Correct 56 ms 1492 KB Output is correct
57 Correct 58 ms 1460 KB Output is correct
58 Correct 63 ms 1624 KB Output is correct
59 Correct 58 ms 1516 KB Output is correct
60 Correct 57 ms 1524 KB Output is correct
61 Correct 60 ms 1508 KB Output is correct
62 Correct 70 ms 1516 KB Output is correct
63 Correct 68 ms 1516 KB Output is correct
64 Correct 59 ms 1492 KB Output is correct
65 Correct 56 ms 1516 KB Output is correct
66 Correct 62 ms 1516 KB Output is correct
67 Correct 57 ms 1524 KB Output is correct
68 Correct 69 ms 1560 KB Output is correct
69 Correct 58 ms 1512 KB Output is correct
70 Correct 59 ms 1516 KB Output is correct
71 Correct 6 ms 468 KB Output is correct
72 Correct 6 ms 468 KB Output is correct
73 Correct 6 ms 468 KB Output is correct
74 Correct 7 ms 468 KB Output is correct
75 Correct 6 ms 544 KB Output is correct
76 Correct 14 ms 676 KB Output is correct
77 Correct 15 ms 760 KB Output is correct
78 Correct 11 ms 756 KB Output is correct
79 Correct 12 ms 724 KB Output is correct
80 Correct 13 ms 852 KB Output is correct
81 Correct 26 ms 1164 KB Output is correct
82 Correct 22 ms 1164 KB Output is correct
83 Correct 23 ms 1156 KB Output is correct
84 Correct 22 ms 1076 KB Output is correct
85 Correct 21 ms 1160 KB Output is correct
86 Correct 21 ms 1108 KB Output is correct
87 Correct 22 ms 1108 KB Output is correct
88 Correct 21 ms 1100 KB Output is correct
89 Correct 21 ms 1208 KB Output is correct
90 Correct 23 ms 1204 KB Output is correct
91 Correct 27 ms 1852 KB Output is correct
92 Correct 28 ms 1876 KB Output is correct
93 Correct 28 ms 1876 KB Output is correct
94 Correct 29 ms 1768 KB Output is correct
95 Correct 34 ms 1868 KB Output is correct
96 Correct 38 ms 2268 KB Output is correct
97 Correct 28 ms 2184 KB Output is correct
98 Correct 28 ms 2188 KB Output is correct
99 Correct 28 ms 2224 KB Output is correct
100 Correct 31 ms 2236 KB Output is correct
101 Correct 31 ms 2180 KB Output is correct
102 Correct 28 ms 2224 KB Output is correct
103 Correct 28 ms 2260 KB Output is correct
104 Correct 29 ms 2252 KB Output is correct
105 Correct 28 ms 2200 KB Output is correct
106 Correct 26 ms 2532 KB Output is correct
107 Correct 27 ms 2376 KB Output is correct
108 Correct 26 ms 2456 KB Output is correct
109 Correct 28 ms 2384 KB Output is correct
110 Correct 27 ms 2388 KB Output is correct
111 Correct 27 ms 2380 KB Output is correct
112 Correct 27 ms 2408 KB Output is correct
113 Correct 30 ms 2372 KB Output is correct
114 Correct 33 ms 2380 KB Output is correct
115 Correct 33 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 13 ms 732 KB Output is correct
15 Correct 29 ms 1084 KB Output is correct
16 Correct 42 ms 1436 KB Output is correct
17 Correct 36 ms 1492 KB Output is correct
18 Correct 41 ms 1488 KB Output is correct
19 Correct 41 ms 1492 KB Output is correct
20 Correct 42 ms 1388 KB Output is correct
21 Correct 54 ms 1496 KB Output is correct
22 Correct 55 ms 1488 KB Output is correct
23 Correct 41 ms 1492 KB Output is correct
24 Correct 43 ms 1500 KB Output is correct
25 Correct 42 ms 1504 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 17 ms 860 KB Output is correct
30 Correct 35 ms 1108 KB Output is correct
31 Correct 51 ms 1612 KB Output is correct
32 Correct 49 ms 1488 KB Output is correct
33 Correct 48 ms 1448 KB Output is correct
34 Correct 48 ms 1440 KB Output is correct
35 Correct 51 ms 1492 KB Output is correct
36 Correct 58 ms 1492 KB Output is correct
37 Correct 53 ms 1492 KB Output is correct
38 Correct 48 ms 1492 KB Output is correct
39 Correct 50 ms 1492 KB Output is correct
40 Correct 51 ms 1468 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 9 ms 548 KB Output is correct
48 Correct 7 ms 468 KB Output is correct
49 Correct 18 ms 744 KB Output is correct
50 Correct 35 ms 1104 KB Output is correct
51 Correct 57 ms 1512 KB Output is correct
52 Correct 58 ms 1492 KB Output is correct
53 Correct 63 ms 1416 KB Output is correct
54 Correct 59 ms 1492 KB Output is correct
55 Correct 56 ms 1520 KB Output is correct
56 Correct 56 ms 1492 KB Output is correct
57 Correct 58 ms 1460 KB Output is correct
58 Correct 63 ms 1624 KB Output is correct
59 Correct 58 ms 1516 KB Output is correct
60 Correct 57 ms 1524 KB Output is correct
61 Correct 60 ms 1508 KB Output is correct
62 Correct 70 ms 1516 KB Output is correct
63 Correct 68 ms 1516 KB Output is correct
64 Correct 59 ms 1492 KB Output is correct
65 Correct 56 ms 1516 KB Output is correct
66 Correct 62 ms 1516 KB Output is correct
67 Correct 57 ms 1524 KB Output is correct
68 Correct 69 ms 1560 KB Output is correct
69 Correct 58 ms 1512 KB Output is correct
70 Correct 59 ms 1516 KB Output is correct
71 Correct 6 ms 468 KB Output is correct
72 Correct 6 ms 468 KB Output is correct
73 Correct 6 ms 468 KB Output is correct
74 Correct 7 ms 468 KB Output is correct
75 Correct 6 ms 544 KB Output is correct
76 Correct 14 ms 676 KB Output is correct
77 Correct 15 ms 760 KB Output is correct
78 Correct 11 ms 756 KB Output is correct
79 Correct 12 ms 724 KB Output is correct
80 Correct 13 ms 852 KB Output is correct
81 Correct 26 ms 1164 KB Output is correct
82 Correct 22 ms 1164 KB Output is correct
83 Correct 23 ms 1156 KB Output is correct
84 Correct 22 ms 1076 KB Output is correct
85 Correct 21 ms 1160 KB Output is correct
86 Correct 21 ms 1108 KB Output is correct
87 Correct 22 ms 1108 KB Output is correct
88 Correct 21 ms 1100 KB Output is correct
89 Correct 21 ms 1208 KB Output is correct
90 Correct 23 ms 1204 KB Output is correct
91 Correct 27 ms 1852 KB Output is correct
92 Correct 28 ms 1876 KB Output is correct
93 Correct 28 ms 1876 KB Output is correct
94 Correct 29 ms 1768 KB Output is correct
95 Correct 34 ms 1868 KB Output is correct
96 Correct 38 ms 2268 KB Output is correct
97 Correct 28 ms 2184 KB Output is correct
98 Correct 28 ms 2188 KB Output is correct
99 Correct 28 ms 2224 KB Output is correct
100 Correct 31 ms 2236 KB Output is correct
101 Correct 31 ms 2180 KB Output is correct
102 Correct 28 ms 2224 KB Output is correct
103 Correct 28 ms 2260 KB Output is correct
104 Correct 29 ms 2252 KB Output is correct
105 Correct 28 ms 2200 KB Output is correct
106 Correct 26 ms 2532 KB Output is correct
107 Correct 27 ms 2376 KB Output is correct
108 Correct 26 ms 2456 KB Output is correct
109 Correct 28 ms 2384 KB Output is correct
110 Correct 27 ms 2388 KB Output is correct
111 Correct 27 ms 2380 KB Output is correct
112 Correct 27 ms 2408 KB Output is correct
113 Correct 30 ms 2372 KB Output is correct
114 Correct 33 ms 2380 KB Output is correct
115 Correct 33 ms 2388 KB Output is correct
116 Correct 487 ms 2116 KB Output is correct
117 Correct 459 ms 2232 KB Output is correct
118 Correct 2430 ms 2716 KB Output is correct
119 Correct 1551 ms 2764 KB Output is correct
120 Correct 1133 ms 2772 KB Output is correct
121 Correct 2751 ms 3168 KB Output is correct
122 Correct 2009 ms 3228 KB Output is correct
123 Correct 6815 ms 4016 KB Output is correct
124 Correct 3301 ms 4304 KB Output is correct
125 Correct 2505 ms 4344 KB Output is correct
126 Execution timed out 7057 ms 4192 KB Time limit exceeded
127 Halted 0 ms 0 KB -