Submission #799337

# Submission time Handle Problem Language Result Execution time Memory
799337 2023-07-31T12:50:10 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 4732 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]];
        if (cnts[heatmap[x]] == 0)
            cnts.erase(heatmap[x]);
    }
    ++heatmap[x];
    ++cnts[heatmap[x]];
}
void sub(int x)
{
    --cnts[heatmap[x]];
    if (cnts[heatmap[x]] == 0)
        cnts.erase(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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 15 ms 1236 KB Output is correct
5 Correct 30 ms 2004 KB Output is correct
6 Correct 45 ms 2788 KB Output is correct
7 Correct 35 ms 2772 KB Output is correct
8 Correct 46 ms 2704 KB Output is correct
9 Correct 38 ms 2772 KB Output is correct
10 Correct 38 ms 2760 KB Output is correct
11 Correct 38 ms 2772 KB Output is correct
12 Correct 46 ms 2772 KB Output is correct
13 Correct 47 ms 2772 KB Output is correct
14 Correct 45 ms 2772 KB Output is correct
15 Correct 46 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 15 ms 1236 KB Output is correct
5 Correct 30 ms 2004 KB Output is correct
6 Correct 45 ms 2788 KB Output is correct
7 Correct 35 ms 2772 KB Output is correct
8 Correct 46 ms 2704 KB Output is correct
9 Correct 38 ms 2772 KB Output is correct
10 Correct 38 ms 2760 KB Output is correct
11 Correct 38 ms 2772 KB Output is correct
12 Correct 46 ms 2772 KB Output is correct
13 Correct 47 ms 2772 KB Output is correct
14 Correct 45 ms 2772 KB Output is correct
15 Correct 46 ms 2772 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 15 ms 1248 KB Output is correct
20 Correct 37 ms 2004 KB Output is correct
21 Correct 45 ms 2772 KB Output is correct
22 Correct 60 ms 2772 KB Output is correct
23 Correct 53 ms 2808 KB Output is correct
24 Correct 46 ms 2772 KB Output is correct
25 Correct 48 ms 2784 KB Output is correct
26 Correct 46 ms 2772 KB Output is correct
27 Correct 48 ms 2788 KB Output is correct
28 Correct 44 ms 2772 KB Output is correct
29 Correct 48 ms 2784 KB Output is correct
30 Correct 47 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 15 ms 1236 KB Output is correct
5 Correct 30 ms 2004 KB Output is correct
6 Correct 45 ms 2788 KB Output is correct
7 Correct 35 ms 2772 KB Output is correct
8 Correct 46 ms 2704 KB Output is correct
9 Correct 38 ms 2772 KB Output is correct
10 Correct 38 ms 2760 KB Output is correct
11 Correct 38 ms 2772 KB Output is correct
12 Correct 46 ms 2772 KB Output is correct
13 Correct 47 ms 2772 KB Output is correct
14 Correct 45 ms 2772 KB Output is correct
15 Correct 46 ms 2772 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 15 ms 1248 KB Output is correct
20 Correct 37 ms 2004 KB Output is correct
21 Correct 45 ms 2772 KB Output is correct
22 Correct 60 ms 2772 KB Output is correct
23 Correct 53 ms 2808 KB Output is correct
24 Correct 46 ms 2772 KB Output is correct
25 Correct 48 ms 2784 KB Output is correct
26 Correct 46 ms 2772 KB Output is correct
27 Correct 48 ms 2788 KB Output is correct
28 Correct 44 ms 2772 KB Output is correct
29 Correct 48 ms 2784 KB Output is correct
30 Correct 47 ms 2764 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 11 ms 852 KB Output is correct
38 Correct 8 ms 868 KB Output is correct
39 Correct 20 ms 1260 KB Output is correct
40 Correct 38 ms 2044 KB Output is correct
41 Correct 63 ms 2824 KB Output is correct
42 Correct 61 ms 2816 KB Output is correct
43 Correct 63 ms 2816 KB Output is correct
44 Correct 64 ms 2832 KB Output is correct
45 Correct 69 ms 2800 KB Output is correct
46 Correct 61 ms 2820 KB Output is correct
47 Correct 61 ms 2820 KB Output is correct
48 Correct 62 ms 2812 KB Output is correct
49 Correct 60 ms 2824 KB Output is correct
50 Correct 68 ms 2932 KB Output is correct
51 Correct 62 ms 2816 KB Output is correct
52 Correct 69 ms 2820 KB Output is correct
53 Correct 62 ms 2808 KB Output is correct
54 Correct 61 ms 2812 KB Output is correct
55 Correct 61 ms 2804 KB Output is correct
56 Correct 69 ms 2816 KB Output is correct
57 Correct 61 ms 2816 KB Output is correct
58 Correct 70 ms 2788 KB Output is correct
59 Correct 61 ms 2820 KB Output is correct
60 Correct 61 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 15 ms 1236 KB Output is correct
15 Correct 30 ms 2004 KB Output is correct
16 Correct 45 ms 2788 KB Output is correct
17 Correct 35 ms 2772 KB Output is correct
18 Correct 46 ms 2704 KB Output is correct
19 Correct 38 ms 2772 KB Output is correct
20 Correct 38 ms 2760 KB Output is correct
21 Correct 38 ms 2772 KB Output is correct
22 Correct 46 ms 2772 KB Output is correct
23 Correct 47 ms 2772 KB Output is correct
24 Correct 45 ms 2772 KB Output is correct
25 Correct 46 ms 2772 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 15 ms 1248 KB Output is correct
30 Correct 37 ms 2004 KB Output is correct
31 Correct 45 ms 2772 KB Output is correct
32 Correct 60 ms 2772 KB Output is correct
33 Correct 53 ms 2808 KB Output is correct
34 Correct 46 ms 2772 KB Output is correct
35 Correct 48 ms 2784 KB Output is correct
36 Correct 46 ms 2772 KB Output is correct
37 Correct 48 ms 2788 KB Output is correct
38 Correct 44 ms 2772 KB Output is correct
39 Correct 48 ms 2784 KB Output is correct
40 Correct 47 ms 2764 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 2 ms 332 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 11 ms 852 KB Output is correct
48 Correct 8 ms 868 KB Output is correct
49 Correct 20 ms 1260 KB Output is correct
50 Correct 38 ms 2044 KB Output is correct
51 Correct 63 ms 2824 KB Output is correct
52 Correct 61 ms 2816 KB Output is correct
53 Correct 63 ms 2816 KB Output is correct
54 Correct 64 ms 2832 KB Output is correct
55 Correct 69 ms 2800 KB Output is correct
56 Correct 61 ms 2820 KB Output is correct
57 Correct 61 ms 2820 KB Output is correct
58 Correct 62 ms 2812 KB Output is correct
59 Correct 60 ms 2824 KB Output is correct
60 Correct 68 ms 2932 KB Output is correct
61 Correct 62 ms 2816 KB Output is correct
62 Correct 69 ms 2820 KB Output is correct
63 Correct 62 ms 2808 KB Output is correct
64 Correct 61 ms 2812 KB Output is correct
65 Correct 61 ms 2804 KB Output is correct
66 Correct 69 ms 2816 KB Output is correct
67 Correct 61 ms 2816 KB Output is correct
68 Correct 70 ms 2788 KB Output is correct
69 Correct 61 ms 2820 KB Output is correct
70 Correct 61 ms 2816 KB Output is correct
71 Correct 6 ms 852 KB Output is correct
72 Correct 6 ms 852 KB Output is correct
73 Correct 8 ms 840 KB Output is correct
74 Correct 8 ms 852 KB Output is correct
75 Correct 13 ms 832 KB Output is correct
76 Correct 11 ms 1236 KB Output is correct
77 Correct 12 ms 1232 KB Output is correct
78 Correct 11 ms 1236 KB Output is correct
79 Correct 12 ms 1236 KB Output is correct
80 Correct 11 ms 1248 KB Output is correct
81 Correct 22 ms 2108 KB Output is correct
82 Correct 22 ms 2112 KB Output is correct
83 Correct 22 ms 2116 KB Output is correct
84 Correct 22 ms 2004 KB Output is correct
85 Correct 23 ms 2024 KB Output is correct
86 Correct 21 ms 2156 KB Output is correct
87 Correct 22 ms 2132 KB Output is correct
88 Correct 23 ms 2188 KB Output is correct
89 Correct 21 ms 2132 KB Output is correct
90 Correct 20 ms 2132 KB Output is correct
91 Correct 26 ms 3520 KB Output is correct
92 Correct 27 ms 3572 KB Output is correct
93 Correct 25 ms 3596 KB Output is correct
94 Correct 25 ms 3540 KB Output is correct
95 Correct 25 ms 3576 KB Output is correct
96 Correct 24 ms 4300 KB Output is correct
97 Correct 25 ms 4348 KB Output is correct
98 Correct 25 ms 4336 KB Output is correct
99 Correct 24 ms 4340 KB Output is correct
100 Correct 29 ms 4332 KB Output is correct
101 Correct 25 ms 4344 KB Output is correct
102 Correct 24 ms 4296 KB Output is correct
103 Correct 25 ms 4308 KB Output is correct
104 Correct 24 ms 4344 KB Output is correct
105 Correct 24 ms 4348 KB Output is correct
106 Correct 29 ms 4732 KB Output is correct
107 Correct 29 ms 4728 KB Output is correct
108 Correct 23 ms 4728 KB Output is correct
109 Correct 23 ms 4716 KB Output is correct
110 Correct 24 ms 4732 KB Output is correct
111 Correct 23 ms 4724 KB Output is correct
112 Correct 23 ms 4728 KB Output is correct
113 Correct 29 ms 4688 KB Output is correct
114 Correct 23 ms 4728 KB Output is correct
115 Correct 23 ms 4692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 15 ms 1236 KB Output is correct
15 Correct 30 ms 2004 KB Output is correct
16 Correct 45 ms 2788 KB Output is correct
17 Correct 35 ms 2772 KB Output is correct
18 Correct 46 ms 2704 KB Output is correct
19 Correct 38 ms 2772 KB Output is correct
20 Correct 38 ms 2760 KB Output is correct
21 Correct 38 ms 2772 KB Output is correct
22 Correct 46 ms 2772 KB Output is correct
23 Correct 47 ms 2772 KB Output is correct
24 Correct 45 ms 2772 KB Output is correct
25 Correct 46 ms 2772 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 15 ms 1248 KB Output is correct
30 Correct 37 ms 2004 KB Output is correct
31 Correct 45 ms 2772 KB Output is correct
32 Correct 60 ms 2772 KB Output is correct
33 Correct 53 ms 2808 KB Output is correct
34 Correct 46 ms 2772 KB Output is correct
35 Correct 48 ms 2784 KB Output is correct
36 Correct 46 ms 2772 KB Output is correct
37 Correct 48 ms 2788 KB Output is correct
38 Correct 44 ms 2772 KB Output is correct
39 Correct 48 ms 2784 KB Output is correct
40 Correct 47 ms 2764 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 2 ms 332 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 11 ms 852 KB Output is correct
48 Correct 8 ms 868 KB Output is correct
49 Correct 20 ms 1260 KB Output is correct
50 Correct 38 ms 2044 KB Output is correct
51 Correct 63 ms 2824 KB Output is correct
52 Correct 61 ms 2816 KB Output is correct
53 Correct 63 ms 2816 KB Output is correct
54 Correct 64 ms 2832 KB Output is correct
55 Correct 69 ms 2800 KB Output is correct
56 Correct 61 ms 2820 KB Output is correct
57 Correct 61 ms 2820 KB Output is correct
58 Correct 62 ms 2812 KB Output is correct
59 Correct 60 ms 2824 KB Output is correct
60 Correct 68 ms 2932 KB Output is correct
61 Correct 62 ms 2816 KB Output is correct
62 Correct 69 ms 2820 KB Output is correct
63 Correct 62 ms 2808 KB Output is correct
64 Correct 61 ms 2812 KB Output is correct
65 Correct 61 ms 2804 KB Output is correct
66 Correct 69 ms 2816 KB Output is correct
67 Correct 61 ms 2816 KB Output is correct
68 Correct 70 ms 2788 KB Output is correct
69 Correct 61 ms 2820 KB Output is correct
70 Correct 61 ms 2816 KB Output is correct
71 Correct 6 ms 852 KB Output is correct
72 Correct 6 ms 852 KB Output is correct
73 Correct 8 ms 840 KB Output is correct
74 Correct 8 ms 852 KB Output is correct
75 Correct 13 ms 832 KB Output is correct
76 Correct 11 ms 1236 KB Output is correct
77 Correct 12 ms 1232 KB Output is correct
78 Correct 11 ms 1236 KB Output is correct
79 Correct 12 ms 1236 KB Output is correct
80 Correct 11 ms 1248 KB Output is correct
81 Correct 22 ms 2108 KB Output is correct
82 Correct 22 ms 2112 KB Output is correct
83 Correct 22 ms 2116 KB Output is correct
84 Correct 22 ms 2004 KB Output is correct
85 Correct 23 ms 2024 KB Output is correct
86 Correct 21 ms 2156 KB Output is correct
87 Correct 22 ms 2132 KB Output is correct
88 Correct 23 ms 2188 KB Output is correct
89 Correct 21 ms 2132 KB Output is correct
90 Correct 20 ms 2132 KB Output is correct
91 Correct 26 ms 3520 KB Output is correct
92 Correct 27 ms 3572 KB Output is correct
93 Correct 25 ms 3596 KB Output is correct
94 Correct 25 ms 3540 KB Output is correct
95 Correct 25 ms 3576 KB Output is correct
96 Correct 24 ms 4300 KB Output is correct
97 Correct 25 ms 4348 KB Output is correct
98 Correct 25 ms 4336 KB Output is correct
99 Correct 24 ms 4340 KB Output is correct
100 Correct 29 ms 4332 KB Output is correct
101 Correct 25 ms 4344 KB Output is correct
102 Correct 24 ms 4296 KB Output is correct
103 Correct 25 ms 4308 KB Output is correct
104 Correct 24 ms 4344 KB Output is correct
105 Correct 24 ms 4348 KB Output is correct
106 Correct 29 ms 4732 KB Output is correct
107 Correct 29 ms 4728 KB Output is correct
108 Correct 23 ms 4728 KB Output is correct
109 Correct 23 ms 4716 KB Output is correct
110 Correct 24 ms 4732 KB Output is correct
111 Correct 23 ms 4724 KB Output is correct
112 Correct 23 ms 4728 KB Output is correct
113 Correct 29 ms 4688 KB Output is correct
114 Correct 23 ms 4728 KB Output is correct
115 Correct 23 ms 4692 KB Output is correct
116 Correct 408 ms 1888 KB Output is correct
117 Correct 436 ms 1996 KB Output is correct
118 Correct 2283 ms 2200 KB Output is correct
119 Correct 1835 ms 2984 KB Output is correct
120 Correct 1286 ms 2984 KB Output is correct
121 Correct 3954 ms 3600 KB Output is correct
122 Correct 2797 ms 3644 KB Output is correct
123 Execution timed out 7031 ms 4308 KB Time limit exceeded
124 Halted 0 ms 0 KB -