답안 #492939

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
492939 2021-12-09T19:53:06 Z blue Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 14716 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;

const int mx = 300'000;
const int mxQ = 50'000;
const int rt = 500;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
#define sz(x) (int(x.size()))

int N, Q;
vi a(1+mx);

vi l(1+mxQ), r(1+mxQ);
vll ans(1+mxQ);

int L = 1;
int R = 0;

vll ct(1+mx, 0);
vll freq(1+mx, 0);

vector<ll> sizes;
vector<bool> occ(1+mx, 0);

ll get_ans()
{
    vector<ll> sizes2;
    for(ll s: sizes)
    {
        if(occ[s]) continue;
        if(freq[s] == 0) continue;
        sizes2.push_back(s);
        occ[s] = 1;
    }
    sizes = sizes2;
    for(ll s: sizes) occ[s] = 0;

    sort(sizes.begin(), sizes.end(), [] (int u, int v)
    {
        return u > v;
    });

    deque< pair<ll, ll> > ord;
    int z = 0;
    for(ll s: sizes)
    {
        if(freq[s] % 2 == 1)
        {
            if(z) ord.push_back({s, 1});
            else ord.push_front({s, 1});
            z = !z;
        }

        if(freq[s]/2)
        {
            ord.push_back({s, freq[s]/2});
            ord.push_front({s, freq[s]/2});
        }
    }

    vll prefsum(1, 0);
    for(auto o: ord)
    {
        for(int y = 1; y <= o.second; y++)
        {
            prefsum.push_back(prefsum.back() + o.first);
        }
    }

    ll ans = 0;

    int P = sz(prefsum) - 1;

    for(int i = 1; i <= P; i++)
    {
        ans += ((prefsum[i] - prefsum[i-1])*(prefsum[i] - prefsum[i-1] + 1))/2;
        ans += (prefsum[i] - prefsum[i-1])*prefsum[i-1];
        ans += (prefsum[i] - prefsum[i-1])*(prefsum[P] - prefsum[i]);
        ans += (prefsum[P] - prefsum[i])*prefsum[i-1];
    }

    return ans;
}

void add_species(int a)
{
    freq[ct[a]]--;
    ct[a]++;
    freq[ct[a]]++;
    sizes.push_back(ct[a]);
}

void remove_species(int a)
{
    freq[ct[a]]--;
    ct[a]--;
    freq[ct[a]]++;
    sizes.push_back(ct[a]);
}

void expand_right()
{
    R++;
    add_species(a[R]);
}

void expand_left()
{
    L--;
    add_species(a[L]);
}

void contract_right()
{
    remove_species(a[R]);
    R--;
}

void contract_left()
{
    remove_species(a[L]);
    L++;
}

