답안 #799686

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799686 2023-07-31T20:11:50 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 7368 KB
#include "bits/stdc++.h"
using namespace std;

#pragma GCC optimize "Ofast"
#pragma GCC target("avx2")

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;
int A[300007];
ll ans[50005];
int freq[300007];
ll tmp;
int 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 + 2) / 6;
    // return x * (x + 1) * (x + 1) / 2 - (2 * x + 1) * (x + 1) * x / 6;
}
void doStuff(ll x, ll y)
{
    ll len = x * y;

    // if both are in set
    // tmp += x * x * (totalLengthOfSubsequences(y) - y) + y * numberOfSubsequences(x);
    tmp += x * x * (totalLengthOfSubsequences(y) - y) + len * (x + 1) / 2;

    // if one is inside the other isn't
    // tmp += (n - len) * (x * y * (y + 1) / 2);
    tmp += (n - len) * (len * (y + 1) / 2);
    // if both are outside
    tmp += y * pref * (n - pref - len);

    pref += len;
    swap(pref, suff);
}
struct segtree
{
    int arr[1 << 20];
    int size;
    void init(int _size)
    {
        size = 1;
        while (size < _size)
            size *= 2;
        fill(arr, arr + 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
            doStuff(x - size, (arr[x] + 1) / 2);
            if (arr[x] > 1)
            {
                doStuff(x - size, arr[x] / 2);
                if (arr[x] & 1)
                    swap(pref, suff);
            }
        }
    }
};
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;
    for (int i = 0; i < N; ++i)
        cin >> A[i];

    vector<query> Qu(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);

    int lx = 0, rx = 0;
    fill(freq, freq + *max_element(A, A + N) + 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 = 0;

        seg.dfs(1);

        ans[q.idx] = tmp;
    }
    for (int i = 0; i < Q; ++i)
        cout << ans[i] << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 328 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 0 ms 324 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 7 ms 1836 KB Output is correct
