답안 #799413

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799413 2023-07-31T13:57:10 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 17112 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;
ll tmp, pref, suff, n;

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

ll numberOfSubsequences(ll x)
{
    return x * (x + 1) / 2;
}
ll cach3[300010];
ll totalLengthOfSubsequences(ll x)
{
    if (cach3[x] == -1)
        cach3[x] = x * (x + 1) * (x + 1) / 2 - (2 * x + 1) * (x + 1) * x / 6;
    return cach3[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);
}
struct segtree
{
    vi arr;
    int size;
    void init(int _size)
    {
        size = 1;
        while (size < _size)
            size *= 2;
        arr.assign(2 * size, 0);
    }
    void add(int i, int v)
    {
        i += size;
        while (i > 0)
        {
            arr[i] += v;
            i /= 2;
        }
    }
    void dfs(int x)
    {
        if (arr[x] == 0)
            return;
        if (x < size)
        {
            dfs(2 * x);
            dfs(2 * x + 1);
        }
        else
        {
            // arr[x] := how often
            if (arr[x] & 1)
                doStuff(x - size, 1);
            if (arr[x] > 1)
            {
                doStuff(x - size, arr[x] / 2);
                doStuff(x - size, arr[x] / 2);
            }
        }
    }
};
segtree seg;
void add(int x)
{
    if (freq[x] > 0)
        seg.add(freq[x], -1);
    ++freq[x];
    seg.add(freq[x], 1);
}
void sub(int x)
{
    seg.add(freq[x], -1);
    --freq[x];
    if (freq[x])
        seg.add(freq[x], 1);
}

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 = ceil(sqrt(N));
    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);

    fill(cach3, cach3 + N + 2, -1);

    int lx = 0, rx = 0;
    freq.assign(*max_element(all(A)) + 1, 0);
    seg.init(N + 1);
    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);

        seg.dfs(1);

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

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 236 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 720 KB Output is correct
4 Correct 9 ms 4052 KB Output is correct
5 Correct 18 ms 7892 KB Output is correct
6 Correct 25 ms 13808 KB Output is correct
7 Correct 24 ms 13776 KB Output is correct
8 Correct 31 ms 13848 KB Output is correct
9 Correct 30 ms 13796 KB Output is correct
10 Correct 41 ms 13808 KB Output is correct
11 Correct 34 ms 13808 KB Output is correct
12 Correct 25 ms 13908 KB Output is correct
13 Correct 28 ms 13796 KB Output is correct
14 Correct 25 ms 13860 KB Output is correct
15 Correct 28 ms 13796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 720 KB Output is correct
4 Correct 9 ms 4052 KB Output is correct
5 Correct 18 ms 7892 KB Output is correct
6 Correct 25 ms 13808 KB Output is correct
7 Correct 24 ms 13776 KB Output is correct
8 Correct 31 ms 13848 KB Output is correct
9 Correct 30 ms 13796 KB Output is correct
10 Correct 41 ms 13808 KB Output is correct
11 Correct 34 ms 13808 KB Output is correct
12 Correct 25 ms 13908 KB Output is correct
13 Correct 28 ms 13796 KB Output is correct
14 Correct 25 ms 13860 KB Output is correct
15 Correct 28 ms 13796 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 724 KB Output is correct
19 Correct 9 ms 4176 KB Output is correct
20 Correct 19 ms 8020 KB Output is correct
21 Correct 27 ms 14040 KB Output is correct
22 Correct 29 ms 13980 KB Output is correct
23 Correct 32 ms 13964 KB Output is correct
24 Correct 33 ms 13936 KB Output is correct
25 Correct 32 ms 13956 KB Output is correct
26 Correct 39 ms 13932 KB Output is correct
27 Correct 26 ms 13936 KB Output is correct
28 Correct 29 ms 13896 KB Output is correct
29 Correct 30 ms 13932 KB Output is correct
30 Correct 26 ms 13980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 720 KB Output is correct
4 Correct 9 ms 4052 KB Output is correct
5 Correct 18 ms 7892 KB Output is correct
6 Correct 25 ms 13808 KB Output is correct
7 Correct 24 ms 13776 KB Output is correct
8 Correct 31 ms 13848 KB Output is correct
9 Correct 30 ms 13796 KB Output is correct
10 Correct 41 ms 13808 KB Output is correct
11 Correct 34 ms 13808 KB Output is correct
12 Correct 25 ms 13908 KB Output is correct
13 Correct 28 ms 13796 KB Output is correct
14 Correct 25 ms 13860 KB Output is correct
15 Correct 28 ms 13796 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 724 KB Output is correct
19 Correct 9 ms 4176 KB Output is correct
20 Correct 19 ms 8020 KB Output is correct
21 Correct 27 ms 14040 KB Output is correct
22 Correct 29 ms 13980 KB Output is correct
23 Correct 32 ms 13964 KB Output is correct
24 Correct 33 ms 13936 KB Output is correct
25 Correct 32 ms 13956 KB Output is correct
26 Correct 39 ms 13932 KB Output is correct
27 Correct 26 ms 13936 KB Output is correct
28 Correct 29 ms 13896 KB Output is correct
29 Correct 30 ms 13932 KB Output is correct
30 Correct 26 ms 13980 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 3 ms 728 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 1 ms 724 KB Output is correct
37 Correct 4 ms 2260 KB Output is correct
38 Correct 5 ms 2260 KB Output is correct
39 Correct 10 ms 4308 KB Output is correct
40 Correct 19 ms 8248 KB Output is correct
41 Correct 32 ms 14364 KB Output is correct
42 Correct 33 ms 14296 KB Output is correct
43 Correct 35 ms 14372 KB Output is correct
44 Correct 32 ms 14284 KB Output is correct
45 Correct 29 ms 14376 KB Output is correct
46 Correct 30 ms 14320 KB Output is correct
47 Correct 35 ms 14296 KB Output is correct
48 Correct 32 ms 14316 KB Output is correct
49 Correct 30 ms 14300 KB Output is correct
50 Correct 29 ms 14364 KB Output is correct
51 Correct 37 ms 14312 KB Output is correct
52 Correct 28 ms 14328 KB Output is correct
53 Correct 28 ms 14284 KB Output is correct
54 Correct 41 ms 14372 KB Output is correct
55 Correct 29 ms 14328 KB Output is correct
56 Correct 35 ms 14300 KB Output is correct
57 Correct 31 ms 14316 KB Output is correct
58 Correct 31 ms 14376 KB Output is correct
59 Correct 36 ms 14320 KB Output is correct
60 Correct 30 ms 14316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 236 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 720 KB Output is correct
14 Correct 9 ms 4052 KB Output is correct
15 Correct 18 ms 7892 KB Output is correct
16 Correct 25 ms 13808 KB Output is correct
17 Correct 24 ms 13776 KB Output is correct
18 Correct 31 ms 13848 KB Output is correct
19 Correct 30 ms 13796 KB Output is correct
20 Correct 41 ms 13808 KB Output is correct
21 Correct 34 ms 13808 KB Output is correct
22 Correct 25 ms 13908 KB Output is correct
23 Correct 28 ms 13796 KB Output is correct
24 Correct 25 ms 13860 KB Output is correct
25 Correct 28 ms 13796 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 724 KB Output is correct
29 Correct 9 ms 4176 KB Output is correct
30 Correct 19 ms 8020 KB Output is correct
31 Correct 27 ms 14040 KB Output is correct
32 Correct 29 ms 13980 KB Output is correct
33 Correct 32 ms 13964 KB Output is correct
34 Correct 33 ms 13936 KB Output is correct
35 Correct 32 ms 13956 KB Output is correct
36 Correct 39 ms 13932 KB Output is correct
37 Correct 26 ms 13936 KB Output is correct
38 Correct 29 ms 13896 KB Output is correct
39 Correct 30 ms 13932 KB Output is correct
40 Correct 26 ms 13980 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 3 ms 728 KB Output is correct
45 Correct 2 ms 724 KB Output is correct
46 Correct 1 ms 724 KB Output is correct
47 Correct 4 ms 2260 KB Output is correct
48 Correct 5 ms 2260 KB Output is correct
49 Correct 10 ms 4308 KB Output is correct
50 Correct 19 ms 8248 KB Output is correct
51 Correct 32 ms 14364 KB Output is correct
52 Correct 33 ms 14296 KB Output is correct
53 Correct 35 ms 14372 KB Output is correct
54 Correct 32 ms 14284 KB Output is correct
55 Correct 29 ms 14376 KB Output is correct
56 Correct 30 ms 14320 KB Output is correct
57 Correct 35 ms 14296 KB Output is correct
58 Correct 32 ms 14316 KB Output is correct
59 Correct 30 ms 14300 KB Output is correct
60 Correct 29 ms 14364 KB Output is correct
61 Correct 37 ms 14312 KB Output is correct
62 Correct 28 ms 14328 KB Output is correct
63 Correct 28 ms 14284 KB Output is correct
64 Correct 41 ms 14372 KB Output is correct
65 Correct 29 ms 14328 KB Output is correct
66 Correct 35 ms 14300 KB Output is correct
67 Correct 31 ms 14316 KB Output is correct
68 Correct 31 ms 14376 KB Output is correct
69 Correct 36 ms 14320 KB Output is correct
70 Correct 30 ms 14316 KB Output is correct
71 Correct 6 ms 2376 KB Output is correct
72 Correct 9 ms 2388 KB Output is correct
73 Correct 5 ms 2360 KB Output is correct
74 Correct 8 ms 2392 KB Output is correct
75 Correct 8 ms 2260 KB Output is correct
76 Correct 11 ms 4480 KB Output is correct
77 Correct 11 ms 4432 KB Output is correct
78 Correct 12 ms 4408 KB Output is correct
79 Correct 14 ms 4440 KB Output is correct
80 Correct 16 ms 4412 KB Output is correct
81 Correct 28 ms 8588 KB Output is correct
82 Correct 25 ms 8548 KB Output is correct
83 Correct 25 ms 8608 KB Output is correct
84 Correct 25 ms 8592 KB Output is correct
85 Correct 28 ms 8536 KB Output is correct
86 Correct 30 ms 8788 KB Output is correct
87 Correct 29 ms 8788 KB Output is correct
88 Correct 30 ms 8788 KB Output is correct
89 Correct 30 ms 8688 KB Output is correct
90 Correct 31 ms 8728 KB Output is correct
91 Correct 61 ms 15736 KB Output is correct
92 Correct 57 ms 15744 KB Output is correct
93 Correct 64 ms 15700 KB Output is correct
94 Correct 61 ms 15736 KB Output is correct
95 Correct 60 ms 15724 KB Output is correct
96 Correct 59 ms 16676 KB Output is correct
97 Correct 55 ms 16604 KB Output is correct
98 Correct 52 ms 16596 KB Output is correct
99 Correct 55 ms 16616 KB Output is correct
100 Correct 57 ms 16684 KB Output is correct
101 Correct 55 ms 16596 KB Output is correct
102 Correct 61 ms 16604 KB Output is correct
103 Correct 58 ms 16616 KB Output is correct
104 Correct 55 ms 16596 KB Output is correct
105 Correct 54 ms 16696 KB Output is correct
106 Correct 52 ms 17104 KB Output is correct
107 Correct 50 ms 17000 KB Output is correct
108 Correct 54 ms 17112 KB Output is correct
109 Correct 52 ms 16984 KB Output is correct
110 Correct 57 ms 17024 KB Output is correct
111 Correct 51 ms 17044 KB Output is correct
112 Correct 52 ms 17100 KB Output is correct
113 Correct 58 ms 16984 KB Output is correct
114 Correct 58 ms 17108 KB Output is correct
115 Correct 51 ms 17104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 236 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 720 KB Output is correct
14 Correct 9 ms 4052 KB Output is correct
15 Correct 18 ms 7892 KB Output is correct
16 Correct 25 ms 13808 KB Output is correct
17 Correct 24 ms 13776 KB Output is correct
18 Correct 31 ms 13848 KB Output is correct
19 Correct 30 ms 13796 KB Output is correct
20 Correct 41 ms 13808 KB Output is correct
21 Correct 34 ms 13808 KB Output is correct
22 Correct 25 ms 13908 KB Output is correct
23 Correct 28 ms 13796 KB Output is correct
24 Correct 25 ms 13860 KB Output is correct
25 Correct 28 ms 13796 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 724 KB Output is correct
29 Correct 9 ms 4176 KB Output is correct
30 Correct 19 ms 8020 KB Output is correct
31 Correct 27 ms 14040 KB Output is correct
32 Correct 29 ms 13980 KB Output is correct
33 Correct 32 ms 13964 KB Output is correct
34 Correct 33 ms 13936 KB Output is correct
35 Correct 32 ms 13956 KB Output is correct
36 Correct 39 ms 13932 KB Output is correct
37 Correct 26 ms 13936 KB Output is correct
38 Correct 29 ms 13896 KB Output is correct
39 Correct 30 ms 13932 KB Output is correct
40 Correct 26 ms 13980 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 3 ms 728 KB Output is correct
45 Correct 2 ms 724 KB Output is correct
46 Correct 1 ms 724 KB Output is correct
47 Correct 4 ms 2260 KB Output is correct
48 Correct 5 ms 2260 KB Output is correct
49 Correct 10 ms 4308 KB Output is correct
50 Correct 19 ms 8248 KB Output is correct
51 Correct 32 ms 14364 KB Output is correct
52 Correct 33 ms 14296 KB Output is correct
53 Correct 35 ms 14372 KB Output is correct
54 Correct 32 ms 14284 KB Output is correct
55 Correct 29 ms 14376 KB Output is correct
56 Correct 30 ms 14320 KB Output is correct
57 Correct 35 ms 14296 KB Output is correct
58 Correct 32 ms 14316 KB Output is correct
59 Correct 30 ms 14300 KB Output is correct
60 Correct 29 ms 14364 KB Output is correct
61 Correct 37 ms 14312 KB Output is correct
62 Correct 28 ms 14328 KB Output is correct
63 Correct 28 ms 14284 KB Output is correct
64 Correct 41 ms 14372 KB Output is correct
65 Correct 29 ms 14328 KB Output is correct
66 Correct 35 ms 14300 KB Output is correct
67 Correct 31 ms 14316 KB Output is correct
68 Correct 31 ms 14376 KB Output is correct
69 Correct 36 ms 14320 KB Output is correct
70 Correct 30 ms 14316 KB Output is correct
71 Correct 6 ms 2376 KB Output is correct
72 Correct 9 ms 2388 KB Output is correct
73 Correct 5 ms 2360 KB Output is correct
74 Correct 8 ms 2392 KB Output is correct
75 Correct 8 ms 2260 KB Output is correct
76 Correct 11 ms 4480 KB Output is correct
77 Correct 11 ms 4432 KB Output is correct
78 Correct 12 ms 4408 KB Output is correct
79 Correct 14 ms 4440 KB Output is correct
80 Correct 16 ms 4412 KB Output is correct
81 Correct 28 ms 8588 KB Output is correct
82 Correct 25 ms 8548 KB Output is correct
83 Correct 25 ms 8608 KB Output is correct
84 Correct 25 ms 8592 KB Output is correct
85 Correct 28 ms 8536 KB Output is correct
86 Correct 30 ms 8788 KB Output is correct
87 Correct 29 ms 8788 KB Output is correct
88 Correct 30 ms 8788 KB Output is correct
89 Correct 30 ms 8688 KB Output is correct
90 Correct 31 ms 8728 KB Output is correct
91 Correct 61 ms 15736 KB Output is correct
92 Correct 57 ms 15744 KB Output is correct
93 Correct 64 ms 15700 KB Output is correct
94 Correct 61 ms 15736 KB Output is correct
95 Correct 60 ms 15724 KB Output is correct
96 Correct 59 ms 16676 KB Output is correct
97 Correct 55 ms 16604 KB Output is correct
98 Correct 52 ms 16596 KB Output is correct
99 Correct 55 ms 16616 KB Output is correct
100 Correct 57 ms 16684 KB Output is correct
101 Correct 55 ms 16596 KB Output is correct
102 Correct 61 ms 16604 KB Output is correct
103 Correct 58 ms 16616 KB Output is correct
104 Correct 55 ms 16596 KB Output is correct
105 Correct 54 ms 16696 KB Output is correct
106 Correct 52 ms 17104 KB Output is correct
107 Correct 50 ms 17000 KB Output is correct
108 Correct 54 ms 17112 KB Output is correct
109 Correct 52 ms 16984 KB Output is correct
110 Correct 57 ms 17024 KB Output is correct
111 Correct 51 ms 17044 KB Output is correct
112 Correct 52 ms 17100 KB Output is correct
113 Correct 58 ms 16984 KB Output is correct
114 Correct 58 ms 17108 KB Output is correct
115 Correct 51 ms 17104 KB Output is correct
116 Correct 125 ms 2624 KB Output is correct
117 Correct 125 ms 2604 KB Output is correct
118 Correct 438 ms 4264 KB Output is correct
119 Correct 575 ms 4388 KB Output is correct
120 Correct 450 ms 4400 KB Output is correct
121 Correct 1169 ms 6404 KB Output is correct
122 Correct 1266 ms 6468 KB Output is correct
123 Correct 3259 ms 10584 KB Output is correct
124 Correct 4925 ms 10780 KB Output is correct
125 Correct 6367 ms 10848 KB Output is correct
126 Correct 4783 ms 16440 KB Output is correct
127 Correct 4635 ms 16444 KB Output is correct
128 Correct 4632 ms 16488 KB Output is correct
129 Correct 4648 ms 16444 KB Output is correct
130 Correct 4696 ms 16432 KB Output is correct
131 Correct 5183 ms 16612 KB Output is correct
132 Correct 5348 ms 16616 KB Output is correct
133 Correct 5181 ms 16596 KB Output is correct
134 Correct 5111 ms 16604 KB Output is correct
135 Correct 5249 ms 16608 KB Output is correct
136 Correct 5303 ms 16604 KB Output is correct
137 Correct 5136 ms 16508 KB Output is correct
138 Correct 5432 ms 16604 KB Output is correct
139 Correct 5413 ms 16600 KB Output is correct
140 Correct 5545 ms 16604 KB Output is correct
141 Correct 6246 ms 16636 KB Output is correct
142 Correct 6102 ms 16644 KB Output is correct
143 Correct 6272 ms 16648 KB Output is correct
144 Correct 5940 ms 16648 KB Output is correct
145 Correct 6079 ms 16636 KB Output is correct
146 Execution timed out 7029 ms 16228 KB Time limit exceeded
147 Halted 0 ms 0 KB -