Submission #799376

# Submission time Handle Problem Language Result Execution time Memory
799376 2023-07-31T13:30:19 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 7644 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;

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

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 (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]);
    }
}

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++]);

        ll n = q.r - q.l;
        ll left = 0, right = 0;
        ll tmp = totalLengthOfSubsequences(n);

        vpii S;
        for (int f : incnt)
        {
            if (cnts[f] & 1)
                S.push_back({f, 1});
            if (cnts[f] > 1)
            {
                S.push_back({f, cnts[f] / 2});
                S.push_back({f, cnts[f] / 2});
            }
        }

        for (auto it = S.begin(); it != S.end(); ++it)
        {
            ll x = it->first;  // how much
            ll y = it->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 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 320 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 320 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 212 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 320 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 11 ms 2080 KB Output is correct
5 Correct 22 ms 3796 KB Output is correct
6 Correct 36 ms 5652 KB Output is correct
7 Correct 32 ms 5620 KB Output is correct
8 Correct 34 ms 5588 KB Output is correct
9 Correct 34 ms 5588 KB Output is correct
10 Correct 34 ms 5576 KB Output is correct
11 Correct 34 ms 5652 KB Output is correct
12 Correct 37 ms 5600 KB Output is correct
13 Correct 35 ms 5588 KB Output is correct
14 Correct 36 ms 5628 KB Output is correct
15 Correct 34 ms 5588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 11 ms 2080 KB Output is correct
5 Correct 22 ms 3796 KB Output is correct
6 Correct 36 ms 5652 KB Output is correct
7 Correct 32 ms 5620 KB Output is correct
8 Correct 34 ms 5588 KB Output is correct
9 Correct 34 ms 5588 KB Output is correct
10 Correct 34 ms 5576 KB Output is correct
11 Correct 34 ms 5652 KB Output is correct
12 Correct 37 ms 5600 KB Output is correct
13 Correct 35 ms 5588 KB Output is correct
14 Correct 36 ms 5628 KB Output is correct
15 Correct 34 ms 5588 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 464 KB Output is correct
19 Correct 13 ms 2132 KB Output is correct
20 Correct 29 ms 3960 KB Output is correct
21 Correct 38 ms 5792 KB Output is correct
22 Correct 39 ms 5796 KB Output is correct
23 Correct 43 ms 5896 KB Output is correct
24 Correct 39 ms 5716 KB Output is correct
25 Correct 43 ms 5740 KB Output is correct
26 Correct 40 ms 5764 KB Output is correct
27 Correct 39 ms 5732 KB Output is correct
28 Correct 38 ms 5716 KB Output is correct
29 Correct 39 ms 5716 KB Output is correct
30 Correct 41 ms 5736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 11 ms 2080 KB Output is correct
5 Correct 22 ms 3796 KB Output is correct
6 Correct 36 ms 5652 KB Output is correct
7 Correct 32 ms 5620 KB Output is correct
8 Correct 34 ms 5588 KB Output is correct
9 Correct 34 ms 5588 KB Output is correct
10 Correct 34 ms 5576 KB Output is correct
11 Correct 34 ms 5652 KB Output is correct
12 Correct 37 ms 5600 KB Output is correct
13 Correct 35 ms 5588 KB Output is correct
14 Correct 36 ms 5628 KB Output is correct
15 Correct 34 ms 5588 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 464 KB Output is correct
19 Correct 13 ms 2132 KB Output is correct
20 Correct 29 ms 3960 KB Output is correct
21 Correct 38 ms 5792 KB Output is correct
22 Correct 39 ms 5796 KB Output is correct
23 Correct 43 ms 5896 KB Output is correct
24 Correct 39 ms 5716 KB Output is correct
25 Correct 43 ms 5740 KB Output is correct
26 Correct 40 ms 5764 KB Output is correct
27 Correct 39 ms 5732 KB Output is correct
28 Correct 38 ms 5716 KB Output is correct
29 Correct 39 ms 5716 KB Output is correct
30 Correct 41 ms 5736 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 460 KB Output is correct
35 Correct 2 ms 432 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 5 ms 1300 KB Output is correct
38 Correct 4 ms 1228 KB Output is correct
39 Correct 9 ms 2248 KB Output is correct
40 Correct 20 ms 4188 KB Output is correct
41 Correct 33 ms 6124 KB Output is correct
42 Correct 32 ms 6192 KB Output is correct
43 Correct 34 ms 6108 KB Output is correct
44 Correct 33 ms 6200 KB Output is correct
45 Correct 33 ms 6100 KB Output is correct
46 Correct 33 ms 6208 KB Output is correct
47 Correct 32 ms 6184 KB Output is correct
48 Correct 33 ms 6180 KB Output is correct
49 Correct 32 ms 6100 KB Output is correct
50 Correct 33 ms 6212 KB Output is correct
51 Correct 32 ms 6204 KB Output is correct
52 Correct 32 ms 6112 KB Output is correct
53 Correct 34 ms 6184 KB Output is correct
54 Correct 33 ms 6100 KB Output is correct
55 Correct 32 ms 6184 KB Output is correct
56 Correct 32 ms 6112 KB Output is correct
57 Correct 34 ms 6340 KB Output is correct
58 Correct 32 ms 6196 KB Output is correct
59 Correct 33 ms 6200 KB Output is correct
60 Correct 32 ms 6172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 320 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 320 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 212 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 320 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 11 ms 2080 KB Output is correct
15 Correct 22 ms 3796 KB Output is correct
16 Correct 36 ms 5652 KB Output is correct
17 Correct 32 ms 5620 KB Output is correct
18 Correct 34 ms 5588 KB Output is correct
19 Correct 34 ms 5588 KB Output is correct
20 Correct 34 ms 5576 KB Output is correct
21 Correct 34 ms 5652 KB Output is correct
22 Correct 37 ms 5600 KB Output is correct
23 Correct 35 ms 5588 KB Output is correct
24 Correct 36 ms 5628 KB Output is correct
25 Correct 34 ms 5588 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 464 KB Output is correct
29 Correct 13 ms 2132 KB Output is correct
30 Correct 29 ms 3960 KB Output is correct
31 Correct 38 ms 5792 KB Output is correct
32 Correct 39 ms 5796 KB Output is correct
33 Correct 43 ms 5896 KB Output is correct
34 Correct 39 ms 5716 KB Output is correct
35 Correct 43 ms 5740 KB Output is correct
36 Correct 40 ms 5764 KB Output is correct
37 Correct 39 ms 5732 KB Output is correct
38 Correct 38 ms 5716 KB Output is correct
39 Correct 39 ms 5716 KB Output is correct
40 Correct 41 ms 5736 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 460 KB Output is correct
45 Correct 2 ms 432 KB Output is correct
46 Correct 1 ms 460 KB Output is correct
47 Correct 5 ms 1300 KB Output is correct
48 Correct 4 ms 1228 KB Output is correct
49 Correct 9 ms 2248 KB Output is correct
50 Correct 20 ms 4188 KB Output is correct
51 Correct 33 ms 6124 KB Output is correct
52 Correct 32 ms 6192 KB Output is correct
53 Correct 34 ms 6108 KB Output is correct
54 Correct 33 ms 6200 KB Output is correct
55 Correct 33 ms 6100 KB Output is correct
56 Correct 33 ms 6208 KB Output is correct
57 Correct 32 ms 6184 KB Output is correct
58 Correct 33 ms 6180 KB Output is correct
59 Correct 32 ms 6100 KB Output is correct
60 Correct 33 ms 6212 KB Output is correct
61 Correct 32 ms 6204 KB Output is correct
62 Correct 32 ms 6112 KB Output is correct
63 Correct 34 ms 6184 KB Output is correct
64 Correct 33 ms 6100 KB Output is correct
65 Correct 32 ms 6184 KB Output is correct
66 Correct 32 ms 6112 KB Output is correct
67 Correct 34 ms 6340 KB Output is correct
68 Correct 32 ms 6196 KB Output is correct
69 Correct 33 ms 6200 KB Output is correct
70 Correct 32 ms 6172 KB Output is correct
71 Correct 4 ms 1364 KB Output is correct
72 Correct 4 ms 1364 KB Output is correct
73 Correct 4 ms 1364 KB Output is correct
74 Correct 4 ms 1236 KB Output is correct
75 Correct 4 ms 1356 KB Output is correct
76 Correct 7 ms 2380 KB Output is correct
77 Correct 8 ms 2380 KB Output is correct
78 Correct 7 ms 2388 KB Output is correct
79 Correct 7 ms 2388 KB Output is correct
80 Correct 7 ms 2388 KB Output is correct
81 Correct 13 ms 4284 KB Output is correct
82 Correct 13 ms 4300 KB Output is correct
83 Correct 20 ms 4356 KB Output is correct
84 Correct 13 ms 4256 KB Output is correct
85 Correct 13 ms 4292 KB Output is correct
86 Correct 15 ms 4248 KB Output is correct
87 Correct 16 ms 4196 KB Output is correct
88 Correct 14 ms 4256 KB Output is correct
89 Correct 14 ms 4052 KB Output is correct
90 Correct 15 ms 4072 KB Output is correct
91 Correct 22 ms 6268 KB Output is correct
92 Correct 23 ms 6252 KB Output is correct
93 Correct 22 ms 6212 KB Output is correct
94 Correct 23 ms 6256 KB Output is correct
95 Correct 30 ms 6244 KB Output is correct
96 Correct 24 ms 6868 KB Output is correct
97 Correct 25 ms 6888 KB Output is correct
98 Correct 26 ms 6896 KB Output is correct
99 Correct 25 ms 6876 KB Output is correct
100 Correct 25 ms 6864 KB Output is correct
101 Correct 25 ms 6900 KB Output is correct
102 Correct 26 ms 6900 KB Output is correct
103 Correct 25 ms 6904 KB Output is correct
104 Correct 25 ms 6896 KB Output is correct
105 Correct 31 ms 6752 KB Output is correct
106 Correct 25 ms 7152 KB Output is correct
107 Correct 25 ms 7112 KB Output is correct
108 Correct 25 ms 7020 KB Output is correct
109 Correct 24 ms 6996 KB Output is correct
110 Correct 25 ms 7032 KB Output is correct
111 Correct 24 ms 7032 KB Output is correct
112 Correct 24 ms 6968 KB Output is correct
113 Correct 25 ms 6988 KB Output is correct
114 Correct 26 ms 6972 KB Output is correct
115 Correct 31 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 320 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 320 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 212 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 320 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 11 ms 2080 KB Output is correct
15 Correct 22 ms 3796 KB Output is correct
16 Correct 36 ms 5652 KB Output is correct
17 Correct 32 ms 5620 KB Output is correct
18 Correct 34 ms 5588 KB Output is correct
19 Correct 34 ms 5588 KB Output is correct
20 Correct 34 ms 5576 KB Output is correct
21 Correct 34 ms 5652 KB Output is correct
22 Correct 37 ms 5600 KB Output is correct
23 Correct 35 ms 5588 KB Output is correct
24 Correct 36 ms 5628 KB Output is correct
25 Correct 34 ms 5588 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 464 KB Output is correct
29 Correct 13 ms 2132 KB Output is correct
30 Correct 29 ms 3960 KB Output is correct
31 Correct 38 ms 5792 KB Output is correct
32 Correct 39 ms 5796 KB Output is correct
33 Correct 43 ms 5896 KB Output is correct
34 Correct 39 ms 5716 KB Output is correct
35 Correct 43 ms 5740 KB Output is correct
36 Correct 40 ms 5764 KB Output is correct
37 Correct 39 ms 5732 KB Output is correct
38 Correct 38 ms 5716 KB Output is correct
39 Correct 39 ms 5716 KB Output is correct
40 Correct 41 ms 5736 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 460 KB Output is correct
45 Correct 2 ms 432 KB Output is correct
46 Correct 1 ms 460 KB Output is correct
47 Correct 5 ms 1300 KB Output is correct
48 Correct 4 ms 1228 KB Output is correct
49 Correct 9 ms 2248 KB Output is correct
50 Correct 20 ms 4188 KB Output is correct
51 Correct 33 ms 6124 KB Output is correct
52 Correct 32 ms 6192 KB Output is correct
53 Correct 34 ms 6108 KB Output is correct
54 Correct 33 ms 6200 KB Output is correct
55 Correct 33 ms 6100 KB Output is correct
56 Correct 33 ms 6208 KB Output is correct
57 Correct 32 ms 6184 KB Output is correct
58 Correct 33 ms 6180 KB Output is correct
59 Correct 32 ms 6100 KB Output is correct
60 Correct 33 ms 6212 KB Output is correct
61 Correct 32 ms 6204 KB Output is correct
62 Correct 32 ms 6112 KB Output is correct
63 Correct 34 ms 6184 KB Output is correct
64 Correct 33 ms 6100 KB Output is correct
65 Correct 32 ms 6184 KB Output is correct
66 Correct 32 ms 6112 KB Output is correct
67 Correct 34 ms 6340 KB Output is correct
68 Correct 32 ms 6196 KB Output is correct
69 Correct 33 ms 6200 KB Output is correct
70 Correct 32 ms 6172 KB Output is correct
71 Correct 4 ms 1364 KB Output is correct
72 Correct 4 ms 1364 KB Output is correct
73 Correct 4 ms 1364 KB Output is correct
74 Correct 4 ms 1236 KB Output is correct
75 Correct 4 ms 1356 KB Output is correct
76 Correct 7 ms 2380 KB Output is correct
77 Correct 8 ms 2380 KB Output is correct
78 Correct 7 ms 2388 KB Output is correct
79 Correct 7 ms 2388 KB Output is correct
80 Correct 7 ms 2388 KB Output is correct
81 Correct 13 ms 4284 KB Output is correct
82 Correct 13 ms 4300 KB Output is correct
83 Correct 20 ms 4356 KB Output is correct
84 Correct 13 ms 4256 KB Output is correct
85 Correct 13 ms 4292 KB Output is correct
86 Correct 15 ms 4248 KB Output is correct
87 Correct 16 ms 4196 KB Output is correct
88 Correct 14 ms 4256 KB Output is correct
89 Correct 14 ms 4052 KB Output is correct
90 Correct 15 ms 4072 KB Output is correct
91 Correct 22 ms 6268 KB Output is correct
92 Correct 23 ms 6252 KB Output is correct
93 Correct 22 ms 6212 KB Output is correct
94 Correct 23 ms 6256 KB Output is correct
95 Correct 30 ms 6244 KB Output is correct
96 Correct 24 ms 6868 KB Output is correct
97 Correct 25 ms 6888 KB Output is correct
98 Correct 26 ms 6896 KB Output is correct
99 Correct 25 ms 6876 KB Output is correct
100 Correct 25 ms 6864 KB Output is correct
101 Correct 25 ms 6900 KB Output is correct
102 Correct 26 ms 6900 KB Output is correct
103 Correct 25 ms 6904 KB Output is correct
104 Correct 25 ms 6896 KB Output is correct
105 Correct 31 ms 6752 KB Output is correct
106 Correct 25 ms 7152 KB Output is correct
107 Correct 25 ms 7112 KB Output is correct
108 Correct 25 ms 7020 KB Output is correct
109 Correct 24 ms 6996 KB Output is correct
110 Correct 25 ms 7032 KB Output is correct
111 Correct 24 ms 7032 KB Output is correct
112 Correct 24 ms 6968 KB Output is correct
113 Correct 25 ms 6988 KB Output is correct
114 Correct 26 ms 6972 KB Output is correct
115 Correct 31 ms 6904 KB Output is correct
116 Correct 276 ms 1840 KB Output is correct
117 Correct 259 ms 1896 KB Output is correct
118 Correct 1422 ms 2600 KB Output is correct
119 Correct 624 ms 2620 KB Output is correct
120 Correct 359 ms 2624 KB Output is correct
121 Correct 1055 ms 3428 KB Output is correct
122 Correct 579 ms 3416 KB Output is correct
123 Correct 3611 ms 5128 KB Output is correct
124 Correct 870 ms 6700 KB Output is correct
125 Correct 640 ms 6752 KB Output is correct
126 Execution timed out 7084 ms 7644 KB Time limit exceeded
127 Halted 0 ms 0 KB -