Submission #799385

# Submission time Handle Problem Language Result Execution time Memory
799385 2023-07-31T13:39:55 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 7172 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 freq;
vi cnts;
set<int> incnt;
ll tmp, pref, suff, n;

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

ll numberOfSubsequences(ll x)
{
    return x * (x + 1) / 2;
}
ll totalLengthOfSubsequences(ll x)
{
    return x * (x + 1) * (x + 1) / 2 - (2 * x + 1) * (x + 1) * x / 6;
}
void add(int x)
{
    if (freq[x] > 0)
    {
        --cnts[freq[x]];
        if (cnts[freq[x]] == 0)
            incnt.erase(freq[x]);
    }
    ++freq[x];
    ++cnts[freq[x]];
    if (cnts[freq[x]] == 1)
        incnt.insert(freq[x]);
}
void sub(int x)
{
    --cnts[freq[x]];
    if (cnts[freq[x]] == 0)
        incnt.erase(freq[x]);
    --freq[x];
    if (freq[x])
    {
        ++cnts[freq[x]];
        if (cnts[freq[x]] == 1)
            incnt.insert(freq[x]);
    }
}
void doStuff(ll x, ll y)
{
    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) * pref * (n - pref - len);

    pref += len;
    swap(pref, suff);
}

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;
    freq.assign(*max_element(all(A)) + 1, 0);
    cnts.assign(N + 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++]);

        n = q.r - q.l;
        pref = 0, suff = 0;
        tmp = totalLengthOfSubsequences(n);

        for (ll x : incnt)
        {
            // ll x = it->first;  // how much
            ll y = cnts[x]; // how often
            if (y & 1)
                doStuff(x, 1);
            if (y > 1)
            {
                doStuff(x, y / 2);
                doStuff(x, y / 2);
            }
        }

        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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 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 2 ms 468 KB Output is correct