int main()
{
    cin >> N >> Q;
    for(int i = 1; i <= N; i++) cin >> a[i];
    for(int j = 1; j <= Q; j++) cin >> l[j] >> r[j];

    vi I(Q);
    for(int j = 0; j < Q; j++) I[j] = j+1;
    sort(I.begin(), I.end(), [] (int u, int v)
    {
        if(l[u]/rt != l[v]/rt) return l[u]/rt < l[v]/rt;
        else return r[u] < r[v];
    });

    freq[0] = mx;

    for(int q: I)
    {
        while(R < r[q]) expand_right();
        while(L > l[q]) expand_left();
        while(R > r[q]) contract_right();
        while(L < l[q]) contract_left();
        ans[q] = get_ans();
    }

    for(int q = 1; q <= Q; q++) cout << ans[q] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6988 KB Output is correct
2 Correct 3 ms 6988 KB Output is correct
3 Correct 3 ms 6988 KB Output is correct
4 Correct 3 ms 6988 KB Output is correct
5 Correct 3 ms 6988 KB Output is correct
6 Correct 3 ms 6988 KB Output is correct
7 Correct 2 ms 6988 KB Output is correct
8 Correct 3 ms 6988 KB Output is correct
9 Correct 3 ms 6988 KB Output is correct
10 Correct 3 ms 6988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6988 KB Output is correct
2 Correct 3 ms 6988 KB Output is correct
3 Correct 4 ms 7116 KB Output is correct
4 Correct 17 ms 8140 KB Output is correct
5 Correct 33 ms 9156 KB Output is correct
6 Correct 49 ms 11180 KB Output is correct
7 Correct 49 ms 11180 KB Output is correct
8 Correct 46 ms 11200 KB Output is correct
9 Correct 46 ms 11192 KB Output is correct
10 Correct 47 ms 11176 KB Output is correct
11 Correct 47 ms 11176 KB Output is correct
12 Correct 48 ms 11180 KB Output is correct
13 Correct 48 ms 11196 KB Output is correct
14 Correct 46 ms 11180 KB Output is correct
15 Correct 47 ms 11180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6988 KB Output is correct
2 Correct 3 ms 6988 KB Output is correct
3 Correct 4 ms 7116 KB Output is correct
4 Correct 17 ms 8140 KB Output is correct
5 Correct 33 ms 9156 KB Output is correct
6 Correct 49 ms 11180 KB Output is correct
7 Correct 49 ms 11180 KB Output is correct
8 Correct 46 ms 11200 KB Output is correct
9 Correct 46 ms 11192 KB Output is correct
10 Correct 47 ms 11176 KB Output is correct
11 Correct 47 ms 11176 KB Output is correct
12 Correct 48 ms 11180 KB Output is correct
13 Correct 48 ms 11196 KB Output is correct
14 Correct 46 ms 11180 KB Output is correct
15 Correct 47 ms 11180 KB Output is correct
16 Correct 3 ms 6988 KB Output is correct
17 Correct 3 ms 6988 KB Output is correct
18 Correct 4 ms 7116 KB Output is correct
19 Correct 19 ms 8132 KB Output is correct
20 Correct 35 ms 9144 KB Output is correct
21 Correct 54 ms 11192 KB Output is correct
22 Correct 53 ms 11184 KB Output is correct
23 Correct 52 ms 11180 KB Output is correct
24 Correct 55 ms 11192 KB Output is correct
25 Correct 52 ms 11192 KB Output is correct
26 Correct 58 ms 11176 KB Output is correct
27 Correct 54 ms 11196 KB Output is correct
28 Correct 51 ms 11180 KB Output is correct
29 Correct 51 ms 11184 KB Output is correct
30 Correct 53 ms 11184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6988 KB Output is correct
2 Correct 3 ms 6988 KB Output is correct
3 Correct 4 ms 7116 KB Output is correct
4 Correct 17 ms 8140 KB Output is correct
5 Correct 33 ms 9156 KB Output is correct
6 Correct 49 ms 11180 KB Output is correct
7 Correct 49 ms 11180 KB Output is correct
8 Correct 46 ms 11200 KB Output is correct
9 Correct 46 ms 11192 KB Output is correct
10 Correct 47 ms 11176 KB Output is correct
11 Correct 47 ms 11176 KB Output is correct
12 Correct 48 ms 11180 KB Output is correct
13 Correct 48 ms 11196 KB Output is correct
14 Correct 46 ms 11180 KB Output is correct
15 Correct 47 ms 11180 KB Output is correct
16 Correct 3 ms 6988 KB Output is correct
17 Correct 3 ms 6988 KB Output is correct
18 Correct 4 ms 7116 KB Output is correct
19 Correct 19 ms 8132 KB Output is correct
20 Correct 35 ms 9144 KB Output is correct
21 Correct 54 ms 11192 KB Output is correct
22 Correct 53 ms 11184 KB Output is correct
23 Correct 52 ms 11180 KB Output is correct
24 Correct 55 ms 11192 KB Output is correct
25 Correct 52 ms 11192 KB Output is correct
26 Correct 58 ms 11176 KB Output is correct
27 Correct 54 ms 11196 KB Output is correct
28 Correct 51 ms 11180 KB Output is correct
29 Correct 51 ms 11184 KB Output is correct
30 Correct 53 ms 11184 KB Output is correct
31 Correct 3 ms 6988 KB Output is correct
32 Correct 3 ms 6988 KB Output is correct
33 Correct 3 ms 6988 KB Output is correct
34 Correct 4 ms 7116 KB Output is correct
35 Correct 5 ms 7116 KB Output is correct
36 Correct 5 ms 7116 KB Output is correct
37 Correct 14 ms 7628 KB Output is correct
38 Correct 13 ms 7624 KB Output is correct
39 Correct 23 ms 8088 KB Output is correct
40 Correct 44 ms 9144 KB Output is correct
41 Correct 65 ms 11188 KB Output is correct
42 Correct 65 ms 11188 KB Output is correct
43 Correct 67 ms 11184 KB Output is correct
44 Correct 68 ms 11188 KB Output is correct
45 Correct 66 ms 11180 KB Output is correct
46 Correct 64 ms 11188 KB Output is correct
47 Correct 65 ms 11184 KB Output is correct
48 Correct 69 ms 11184 KB Output is correct
49 Correct 64 ms 11192 KB Output is correct
50 Correct 73 ms 11192 KB Output is correct
51 Correct 68 ms 11188 KB Output is correct
52 Correct 66 ms 11180 KB Output is correct
53 Correct 65 ms 11188 KB Output is correct
54 Correct 66 ms 11184 KB Output is correct
55 Correct 68 ms 11176 KB Output is correct
56 Correct 65 ms 11184 KB Output is correct
57 Correct 67 ms 11180 KB Output is correct
58 Correct 68 ms 11224 KB Output is correct
59 Correct 64 ms 11184 KB Output is correct
60 Correct 65 ms 11192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6988 KB Output is correct
2 Correct 3 ms 6988 KB Output is correct
3 Correct 3 ms 6988 KB Output is correct
4 Correct 3 ms 6988 KB Output is correct
5 Correct 3 ms 6988 KB Output is correct
6 Correct 3 ms 6988 KB Output is correct
7 Correct 2 ms 6988 KB Output is correct
8 Correct 3 ms 6988 KB Output is correct
9 Correct 3 ms 6988 KB Output is correct
10 Correct 3 ms 6988 KB Output is correct
11 Correct 3 ms 6988 KB Output is correct
12 Correct 3 ms 6988 KB Output is correct
13 Correct 4 ms 7116 KB Output is correct
14 Correct 17 ms 8140 KB Output is correct
15 Correct 33 ms 9156 KB Output is correct
16 Correct 49 ms 11180 KB Output is correct
17 Correct 49 ms 11180 KB Output is correct
18 Correct 46 ms 11200 KB Output is correct
19 Correct 46 ms 11192 KB Output is correct
20 Correct 47 ms 11176 KB Output is correct
21 Correct 47 ms 11176 KB Output is correct
22 Correct 48 ms 11180 KB Output is correct
23 Correct 48 ms 11196 KB Output is correct
24 Correct 46 ms 11180 KB Output is correct
25 Correct 47 ms 11180 KB Output is correct
26 Correct 3 ms 6988 KB Output is correct
27 Correct 3 ms 6988 KB Output is correct
28 Correct 4 ms 7116 KB Output is correct
29 Correct 19 ms 8132 KB Output is correct
30 Correct 35 ms 9144 KB Output is correct
31 Correct 54 ms 11192 KB Output is correct
32 Correct 53 ms 11184 KB Output is correct
33 Correct 52 ms 11180 KB Output is correct
34 Correct 55 ms 11192 KB Output is correct
35 Correct 52 ms 11192 KB Output is correct
36 Correct 58 ms 11176 KB Output is correct
37 Correct 54 ms 11196 KB Output is correct
38 Correct 51 ms 11180 KB Output is correct
39 Correct 51 ms 11184 KB Output is correct
40 Correct 53 ms 11184 KB Output is correct
41 Correct 3 ms 6988 KB Output is correct
42 Correct 3 ms 6988 KB Output is correct
43 Correct 3 ms 6988 KB Output is correct
44 Correct 4 ms 7116 KB Output is correct
45 Correct 5 ms 7116 KB Output is correct
46 Correct 5 ms 7116 KB Output is correct
47 Correct 14 ms 7628 KB Output is correct
48 Correct 13 ms 7624 KB Output is correct
49 Correct 23 ms 8088 KB Output is correct
50 Correct 44 ms 9144 KB Output is correct
51 Correct 65 ms 11188 KB Output is correct
52 Correct 65 ms 11188 KB Output is correct
53 Correct 67 ms 11184 KB Output is correct
54 Correct 68 ms 11188 KB Output is correct
55 Correct 66 ms 11180 KB Output is correct
56 Correct 64 ms 11188 KB Output is correct
57 Correct 65 ms 11184 KB Output is correct
58 Correct 69 ms 11184 KB Output is correct
59 Correct 64 ms 11192 KB Output is correct
60 Correct 73 ms 11192 KB Output is correct
61 Correct 68 ms 11188 KB Output is correct
62 Correct 66 ms 11180 KB Output is correct
63 Correct 65 ms 11188 KB Output is correct
64 Correct 66 ms 11184 KB Output is correct
65 Correct 68 ms 11176 KB Output is correct
66 Correct 65 ms 11184 KB Output is correct
67 Correct 67 ms 11180 KB Output is correct
68 Correct 68 ms 11224 KB Output is correct
69 Correct 64 ms 11184 KB Output is correct
70 Correct 65 ms 11192 KB Output is correct
71 Correct 14 ms 7628 KB Output is correct
72 Correct 14 ms 7628 KB Output is correct
73 Correct 14 ms 7624 KB Output is correct
74 Correct 17 ms 7624 KB Output is correct
75 Correct 13 ms 7624 KB Output is correct
76 Correct 27 ms 8140 KB Output is correct
77 Correct 30 ms 8116 KB Output is correct
78 Correct 27 ms 8260 KB Output is correct
79 Correct 27 ms 8140 KB Output is correct
80 Correct 34 ms 8120 KB Output is correct
81 Correct 49 ms 9120 KB Output is correct
82 Correct 55 ms 9132 KB Output is correct
83 Correct 49 ms 9144 KB Output is correct
84 Correct 50 ms 9148 KB Output is correct
85 Correct 53 ms 9148 KB Output is correct
86 Correct 54 ms 9148 KB Output is correct
87 Correct 57 ms 9140 KB Output is correct
88 Correct 53 ms 9144 KB Output is correct
89 Correct 54 ms 9144 KB Output is correct
90 Correct 55 ms 9144 KB Output is correct
91 Correct 88 ms 11188 KB Output is correct
92 Correct 85 ms 11188 KB Output is correct
93 Correct 84 ms 11188 KB Output is correct
94 Correct 91 ms 11212 KB Output is correct
95 Correct 91 ms 11184 KB Output is correct
96 Correct 101 ms 12848 KB Output is correct
97 Correct 92 ms 12844 KB Output is correct
98 Correct 94 ms 12848 KB Output is correct
99 Correct 97 ms 12844 KB Output is correct
100 Correct 95 ms 12848 KB Output is correct
101 Correct 92 ms 12840 KB Output is correct
102 Correct 94 ms 12852 KB Output is correct
103 Correct 91 ms 12856 KB Output is correct
104 Correct 91 ms 12852 KB Output is correct
105 Correct 96 ms 12844 KB Output is correct
106 Correct 94 ms 13232 KB Output is correct
107 Correct 95 ms 13220 KB Output is correct
108 Correct 103 ms 13240 KB Output is correct
109 Correct 100 ms 13220 KB Output is correct
110 Correct 104 ms 13236 KB Output is correct
111 Correct 97 ms 13232 KB Output is correct
112 Correct 97 ms 13236 KB Output is correct
113 Correct 94 ms 13240 KB Output is correct
114 Correct 97 ms 13228 KB Output is correct
115 Correct 94 ms 13224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6988 KB Output is correct
2 Correct 3 ms 6988 KB Output is correct
3 Correct 3 ms 6988 KB Output is correct
4 Correct 3 ms 6988 KB Output is correct
5 Correct 3 ms 6988 KB Output is correct
6 Correct 3 ms 6988 KB Output is correct
7 Correct 2 ms 6988 KB Output is correct
8 Correct 3 ms 6988 KB Output is correct
9 Correct 3 ms 6988 KB Output is correct
10 Correct 3 ms 6988 KB Output is correct
11 Correct 3 ms 6988 KB Output is correct
12 Correct 3 ms 6988 KB Output is correct
13 Correct 4 ms 7116 KB Output is correct
14 Correct 17 ms 8140 KB Output is correct
15 Correct 33 ms 9156 KB Output is correct
16 Correct 49 ms 11180 KB Output is correct
17 Correct 49 ms 11180 KB Output is correct
18 Correct 46 ms 11200 KB Output is correct
19 Correct 46 ms 11192 KB Output is correct
20 Correct 47 ms 11176 KB Output is correct
21 Correct 47 ms 11176 KB Output is correct
22 Correct 48 ms 11180 KB Output is correct
23 Correct 48 ms 11196 KB Output is correct
24 Correct 46 ms 11180 KB Output is correct
25 Correct 47 ms 11180 KB Output is correct
26 Correct 3 ms 6988 KB Output is correct
27 Correct 3 ms 6988 KB Output is correct
28 Correct 4 ms 7116 KB Output is correct
29 Correct 19 ms 8132 KB Output is correct
30 Correct 35 ms 9144 KB Output is correct
31 Correct 54 ms 11192 KB Output is correct
32 Correct 53 ms 11184 KB Output is correct
33 Correct 52 ms 11180 KB Output is correct
34 Correct 55 ms 11192 KB Output is correct
35 Correct 52 ms 11192 KB Output is correct
36 Correct 58 ms 11176 KB Output is correct
37 Correct 54 ms 11196 KB Output is correct
38 Correct 51 ms 11180 KB Output is correct
39 Correct 51 ms 11184 KB Output is correct
40 Correct 53 ms 11184 KB Output is correct
41 Correct 3 ms 6988 KB Output is correct
42 Correct 3 ms 6988 KB Output is correct
43 Correct 3 ms 6988 KB Output is correct
44 Correct 4 ms 7116 KB Output is correct
45 Correct 5 ms 7116 KB Output is correct
46 Correct 5 ms 7116 KB Output is correct
47 Correct 14 ms 7628 KB Output is correct
48 Correct 13 ms 7624 KB Output is correct
49 Correct 23 ms 8088 KB Output is correct
50 Correct 44 ms 9144 KB Output is correct
51 Correct 65 ms 11188 KB Output is correct
52 Correct 65 ms 11188 KB Output is correct
53 Correct 67 ms 11184 KB Output is correct
54 Correct 68 ms 11188 KB Output is correct
55 Correct 66 ms 11180 KB Output is correct
56 Correct 64 ms 11188 KB Output is correct
57 Correct 65 ms 11184 KB Output is correct
58 Correct 69 ms 11184 KB Output is correct
59 Correct 64 ms 11192 KB Output is correct
60 Correct 73 ms 11192 KB Output is correct
61 Correct 68 ms 11188 KB Output is correct
62 Correct 66 ms 11180 KB Output is correct
63 Correct 65 ms 11188 KB Output is correct
64 Correct 66 ms 11184 KB Output is correct
65 Correct 68 ms 11176 KB Output is correct
66 Correct 65 ms 11184 KB Output is correct
67 Correct 67 ms 11180 KB Output is correct
68 Correct 68 ms 11224 KB Output is correct
69 Correct 64 ms 11184 KB Output is correct
70 Correct 65 ms 11192 KB Output is correct
71 Correct 14 ms 7628 KB Output is correct
72 Correct 14 ms 7628 KB Output is correct
73 Correct 14 ms 7624 KB Output is correct
74 Correct 17 ms 7624 KB Output is correct
75 Correct 13 ms 7624 KB Output is correct
76 Correct 27 ms 8140 KB Output is correct
77 Correct 30 ms 8116 KB Output is correct
78 Correct 27 ms 8260 KB Output is correct
79 Correct 27 ms 8140 KB Output is correct
80 Correct 34 ms 8120 KB Output is correct
81 Correct 49 ms 9120 KB Output is correct
82 Correct 55 ms 9132 KB Output is correct
83 Correct 49 ms 9144 KB Output is correct
84 Correct 50 ms 9148 KB Output is correct
85 Correct 53 ms 9148 KB Output is correct
86 Correct 54 ms 9148 KB Output is correct
87 Correct 57 ms 9140 KB Output is correct
88 Correct 53 ms 9144 KB Output is correct
89 Correct 54 ms 9144 KB Output is correct
90 Correct 55 ms 9144 KB Output is correct
91 Correct 88 ms 11188 KB Output is correct
92 Correct 85 ms 11188 KB Output is correct
93 Correct 84 ms 11188 KB Output is correct
94 Correct 91 ms 11212 KB Output is correct
95 Correct 91 ms 11184 KB Output is correct
96 Correct 101 ms 12848 KB Output is correct
97 Correct 92 ms 12844 KB Output is correct
98 Correct 94 ms 12848 KB Output is correct
99 Correct 97 ms 12844 KB Output is correct
100 Correct 95 ms 12848 KB Output is correct
101 Correct 92 ms 12840 KB Output is correct
102 Correct 94 ms 12852 KB Output is correct
103 Correct 91 ms 12856 KB Output is correct
104 Correct 91 ms 12852 KB Output is correct
105 Correct 96 ms 12844 KB Output is correct
106 Correct 94 ms 13232 KB Output is correct
107 Correct 95 ms 13220 KB Output is correct
108 Correct 103 ms 13240 KB Output is correct
109 Correct 100 ms 13220 KB Output is correct
110 Correct 104 ms 13236 KB Output is correct
111 Correct 97 ms 13232 KB Output is correct
112 Correct 97 ms 13236 KB Output is correct
113 Correct 94 ms 13240 KB Output is correct
114 Correct 97 ms 13228 KB Output is correct
115 Correct 94 ms 13224 KB Output is correct
116 Execution timed out 7020 ms 14716 KB Time limit exceeded
117 Halted 0 ms 0 KB -