Submission #799334

# Submission time Handle Problem Language Result Execution time Memory
799334 2023-07-31T12:48:25 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 19180 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;
vi ans;
vi heatmap;
map<int, int> cnts;

struct query
{
    int l, r, idx;
};

vi split(int x)
{
    vi sp;
    if (x == 1)
        sp.push_back(1);
    else
    {
        sp.push_back(x / 2);
        sp.push_back(x / 2);
        if (x & 1)
            sp.push_back(1);
    }
    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;
}
void add(int x)
{
    if (heatmap[x] > 0)
        --cnts[heatmap[x]];
    ++heatmap[x];
    ++cnts[heatmap[x]];
}
void sub(int x)
{
    --cnts[heatmap[x]];
    --heatmap[x];
    if (heatmap[x])
        ++cnts[heatmap[x]];
}

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

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

    vector<query> Qu(Q);
    ans.resize(Q);
    for (int i = 0; i < Q; ++i)
        cin >> Qu[i].l >> Qu[i].r, Qu[i].idx = i, --Qu[i].l;

    int sq = 1;
    while (sq * sq < N)
        ++sq;
    auto cmp = [&](const query &a, const query &b)
    { if (a.l / sq == b.l / sq) 
        return a.r < b.r;
    else
        return a.l < b.l; };
    sort(all(Qu), cmp);

    int lx = 0, rx = 0;
    heatmap.assign(*max_element(all(A)) + 1, 0);
    for (query q : Qu)
    {
        while (rx < q.r)
            add(A[rx++]);
        while (lx > q.l)
            add(A[--lx]);
        while (rx > q.r)
            sub(A[--rx]);
        while (lx < q.l)
            sub(A[lx++]);

        ll n = q.r - q.l;

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

        int num = sz(S);
        ll left = 0, right = 0;
        ll tmp = totalLengthOfSubsequences(n);

        for (int j = 0; j < num; ++j)
        {

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

            // if both are in set
            tmp -= totalLengthOfSubsequences(len) - x * x * (totalLengthOfSubsequences(y) - y) - y * numberOfSubsequences(x);
            // if one is inside the other isn't
            tmp -= (n - len) * (len * (len + 1) / 2 - x * y * (y + 1) / 2);
            // if both are outside
            tmp -= (len - y) * left * (n - left - len);

            left += len;
            swap(left, right);
        }

        ans[q.idx] = tmp;
    }
    for (ll x : ans)
        cout << x << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 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 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 716 KB Output is correct
