Submission #589907

# Submission time Handle Problem Language Result Execution time Memory
589907 2022-07-05T11:39:58 Z alextodoran Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 4340 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 () {
    int 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++) {
        int len, occ; tie(len, occ) = *it;

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

        for (char c : {'L', 'R'}) {
            ll sum1L = (ll) L * (L - 1) / 2, sum2L = (ll) L * (L - 1) * (2 * L - 1) / 6;
            Y += len * (sumRL * L + (ll) len * cnt * sum1L + len * (sum2L - sum1L) / 2);
            ll aux = 0;
            aux += (ll) sum * sum * L;
            aux += (ll) 2 * sum * len * sum1L;
            aux += (ll) len * len * sum2L;
            aux -= X * L + (ll) len * len * sum1L;
            aux /= 2;
            Y += aux;
            X += (ll) len * len * L;
            sumLR += (ll) sum * L + len * sum1L;
            sumRL += (ll) len * cnt * L + len * sum1L;
            sum += (ll) len * L;
            cnt += L;
            swap(L, R);
            swap(sumLR, sumRL);
        }

        if (sumLR < sumRL) {
            Y += sumRL * len + ((ll) sum * sum - X) / 2;
            X += (ll) len * len;
            sumLR += sum;
            sumRL += (ll) len * cnt;
            sum += len;
            cnt += 1;
        } else {
            Y += sumLR * len + ((ll) sum * sum - X) / 2;
            X += (ll) len * len;
            sumRL += sum;
            sumLR += (ll) len * cnt;
            sum += len;
            cnt += 1;
        }
    }
    ll ret = (cnt - 1) * X + 2 * Y;
    ret += (ll) sum * (cnt - 1);
    ret /= 2;
    ret = (ll) 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;
}

Compilation message