4 Correct 13 ms 2036 KB Output is correct
5 Correct 23 ms 3532 KB Output is correct
6 Correct 41 ms 5112 KB Output is correct
7 Correct 33 ms 5068 KB Output is correct
8 Correct 36 ms 5076 KB Output is correct
9 Correct 35 ms 5108 KB Output is correct
10 Correct 34 ms 5076 KB Output is correct
11 Correct 35 ms 5116 KB Output is correct
12 Correct 36 ms 5052 KB Output is correct
13 Correct 35 ms 5096 KB Output is correct
14 Correct 38 ms 5068 KB Output is correct
15 Correct 34 ms 5120 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 2 ms 468 KB Output is correct
4 Correct 13 ms 2036 KB Output is correct
5 Correct 23 ms 3532 KB Output is correct
6 Correct 41 ms 5112 KB Output is correct
7 Correct 33 ms 5068 KB Output is correct
8 Correct 36 ms 5076 KB Output is correct
9 Correct 35 ms 5108 KB Output is correct
10 Correct 34 ms 5076 KB Output is correct
11 Correct 35 ms 5116 KB Output is correct
12 Correct 36 ms 5052 KB Output is correct
13 Correct 35 ms 5096 KB Output is correct
14 Correct 38 ms 5068 KB Output is correct
15 Correct 34 ms 5120 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 13 ms 2012 KB Output is correct
20 Correct 26 ms 3596 KB Output is correct
21 Correct 40 ms 5108 KB Output is correct
22 Correct 39 ms 5116 KB Output is correct
23 Correct 39 ms 5060 KB Output is correct
24 Correct 40 ms 5196 KB Output is correct
25 Correct 39 ms 5076 KB Output is correct
26 Correct 45 ms 5108 KB Output is correct
27 Correct 39 ms 5120 KB Output is correct
28 Correct 38 ms 5112 KB Output is correct
29 Correct 39 ms 5076 KB Output is correct
30 Correct 39 ms 5136 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 2 ms 468 KB Output is correct
4 Correct 13 ms 2036 KB Output is correct
5 Correct 23 ms 3532 KB Output is correct
6 Correct 41 ms 5112 KB Output is correct
7 Correct 33 ms 5068 KB Output is correct
8 Correct 36 ms 5076 KB Output is correct
9 Correct 35 ms 5108 KB Output is correct
10 Correct 34 ms 5076 KB Output is correct
11 Correct 35 ms 5116 KB Output is correct
12 Correct 36 ms 5052 KB Output is correct
13 Correct 35 ms 5096 KB Output is correct
14 Correct 38 ms 5068 KB Output is correct
15 Correct 34 ms 5120 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 13 ms 2012 KB Output is correct
20 Correct 26 ms 3596 KB Output is correct
21 Correct 40 ms 5108 KB Output is correct
22 Correct 39 ms 5116 KB Output is correct
23 Correct 39 ms 5060 KB Output is correct
24 Correct 40 ms 5196 KB Output is correct
25 Correct 39 ms 5076 KB Output is correct
26 Correct 45 ms 5108 KB Output is correct
27 Correct 39 ms 5120 KB Output is correct
28 Correct 38 ms 5112 KB Output is correct
29 Correct 39 ms 5076 KB Output is correct
30 Correct 39 ms 5136 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 456 KB Output is correct
37 Correct 6 ms 1196 KB Output is correct
38 Correct 4 ms 1236 KB Output is correct
39 Correct 9 ms 2004 KB Output is correct
40 Correct 20 ms 3516 KB Output is correct
41 Correct 33 ms 5152 KB Output is correct
42 Correct 32 ms 5100 KB Output is correct
43 Correct 33 ms 5124 KB Output is correct
44 Correct 32 ms 5116 KB Output is correct
45 Correct 34 ms 5132 KB Output is correct
46 Correct 32 ms 5068 KB Output is correct
47 Correct 32 ms 5104 KB Output is correct
48 Correct 33 ms 5160 KB Output is correct
49 Correct 32 ms 5132 KB Output is correct
50 Correct 33 ms 5164 KB Output is correct
51 Correct 32 ms 5100 KB Output is correct
52 Correct 32 ms 5104 KB Output is correct
53 Correct 33 ms 5176 KB Output is correct
54 Correct 33 ms 5076 KB Output is correct
55 Correct 31 ms 5072 KB Output is correct
56 Correct 31 ms 5172 KB Output is correct
57 Correct 31 ms 5108 KB Output is correct
58 Correct 32 ms 5120 KB Output is correct
59 Correct 32 ms 5108 KB Output is correct
60 Correct 32 ms 5056 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 13 ms 2036 KB Output is correct
15 Correct 23 ms 3532 KB Output is correct
16 Correct 41 ms 5112 KB Output is correct
17 Correct 33 ms 5068 KB Output is correct
18 Correct 36 ms 5076 KB Output is correct
19 Correct 35 ms 5108 KB Output is correct
20 Correct 34 ms 5076 KB Output is correct
21 Correct 35 ms 5116 KB Output is correct
22 Correct 36 ms 5052 KB Output is correct
23 Correct 35 ms 5096 KB Output is correct
24 Correct 38 ms 5068 KB Output is correct
25 Correct 34 ms 5120 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 13 ms 2012 KB Output is correct
30 Correct 26 ms 3596 KB Output is correct
31 Correct 40 ms 5108 KB Output is correct
32 Correct 39 ms 5116 KB Output is correct
33 Correct 39 ms 5060 KB Output is correct
34 Correct 40 ms 5196 KB Output is correct
35 Correct 39 ms 5076 KB Output is correct
36 Correct 45 ms 5108 KB Output is correct
37 Correct 39 ms 5120 KB Output is correct
38 Correct 38 ms 5112 KB Output is correct
39 Correct 39 ms 5076 KB Output is correct
40 Correct 39 ms 5136 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 456 KB Output is correct
47 Correct 6 ms 1196 KB Output is correct
48 Correct 4 ms 1236 KB Output is correct
49 Correct 9 ms 2004 KB Output is correct
50 Correct 20 ms 3516 KB Output is correct
51 Correct 33 ms 5152 KB Output is correct
52 Correct 32 ms 5100 KB Output is correct
53 Correct 33 ms 5124 KB Output is correct
54 Correct 32 ms 5116 KB Output is correct
55 Correct 34 ms 5132 KB Output is correct
56 Correct 32 ms 5068 KB Output is correct
57 Correct 32 ms 5104 KB Output is correct
58 Correct 33 ms 5160 KB Output is correct
59 Correct 32 ms 5132 KB Output is correct
60 Correct 33 ms 5164 KB Output is correct
61 Correct 32 ms 5100 KB Output is correct
62 Correct 32 ms 5104 KB Output is correct
63 Correct 33 ms 5176 KB Output is correct
64 Correct 33 ms 5076 KB Output is correct
65 Correct 31 ms 5072 KB Output is correct
66 Correct 31 ms 5172 KB Output is correct
67 Correct 31 ms 5108 KB Output is correct
68 Correct 32 ms 5120 KB Output is correct
69 Correct 32 ms 5108 KB Output is correct
70 Correct 32 ms 5056 KB Output is correct
71 Correct 4 ms 1184 KB Output is correct
72 Correct 4 ms 1236 KB Output is correct
73 Correct 4 ms 1232 KB Output is correct
74 Correct 4 ms 1204 KB Output is correct
75 Correct 4 ms 1236 KB Output is correct
76 Correct 7 ms 2004 KB Output is correct
77 Correct 7 ms 2004 KB Output is correct
78 Correct 7 ms 1992 KB Output is correct
79 Correct 7 ms 2004 KB Output is correct
80 Correct 7 ms 1984 KB Output is correct
81 Correct 13 ms 3744 KB Output is correct
82 Correct 13 ms 3660 KB Output is correct
83 Correct 13 ms 3732 KB Output is correct
84 Correct 14 ms 3684 KB Output is correct
85 Correct 13 ms 3796 KB Output is correct
86 Correct 15 ms 3748 KB Output is correct
87 Correct 15 ms 3788 KB Output is correct
88 Correct 15 ms 3780 KB Output is correct
89 Correct 15 ms 3808 KB Output is correct
90 Correct 15 ms 3816 KB Output is correct
91 Correct 22 ms 6000 KB Output is correct
92 Correct 23 ms 6028 KB Output is correct
93 Correct 22 ms 5944 KB Output is correct
94 Correct 22 ms 6036 KB Output is correct
95 Correct 23 ms 5968 KB Output is correct
96 Correct 26 ms 6772 KB Output is correct
97 Correct 25 ms 6760 KB Output is correct
98 Correct 25 ms 6748 KB Output is correct
99 Correct 26 ms 6780 KB Output is correct
100 Correct 25 ms 6732 KB Output is correct
101 Correct 25 ms 6764 KB Output is correct
102 Correct 26 ms 6764 KB Output is correct
103 Correct 25 ms 6836 KB Output is correct
104 Correct 25 ms 6732 KB Output is correct
105 Correct 25 ms 6768 KB Output is correct
106 Correct 27 ms 7172 KB Output is correct
107 Correct 25 ms 7148 KB Output is correct
108 Correct 24 ms 7144 KB Output is correct
109 Correct 24 ms 7124 KB Output is correct
110 Correct 25 ms 7116 KB Output is correct
111 Correct 24 ms 7152 KB Output is correct
112 Correct 24 ms 7028 KB Output is correct
113 Correct 24 ms 7036 KB Output is correct
114 Correct 25 ms 7012 KB Output is correct
115 Correct 24 ms 7032 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 13 ms 2036 KB Output is correct
15 Correct 23 ms 3532 KB Output is correct
16 Correct 41 ms 5112 KB Output is correct
17 Correct 33 ms 5068 KB Output is correct
18 Correct 36 ms 5076 KB Output is correct
19 Correct 35 ms 5108 KB Output is correct
20 Correct 34 ms 5076 KB Output is correct
21 Correct 35 ms 5116 KB Output is correct
22 Correct 36 ms 5052 KB Output is correct
23 Correct 35 ms 5096 KB Output is correct
24 Correct 38 ms 5068 KB Output is correct
25 Correct 34 ms 5120 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 13 ms 2012 KB Output is correct
30 Correct 26 ms 3596 KB Output is correct
31 Correct 40 ms 5108 KB Output is correct
32 Correct 39 ms 5116 KB Output is correct
33 Correct 39 ms 5060 KB Output is correct
34 Correct 40 ms 5196 KB Output is correct
35 Correct 39 ms 5076 KB Output is correct
36 Correct 45 ms 5108 KB Output is correct
37 Correct 39 ms 5120 KB Output is correct
38 Correct 38 ms 5112 KB Output is correct
39 Correct 39 ms 5076 KB Output is correct
40 Correct 39 ms 5136 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 456 KB Output is correct
47 Correct 6 ms 1196 KB Output is correct
48 Correct 4 ms 1236 KB Output is correct
49 Correct 9 ms 2004 KB Output is correct
50 Correct 20 ms 3516 KB Output is correct
51 Correct 33 ms 5152 KB Output is correct
52 Correct 32 ms 5100 KB Output is correct
53 Correct 33 ms 5124 KB Output is correct
54 Correct 32 ms 5116 KB Output is correct
55 Correct 34 ms 5132 KB Output is correct
56 Correct 32 ms 5068 KB Output is correct
57 Correct 32 ms 5104 KB Output is correct
58 Correct 33 ms 5160 KB Output is correct
59 Correct 32 ms 5132 KB Output is correct
60 Correct 33 ms 5164 KB Output is correct
61 Correct 32 ms 5100 KB Output is correct
62 Correct 32 ms 5104 KB Output is correct
63 Correct 33 ms 5176 KB Output is correct
64 Correct 33 ms 5076 KB Output is correct
65 Correct 31 ms 5072 KB Output is correct
66 Correct 31 ms 5172 KB Output is correct
67 Correct 31 ms 5108 KB Output is correct
68 Correct 32 ms 5120 KB Output is correct
69 Correct 32 ms 5108 KB Output is correct
70 Correct 32 ms 5056 KB Output is correct
71 Correct 4 ms 1184 KB Output is correct
72 Correct 4 ms 1236 KB Output is correct
73 Correct 4 ms 1232 KB Output is correct
74 Correct 4 ms 1204 KB Output is correct
75 Correct 4 ms 1236 KB Output is correct
76 Correct 7 ms 2004 KB Output is correct
77 Correct 7 ms 2004 KB Output is correct
78 Correct 7 ms 1992 KB Output is correct
79 Correct 7 ms 2004 KB Output is correct
80 Correct 7 ms 1984 KB Output is correct
81 Correct 13 ms 3744 KB Output is correct
82 Correct 13 ms 3660 KB Output is correct
83 Correct 13 ms 3732 KB Output is correct
84 Correct 14 ms 3684 KB Output is correct
85 Correct 13 ms 3796 KB Output is correct
86 Correct 15 ms 3748 KB Output is correct
87 Correct 15 ms 3788 KB Output is correct
88 Correct 15 ms 3780 KB Output is correct
89 Correct 15 ms 3808 KB Output is correct
90 Correct 15 ms 3816 KB Output is correct
91 Correct 22 ms 6000 KB Output is correct
92 Correct 23 ms 6028 KB Output is correct
93 Correct 22 ms 5944 KB Output is correct
94 Correct 22 ms 6036 KB Output is correct
95 Correct 23 ms 5968 KB Output is correct
96 Correct 26 ms 6772 KB Output is correct
97 Correct 25 ms 6760 KB Output is correct
98 Correct 25 ms 6748 KB Output is correct
99 Correct 26 ms 6780 KB Output is correct
100 Correct 25 ms 6732 KB Output is correct
101 Correct 25 ms 6764 KB Output is correct
102 Correct 26 ms 6764 KB Output is correct
103 Correct 25 ms 6836 KB Output is correct
104 Correct 25 ms 6732 KB Output is correct
105 Correct 25 ms 6768 KB Output is correct
106 Correct 27 ms 7172 KB Output is correct
107 Correct 25 ms 7148 KB Output is correct
108 Correct 24 ms 7144 KB Output is correct
109 Correct 24 ms 7124 KB Output is correct
110 Correct 25 ms 7116 KB Output is correct
111 Correct 24 ms 7152 KB Output is correct
112 Correct 24 ms 7028 KB Output is correct
113 Correct 24 ms 7036 KB Output is correct
114 Correct 25 ms 7012 KB Output is correct
115 Correct 24 ms 7032 KB Output is correct
116 Correct 261 ms 1960 KB Output is correct
117 Correct 216 ms 1972 KB Output is correct
118 Correct 1350 ms 2728 KB Output is correct
119 Correct 565 ms 2740 KB Output is correct
120 Correct 328 ms 2724 KB Output is correct
121 Correct 981 ms 3524 KB Output is correct
122 Correct 516 ms 3528 KB Output is correct
123 Correct 3446 ms 5220 KB Output is correct
124 Correct 820 ms 5300 KB Output is correct
125 Correct 620 ms 5408 KB Output is correct
126 Execution timed out 7046 ms 6220 KB Time limit exceeded
127 Halted 0 ms 0 KB -