Submission #798908

# Submission time Handle Problem Language Result Execution time Memory
798908 2023-07-31T06:52:12 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 13644 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const ll INF = 1e18;
int N, Q;
vi A, AA;
ll ans;

vi split(int x)
{
    vi sp;
    int j = 0;
    while (2 * (1 << j) <= x)
    {
        sp.push_back(1 << j);
        x -= 1 << j;
        sp.push_back(1 << j);
        x -= 1 << j;
        ++j;
    }
    j = 0;
    while (x > 0)
    {
        if (x & (1 << j))
            sp.push_back(1 << j), x ^= 1 << j;
        ++j;
    }
    return sp;
}

ll numberOfSubsequences(ll n)
{
    return n * (n + 1) / 2;
}
ll totalLengthOfSubsequences(ll n)
{
    return n * (n + 1) * (n + 1) / 2 - (2 * n + 1) * (n + 1) * n / 6;
}

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

    cin >> N >> Q;
    AA.resize(N);
    for (int i = 0; i < N; ++i)
        cin >> AA[i];

    while (Q--)
    {
        {
            int l, r;
            cin >> l >> r;
            --l;
            A = vi(AA.begin() + l, AA.begin() + r);
        }
        ll n = sz(A);

        vi heatMap(*max_element(all(A)) + 1, 0);
        for (int i = 0; i < sz(A); ++i)
            ++heatMap[A[i]];
        map<int, int> cnts;
        for (int i = 0; i < sz(heatMap); ++i)
            if (heatMap[i])
                ++cnts[heatMap[i]];

        vpii S;
        for (auto it = cnts.begin(); it != cnts.end(); ++it)
            for (int x : split(it->second))
                S.push_back({it->first, x});

        ll pref = 0;
        int num = sz(S);

        vi dp[2];
        for (int foo = 0; foo < 2; ++foo)
            dp[foo].reserve(N + 1), dp[foo].resize(N + 1);
        fill(all(dp[0]), INF);
        dp[0][0] = totalLengthOfSubsequences(n);

        for (int j = 0; j < num; ++j)
        {
            int ii = j & 1;
            int ni = ii ^ 1;
            fill(all(dp[ni]), INF);

            ll x = S[j].first;  // how much
            ll y = S[j].second; // how often
            ll len = x * y;

            ll subsure = 0;
            // if both are in set
            subsure += totalLengthOfSubsequences(len) - x * x * (totalLengthOfSubsequences(y) - y) - y * numberOfSubsequences(x);

            // either with left or right endpoint in set
            ll factor = len * (len + 1) / 2 - x * y * (y + 1) / 2;
            subsure += factor * (n - len);

            for (ll left = 0; left < min(pref + 1, (ll)sz(dp[ii])); ++left)
            {
                ll tmp = dp[ii][left];
                if (tmp == INF)
                    continue;
                tmp -= subsure;

                // left
                dp[ni][left + len] = min(dp[ni][left + len], tmp - (len - y) * left * (n - left - len));
                // right
                ll right = pref - left;
                dp[ni][left] = min(dp[ni][left], tmp - (len - y) * right * (n - right - len));
            }
            pref += len;
        }
        ans = *min_element(all(dp[num & 1]));
        cout << ans << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 548 KB Output is correct
4 Correct 6 ms 3660 KB Output is correct
5 Correct 11 ms 6868 KB Output is correct
6 Correct 19 ms 10344 KB Output is correct
7 Correct 17 ms 10324 KB Output is correct
8 Correct 17 ms 10248 KB Output is correct
9 Correct 17 ms 10344 KB Output is correct
10 Correct 18 ms 10388 KB Output is correct
11 Correct 18 ms 10324 KB Output is correct
12 Correct 18 ms 10324 KB Output is correct
13 Correct 18 ms 10324 KB Output is correct
14 Correct 17 ms 10352 KB Output is correct
15 Correct 17 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 548 KB Output is correct
4 Correct 6 ms 3660 KB Output is correct
5 Correct 11 ms 6868 KB Output is correct
6 Correct 19 ms 10344 KB Output is correct
7 Correct 17 ms 10324 KB Output is correct
8 Correct 17 ms 10248 KB Output is correct
9 Correct 17 ms 10344 KB Output is correct
10 Correct 18 ms 10388 KB Output is correct
11 Correct 18 ms 10324 KB Output is correct
12 Correct 18 ms 10324 KB Output is correct
13 Correct 18 ms 10324 KB Output is correct
14 Correct 17 ms 10352 KB Output is correct
15 Correct 17 ms 10452 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 7 ms 3668 KB Output is correct
20 Correct 15 ms 7008 KB Output is correct
21 Correct 22 ms 10452 KB Output is correct
22 Correct 22 ms 10468 KB Output is correct
23 Correct 28 ms 10504 KB Output is correct
24 Correct 23 ms 10476 KB Output is correct
25 Correct 23 ms 10472 KB Output is correct
26 Correct 24 ms 10484 KB Output is correct
27 Correct 23 ms 10472 KB Output is correct
28 Correct 22 ms 10312 KB Output is correct
29 Correct 23 ms 10472 KB Output is correct
30 Correct 23 ms 10384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 548 KB Output is correct
4 Correct 6 ms 3660 KB Output is correct
5 Correct 11 ms 6868 KB Output is correct
6 Correct 19 ms 10344 KB Output is correct
7 Correct 17 ms 10324 KB Output is correct
8 Correct 17 ms 10248 KB Output is correct
9 Correct 17 ms 10344 KB Output is correct
10 Correct 18 ms 10388 KB Output is correct
11 Correct 18 ms 10324 KB Output is correct
12 Correct 18 ms 10324 KB Output is correct
13 Correct 18 ms 10324 KB Output is correct
14 Correct 17 ms 10352 KB Output is correct
15 Correct 17 ms 10452 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 7 ms 3668 KB Output is correct
20 Correct 15 ms 7008 KB Output is correct
21 Correct 22 ms 10452 KB Output is correct
22 Correct 22 ms 10468 KB Output is correct
23 Correct 28 ms 10504 KB Output is correct
24 Correct 23 ms 10476 KB Output is correct
25 Correct 23 ms 10472 KB Output is correct
26 Correct 24 ms 10484 KB Output is correct
27 Correct 23 ms 10472 KB Output is correct
28 Correct 22 ms 10312 KB Output is correct
29 Correct 23 ms 10472 KB Output is correct
30 Correct 23 ms 10384 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 4 ms 596 KB Output is correct
37 Correct 21 ms 2004 KB Output is correct
38 Correct 33 ms 2136 KB Output is correct
39 Correct 75 ms 3884 KB Output is correct
40 Correct 161 ms 7400 KB Output is correct
41 Correct 252 ms 10912 KB Output is correct
42 Correct 255 ms 10828 KB Output is correct
43 Correct 243 ms 10856 KB Output is correct
44 Correct 248 ms 10856 KB Output is correct
45 Correct 267 ms 10848 KB Output is correct
46 Correct 251 ms 10924 KB Output is correct
47 Correct 258 ms 10912 KB Output is correct
48 Correct 247 ms 10836 KB Output is correct
49 Correct 255 ms 10908 KB Output is correct
50 Correct 247 ms 10912 KB Output is correct
51 Correct 251 ms 10916 KB Output is correct
52 Correct 251 ms 10920 KB Output is correct
53 Correct 249 ms 10916 KB Output is correct
54 Correct 246 ms 10912 KB Output is correct
55 Correct 249 ms 10916 KB Output is correct
56 Correct 260 ms 10916 KB Output is correct
57 Correct 252 ms 10828 KB Output is correct
58 Correct 250 ms 10852 KB Output is correct
59 Correct 256 ms 10928 KB Output is correct
60 Correct 252 ms 10904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 548 KB Output is correct
14 Correct 6 ms 3660 KB Output is correct
15 Correct 11 ms 6868 KB Output is correct
16 Correct 19 ms 10344 KB Output is correct
17 Correct 17 ms 10324 KB Output is correct
18 Correct 17 ms 10248 KB Output is correct
19 Correct 17 ms 10344 KB Output is correct
20 Correct 18 ms 10388 KB Output is correct
21 Correct 18 ms 10324 KB Output is correct
22 Correct 18 ms 10324 KB Output is correct
23 Correct 18 ms 10324 KB Output is correct
24 Correct 17 ms 10352 KB Output is correct
25 Correct 17 ms 10452 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 7 ms 3668 KB Output is correct
30 Correct 15 ms 7008 KB Output is correct
31 Correct 22 ms 10452 KB Output is correct
32 Correct 22 ms 10468 KB Output is correct
33 Correct 28 ms 10504 KB Output is correct
34 Correct 23 ms 10476 KB Output is correct
35 Correct 23 ms 10472 KB Output is correct
36 Correct 24 ms 10484 KB Output is correct
37 Correct 23 ms 10472 KB Output is correct
38 Correct 22 ms 10312 KB Output is correct
39 Correct 23 ms 10472 KB Output is correct
40 Correct 23 ms 10384 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 596 KB Output is correct
45 Correct 3 ms 596 KB Output is correct
46 Correct 4 ms 596 KB Output is correct
47 Correct 21 ms 2004 KB Output is correct
48 Correct 33 ms 2136 KB Output is correct
49 Correct 75 ms 3884 KB Output is correct
50 Correct 161 ms 7400 KB Output is correct
51 Correct 252 ms 10912 KB Output is correct
52 Correct 255 ms 10828 KB Output is correct
53 Correct 243 ms 10856 KB Output is correct
54 Correct 248 ms 10856 KB Output is correct
55 Correct 267 ms 10848 KB Output is correct
56 Correct 251 ms 10924 KB Output is correct
57 Correct 258 ms 10912 KB Output is correct
58 Correct 247 ms 10836 KB Output is correct
59 Correct 255 ms 10908 KB Output is correct
60 Correct 247 ms 10912 KB Output is correct
61 Correct 251 ms 10916 KB Output is correct
62 Correct 251 ms 10920 KB Output is correct
63 Correct 249 ms 10916 KB Output is correct
64 Correct 246 ms 10912 KB Output is correct
65 Correct 249 ms 10916 KB Output is correct
66 Correct 260 ms 10916 KB Output is correct
67 Correct 252 ms 10828 KB Output is correct
68 Correct 250 ms 10852 KB Output is correct
69 Correct 256 ms 10928 KB Output is correct
70 Correct 252 ms 10904 KB Output is correct
71 Correct 39 ms 2132 KB Output is correct
72 Correct 39 ms 2132 KB Output is correct
73 Correct 39 ms 2092 KB Output is correct
74 Correct 42 ms 2164 KB Output is correct
75 Correct 39 ms 2132 KB Output is correct
76 Correct 110 ms 3924 KB Output is correct
77 Correct 109 ms 4004 KB Output is correct
78 Correct 110 ms 3924 KB Output is correct
79 Correct 117 ms 3992 KB Output is correct
80 Correct 108 ms 3916 KB Output is correct
81 Correct 286 ms 7676 KB Output is correct
82 Correct 298 ms 7672 KB Output is correct
83 Correct 288 ms 7652 KB Output is correct
84 Correct 294 ms 7636 KB Output is correct
85 Correct 298 ms 7636 KB Output is correct
86 Correct 245 ms 7824 KB Output is correct
87 Correct 246 ms 7844 KB Output is correct
88 Correct 241 ms 7960 KB Output is correct
89 Correct 245 ms 7840 KB Output is correct
90 Correct 243 ms 7844 KB Output is correct
91 Correct 239 ms 12240 KB Output is correct
92 Correct 231 ms 12244 KB Output is correct
93 Correct 246 ms 12236 KB Output is correct
94 Correct 224 ms 12236 KB Output is correct
95 Correct 232 ms 12240 KB Output is correct
96 Correct 137 ms 13172 KB Output is correct
97 Correct 138 ms 13172 KB Output is correct
98 Correct 139 ms 13160 KB Output is correct
99 Correct 137 ms 13168 KB Output is correct
100 Correct 137 ms 13152 KB Output is correct
101 Correct 148 ms 13260 KB Output is correct
102 Correct 140 ms 13132 KB Output is correct
103 Correct 137 ms 13180 KB Output is correct
104 Correct 139 ms 13132 KB Output is correct
105 Correct 145 ms 13264 KB Output is correct
106 Correct 107 ms 13536 KB Output is correct
107 Correct 101 ms 13596 KB Output is correct
108 Correct 106 ms 13600 KB Output is correct
109 Correct 101 ms 13544 KB Output is correct
110 Correct 109 ms 13644 KB Output is correct
111 Correct 105 ms 13524 KB Output is correct
112 Correct 97 ms 13524 KB Output is correct
113 Correct 99 ms 13532 KB Output is correct
114 Correct 105 ms 13516 KB Output is correct
115 Correct 103 ms 13532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 548 KB Output is correct
14 Correct 6 ms 3660 KB Output is correct
15 Correct 11 ms 6868 KB Output is correct
16 Correct 19 ms 10344 KB Output is correct
17 Correct 17 ms 10324 KB Output is correct
18 Correct 17 ms 10248 KB Output is correct
19 Correct 17 ms 10344 KB Output is correct
20 Correct 18 ms 10388 KB Output is correct
21 Correct 18 ms 10324 KB Output is correct
22 Correct 18 ms 10324 KB Output is correct
23 Correct 18 ms 10324 KB Output is correct
24 Correct 17 ms 10352 KB Output is correct
25 Correct 17 ms 10452 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 7 ms 3668 KB Output is correct
30 Correct 15 ms 7008 KB Output is correct
31 Correct 22 ms 10452 KB Output is correct
32 Correct 22 ms 10468 KB Output is correct
33 Correct 28 ms 10504 KB Output is correct
34 Correct 23 ms 10476 KB Output is correct
35 Correct 23 ms 10472 KB Output is correct
36 Correct 24 ms 10484 KB Output is correct
37 Correct 23 ms 10472 KB Output is correct
38 Correct 22 ms 10312 KB Output is correct
39 Correct 23 ms 10472 KB Output is correct
40 Correct 23 ms 10384 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 596 KB Output is correct
45 Correct 3 ms 596 KB Output is correct
46 Correct 4 ms 596 KB Output is correct
47 Correct 21 ms 2004 KB Output is correct
48 Correct 33 ms 2136 KB Output is correct
49 Correct 75 ms 3884 KB Output is correct
50 Correct 161 ms 7400 KB Output is correct
51 Correct 252 ms 10912 KB Output is correct
52 Correct 255 ms 10828 KB Output is correct
53 Correct 243 ms 10856 KB Output is correct
54 Correct 248 ms 10856 KB Output is correct
55 Correct 267 ms 10848 KB Output is correct
56 Correct 251 ms 10924 KB Output is correct
57 Correct 258 ms 10912 KB Output is correct
58 Correct 247 ms 10836 KB Output is correct
59 Correct 255 ms 10908 KB Output is correct
60 Correct 247 ms 10912 KB Output is correct
61 Correct 251 ms 10916 KB Output is correct
62 Correct 251 ms 10920 KB Output is correct
63 Correct 249 ms 10916 KB Output is correct
64 Correct 246 ms 10912 KB Output is correct
65 Correct 249 ms 10916 KB Output is correct
66 Correct 260 ms 10916 KB Output is correct
67 Correct 252 ms 10828 KB Output is correct
68 Correct 250 ms 10852 KB Output is correct
69 Correct 256 ms 10928 KB Output is correct
70 Correct 252 ms 10904 KB Output is correct
71 Correct 39 ms 2132 KB Output is correct
72 Correct 39 ms 2132 KB Output is correct
73 Correct 39 ms 2092 KB Output is correct
74 Correct 42 ms 2164 KB Output is correct
75 Correct 39 ms 2132 KB Output is correct
76 Correct 110 ms 3924 KB Output is correct
77 Correct 109 ms 4004 KB Output is correct
78 Correct 110 ms 3924 KB Output is correct
79 Correct 117 ms 3992 KB Output is correct
80 Correct 108 ms 3916 KB Output is correct
81 Correct 286 ms 7676 KB Output is correct
82 Correct 298 ms 7672 KB Output is correct
83 Correct 288 ms 7652 KB Output is correct
84 Correct 294 ms 7636 KB Output is correct
85 Correct 298 ms 7636 KB Output is correct
86 Correct 245 ms 7824 KB Output is correct
87 Correct 246 ms 7844 KB Output is correct
88 Correct 241 ms 7960 KB Output is correct
89 Correct 245 ms 7840 KB Output is correct
90 Correct 243 ms 7844 KB Output is correct
91 Correct 239 ms 12240 KB Output is correct
92 Correct 231 ms 12244 KB Output is correct
93 Correct 246 ms 12236 KB Output is correct
94 Correct 224 ms 12236 KB Output is correct
95 Correct 232 ms 12240 KB Output is correct
96 Correct 137 ms 13172 KB Output is correct
97 Correct 138 ms 13172 KB Output is correct
98 Correct 139 ms 13160 KB Output is correct
99 Correct 137 ms 13168 KB Output is correct
100 Correct 137 ms 13152 KB Output is correct
101 Correct 148 ms 13260 KB Output is correct
102 Correct 140 ms 13132 KB Output is correct
103 Correct 137 ms 13180 KB Output is correct
104 Correct 139 ms 13132 KB Output is correct
105 Correct 145 ms 13264 KB Output is correct
106 Correct 107 ms 13536 KB Output is correct
107 Correct 101 ms 13596 KB Output is correct
108 Correct 106 ms 13600 KB Output is correct
109 Correct 101 ms 13544 KB Output is correct
110 Correct 109 ms 13644 KB Output is correct
111 Correct 105 ms 13524 KB Output is correct
112 Correct 97 ms 13524 KB Output is correct
113 Correct 99 ms 13532 KB Output is correct
114 Correct 105 ms 13516 KB Output is correct
115 Correct 103 ms 13532 KB Output is correct
116 Execution timed out 7003 ms 1484 KB Time limit exceeded
117 Halted 0 ms 0 KB -