Submission #799359

# Submission time Handle Problem Language Result Execution time Memory
799359 2023-07-31T13:17:24 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 18348 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;
multiset<int> cnts;

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 (heatmap[x] > 0)
    {
        auto it = cnts.find(heatmap[x]);
        cnts.erase(it);
    }
    ++heatmap[x];
    cnts.insert(heatmap[x]);
}
void sub(int x)
{
    auto it = cnts.find(heatmap[x]);
    cnts.erase(it);
    --heatmap[x];
    if (heatmap[x])
        cnts.insert(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;
        ll left = 0, right = 0;
        ll tmp = totalLengthOfSubsequences(n);

        for (auto it = cnts.begin(); it != cnts.end(); ++it)
        {
            ll x = *it; // how much
            ll y = 1;   // 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 1 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 324 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 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 14 ms 1236 KB Output is correct
5 Correct 20 ms 2260 KB Output is correct
6 Correct 32 ms 3284 KB Output is correct
7 Correct 29 ms 3172 KB Output is correct
8 Correct 31 ms 3184 KB Output is correct
9 Correct 33 ms 3284 KB Output is correct
10 Correct 30 ms 3284 KB Output is correct
11 Correct 31 ms 3284 KB Output is correct
12 Correct 32 ms 3292 KB Output is correct
13 Correct 33 ms 3284 KB Output is correct
14 Correct 45 ms 3300 KB Output is correct
15 Correct 31 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 14 ms 1236 KB Output is correct
5 Correct 20 ms 2260 KB Output is correct
6 Correct 32 ms 3284 KB Output is correct
7 Correct 29 ms 3172 KB Output is correct
8 Correct 31 ms 3184 KB Output is correct
9 Correct 33 ms 3284 KB Output is correct
10 Correct 30 ms 3284 KB Output is correct
11 Correct 31 ms 3284 KB Output is correct
12 Correct 32 ms 3292 KB Output is correct
13 Correct 33 ms 3284 KB Output is correct
14 Correct 45 ms 3300 KB Output is correct
15 Correct 31 ms 3284 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 12 ms 1360 KB Output is correct
20 Correct 24 ms 2388 KB Output is correct
21 Correct 48 ms 3404 KB Output is correct
22 Correct 35 ms 3424 KB Output is correct
23 Correct 36 ms 3412 KB Output is correct
24 Correct 40 ms 3440 KB Output is correct
25 Correct 42 ms 3408 KB Output is correct
26 Correct 37 ms 3428 KB Output is correct
27 Correct 37 ms 3428 KB Output is correct
28 Correct 35 ms 3300 KB Output is correct
29 Correct 35 ms 3304 KB Output is correct
30 Correct 36 ms 3436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 14 ms 1236 KB Output is correct
5 Correct 20 ms 2260 KB Output is correct
6 Correct 32 ms 3284 KB Output is correct
7 Correct 29 ms 3172 KB Output is correct
8 Correct 31 ms 3184 KB Output is correct
9 Correct 33 ms 3284 KB Output is correct
10 Correct 30 ms 3284 KB Output is correct
11 Correct 31 ms 3284 KB Output is correct
12 Correct 32 ms 3292 KB Output is correct
13 Correct 33 ms 3284 KB Output is correct
14 Correct 45 ms 3300 KB Output is correct
15 Correct 31 ms 3284 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 12 ms 1360 KB Output is correct
20 Correct 24 ms 2388 KB Output is correct
21 Correct 48 ms 3404 KB Output is correct
22 Correct 35 ms 3424 KB Output is correct
23 Correct 36 ms 3412 KB Output is correct
24 Correct 40 ms 3440 KB Output is correct
25 Correct 42 ms 3408 KB Output is correct
26 Correct 37 ms 3428 KB Output is correct
27 Correct 37 ms 3428 KB Output is correct
28 Correct 35 ms 3300 KB Output is correct
29 Correct 35 ms 3304 KB Output is correct
30 Correct 36 ms 3436 KB Output is correct
31 Correct 1 ms 324 KB Output is correct
32 Correct 1 ms 324 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 9 ms 840 KB Output is correct
38 Correct 9 ms 968 KB Output is correct
39 Correct 19 ms 1508 KB Output is correct
40 Correct 36 ms 2652 KB Output is correct
41 Correct 56 ms 3660 KB Output is correct
42 Correct 55 ms 3668 KB Output is correct
43 Correct 55 ms 3720 KB Output is correct
44 Correct 55 ms 3672 KB Output is correct
45 Correct 55 ms 3668 KB Output is correct
46 Correct 55 ms 3696 KB Output is correct
47 Correct 55 ms 3796 KB Output is correct
48 Correct 54 ms 3812 KB Output is correct
49 Correct 54 ms 3816 KB Output is correct
50 Correct 54 ms 3816 KB Output is correct
51 Correct 54 ms 3816 KB Output is correct
52 Correct 55 ms 3808 KB Output is correct
53 Correct 54 ms 3788 KB Output is correct
54 Correct 53 ms 3812 KB Output is correct
55 Correct 54 ms 3796 KB Output is correct
56 Correct 60 ms 3808 KB Output is correct
57 Correct 53 ms 3816 KB Output is correct
58 Correct 54 ms 3804 KB Output is correct
59 Correct 55 ms 3812 KB Output is correct
60 Correct 56 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 324 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 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 14 ms 1236 KB Output is correct
15 Correct 20 ms 2260 KB Output is correct
16 Correct 32 ms 3284 KB Output is correct
17 Correct 29 ms 3172 KB Output is correct
18 Correct 31 ms 3184 KB Output is correct
19 Correct 33 ms 3284 KB Output is correct
20 Correct 30 ms 3284 KB Output is correct
21 Correct 31 ms 3284 KB Output is correct
22 Correct 32 ms 3292 KB Output is correct
23 Correct 33 ms 3284 KB Output is correct
24 Correct 45 ms 3300 KB Output is correct
25 Correct 31 ms 3284 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 12 ms 1360 KB Output is correct
30 Correct 24 ms 2388 KB Output is correct
31 Correct 48 ms 3404 KB Output is correct
32 Correct 35 ms 3424 KB Output is correct
33 Correct 36 ms 3412 KB Output is correct
34 Correct 40 ms 3440 KB Output is correct
35 Correct 42 ms 3408 KB Output is correct
36 Correct 37 ms 3428 KB Output is correct
37 Correct 37 ms 3428 KB Output is correct
38 Correct 35 ms 3300 KB Output is correct
39 Correct 35 ms 3304 KB Output is correct
40 Correct 36 ms 3436 KB Output is correct
41 Correct 1 ms 324 KB Output is correct
42 Correct 1 ms 324 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 9 ms 840 KB Output is correct
48 Correct 9 ms 968 KB Output is correct
49 Correct 19 ms 1508 KB Output is correct
50 Correct 36 ms 2652 KB Output is correct
51 Correct 56 ms 3660 KB Output is correct
52 Correct 55 ms 3668 KB Output is correct
53 Correct 55 ms 3720 KB Output is correct
54 Correct 55 ms 3672 KB Output is correct
55 Correct 55 ms 3668 KB Output is correct
56 Correct 55 ms 3696 KB Output is correct
57 Correct 55 ms 3796 KB Output is correct
58 Correct 54 ms 3812 KB Output is correct
59 Correct 54 ms 3816 KB Output is correct
60 Correct 54 ms 3816 KB Output is correct
61 Correct 54 ms 3816 KB Output is correct
62 Correct 55 ms 3808 KB Output is correct
63 Correct 54 ms 3788 KB Output is correct
64 Correct 53 ms 3812 KB Output is correct
65 Correct 54 ms 3796 KB Output is correct
66 Correct 60 ms 3808 KB Output is correct
67 Correct 53 ms 3816 KB Output is correct
68 Correct 54 ms 3804 KB Output is correct
69 Correct 55 ms 3812 KB Output is correct
70 Correct 56 ms 3796 KB Output is correct
71 Correct 10 ms 992 KB Output is correct
72 Correct 10 ms 968 KB Output is correct
73 Correct 10 ms 964 KB Output is correct
74 Correct 15 ms 980 KB Output is correct
75 Correct 11 ms 980 KB Output is correct
76 Correct 22 ms 1736 KB Output is correct
77 Correct 22 ms 1836 KB Output is correct
78 Correct 23 ms 1844 KB Output is correct
79 Correct 22 ms 1748 KB Output is correct
80 Correct 22 ms 1748 KB Output is correct
81 Correct 48 ms 3380 KB Output is correct
82 Correct 49 ms 3264 KB Output is correct
83 Correct 48 ms 3380 KB Output is correct
84 Correct 53 ms 3380 KB Output is correct
85 Correct 48 ms 3376 KB Output is correct
86 Correct 52 ms 4036 KB Output is correct
87 Correct 50 ms 3916 KB Output is correct
88 Correct 50 ms 3924 KB Output is correct
89 Correct 54 ms 4040 KB Output is correct
90 Correct 50 ms 3988 KB Output is correct
91 Correct 93 ms 9956 KB Output is correct
92 Correct 83 ms 9788 KB Output is correct
93 Correct 83 ms 9824 KB Output is correct
94 Correct 85 ms 9808 KB Output is correct
95 Correct 82 ms 9812 KB Output is correct
96 Correct 88 ms 15564 KB Output is correct
97 Correct 84 ms 15384 KB Output is correct
98 Correct 82 ms 15436 KB Output is correct
99 Correct 93 ms 15404 KB Output is correct
100 Correct 83 ms 15444 KB Output is correct
101 Correct 89 ms 15508 KB Output is correct
102 Correct 81 ms 15476 KB Output is correct
103 Correct 82 ms 15440 KB Output is correct
104 Correct 82 ms 15496 KB Output is correct
105 Correct 83 ms 15500 KB Output is correct
106 Correct 83 ms 18288 KB Output is correct
107 Correct 86 ms 18184 KB Output is correct
108 Correct 82 ms 18256 KB Output is correct
109 Correct 84 ms 18192 KB Output is correct
110 Correct 79 ms 18248 KB Output is correct
111 Correct 79 ms 18264 KB Output is correct
112 Correct 89 ms 18348 KB Output is correct
113 Correct 85 ms 18252 KB Output is correct
114 Correct 80 ms 18224 KB Output is correct
115 Correct 80 ms 18268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 324 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 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 14 ms 1236 KB Output is correct
15 Correct 20 ms 2260 KB Output is correct
16 Correct 32 ms 3284 KB Output is correct
17 Correct 29 ms 3172 KB Output is correct
18 Correct 31 ms 3184 KB Output is correct
19 Correct 33 ms 3284 KB Output is correct
20 Correct 30 ms 3284 KB Output is correct
21 Correct 31 ms 3284 KB Output is correct
22 Correct 32 ms 3292 KB Output is correct
23 Correct 33 ms 3284 KB Output is correct
24 Correct 45 ms 3300 KB Output is correct
25 Correct 31 ms 3284 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 12 ms 1360 KB Output is correct
30 Correct 24 ms 2388 KB Output is correct
31 Correct 48 ms 3404 KB Output is correct
32 Correct 35 ms 3424 KB Output is correct
33 Correct 36 ms 3412 KB Output is correct
34 Correct 40 ms 3440 KB Output is correct
35 Correct 42 ms 3408 KB Output is correct
36 Correct 37 ms 3428 KB Output is correct
37 Correct 37 ms 3428 KB Output is correct
38 Correct 35 ms 3300 KB Output is correct
39 Correct 35 ms 3304 KB Output is correct
40 Correct 36 ms 3436 KB Output is correct
41 Correct 1 ms 324 KB Output is correct
42 Correct 1 ms 324 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 9 ms 840 KB Output is correct
48 Correct 9 ms 968 KB Output is correct
49 Correct 19 ms 1508 KB Output is correct
50 Correct 36 ms 2652 KB Output is correct
51 Correct 56 ms 3660 KB Output is correct
52 Correct 55 ms 3668 KB Output is correct
53 Correct 55 ms 3720 KB Output is correct
54 Correct 55 ms 3672 KB Output is correct
55 Correct 55 ms 3668 KB Output is correct
56 Correct 55 ms 3696 KB Output is correct
57 Correct 55 ms 3796 KB Output is correct
58 Correct 54 ms 3812 KB Output is correct
59 Correct 54 ms 3816 KB Output is correct
60 Correct 54 ms 3816 KB Output is correct
61 Correct 54 ms 3816 KB Output is correct
62 Correct 55 ms 3808 KB Output is correct
63 Correct 54 ms 3788 KB Output is correct
64 Correct 53 ms 3812 KB Output is correct
65 Correct 54 ms 3796 KB Output is correct
66 Correct 60 ms 3808 KB Output is correct
67 Correct 53 ms 3816 KB Output is correct
68 Correct 54 ms 3804 KB Output is correct
69 Correct 55 ms 3812 KB Output is correct
70 Correct 56 ms 3796 KB Output is correct
71 Correct 10 ms 992 KB Output is correct
72 Correct 10 ms 968 KB Output is correct
73 Correct 10 ms 964 KB Output is correct
74 Correct 15 ms 980 KB Output is correct
75 Correct 11 ms 980 KB Output is correct
76 Correct 22 ms 1736 KB Output is correct
77 Correct 22 ms 1836 KB Output is correct
78 Correct 23 ms 1844 KB Output is correct
79 Correct 22 ms 1748 KB Output is correct
80 Correct 22 ms 1748 KB Output is correct
81 Correct 48 ms 3380 KB Output is correct
82 Correct 49 ms 3264 KB Output is correct
83 Correct 48 ms 3380 KB Output is correct
84 Correct 53 ms 3380 KB Output is correct
85 Correct 48 ms 3376 KB Output is correct
86 Correct 52 ms 4036 KB Output is correct
87 Correct 50 ms 3916 KB Output is correct
88 Correct 50 ms 3924 KB Output is correct
89 Correct 54 ms 4040 KB Output is correct
90 Correct 50 ms 3988 KB Output is correct
91 Correct 93 ms 9956 KB Output is correct
92 Correct 83 ms 9788 KB Output is correct
93 Correct 83 ms 9824 KB Output is correct
94 Correct 85 ms 9808 KB Output is correct
95 Correct 82 ms 9812 KB Output is correct
96 Correct 88 ms 15564 KB Output is correct
97 Correct 84 ms 15384 KB Output is correct
98 Correct 82 ms 15436 KB Output is correct
99 Correct 93 ms 15404 KB Output is correct
100 Correct 83 ms 15444 KB Output is correct
101 Correct 89 ms 15508 KB Output is correct
102 Correct 81 ms 15476 KB Output is correct
103 Correct 82 ms 15440 KB Output is correct
104 Correct 82 ms 15496 KB Output is correct
105 Correct 83 ms 15500 KB Output is correct
106 Correct 83 ms 18288 KB Output is correct
107 Correct 86 ms 18184 KB Output is correct
108 Correct 82 ms 18256 KB Output is correct
109 Correct 84 ms 18192 KB Output is correct
110 Correct 79 ms 18248 KB Output is correct
111 Correct 79 ms 18264 KB Output is correct
112 Correct 89 ms 18348 KB Output is correct
113 Correct 85 ms 18252 KB Output is correct
114 Correct 80 ms 18224 KB Output is correct
115 Correct 80 ms 18268 KB Output is correct
116 Correct 261 ms 2268 KB Output is correct
117 Correct 313 ms 2268 KB Output is correct
118 Correct 1459 ms 2904 KB Output is correct
119 Correct 1920 ms 3020 KB Output is correct
120 Correct 2259 ms 3016 KB Output is correct
121 Correct 5163 ms 3612 KB Output is correct
122 Correct 5988 ms 3720 KB Output is correct
123 Execution timed out 7059 ms 4260 KB Time limit exceeded
124 Halted 0 ms 0 KB -