4 Correct 21 ms 3696 KB Output is correct
5 Correct 49 ms 10080 KB Output is correct
6 Correct 66 ms 8212 KB Output is correct
7 Correct 87 ms 19180 KB Output is correct
8 Correct 64 ms 7772 KB Output is correct
9 Correct 64 ms 7968 KB Output is correct
10 Correct 78 ms 12212 KB Output is correct
11 Correct 70 ms 10832 KB Output is correct
12 Correct 65 ms 8000 KB Output is correct
13 Correct 67 ms 7808 KB Output is correct
14 Correct 84 ms 11068 KB Output is correct
15 Correct 68 ms 10704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 716 KB Output is correct
4 Correct 21 ms 3696 KB Output is correct
5 Correct 49 ms 10080 KB Output is correct
6 Correct 66 ms 8212 KB Output is correct
7 Correct 87 ms 19180 KB Output is correct
8 Correct 64 ms 7772 KB Output is correct
9 Correct 64 ms 7968 KB Output is correct
10 Correct 78 ms 12212 KB Output is correct
11 Correct 70 ms 10832 KB Output is correct
12 Correct 65 ms 8000 KB Output is correct
13 Correct 67 ms 7808 KB Output is correct
14 Correct 84 ms 11068 KB Output is correct
15 Correct 68 ms 10704 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 20 ms 3388 KB Output is correct
20 Correct 40 ms 4904 KB Output is correct
21 Correct 68 ms 8428 KB Output is correct
22 Correct 62 ms 7448 KB Output is correct
23 Correct 63 ms 8044 KB Output is correct
24 Correct 62 ms 7228 KB Output is correct
25 Correct 63 ms 7636 KB Output is correct
26 Correct 61 ms 7396 KB Output is correct
27 Correct 63 ms 7732 KB Output is correct
28 Correct 71 ms 11308 KB Output is correct
29 Correct 63 ms 7472 KB Output is correct
30 Correct 61 ms 7724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 716 KB Output is correct
4 Correct 21 ms 3696 KB Output is correct
5 Correct 49 ms 10080 KB Output is correct
6 Correct 66 ms 8212 KB Output is correct
7 Correct 87 ms 19180 KB Output is correct
8 Correct 64 ms 7772 KB Output is correct
9 Correct 64 ms 7968 KB Output is correct
10 Correct 78 ms 12212 KB Output is correct
11 Correct 70 ms 10832 KB Output is correct
12 Correct 65 ms 8000 KB Output is correct
13 Correct 67 ms 7808 KB Output is correct
14 Correct 84 ms 11068 KB Output is correct
15 Correct 68 ms 10704 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 20 ms 3388 KB Output is correct
20 Correct 40 ms 4904 KB Output is correct
21 Correct 68 ms 8428 KB Output is correct
22 Correct 62 ms 7448 KB Output is correct
23 Correct 63 ms 8044 KB Output is correct
24 Correct 62 ms 7228 KB Output is correct
25 Correct 63 ms 7636 KB Output is correct
26 Correct 61 ms 7396 KB Output is correct
27 Correct 63 ms 7732 KB Output is correct
28 Correct 71 ms 11308 KB Output is correct
29 Correct 63 ms 7472 KB Output is correct
30 Correct 61 ms 7724 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 468 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 7 ms 980 KB Output is correct
38 Correct 6 ms 852 KB Output is correct
39 Correct 13 ms 1524 KB Output is correct
40 Correct 27 ms 2836 KB Output is correct
41 Correct 42 ms 3956 KB Output is correct
42 Correct 43 ms 4064 KB Output is correct
43 Correct 44 ms 4068 KB Output is correct
44 Correct 45 ms 4108 KB Output is correct
45 Correct 43 ms 3956 KB Output is correct
46 Correct 44 ms 3916 KB Output is correct
47 Correct 44 ms 4004 KB Output is correct
48 Correct 43 ms 4060 KB Output is correct
49 Correct 43 ms 3992 KB Output is correct
50 Correct 43 ms 3916 KB Output is correct
51 Correct 42 ms 4044 KB Output is correct
52 Correct 43 ms 4092 KB Output is correct
53 Correct 46 ms 3992 KB Output is correct
54 Correct 43 ms 4036 KB Output is correct
55 Correct 42 ms 3916 KB Output is correct
56 Correct 42 ms 4044 KB Output is correct
57 Correct 42 ms 4008 KB Output is correct
58 Correct 42 ms 3996 KB Output is correct
59 Correct 43 ms 4076 KB Output is correct
60 Correct 43 ms 4004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 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 320 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 716 KB Output is correct
14 Correct 21 ms 3696 KB Output is correct
15 Correct 49 ms 10080 KB Output is correct
16 Correct 66 ms 8212 KB Output is correct
17 Correct 87 ms 19180 KB Output is correct
18 Correct 64 ms 7772 KB Output is correct
19 Correct 64 ms 7968 KB Output is correct
20 Correct 78 ms 12212 KB Output is correct
21 Correct 70 ms 10832 KB Output is correct
22 Correct 65 ms 8000 KB Output is correct
23 Correct 67 ms 7808 KB Output is correct
24 Correct 84 ms 11068 KB Output is correct
25 Correct 68 ms 10704 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 20 ms 3388 KB Output is correct
30 Correct 40 ms 4904 KB Output is correct
31 Correct 68 ms 8428 KB Output is correct
32 Correct 62 ms 7448 KB Output is correct
33 Correct 63 ms 8044 KB Output is correct
34 Correct 62 ms 7228 KB Output is correct
35 Correct 63 ms 7636 KB Output is correct
36 Correct 61 ms 7396 KB Output is correct
37 Correct 63 ms 7732 KB Output is correct
38 Correct 71 ms 11308 KB Output is correct
39 Correct 63 ms 7472 KB Output is correct
40 Correct 61 ms 7724 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 468 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 7 ms 980 KB Output is correct
48 Correct 6 ms 852 KB Output is correct
49 Correct 13 ms 1524 KB Output is correct
50 Correct 27 ms 2836 KB Output is correct
51 Correct 42 ms 3956 KB Output is correct
52 Correct 43 ms 4064 KB Output is correct
53 Correct 44 ms 4068 KB Output is correct
54 Correct 45 ms 4108 KB Output is correct
55 Correct 43 ms 3956 KB Output is correct
56 Correct 44 ms 3916 KB Output is correct
57 Correct 44 ms 4004 KB Output is correct
58 Correct 43 ms 4060 KB Output is correct
59 Correct 43 ms 3992 KB Output is correct
60 Correct 43 ms 3916 KB Output is correct
61 Correct 42 ms 4044 KB Output is correct
62 Correct 43 ms 4092 KB Output is correct
63 Correct 46 ms 3992 KB Output is correct
64 Correct 43 ms 4036 KB Output is correct
65 Correct 42 ms 3916 KB Output is correct
66 Correct 42 ms 4044 KB Output is correct
67 Correct 42 ms 4008 KB Output is correct
68 Correct 42 ms 3996 KB Output is correct
69 Correct 43 ms 4076 KB Output is correct
70 Correct 43 ms 4004 KB Output is correct
71 Correct 6 ms 940 KB Output is correct
72 Correct 6 ms 944 KB Output is correct
73 Correct 6 ms 948 KB Output is correct
74 Correct 6 ms 968 KB Output is correct
75 Correct 6 ms 980 KB Output is correct
76 Correct 10 ms 1620 KB Output is correct
77 Correct 10 ms 1608 KB Output is correct
78 Correct 10 ms 1620 KB Output is correct
79 Correct 10 ms 1612 KB Output is correct
80 Correct 10 ms 1632 KB Output is correct
81 Correct 20 ms 2896 KB Output is correct
82 Correct 19 ms 2908 KB Output is correct
83 Correct 20 ms 2932 KB Output is correct
84 Correct 19 ms 2940 KB Output is correct
85 Correct 20 ms 2900 KB Output is correct
86 Correct 19 ms 3040 KB Output is correct
87 Correct 19 ms 3116 KB Output is correct
88 Correct 19 ms 3020 KB Output is correct
89 Correct 19 ms 3124 KB Output is correct
90 Correct 19 ms 3032 KB Output is correct
91 Correct 25 ms 5092 KB Output is correct
92 Correct 25 ms 5176 KB Output is correct
93 Correct 24 ms 5156 KB Output is correct
94 Correct 25 ms 5088 KB Output is correct
95 Correct 25 ms 5152 KB Output is correct
96 Correct 25 ms 6076 KB Output is correct
97 Correct 26 ms 6108 KB Output is correct
98 Correct 26 ms 6096 KB Output is correct
99 Correct 26 ms 6108 KB Output is correct
100 Correct 26 ms 6104 KB Output is correct
101 Correct 25 ms 6092 KB Output is correct
102 Correct 25 ms 6120 KB Output is correct
103 Correct 28 ms 6100 KB Output is correct
104 Correct 25 ms 6112 KB Output is correct
105 Correct 26 ms 6092 KB Output is correct
106 Correct 25 ms 6516 KB Output is correct
107 Correct 24 ms 6544 KB Output is correct
108 Correct 23 ms 6504 KB Output is correct
109 Correct 24 ms 6500 KB Output is correct
110 Correct 24 ms 6476 KB Output is correct
111 Correct 24 ms 6500 KB Output is correct
112 Correct 23 ms 6496 KB Output is correct
113 Correct 24 ms 6500 KB Output is correct
114 Correct 24 ms 6476 KB Output is correct
115 Correct 25 ms 6456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 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 320 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 716 KB Output is correct
14 Correct 21 ms 3696 KB Output is correct
15 Correct 49 ms 10080 KB Output is correct
16 Correct 66 ms 8212 KB Output is correct
17 Correct 87 ms 19180 KB Output is correct
18 Correct 64 ms 7772 KB Output is correct
19 Correct 64 ms 7968 KB Output is correct
20 Correct 78 ms 12212 KB Output is correct
21 Correct 70 ms 10832 KB Output is correct
22 Correct 65 ms 8000 KB Output is correct
23 Correct 67 ms 7808 KB Output is correct
24 Correct 84 ms 11068 KB Output is correct
25 Correct 68 ms 10704 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 20 ms 3388 KB Output is correct
30 Correct 40 ms 4904 KB Output is correct
31 Correct 68 ms 8428 KB Output is correct
32 Correct 62 ms 7448 KB Output is correct
33 Correct 63 ms 8044 KB Output is correct
34 Correct 62 ms 7228 KB Output is correct
35 Correct 63 ms 7636 KB Output is correct
36 Correct 61 ms 7396 KB Output is correct
37 Correct 63 ms 7732 KB Output is correct
38 Correct 71 ms 11308 KB Output is correct
39 Correct 63 ms 7472 KB Output is correct
40 Correct 61 ms 7724 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 468 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 7 ms 980 KB Output is correct
48 Correct 6 ms 852 KB Output is correct
49 Correct 13 ms 1524 KB Output is correct
50 Correct 27 ms 2836 KB Output is correct
51 Correct 42 ms 3956 KB Output is correct
52 Correct 43 ms 4064 KB Output is correct
53 Correct 44 ms 4068 KB Output is correct
54 Correct 45 ms 4108 KB Output is correct
55 Correct 43 ms 3956 KB Output is correct
56 Correct 44 ms 3916 KB Output is correct
57 Correct 44 ms 4004 KB Output is correct
58 Correct 43 ms 4060 KB Output is correct
59 Correct 43 ms 3992 KB Output is correct
60 Correct 43 ms 3916 KB Output is correct
61 Correct 42 ms 4044 KB Output is correct
62 Correct 43 ms 4092 KB Output is correct
63 Correct 46 ms 3992 KB Output is correct
64 Correct 43 ms 4036 KB Output is correct
65 Correct 42 ms 3916 KB Output is correct
66 Correct 42 ms 4044 KB Output is correct
67 Correct 42 ms 4008 KB Output is correct
68 Correct 42 ms 3996 KB Output is correct
69 Correct 43 ms 4076 KB Output is correct
70 Correct 43 ms 4004 KB Output is correct
71 Correct 6 ms 940 KB Output is correct
72 Correct 6 ms 944 KB Output is correct
73 Correct 6 ms 948 KB Output is correct
74 Correct 6 ms 968 KB Output is correct
75 Correct 6 ms 980 KB Output is correct
76 Correct 10 ms 1620 KB Output is correct
77 Correct 10 ms 1608 KB Output is correct
78 Correct 10 ms 1620 KB Output is correct
79 Correct 10 ms 1612 KB Output is correct
80 Correct 10 ms 1632 KB Output is correct
81 Correct 20 ms 2896 KB Output is correct
82 Correct 19 ms 2908 KB Output is correct
83 Correct 20 ms 2932 KB Output is correct
84 Correct 19 ms 2940 KB Output is correct
85 Correct 20 ms 2900 KB Output is correct
86 Correct 19 ms 3040 KB Output is correct
87 Correct 19 ms 3116 KB Output is correct
88 Correct 19 ms 3020 KB Output is correct
89 Correct 19 ms 3124 KB Output is correct
90 Correct 19 ms 3032 KB Output is correct
91 Correct 25 ms 5092 KB Output is correct
92 Correct 25 ms 5176 KB Output is correct
93 Correct 24 ms 5156 KB Output is correct
94 Correct 25 ms 5088 KB Output is correct
95 Correct 25 ms 5152 KB Output is correct
96 Correct 25 ms 6076 KB Output is correct
97 Correct 26 ms 6108 KB Output is correct
98 Correct 26 ms 6096 KB Output is correct
99 Correct 26 ms 6108 KB Output is correct
100 Correct 26 ms 6104 KB Output is correct
101 Correct 25 ms 6092 KB Output is correct
102 Correct 25 ms 6120 KB Output is correct
103 Correct 28 ms 6100 KB Output is correct
104 Correct 25 ms 6112 KB Output is correct
105 Correct 26 ms 6092 KB Output is correct
106 Correct 25 ms 6516 KB Output is correct
107 Correct 24 ms 6544 KB Output is correct
108 Correct 23 ms 6504 KB Output is correct
109 Correct 24 ms 6500 KB Output is correct
110 Correct 24 ms 6476 KB Output is correct
111 Correct 24 ms 6500 KB Output is correct
112 Correct 23 ms 6496 KB Output is correct
113 Correct 24 ms 6500 KB Output is correct
114 Correct 24 ms 6476 KB Output is correct
115 Correct 25 ms 6456 KB Output is correct
116 Correct 3188 ms 2364 KB Output is correct
117 Correct 1928 ms 2372 KB Output is correct
118 Execution timed out 7041 ms 2820 KB Time limit exceeded
119 Halted 0 ms 0 KB -