답안 #799421

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

#pragma GCC optimize("O3,unroll-loops")
#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;
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 totalLengthOfSubsequences(ll x)
{
    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 -= 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);

    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 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 316 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 596 KB Output is correct
4 Correct 7 ms 3284 KB Output is correct
5 Correct 14 ms 6356 KB Output is correct
6 Correct 23 ms 11476 KB Output is correct
7 Correct 22 ms 11480 KB Output is correct
8 Correct 23 ms 11472 KB Output is correct
9 Correct 22 ms 11464 KB Output is correct
10 Correct 22 ms 11488 KB Output is correct
11 Correct 22 ms 11468 KB Output is correct
12 Correct 23 ms 11484 KB Output is correct
13 Correct 22 ms 11476 KB Output is correct
14 Correct 23 ms 11520 KB Output is correct
15 Correct 22 ms 11504 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 596 KB Output is correct
4 Correct 7 ms 3284 KB Output is correct
5 Correct 14 ms 6356 KB Output is correct
6 Correct 23 ms 11476 KB Output is correct
7 Correct 22 ms 11480 KB Output is correct
8 Correct 23 ms 11472 KB Output is correct
9 Correct 22 ms 11464 KB Output is correct
10 Correct 22 ms 11488 KB Output is correct
11 Correct 22 ms 11468 KB Output is correct
12 Correct 23 ms 11484 KB Output is correct
13 Correct 22 ms 11476 KB Output is correct
14 Correct 23 ms 11520 KB Output is correct
15 Correct 22 ms 11504 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 584 KB Output is correct
19 Correct 8 ms 3404 KB Output is correct
20 Correct 15 ms 6444 KB Output is correct
21 Correct 23 ms 11596 KB Output is correct
22 Correct 23 ms 11628 KB Output is correct
23 Correct 24 ms 11620 KB Output is correct
24 Correct 29 ms 11588 KB Output is correct
25 Correct 23 ms 11648 KB Output is correct
26 Correct 29 ms 11620 KB Output is correct
27 Correct 23 ms 11636 KB Output is correct
28 Correct 23 ms 11492 KB Output is correct
29 Correct 23 ms 11604 KB Output is correct
30 Correct 23 ms 11684 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 596 KB Output is correct
4 Correct 7 ms 3284 KB Output is correct
5 Correct 14 ms 6356 KB Output is correct
6 Correct 23 ms 11476 KB Output is correct
7 Correct 22 ms 11480 KB Output is correct
8 Correct 23 ms 11472 KB Output is correct
9 Correct 22 ms 11464 KB Output is correct
10 Correct 22 ms 11488 KB Output is correct
11 Correct 22 ms 11468 KB Output is correct
12 Correct 23 ms 11484 KB Output is correct
13 Correct 22 ms 11476 KB Output is correct
14 Correct 23 ms 11520 KB Output is correct
15 Correct 22 ms 11504 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 584 KB Output is correct
19 Correct 8 ms 3404 KB Output is correct
20 Correct 15 ms 6444 KB Output is correct
21 Correct 23 ms 11596 KB Output is correct
22 Correct 23 ms 11628 KB Output is correct
23 Correct 24 ms 11620 KB Output is correct
24 Correct 29 ms 11588 KB Output is correct
25 Correct 23 ms 11648 KB Output is correct
26 Correct 29 ms 11620 KB Output is correct
27 Correct 23 ms 11636 KB Output is correct
28 Correct 23 ms 11492 KB Output is correct
29 Correct 23 ms 11604 KB Output is correct
30 Correct 23 ms 11684 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 324 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 1 ms 716 KB Output is correct
37 Correct 4 ms 1876 KB Output is correct
38 Correct 4 ms 1876 KB Output is correct
39 Correct 9 ms 3532 KB Output is correct
40 Correct 17 ms 6740 KB Output is correct
41 Correct 31 ms 12024 KB Output is correct
42 Correct 27 ms 12008 KB Output is correct
43 Correct 27 ms 12012 KB Output is correct
44 Correct 26 ms 12004 KB Output is correct
45 Correct 28 ms 12012 KB Output is correct
46 Correct 27 ms 12012 KB Output is correct
47 Correct 27 ms 12004 KB Output is correct
48 Correct 27 ms 12004 KB Output is correct
49 Correct 27 ms 11988 KB Output is correct
50 Correct 27 ms 12036 KB Output is correct
51 Correct 27 ms 11988 KB Output is correct
52 Correct 26 ms 11992 KB Output is correct
53 Correct 27 ms 12048 KB Output is correct
54 Correct 28 ms 11972 KB Output is correct
55 Correct 28 ms 12012 KB Output is correct
56 Correct 27 ms 11988 KB Output is correct
57 Correct 27 ms 11932 KB Output is correct
58 Correct 27 ms 12008 KB Output is correct
59 Correct 27 ms 12000 KB Output is correct
60 Correct 26 ms 11996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 316 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 596 KB Output is correct
14 Correct 7 ms 3284 KB Output is correct
15 Correct 14 ms 6356 KB Output is correct
16 Correct 23 ms 11476 KB Output is correct
17 Correct 22 ms 11480 KB Output is correct
18 Correct 23 ms 11472 KB Output is correct
19 Correct 22 ms 11464 KB Output is correct
20 Correct 22 ms 11488 KB Output is correct
21 Correct 22 ms 11468 KB Output is correct
22 Correct 23 ms 11484 KB Output is correct
23 Correct 22 ms 11476 KB Output is correct
24 Correct 23 ms 11520 KB Output is correct
25 Correct 22 ms 11504 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 1 ms 584 KB Output is correct
29 Correct 8 ms 3404 KB Output is correct
30 Correct 15 ms 6444 KB Output is correct
31 Correct 23 ms 11596 KB Output is correct
32 Correct 23 ms 11628 KB Output is correct
33 Correct 24 ms 11620 KB Output is correct
34 Correct 29 ms 11588 KB Output is correct
35 Correct 23 ms 11648 KB Output is correct
36 Correct 29 ms 11620 KB Output is correct
37 Correct 23 ms 11636 KB Output is correct
38 Correct 23 ms 11492 KB Output is correct
39 Correct 23 ms 11604 KB Output is correct
40 Correct 23 ms 11684 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 324 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 596 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 1 ms 716 KB Output is correct
47 Correct 4 ms 1876 KB Output is correct
48 Correct 4 ms 1876 KB Output is correct
49 Correct 9 ms 3532 KB Output is correct
50 Correct 17 ms 6740 KB Output is correct
51 Correct 31 ms 12024 KB Output is correct
52 Correct 27 ms 12008 KB Output is correct
53 Correct 27 ms 12012 KB Output is correct
54 Correct 26 ms 12004 KB Output is correct
55 Correct 28 ms 12012 KB Output is correct
56 Correct 27 ms 12012 KB Output is correct
57 Correct 27 ms 12004 KB Output is correct
58 Correct 27 ms 12004 KB Output is correct
59 Correct 27 ms 11988 KB Output is correct
60 Correct 27 ms 12036 KB Output is correct
61 Correct 27 ms 11988 KB Output is correct
62 Correct 26 ms 11992 KB Output is correct
63 Correct 27 ms 12048 KB Output is correct
64 Correct 28 ms 11972 KB Output is correct
65 Correct 28 ms 12012 KB Output is correct
66 Correct 27 ms 11988 KB Output is correct
67 Correct 27 ms 11932 KB Output is correct
68 Correct 27 ms 12008 KB Output is correct
69 Correct 27 ms 12000 KB Output is correct
70 Correct 26 ms 11996 KB Output is correct
71 Correct 5 ms 1876 KB Output is correct
72 Correct 5 ms 1864 KB Output is correct
73 Correct 5 ms 1876 KB Output is correct
74 Correct 5 ms 1868 KB Output is correct
75 Correct 5 ms 1876 KB Output is correct
76 Correct 10 ms 3656 KB Output is correct
77 Correct 11 ms 3680 KB Output is correct
78 Correct 10 ms 3668 KB Output is correct
79 Correct 10 ms 3668 KB Output is correct
80 Correct 10 ms 3660 KB Output is correct
81 Correct 24 ms 7008 KB Output is correct
82 Correct 24 ms 6996 KB Output is correct
83 Correct 24 ms 7128 KB Output is correct
84 Correct 24 ms 6948 KB Output is correct
85 Correct 24 ms 7004 KB Output is correct
86 Correct 29 ms 7132 KB Output is correct
87 Correct 29 ms 7124 KB Output is correct
88 Correct 30 ms 7180 KB Output is correct
89 Correct 29 ms 7180 KB Output is correct
90 Correct 32 ms 7216 KB Output is correct
91 Correct 59 ms 13248 KB Output is correct
92 Correct 64 ms 13256 KB Output is correct
93 Correct 57 ms 13196 KB Output is correct
94 Correct 58 ms 13136 KB Output is correct
95 Correct 58 ms 13032 KB Output is correct
96 Correct 54 ms 13824 KB Output is correct
97 Correct 56 ms 13900 KB Output is correct
98 Correct 54 ms 13792 KB Output is correct
99 Correct 54 ms 13812 KB Output is correct
100 Correct 54 ms 13640 KB Output is correct
101 Correct 55 ms 13720 KB Output is correct
102 Correct 55 ms 13648 KB Output is correct
103 Correct 61 ms 13592 KB Output is correct
104 Correct 60 ms 13720 KB Output is correct
105 Correct 54 ms 13692 KB Output is correct
106 Correct 50 ms 13900 KB Output is correct
107 Correct 50 ms 13988 KB Output is correct
108 Correct 51 ms 13824 KB Output is correct
109 Correct 52 ms 13780 KB Output is correct
110 Correct 50 ms 13596 KB Output is correct
111 Correct 50 ms 13724 KB Output is correct
112 Correct 51 ms 13620 KB Output is correct
113 Correct 51 ms 13556 KB Output is correct
114 Correct 50 ms 13516 KB Output is correct
115 Correct 50 ms 13524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 316 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 596 KB Output is correct
14 Correct 7 ms 3284 KB Output is correct
15 Correct 14 ms 6356 KB Output is correct
16 Correct 23 ms 11476 KB Output is correct
17 Correct 22 ms 11480 KB Output is correct
18 Correct 23 ms 11472 KB Output is correct
19 Correct 22 ms 11464 KB Output is correct
20 Correct 22 ms 11488 KB Output is correct
21 Correct 22 ms 11468 KB Output is correct
22 Correct 23 ms 11484 KB Output is correct
23 Correct 22 ms 11476 KB Output is correct
24 Correct 23 ms 11520 KB Output is correct
25 Correct 22 ms 11504 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 1 ms 584 KB Output is correct
29 Correct 8 ms 3404 KB Output is correct
30 Correct 15 ms 6444 KB Output is correct
31 Correct 23 ms 11596 KB Output is correct
32 Correct 23 ms 11628 KB Output is correct
33 Correct 24 ms 11620 KB Output is correct
34 Correct 29 ms 11588 KB Output is correct
35 Correct 23 ms 11648 KB Output is correct
36 Correct 29 ms 11620 KB Output is correct
37 Correct 23 ms 11636 KB Output is correct
38 Correct 23 ms 11492 KB Output is correct
39 Correct 23 ms 11604 KB Output is correct
40 Correct 23 ms 11684 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 324 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 596 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 1 ms 716 KB Output is correct
47 Correct 4 ms 1876 KB Output is correct
48 Correct 4 ms 1876 KB Output is correct
49 Correct 9 ms 3532 KB Output is correct
50 Correct 17 ms 6740 KB Output is correct
51 Correct 31 ms 12024 KB Output is correct
52 Correct 27 ms 12008 KB Output is correct
53 Correct 27 ms 12012 KB Output is correct
54 Correct 26 ms 12004 KB Output is correct
55 Correct 28 ms 12012 KB Output is correct
56 Correct 27 ms 12012 KB Output is correct
57 Correct 27 ms 12004 KB Output is correct
58 Correct 27 ms 12004 KB Output is correct
59 Correct 27 ms 11988 KB Output is correct
60 Correct 27 ms 12036 KB Output is correct
61 Correct 27 ms 11988 KB Output is correct
62 Correct 26 ms 11992 KB Output is correct
63 Correct 27 ms 12048 KB Output is correct
64 Correct 28 ms 11972 KB Output is correct
65 Correct 28 ms 12012 KB Output is correct
66 Correct 27 ms 11988 KB Output is correct
67 Correct 27 ms 11932 KB Output is correct
68 Correct 27 ms 12008 KB Output is correct
69 Correct 27 ms 12000 KB Output is correct
70 Correct 26 ms 11996 KB Output is correct
71 Correct 5 ms 1876 KB Output is correct
72 Correct 5 ms 1864 KB Output is correct
73 Correct 5 ms 1876 KB Output is correct
74 Correct 5 ms 1868 KB Output is correct
75 Correct 5 ms 1876 KB Output is correct
76 Correct 10 ms 3656 KB Output is correct
77 Correct 11 ms 3680 KB Output is correct
78 Correct 10 ms 3668 KB Output is correct
79 Correct 10 ms 3668 KB Output is correct
80 Correct 10 ms 3660 KB Output is correct
81 Correct 24 ms 7008 KB Output is correct
82 Correct 24 ms 6996 KB Output is correct
83 Correct 24 ms 7128 KB Output is correct
84 Correct 24 ms 6948 KB Output is correct
85 Correct 24 ms 7004 KB Output is correct
86 Correct 29 ms 7132 KB Output is correct
87 Correct 29 ms 7124 KB Output is correct
88 Correct 30 ms 7180 KB Output is correct
89 Correct 29 ms 7180 KB Output is correct
90 Correct 32 ms 7216 KB Output is correct
91 Correct 59 ms 13248 KB Output is correct
92 Correct 64 ms 13256 KB Output is correct
93 Correct 57 ms 13196 KB Output is correct
94 Correct 58 ms 13136 KB Output is correct
95 Correct 58 ms 13032 KB Output is correct
96 Correct 54 ms 13824 KB Output is correct
97 Correct 56 ms 13900 KB Output is correct
98 Correct 54 ms 13792 KB Output is correct
99 Correct 54 ms 13812 KB Output is correct
100 Correct 54 ms 13640 KB Output is correct
101 Correct 55 ms 13720 KB Output is correct
102 Correct 55 ms 13648 KB Output is correct
103 Correct 61 ms 13592 KB Output is correct
104 Correct 60 ms 13720 KB Output is correct
105 Correct 54 ms 13692 KB Output is correct
106 Correct 50 ms 13900 KB Output is correct
107 Correct 50 ms 13988 KB Output is correct
108 Correct 51 ms 13824 KB Output is correct
109 Correct 52 ms 13780 KB Output is correct
110 Correct 50 ms 13596 KB Output is correct
111 Correct 50 ms 13724 KB Output is correct
112 Correct 51 ms 13620 KB Output is correct
113 Correct 51 ms 13556 KB Output is correct
114 Correct 50 ms 13516 KB Output is correct
115 Correct 50 ms 13524 KB Output is correct
116 Correct 114 ms 2536 KB Output is correct
117 Correct 130 ms 2540 KB Output is correct
118 Correct 401 ms 3876 KB Output is correct
119 Correct 446 ms 3744 KB Output is correct
120 Correct 482 ms 3660 KB Output is correct
121 Correct 1121 ms 5040 KB Output is correct
122 Correct 1260 ms 5048 KB Output is correct
123 Correct 2891 ms 7872 KB Output is correct
124 Correct 4941 ms 7648 KB Output is correct
125 Correct 6364 ms 7688 KB Output is correct
126 Correct 4512 ms 12364 KB Output is correct
127 Correct 4432 ms 12368 KB Output is correct
128 Correct 4397 ms 12368 KB Output is correct
129 Correct 4384 ms 12368 KB Output is correct
130 Correct 4544 ms 12376 KB Output is correct
131 Correct 4864 ms 12492 KB Output is correct
132 Correct 4855 ms 12492 KB Output is correct
133 Correct 4884 ms 12500 KB Output is correct
134 Correct 4839 ms 12500 KB Output is correct
135 Correct 4831 ms 12496 KB Output is correct
136 Correct 4867 ms 12388 KB Output is correct
137 Correct 4825 ms 12496 KB Output is correct
138 Correct 4940 ms 12496 KB Output is correct
139 Correct 5082 ms 12504 KB Output is correct
140 Correct 5080 ms 12500 KB Output is correct
141 Correct 5981 ms 12504 KB Output is correct
142 Correct 6063 ms 12504 KB Output is correct
143 Correct 6124 ms 12500 KB Output is correct
144 Correct 5985 ms 12500 KB Output is correct
145 Correct 6058 ms 12504 KB Output is correct
146 Execution timed out 7029 ms 11836 KB Time limit exceeded
147 Halted 0 ms 0 KB -