diversity.cpp: In function 'll solve()':
diversity.cpp:56:19: warning: unused variable 'c' [-Wunused-variable]
   56 |         for (char c : {'L', 'R'}) {
      |                   ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 308 KB Output is correct
4 Correct 13 ms 852 KB Output is correct
5 Correct 27 ms 1504 KB Output is correct
6 Correct 44 ms 2000 KB Output is correct
7 Correct 42 ms 2080 KB Output is correct
8 Correct 56 ms 2008 KB Output is correct
9 Correct 43 ms 1936 KB Output is correct
10 Correct 41 ms 1996 KB Output is correct
11 Correct 42 ms 1992 KB Output is correct
12 Correct 44 ms 2004 KB Output is correct
13 Correct 44 ms 2004 KB Output is correct
14 Correct 44 ms 2004 KB Output is correct
15 Correct 50 ms 1980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 308 KB Output is correct
4 Correct 13 ms 852 KB Output is correct
5 Correct 27 ms 1504 KB Output is correct
6 Correct 44 ms 2000 KB Output is correct
7 Correct 42 ms 2080 KB Output is correct
8 Correct 56 ms 2008 KB Output is correct
9 Correct 43 ms 1936 KB Output is correct
10 Correct 41 ms 1996 KB Output is correct
11 Correct 42 ms 1992 KB Output is correct
12 Correct 44 ms 2004 KB Output is correct
13 Correct 44 ms 2004 KB Output is correct
14 Correct 44 ms 2004 KB Output is correct
15 Correct 50 ms 1980 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 21 ms 972 KB Output is correct
20 Correct 33 ms 1628 KB Output is correct
21 Correct 50 ms 2004 KB Output is correct
22 Correct 53 ms 1916 KB Output is correct
23 Correct 49 ms 2004 KB Output is correct
24 Correct 52 ms 2004 KB Output is correct
25 Correct 53 ms 1988 KB Output is correct
26 Correct 55 ms 2004 KB Output is correct
27 Correct 52 ms 2008 KB Output is correct
28 Correct 52 ms 2080 KB Output is correct
29 Correct 51 ms 1924 KB Output is correct
30 Correct 52 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 308 KB Output is correct
4 Correct 13 ms 852 KB Output is correct
5 Correct 27 ms 1504 KB Output is correct
6 Correct 44 ms 2000 KB Output is correct
7 Correct 42 ms 2080 KB Output is correct
8 Correct 56 ms 2008 KB Output is correct
9 Correct 43 ms 1936 KB Output is correct
10 Correct 41 ms 1996 KB Output is correct
11 Correct 42 ms 1992 KB Output is correct
12 Correct 44 ms 2004 KB Output is correct
13 Correct 44 ms 2004 KB Output is correct
14 Correct 44 ms 2004 KB Output is correct
15 Correct 50 ms 1980 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 21 ms 972 KB Output is correct
20 Correct 33 ms 1628 KB Output is correct
21 Correct 50 ms 2004 KB Output is correct
22 Correct 53 ms 1916 KB Output is correct
23 Correct 49 ms 2004 KB Output is correct
24 Correct 52 ms 2004 KB Output is correct
25 Correct 53 ms 1988 KB Output is correct
26 Correct 55 ms 2004 KB Output is correct
27 Correct 52 ms 2008 KB Output is correct
28 Correct 52 ms 2080 KB Output is correct
29 Correct 51 ms 1924 KB Output is correct
30 Correct 52 ms 2004 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 9 ms 724 KB Output is correct
38 Correct 7 ms 724 KB Output is correct
39 Correct 19 ms 1108 KB Output is correct
40 Correct 37 ms 1612 KB Output is correct
41 Correct 59 ms 1996 KB Output is correct
42 Correct 62 ms 1960 KB Output is correct
43 Correct 64 ms 2032 KB Output is correct
44 Correct 58 ms 2028 KB Output is correct
45 Correct 58 ms 1940 KB Output is correct
46 Correct 74 ms 1936 KB Output is correct
47 Correct 74 ms 2016 KB Output is correct
48 Correct 59 ms 2024 KB Output is correct
49 Correct 62 ms 2000 KB Output is correct
50 Correct 59 ms 2016 KB Output is correct
51 Correct 64 ms 2028 KB Output is correct
52 Correct 60 ms 1964 KB Output is correct
53 Correct 59 ms 2000 KB Output is correct
54 Correct 58 ms 1988 KB Output is correct
55 Correct 55 ms 1960 KB Output is correct
56 Correct 56 ms 1988 KB Output is correct
57 Correct 57 ms 2064 KB Output is correct
58 Correct 56 ms 1928 KB Output is correct
59 Correct 60 ms 1996 KB Output is correct
60 Correct 57 ms 1972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 308 KB Output is correct
14 Correct 13 ms 852 KB Output is correct
15 Correct 27 ms 1504 KB Output is correct
16 Correct 44 ms 2000 KB Output is correct
17 Correct 42 ms 2080 KB Output is correct
18 Correct 56 ms 2008 KB Output is correct
19 Correct 43 ms 1936 KB Output is correct
20 Correct 41 ms 1996 KB Output is correct
21 Correct 42 ms 1992 KB Output is correct
22 Correct 44 ms 2004 KB Output is correct
23 Correct 44 ms 2004 KB Output is correct
24 Correct 44 ms 2004 KB Output is correct
25 Correct 50 ms 1980 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 21 ms 972 KB Output is correct
30 Correct 33 ms 1628 KB Output is correct
31 Correct 50 ms 2004 KB Output is correct
32 Correct 53 ms 1916 KB Output is correct
33 Correct 49 ms 2004 KB Output is correct
34 Correct 52 ms 2004 KB Output is correct
35 Correct 53 ms 1988 KB Output is correct
36 Correct 55 ms 2004 KB Output is correct
37 Correct 52 ms 2008 KB Output is correct
38 Correct 52 ms 2080 KB Output is correct
39 Correct 51 ms 1924 KB Output is correct
40 Correct 52 ms 2004 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 328 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 348 KB Output is correct
47 Correct 9 ms 724 KB Output is correct
48 Correct 7 ms 724 KB Output is correct
49 Correct 19 ms 1108 KB Output is correct
50 Correct 37 ms 1612 KB Output is correct
51 Correct 59 ms 1996 KB Output is correct
52 Correct 62 ms 1960 KB Output is correct
53 Correct 64 ms 2032 KB Output is correct
54 Correct 58 ms 2028 KB Output is correct
55 Correct 58 ms 1940 KB Output is correct
56 Correct 74 ms 1936 KB Output is correct
57 Correct 74 ms 2016 KB Output is correct
58 Correct 59 ms 2024 KB Output is correct
59 Correct 62 ms 2000 KB Output is correct
60 Correct 59 ms 2016 KB Output is correct
61 Correct 64 ms 2028 KB Output is correct
62 Correct 60 ms 1964 KB Output is correct
63 Correct 59 ms 2000 KB Output is correct
64 Correct 58 ms 1988 KB Output is correct
65 Correct 55 ms 1960 KB Output is correct
66 Correct 56 ms 1988 KB Output is correct
67 Correct 57 ms 2064 KB Output is correct
68 Correct 56 ms 1928 KB Output is correct
69 Correct 60 ms 1996 KB Output is correct
70 Correct 57 ms 1972 KB Output is correct
71 Correct 6 ms 724 KB Output is correct
72 Correct 6 ms 724 KB Output is correct
73 Correct 6 ms 648 KB Output is correct
74 Correct 7 ms 724 KB Output is correct
75 Correct 9 ms 652 KB Output is correct
76 Correct 13 ms 1116 KB Output is correct
77 Correct 12 ms 1180 KB Output is correct
78 Correct 13 ms 1176 KB Output is correct
79 Correct 12 ms 1100 KB Output is correct
80 Correct 15 ms 1104 KB Output is correct
81 Correct 22 ms 1536 KB Output is correct
82 Correct 23 ms 1652 KB Output is correct
83 Correct 22 ms 1660 KB Output is correct
84 Correct 23 ms 1616 KB Output is correct
85 Correct 27 ms 1640 KB Output is correct
86 Correct 27 ms 1648 KB Output is correct
87 Correct 29 ms 1608 KB Output is correct
88 Correct 26 ms 1616 KB Output is correct
89 Correct 23 ms 1612 KB Output is correct
90 Correct 23 ms 1612 KB Output is correct
91 Correct 28 ms 2384 KB Output is correct
92 Correct 30 ms 2480 KB Output is correct
93 Correct 29 ms 2332 KB Output is correct
94 Correct 34 ms 2388 KB Output is correct
95 Correct 42 ms 2324 KB Output is correct
96 Correct 32 ms 2700 KB Output is correct
97 Correct 31 ms 2680 KB Output is correct
98 Correct 30 ms 2776 KB Output is correct
99 Correct 30 ms 2764 KB Output is correct
100 Correct 35 ms 2792 KB Output is correct
101 Correct 30 ms 2748 KB Output is correct
102 Correct 31 ms 2708 KB Output is correct
103 Correct 32 ms 2756 KB Output is correct
104 Correct 31 ms 2760 KB Output is correct
105 Correct 33 ms 2780 KB Output is correct
106 Correct 39 ms 2956 KB Output is correct
107 Correct 30 ms 2944 KB Output is correct
108 Correct 31 ms 2964 KB Output is correct
109 Correct 28 ms 2892 KB Output is correct
110 Correct 29 ms 2892 KB Output is correct
111 Correct 36 ms 2928 KB Output is correct
112 Correct 37 ms 2932 KB Output is correct
113 Correct 32 ms 2928 KB Output is correct
114 Correct 29 ms 2976 KB Output is correct
115 Correct 27 ms 2928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 308 KB Output is correct
14 Correct 13 ms 852 KB Output is correct
15 Correct 27 ms 1504 KB Output is correct
16 Correct 44 ms 2000 KB Output is correct
17 Correct 42 ms 2080 KB Output is correct
18 Correct 56 ms 2008 KB Output is correct
19 Correct 43 ms 1936 KB Output is correct
20 Correct 41 ms 1996 KB Output is correct
21 Correct 42 ms 1992 KB Output is correct
22 Correct 44 ms 2004 KB Output is correct
23 Correct 44 ms 2004 KB Output is correct
24 Correct 44 ms 2004 KB Output is correct
25 Correct 50 ms 1980 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 21 ms 972 KB Output is correct
30 Correct 33 ms 1628 KB Output is correct
31 Correct 50 ms 2004 KB Output is correct
32 Correct 53 ms 1916 KB Output is correct
33 Correct 49 ms 2004 KB Output is correct
34 Correct 52 ms 2004 KB Output is correct
35 Correct 53 ms 1988 KB Output is correct
36 Correct 55 ms 2004 KB Output is correct
37 Correct 52 ms 2008 KB Output is correct
38 Correct 52 ms 2080 KB Output is correct
39 Correct 51 ms 1924 KB Output is correct
40 Correct 52 ms 2004 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 328 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 348 KB Output is correct
47 Correct 9 ms 724 KB Output is correct
48 Correct 7 ms 724 KB Output is correct
49 Correct 19 ms 1108 KB Output is correct
50 Correct 37 ms 1612 KB Output is correct
51 Correct 59 ms 1996 KB Output is correct
52 Correct 62 ms 1960 KB Output is correct
53 Correct 64 ms 2032 KB Output is correct
54 Correct 58 ms 2028 KB Output is correct
55 Correct 58 ms 1940 KB Output is correct
56 Correct 74 ms 1936 KB Output is correct
57 Correct 74 ms 2016 KB Output is correct
58 Correct 59 ms 2024 KB Output is correct
59 Correct 62 ms 2000 KB Output is correct
60 Correct 59 ms 2016 KB Output is correct
61 Correct 64 ms 2028 KB Output is correct
62 Correct 60 ms 1964 KB Output is correct
63 Correct 59 ms 2000 KB Output is correct
64 Correct 58 ms 1988 KB Output is correct
65 Correct 55 ms 1960 KB Output is correct
66 Correct 56 ms 1988 KB Output is correct
67 Correct 57 ms 2064 KB Output is correct
68 Correct 56 ms 1928 KB Output is correct
69 Correct 60 ms 1996 KB Output is correct
70 Correct 57 ms 1972 KB Output is correct
71 Correct 6 ms 724 KB Output is correct
72 Correct 6 ms 724 KB Output is correct
73 Correct 6 ms 648 KB Output is correct
74 Correct 7 ms 724 KB Output is correct
75 Correct 9 ms 652 KB Output is correct
76 Correct 13 ms 1116 KB Output is correct
77 Correct 12 ms 1180 KB Output is correct
78 Correct 13 ms 1176 KB Output is correct
79 Correct 12 ms 1100 KB Output is correct
80 Correct 15 ms 1104 KB Output is correct
81 Correct 22 ms 1536 KB Output is correct
82 Correct 23 ms 1652 KB Output is correct
83 Correct 22 ms 1660 KB Output is correct
84 Correct 23 ms 1616 KB Output is correct
85 Correct 27 ms 1640 KB Output is correct
86 Correct 27 ms 1648 KB Output is correct
87 Correct 29 ms 1608 KB Output is correct
88 Correct 26 ms 1616 KB Output is correct
89 Correct 23 ms 1612 KB Output is correct
90 Correct 23 ms 1612 KB Output is correct
91 Correct 28 ms 2384 KB Output is correct
92 Correct 30 ms 2480 KB Output is correct
93 Correct 29 ms 2332 KB Output is correct
94 Correct 34 ms 2388 KB Output is correct
95 Correct 42 ms 2324 KB Output is correct
96 Correct 32 ms 2700 KB Output is correct
97 Correct 31 ms 2680 KB Output is correct
98 Correct 30 ms 2776 KB Output is correct
99 Correct 30 ms 2764 KB Output is correct
100 Correct 35 ms 2792 KB Output is correct
101 Correct 30 ms 2748 KB Output is correct
102 Correct 31 ms 2708 KB Output is correct
103 Correct 32 ms 2756 KB Output is correct
104 Correct 31 ms 2760 KB Output is correct
105 Correct 33 ms 2780 KB Output is correct
106 Correct 39 ms 2956 KB Output is correct
107 Correct 30 ms 2944 KB Output is correct
108 Correct 31 ms 2964 KB Output is correct
109 Correct 28 ms 2892 KB Output is correct
110 Correct 29 ms 2892 KB Output is correct
111 Correct 36 ms 2928 KB Output is correct
112 Correct 37 ms 2932 KB Output is correct
113 Correct 32 ms 2928 KB Output is correct
114 Correct 29 ms 2976 KB Output is correct
115 Correct 27 ms 2928 KB Output is correct
116 Correct 476 ms 2228 KB Output is correct
117 Correct 469 ms 2228 KB Output is correct
118 Correct 2429 ms 2708 KB Output is correct
119 Correct 1500 ms 2768 KB Output is correct
120 Correct 1076 ms 2772 KB Output is correct
121 Correct 2725 ms 3176 KB Output is correct
122 Correct 1943 ms 3220 KB Output is correct
123 Correct 6743 ms 4124 KB Output is correct
124 Correct 3179 ms 4308 KB Output is correct
125 Correct 2343 ms 4340 KB Output is correct
126 Execution timed out 7033 ms 4144 KB Time limit exceeded
127 Halted 0 ms 0 KB -