5 Correct 13 ms 3296 KB Output is correct
6 Correct 20 ms 5736 KB Output is correct
7 Correct 20 ms 5740 KB Output is correct
8 Correct 20 ms 5716 KB Output is correct
9 Correct 19 ms 5704 KB Output is correct
10 Correct 21 ms 5668 KB Output is correct
11 Correct 20 ms 5716 KB Output is correct
12 Correct 20 ms 5704 KB Output is correct
13 Correct 20 ms 5732 KB Output is correct
14 Correct 20 ms 5716 KB Output is correct
15 Correct 21 ms 5700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 7 ms 1836 KB Output is correct
5 Correct 13 ms 3296 KB Output is correct
6 Correct 20 ms 5736 KB Output is correct
7 Correct 20 ms 5740 KB Output is correct
8 Correct 20 ms 5716 KB Output is correct
9 Correct 19 ms 5704 KB Output is correct
10 Correct 21 ms 5668 KB Output is correct
11 Correct 20 ms 5716 KB Output is correct
12 Correct 20 ms 5704 KB Output is correct
13 Correct 20 ms 5732 KB Output is correct
14 Correct 20 ms 5716 KB Output is correct
15 Correct 21 ms 5700 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 7 ms 1848 KB Output is correct
20 Correct 14 ms 3276 KB Output is correct
21 Correct 21 ms 5716 KB Output is correct
22 Correct 21 ms 5700 KB Output is correct
23 Correct 21 ms 5720 KB Output is correct
24 Correct 21 ms 5716 KB Output is correct
25 Correct 21 ms 5716 KB Output is correct
26 Correct 21 ms 5712 KB Output is correct
27 Correct 21 ms 5716 KB Output is correct
28 Correct 21 ms 5708 KB Output is correct
29 Correct 21 ms 5684 KB Output is correct
30 Correct 21 ms 5696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 7 ms 1836 KB Output is correct
5 Correct 13 ms 3296 KB Output is correct
6 Correct 20 ms 5736 KB Output is correct
7 Correct 20 ms 5740 KB Output is correct
8 Correct 20 ms 5716 KB Output is correct
9 Correct 19 ms 5704 KB Output is correct
10 Correct 21 ms 5668 KB Output is correct
11 Correct 20 ms 5716 KB Output is correct
12 Correct 20 ms 5704 KB Output is correct
13 Correct 20 ms 5732 KB Output is correct
14 Correct 20 ms 5716 KB Output is correct
15 Correct 21 ms 5700 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 7 ms 1848 KB Output is correct
20 Correct 14 ms 3276 KB Output is correct
21 Correct 21 ms 5716 KB Output is correct
22 Correct 21 ms 5700 KB Output is correct
23 Correct 21 ms 5720 KB Output is correct
24 Correct 21 ms 5716 KB Output is correct
25 Correct 21 ms 5716 KB Output is correct
26 Correct 21 ms 5712 KB Output is correct
27 Correct 21 ms 5716 KB Output is correct
28 Correct 21 ms 5708 KB Output is correct
29 Correct 21 ms 5684 KB Output is correct
30 Correct 21 ms 5696 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 472 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 4 ms 1140 KB Output is correct
38 Correct 5 ms 1076 KB Output is correct
39 Correct 8 ms 1780 KB Output is correct
40 Correct 16 ms 3224 KB Output is correct
41 Correct 25 ms 5716 KB Output is correct
42 Correct 25 ms 5720 KB Output is correct
43 Correct 24 ms 5608 KB Output is correct
44 Correct 26 ms 5684 KB Output is correct
45 Correct 25 ms 5648 KB Output is correct
46 Correct 25 ms 5620 KB Output is correct
47 Correct 25 ms 5688 KB Output is correct
48 Correct 25 ms 5716 KB Output is correct
49 Correct 25 ms 5600 KB Output is correct
50 Correct 25 ms 5712 KB Output is correct
51 Correct 24 ms 5708 KB Output is correct
52 Correct 24 ms 5672 KB Output is correct
53 Correct 24 ms 5664 KB Output is correct
54 Correct 24 ms 5612 KB Output is correct
55 Correct 24 ms 5716 KB Output is correct
56 Correct 25 ms 5716 KB Output is correct
57 Correct 24 ms 5700 KB Output is correct
58 Correct 24 ms 5632 KB Output is correct
59 Correct 25 ms 5740 KB Output is correct
60 Correct 25 ms 5708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 328 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 0 ms 324 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 7 ms 1836 KB Output is correct
15 Correct 13 ms 3296 KB Output is correct
16 Correct 20 ms 5736 KB Output is correct
17 Correct 20 ms 5740 KB Output is correct
18 Correct 20 ms 5716 KB Output is correct
19 Correct 19 ms 5704 KB Output is correct
20 Correct 21 ms 5668 KB Output is correct
21 Correct 20 ms 5716 KB Output is correct
22 Correct 20 ms 5704 KB Output is correct
23 Correct 20 ms 5732 KB Output is correct
24 Correct 20 ms 5716 KB Output is correct
25 Correct 21 ms 5700 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 7 ms 1848 KB Output is correct
30 Correct 14 ms 3276 KB Output is correct
31 Correct 21 ms 5716 KB Output is correct
32 Correct 21 ms 5700 KB Output is correct
33 Correct 21 ms 5720 KB Output is correct
34 Correct 21 ms 5716 KB Output is correct
35 Correct 21 ms 5716 KB Output is correct
36 Correct 21 ms 5712 KB Output is correct
37 Correct 21 ms 5716 KB Output is correct
38 Correct 21 ms 5708 KB Output is correct
39 Correct 21 ms 5684 KB Output is correct
40 Correct 21 ms 5696 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 472 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 4 ms 1140 KB Output is correct
48 Correct 5 ms 1076 KB Output is correct
49 Correct 8 ms 1780 KB Output is correct
50 Correct 16 ms 3224 KB Output is correct
51 Correct 25 ms 5716 KB Output is correct
52 Correct 25 ms 5720 KB Output is correct
53 Correct 24 ms 5608 KB Output is correct
54 Correct 26 ms 5684 KB Output is correct
55 Correct 25 ms 5648 KB Output is correct
56 Correct 25 ms 5620 KB Output is correct
57 Correct 25 ms 5688 KB Output is correct
58 Correct 25 ms 5716 KB Output is correct
59 Correct 25 ms 5600 KB Output is correct
60 Correct 25 ms 5712 KB Output is correct
61 Correct 24 ms 5708 KB Output is correct
62 Correct 24 ms 5672 KB Output is correct
63 Correct 24 ms 5664 KB Output is correct
64 Correct 24 ms 5612 KB Output is correct
65 Correct 24 ms 5716 KB Output is correct
66 Correct 25 ms 5716 KB Output is correct
67 Correct 24 ms 5700 KB Output is correct
68 Correct 24 ms 5632 KB Output is correct
69 Correct 25 ms 5740 KB Output is correct
70 Correct 25 ms 5708 KB Output is correct
71 Correct 4 ms 1108 KB Output is correct
72 Correct 4 ms 1196 KB Output is correct
73 Correct 4 ms 1140 KB Output is correct
74 Correct 4 ms 1108 KB Output is correct
75 Correct 4 ms 1108 KB Output is correct
76 Correct 9 ms 1876 KB Output is correct
77 Correct 9 ms 1812 KB Output is correct
78 Correct 9 ms 1876 KB Output is correct
79 Correct 11 ms 1876 KB Output is correct
80 Correct 9 ms 1892 KB Output is correct
81 Correct 22 ms 3308 KB Output is correct
82 Correct 22 ms 3300 KB Output is correct
83 Correct 22 ms 3268 KB Output is correct
84 Correct 22 ms 3236 KB Output is correct
85 Correct 21 ms 3272 KB Output is correct
86 Correct 25 ms 3284 KB Output is correct
87 Correct 25 ms 3324 KB Output is correct
88 Correct 25 ms 3272 KB Output is correct
89 Correct 25 ms 3316 KB Output is correct
90 Correct 25 ms 3380 KB Output is correct
91 Correct 46 ms 6108 KB Output is correct
92 Correct 45 ms 6104 KB Output is correct
93 Correct 45 ms 6060 KB Output is correct
94 Correct 45 ms 6008 KB Output is correct
95 Correct 45 ms 6116 KB Output is correct
96 Correct 43 ms 6496 KB Output is correct
97 Correct 44 ms 6424 KB Output is correct
98 Correct 42 ms 6428 KB Output is correct
99 Correct 42 ms 6472 KB Output is correct
100 Correct 42 ms 6488 KB Output is correct
101 Correct 43 ms 6476 KB Output is correct
102 Correct 42 ms 6492 KB Output is correct
103 Correct 42 ms 6500 KB Output is correct
104 Correct 42 ms 6436 KB Output is correct
105 Correct 42 ms 6516 KB Output is correct
106 Correct 39 ms 6692 KB Output is correct
107 Correct 39 ms 6616 KB Output is correct
108 Correct 42 ms 6728 KB Output is correct
109 Correct 40 ms 6612 KB Output is correct
110 Correct 40 ms 6704 KB Output is correct
111 Correct 39 ms 6680 KB Output is correct
112 Correct 40 ms 6604 KB Output is correct
113 Correct 39 ms 6604 KB Output is correct
114 Correct 39 ms 6652 KB Output is correct
115 Correct 40 ms 6612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 328 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 0 ms 324 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 7 ms 1836 KB Output is correct
15 Correct 13 ms 3296 KB Output is correct
16 Correct 20 ms 5736 KB Output is correct
17 Correct 20 ms 5740 KB Output is correct
18 Correct 20 ms 5716 KB Output is correct
19 Correct 19 ms 5704 KB Output is correct
20 Correct 21 ms 5668 KB Output is correct
21 Correct 20 ms 5716 KB Output is correct
22 Correct 20 ms 5704 KB Output is correct
23 Correct 20 ms 5732 KB Output is correct
24 Correct 20 ms 5716 KB Output is correct
25 Correct 21 ms 5700 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 7 ms 1848 KB Output is correct
30 Correct 14 ms 3276 KB Output is correct
31 Correct 21 ms 5716 KB Output is correct
32 Correct 21 ms 5700 KB Output is correct
33 Correct 21 ms 5720 KB Output is correct
34 Correct 21 ms 5716 KB Output is correct
35 Correct 21 ms 5716 KB Output is correct
36 Correct 21 ms 5712 KB Output is correct
37 Correct 21 ms 5716 KB Output is correct
38 Correct 21 ms 5708 KB Output is correct
39 Correct 21 ms 5684 KB Output is correct
40 Correct 21 ms 5696 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 472 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 4 ms 1140 KB Output is correct
48 Correct 5 ms 1076 KB Output is correct
49 Correct 8 ms 1780 KB Output is correct
50 Correct 16 ms 3224 KB Output is correct
51 Correct 25 ms 5716 KB Output is correct
52 Correct 25 ms 5720 KB Output is correct
53 Correct 24 ms 5608 KB Output is correct
54 Correct 26 ms 5684 KB Output is correct
55 Correct 25 ms 5648 KB Output is correct
56 Correct 25 ms 5620 KB Output is correct
57 Correct 25 ms 5688 KB Output is correct
58 Correct 25 ms 5716 KB Output is correct
59 Correct 25 ms 5600 KB Output is correct
60 Correct 25 ms 5712 KB Output is correct
61 Correct 24 ms 5708 KB Output is correct
62 Correct 24 ms 5672 KB Output is correct
63 Correct 24 ms 5664 KB Output is correct
64 Correct 24 ms 5612 KB Output is correct
65 Correct 24 ms 5716 KB Output is correct
66 Correct 25 ms 5716 KB Output is correct
67 Correct 24 ms 5700 KB Output is correct
68 Correct 24 ms 5632 KB Output is correct
69 Correct 25 ms 5740 KB Output is correct
70 Correct 25 ms 5708 KB Output is correct
71 Correct 4 ms 1108 KB Output is correct
72 Correct 4 ms 1196 KB Output is correct
73 Correct 4 ms 1140 KB Output is correct
74 Correct 4 ms 1108 KB Output is correct
75 Correct 4 ms 1108 KB Output is correct
76 Correct 9 ms 1876 KB Output is correct
77 Correct 9 ms 1812 KB Output is correct
78 Correct 9 ms 1876 KB Output is correct
79 Correct 11 ms 1876 KB Output is correct
80 Correct 9 ms 1892 KB Output is correct
81 Correct 22 ms 3308 KB Output is correct
82 Correct 22 ms 3300 KB Output is correct
83 Correct 22 ms 3268 KB Output is correct
84 Correct 22 ms 3236 KB Output is correct
85 Correct 21 ms 3272 KB Output is correct
86 Correct 25 ms 3284 KB Output is correct
87 Correct 25 ms 3324 KB Output is correct
88 Correct 25 ms 3272 KB Output is correct
89 Correct 25 ms 3316 KB Output is correct
90 Correct 25 ms 3380 KB Output is correct
91 Correct 46 ms 6108 KB Output is correct
92 Correct 45 ms 6104 KB Output is correct
93 Correct 45 ms 6060 KB Output is correct
94 Correct 45 ms 6008 KB Output is correct
95 Correct 45 ms 6116 KB Output is correct
96 Correct 43 ms 6496 KB Output is correct
97 Correct 44 ms 6424 KB Output is correct
98 Correct 42 ms 6428 KB Output is correct
99 Correct 42 ms 6472 KB Output is correct
100 Correct 42 ms 6488 KB Output is correct
101 Correct 43 ms 6476 KB Output is correct
102 Correct 42 ms 6492 KB Output is correct
103 Correct 42 ms 6500 KB Output is correct
104 Correct 42 ms 6436 KB Output is correct
105 Correct 42 ms 6516 KB Output is correct
106 Correct 39 ms 6692 KB Output is correct
107 Correct 39 ms 6616 KB Output is correct
108 Correct 42 ms 6728 KB Output is correct
109 Correct 40 ms 6612 KB Output is correct
110 Correct 40 ms 6704 KB Output is correct
111 Correct 39 ms 6680 KB Output is correct
112 Correct 40 ms 6604 KB Output is correct
113 Correct 39 ms 6604 KB Output is correct
114 Correct 39 ms 6652 KB Output is correct
115 Correct 40 ms 6612 KB Output is correct
116 Correct 104 ms 1996 KB Output is correct
117 Correct 107 ms 2004 KB Output is correct
118 Correct 375 ms 2664 KB Output is correct
119 Correct 367 ms 2664 KB Output is correct
120 Correct 333 ms 2672 KB Output is correct
121 Correct 935 ms 3376 KB Output is correct
122 Correct 1008 ms 3384 KB Output is correct
123 Correct 2736 ms 4928 KB Output is correct
124 Correct 4431 ms 4940 KB Output is correct
125 Correct 5335 ms 4964 KB Output is correct
126 Correct 4292 ms 7244 KB Output is correct
127 Correct 4330 ms 7224 KB Output is correct
128 Correct 4337 ms 7224 KB Output is correct
129 Correct 4316 ms 7224 KB Output is correct
130 Correct 4349 ms 7224 KB Output is correct
131 Correct 4758 ms 7348 KB Output is correct
132 Correct 4863 ms 7352 KB Output is correct
133 Correct 4790 ms 7344 KB Output is correct
134 Correct 4897 ms 7352 KB Output is correct
135 Correct 4789 ms 7344 KB Output is correct
136 Correct 4854 ms 7320 KB Output is correct
137 Correct 4786 ms 7320 KB Output is correct
138 Correct 4827 ms 7244 KB Output is correct
139 Correct 4793 ms 7352 KB Output is correct
140 Correct 4815 ms 7344 KB Output is correct
141 Correct 5820 ms 7352 KB Output is correct
142 Correct 5763 ms 7368 KB Output is correct
143 Correct 5801 ms 7352 KB Output is correct
144 Correct 5809 ms 7356 KB Output is correct
145 Correct 5814 ms 7360 KB Output is correct
146 Execution timed out 7055 ms 6960 KB Time limit exceeded
147 Halted 0 ms 0 KB -