답안 #858356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
858356 2023-10-08T08:32:46 Z danikoynov Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 27212 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}


const int maxn = 3e5 + 10, maxq = 5e4 + 10;
const int block_size = sqrt(maxn);

struct query
{
    int l, r, idx;
}task[maxq];

bool cmp_mo(query t1, query t2)
{
    if (t1.l / block_size == t2.l / block_size)
        return t1.r < t2.r;
    return t1.l < t2.l;
}

int n, q, a[maxn], freq[maxn], comp[maxn];

unordered_multiset < int > act;
void update(int val, int c)
{
    ///cout << "update " << val << " " << c << endl;
    if (freq[val] != 0)
    act.erase(act.find(freq[val]));
    ///comp[freq[val]] --;
    freq[val] += c;
    ///comp[freq[val]] ++;
    if (freq[val] != 0)
    act.insert(freq[val]);
}

int lf = 1, rf = 0;
ll ans[maxn];

void move_borders(int left, int right)
{
    while(rf < right)
    {
        rf ++;
        update(a[rf], 1);
    }

    while(lf > left)
    {
        lf --;
        update(a[lf], 1);
    }

    while(rf > right)
    {
        update(a[rf], -1);
        rf --;
    }

    while(lf < left)
    {
        update(a[lf], -1);
        lf ++;
    }
}

ll arr[maxn], bef[maxn], aft[maxn];
ll calc_function()
{
    vector < ll > values;
    for (int cur : act)
        values.push_back(cur);
    sort(values.begin(), values.end());
    /**for (int i = 1; i <= n; i ++)
        for (int j = 0; j < comp[i]; j ++)
        values.push_back(i);*/
    /**for (int cur : values)
        cout << cur << " ";
    cout << endl;*/

    int sz = values.size(), pt = 0;

    for (int i = 0; i < sz; i += 2)
    {
        arr[++ pt] = values[i];
    }
    int f = sz - 1;
    if (sz % 2 == 1)
        f --;
    for (int i = f; i >= 0; i -= 2)
        arr[++ pt] = values[i];



    /**for (int i = 1; i <= n; i ++)
        cout << comp[i] << " ";
    cout << endl;*/
    ll bf = 0, tot = 0;
    for (int i = 1; i <= sz; i ++)
    {
        bef[i] = bf;
        bf += arr[i];
    }
    ll af = 0;
    for (int i = sz ; i > 0; i --)
    {
        aft[i] = af;
        af += arr[i];
    }

    for (ll i = 1; i <= sz; i ++)
    {
        tot = tot + i * arr[i] * (bef[i] - aft[i]);
    }

    ll sf = 0;
    for (int i = 1; i <= sz; i ++)
    {
        ll cur = arr[i];
        tot = tot + cur * (cur + 1) / 2;
        tot = tot + arr[i] * sf;
        sf += arr[i];
    }
    return tot;

}

void compress_data()
{
    int cur = 0;
    map < int, int > mp;
    for (int i = 1; i <= n; i ++)
    {
        if (mp[a[i]] == 0)
            mp[a[i]] = ++ cur;
        a[i] = mp[a[i]];
    }
}
void solve()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i ++)
        cin >> a[i];
    compress_data();
    for (int i = 1; i <= q; i ++)
    {
        cin >> task[i].l >> task[i].r;
        task[i].idx = i;
    }

    sort(task + 1, task + q + 1, cmp_mo);

    for (int i = 1; i <= q; i ++)
    {
        move_borders(task[i].l, task[i].r);
        ans[task[i].idx] = calc_function();
    }

    for (int i = 1; i <= q; i ++)
        cout << ans[i] << endl;

}

int main()
{
    speed();
    solve();
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10712 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 3 ms 10684 KB Output is correct
4 Correct 12 ms 10588 KB Output is correct
5 Correct 25 ms 10776 KB Output is correct
6 Correct 36 ms 11100 KB Output is correct
7 Correct 33 ms 11092 KB Output is correct
8 Correct 36 ms 11088 KB Output is correct
9 Correct 37 ms 11092 KB Output is correct
10 Correct 36 ms 11096 KB Output is correct
11 Correct 36 ms 11088 KB Output is correct
12 Correct 37 ms 11132 KB Output is correct
13 Correct 36 ms 11088 KB Output is correct
14 Correct 36 ms 11092 KB Output is correct
15 Correct 36 ms 10888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 3 ms 10684 KB Output is correct
4 Correct 12 ms 10588 KB Output is correct
5 Correct 25 ms 10776 KB Output is correct
6 Correct 36 ms 11100 KB Output is correct
7 Correct 33 ms 11092 KB Output is correct
8 Correct 36 ms 11088 KB Output is correct
9 Correct 37 ms 11092 KB Output is correct
10 Correct 36 ms 11096 KB Output is correct
11 Correct 36 ms 11088 KB Output is correct
12 Correct 37 ms 11132 KB Output is correct
13 Correct 36 ms 11088 KB Output is correct
14 Correct 36 ms 11092 KB Output is correct
15 Correct 36 ms 10888 KB Output is correct
16 Correct 1 ms 10584 KB Output is correct
17 Correct 1 ms 10588 KB Output is correct
18 Correct 2 ms 10672 KB Output is correct
19 Correct 14 ms 10588 KB Output is correct
20 Correct 25 ms 10768 KB Output is correct
21 Correct 38 ms 11092 KB Output is correct
22 Correct 38 ms 10968 KB Output is correct
23 Correct 38 ms 11092 KB Output is correct
24 Correct 38 ms 10896 KB Output is correct
25 Correct 39 ms 11140 KB Output is correct
26 Correct 39 ms 11056 KB Output is correct
27 Correct 38 ms 11088 KB Output is correct
28 Correct 41 ms 11032 KB Output is correct
29 Correct 38 ms 10892 KB Output is correct
30 Correct 38 ms 10900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 3 ms 10684 KB Output is correct
4 Correct 12 ms 10588 KB Output is correct
5 Correct 25 ms 10776 KB Output is correct
6 Correct 36 ms 11100 KB Output is correct
7 Correct 33 ms 11092 KB Output is correct
8 Correct 36 ms 11088 KB Output is correct
9 Correct 37 ms 11092 KB Output is correct
10 Correct 36 ms 11096 KB Output is correct
11 Correct 36 ms 11088 KB Output is correct
12 Correct 37 ms 11132 KB Output is correct
13 Correct 36 ms 11088 KB Output is correct
14 Correct 36 ms 11092 KB Output is correct
15 Correct 36 ms 10888 KB Output is correct
16 Correct 1 ms 10584 KB Output is correct
17 Correct 1 ms 10588 KB Output is correct
18 Correct 2 ms 10672 KB Output is correct
19 Correct 14 ms 10588 KB Output is correct
20 Correct 25 ms 10768 KB Output is correct
21 Correct 38 ms 11092 KB Output is correct
22 Correct 38 ms 10968 KB Output is correct
23 Correct 38 ms 11092 KB Output is correct
24 Correct 38 ms 10896 KB Output is correct
25 Correct 39 ms 11140 KB Output is correct
26 Correct 39 ms 11056 KB Output is correct
27 Correct 38 ms 11088 KB Output is correct
28 Correct 41 ms 11032 KB Output is correct
29 Correct 38 ms 10892 KB Output is correct
30 Correct 38 ms 10900 KB Output is correct
31 Correct 1 ms 10584 KB Output is correct
32 Correct 1 ms 10588 KB Output is correct
33 Correct 1 ms 10588 KB Output is correct
34 Correct 3 ms 10588 KB Output is correct
35 Correct 3 ms 10716 KB Output is correct
36 Correct 3 ms 10588 KB Output is correct
37 Correct 9 ms 10748 KB Output is correct
38 Correct 10 ms 10720 KB Output is correct
39 Correct 18 ms 10724 KB Output is correct
40 Correct 35 ms 10576 KB Output is correct
41 Correct 54 ms 11208 KB Output is correct
42 Correct 58 ms 11296 KB Output is correct
43 Correct 53 ms 11092 KB Output is correct
44 Correct 55 ms 11184 KB Output is correct
45 Correct 55 ms 11092 KB Output is correct
46 Correct 54 ms 11092 KB Output is correct
47 Correct 55 ms 11092 KB Output is correct
48 Correct 56 ms 11348 KB Output is correct
49 Correct 63 ms 11176 KB Output is correct
50 Correct 56 ms 11184 KB Output is correct
51 Correct 55 ms 11092 KB Output is correct
52 Correct 55 ms 11180 KB Output is correct
53 Correct 55 ms 11196 KB Output is correct
54 Correct 70 ms 10964 KB Output is correct
55 Correct 51 ms 11092 KB Output is correct
56 Correct 53 ms 11088 KB Output is correct
57 Correct 57 ms 11196 KB Output is correct
58 Correct 54 ms 11088 KB Output is correct
59 Correct 56 ms 11092 KB Output is correct
60 Correct 61 ms 11180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10712 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 3 ms 10684 KB Output is correct
14 Correct 12 ms 10588 KB Output is correct
15 Correct 25 ms 10776 KB Output is correct
16 Correct 36 ms 11100 KB Output is correct
17 Correct 33 ms 11092 KB Output is correct
18 Correct 36 ms 11088 KB Output is correct
19 Correct 37 ms 11092 KB Output is correct
20 Correct 36 ms 11096 KB Output is correct
21 Correct 36 ms 11088 KB Output is correct
22 Correct 37 ms 11132 KB Output is correct
23 Correct 36 ms 11088 KB Output is correct
24 Correct 36 ms 11092 KB Output is correct
25 Correct 36 ms 10888 KB Output is correct
26 Correct 1 ms 10584 KB Output is correct
27 Correct 1 ms 10588 KB Output is correct
28 Correct 2 ms 10672 KB Output is correct
29 Correct 14 ms 10588 KB Output is correct
30 Correct 25 ms 10768 KB Output is correct
31 Correct 38 ms 11092 KB Output is correct
32 Correct 38 ms 10968 KB Output is correct
33 Correct 38 ms 11092 KB Output is correct
34 Correct 38 ms 10896 KB Output is correct
35 Correct 39 ms 11140 KB Output is correct
36 Correct 39 ms 11056 KB Output is correct
37 Correct 38 ms 11088 KB Output is correct
38 Correct 41 ms 11032 KB Output is correct
39 Correct 38 ms 10892 KB Output is correct
40 Correct 38 ms 10900 KB Output is correct
41 Correct 1 ms 10584 KB Output is correct
42 Correct 1 ms 10588 KB Output is correct
43 Correct 1 ms 10588 KB Output is correct
44 Correct 3 ms 10588 KB Output is correct
45 Correct 3 ms 10716 KB Output is correct
46 Correct 3 ms 10588 KB Output is correct
47 Correct 9 ms 10748 KB Output is correct
48 Correct 10 ms 10720 KB Output is correct
49 Correct 18 ms 10724 KB Output is correct
50 Correct 35 ms 10576 KB Output is correct
51 Correct 54 ms 11208 KB Output is correct
52 Correct 58 ms 11296 KB Output is correct
53 Correct 53 ms 11092 KB Output is correct
54 Correct 55 ms 11184 KB Output is correct
55 Correct 55 ms 11092 KB Output is correct
56 Correct 54 ms 11092 KB Output is correct
57 Correct 55 ms 11092 KB Output is correct
58 Correct 56 ms 11348 KB Output is correct
59 Correct 63 ms 11176 KB Output is correct
60 Correct 56 ms 11184 KB Output is correct
61 Correct 55 ms 11092 KB Output is correct
62 Correct 55 ms 11180 KB Output is correct
63 Correct 55 ms 11196 KB Output is correct
64 Correct 70 ms 10964 KB Output is correct
65 Correct 51 ms 11092 KB Output is correct
66 Correct 53 ms 11088 KB Output is correct
67 Correct 57 ms 11196 KB Output is correct
68 Correct 54 ms 11088 KB Output is correct
69 Correct 56 ms 11092 KB Output is correct
70 Correct 61 ms 11180 KB Output is correct
71 Correct 11 ms 10812 KB Output is correct
72 Correct 12 ms 10856 KB Output is correct
73 Correct 11 ms 10680 KB Output is correct
74 Correct 11 ms 10816 KB Output is correct
75 Correct 11 ms 10804 KB Output is correct
76 Correct 23 ms 10844 KB Output is correct
77 Correct 24 ms 10844 KB Output is correct
78 Correct 23 ms 10844 KB Output is correct
79 Correct 23 ms 10836 KB Output is correct
80 Correct 25 ms 10840 KB Output is correct
81 Correct 54 ms 11184 KB Output is correct
82 Correct 49 ms 11204 KB Output is correct
83 Correct 49 ms 11328 KB Output is correct
84 Correct 54 ms 11188 KB Output is correct
85 Correct 57 ms 11180 KB Output is correct
86 Correct 59 ms 11716 KB Output is correct
87 Correct 60 ms 11824 KB Output is correct
88 Correct 65 ms 11884 KB Output is correct
89 Correct 61 ms 11820 KB Output is correct
90 Correct 59 ms 11824 KB Output is correct
91 Correct 136 ms 17864 KB Output is correct
92 Correct 130 ms 19356 KB Output is correct
93 Correct 140 ms 17976 KB Output is correct
94 Correct 142 ms 17844 KB Output is correct
95 Correct 128 ms 17996 KB Output is correct
96 Correct 169 ms 25400 KB Output is correct
97 Correct 176 ms 25404 KB Output is correct
98 Correct 161 ms 25396 KB Output is correct
99 Correct 170 ms 25372 KB Output is correct
100 Correct 165 ms 25312 KB Output is correct
101 Correct 174 ms 25284 KB Output is correct
102 Correct 171 ms 25396 KB Output is correct
103 Correct 172 ms 25504 KB Output is correct
104 Correct 174 ms 25400 KB Output is correct
105 Correct 165 ms 25436 KB Output is correct
106 Correct 214 ms 26960 KB Output is correct
107 Correct 237 ms 26956 KB Output is correct
108 Correct 189 ms 26964 KB Output is correct
109 Correct 211 ms 26964 KB Output is correct
110 Correct 198 ms 26816 KB Output is correct
111 Correct 200 ms 26964 KB Output is correct
112 Correct 224 ms 27212 KB Output is correct
113 Correct 232 ms 27036 KB Output is correct
114 Correct 212 ms 27036 KB Output is correct
115 Correct 212 ms 26964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10712 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 3 ms 10684 KB Output is correct
14 Correct 12 ms 10588 KB Output is correct
15 Correct 25 ms 10776 KB Output is correct
16 Correct 36 ms 11100 KB Output is correct
17 Correct 33 ms 11092 KB Output is correct
18 Correct 36 ms 11088 KB Output is correct
19 Correct 37 ms 11092 KB Output is correct
20 Correct 36 ms 11096 KB Output is correct
21 Correct 36 ms 11088 KB Output is correct
22 Correct 37 ms 11132 KB Output is correct
23 Correct 36 ms 11088 KB Output is correct
24 Correct 36 ms 11092 KB Output is correct
25 Correct 36 ms 10888 KB Output is correct
26 Correct 1 ms 10584 KB Output is correct
27 Correct 1 ms 10588 KB Output is correct
28 Correct 2 ms 10672 KB Output is correct
29 Correct 14 ms 10588 KB Output is correct
30 Correct 25 ms 10768 KB Output is correct
31 Correct 38 ms 11092 KB Output is correct
32 Correct 38 ms 10968 KB Output is correct
33 Correct 38 ms 11092 KB Output is correct
34 Correct 38 ms 10896 KB Output is correct
35 Correct 39 ms 11140 KB Output is correct
36 Correct 39 ms 11056 KB Output is correct
37 Correct 38 ms 11088 KB Output is correct
38 Correct 41 ms 11032 KB Output is correct
39 Correct 38 ms 10892 KB Output is correct
40 Correct 38 ms 10900 KB Output is correct
41 Correct 1 ms 10584 KB Output is correct
42 Correct 1 ms 10588 KB Output is correct
43 Correct 1 ms 10588 KB Output is correct
44 Correct 3 ms 10588 KB Output is correct
45 Correct 3 ms 10716 KB Output is correct
46 Correct 3 ms 10588 KB Output is correct
47 Correct 9 ms 10748 KB Output is correct
48 Correct 10 ms 10720 KB Output is correct
49 Correct 18 ms 10724 KB Output is correct
50 Correct 35 ms 10576 KB Output is correct
51 Correct 54 ms 11208 KB Output is correct
52 Correct 58 ms 11296 KB Output is correct
53 Correct 53 ms 11092 KB Output is correct
54 Correct 55 ms 11184 KB Output is correct
55 Correct 55 ms 11092 KB Output is correct
56 Correct 54 ms 11092 KB Output is correct
57 Correct 55 ms 11092 KB Output is correct
58 Correct 56 ms 11348 KB Output is correct
59 Correct 63 ms 11176 KB Output is correct
60 Correct 56 ms 11184 KB Output is correct
61 Correct 55 ms 11092 KB Output is correct
62 Correct 55 ms 11180 KB Output is correct
63 Correct 55 ms 11196 KB Output is correct
64 Correct 70 ms 10964 KB Output is correct
65 Correct 51 ms 11092 KB Output is correct
66 Correct 53 ms 11088 KB Output is correct
67 Correct 57 ms 11196 KB Output is correct
68 Correct 54 ms 11088 KB Output is correct
69 Correct 56 ms 11092 KB Output is correct
70 Correct 61 ms 11180 KB Output is correct
71 Correct 11 ms 10812 KB Output is correct
72 Correct 12 ms 10856 KB Output is correct
73 Correct 11 ms 10680 KB Output is correct
74 Correct 11 ms 10816 KB Output is correct
75 Correct 11 ms 10804 KB Output is correct
76 Correct 23 ms 10844 KB Output is correct
77 Correct 24 ms 10844 KB Output is correct
78 Correct 23 ms 10844 KB Output is correct
79 Correct 23 ms 10836 KB Output is correct
80 Correct 25 ms 10840 KB Output is correct
81 Correct 54 ms 11184 KB Output is correct
82 Correct 49 ms 11204 KB Output is correct
83 Correct 49 ms 11328 KB Output is correct
84 Correct 54 ms 11188 KB Output is correct
85 Correct 57 ms 11180 KB Output is correct
86 Correct 59 ms 11716 KB Output is correct
87 Correct 60 ms 11824 KB Output is correct
88 Correct 65 ms 11884 KB Output is correct
89 Correct 61 ms 11820 KB Output is correct
90 Correct 59 ms 11824 KB Output is correct
91 Correct 136 ms 17864 KB Output is correct
92 Correct 130 ms 19356 KB Output is correct
93 Correct 140 ms 17976 KB Output is correct
94 Correct 142 ms 17844 KB Output is correct
95 Correct 128 ms 17996 KB Output is correct
96 Correct 169 ms 25400 KB Output is correct
97 Correct 176 ms 25404 KB Output is correct
98 Correct 161 ms 25396 KB Output is correct
99 Correct 170 ms 25372 KB Output is correct
100 Correct 165 ms 25312 KB Output is correct
101 Correct 174 ms 25284 KB Output is correct
102 Correct 171 ms 25396 KB Output is correct
103 Correct 172 ms 25504 KB Output is correct
104 Correct 174 ms 25400 KB Output is correct
105 Correct 165 ms 25436 KB Output is correct
106 Correct 214 ms 26960 KB Output is correct
107 Correct 237 ms 26956 KB Output is correct
108 Correct 189 ms 26964 KB Output is correct
109 Correct 211 ms 26964 KB Output is correct
110 Correct 198 ms 26816 KB Output is correct
111 Correct 200 ms 26964 KB Output is correct
112 Correct 224 ms 27212 KB Output is correct
113 Correct 232 ms 27036 KB Output is correct
114 Correct 212 ms 27036 KB Output is correct
115 Correct 212 ms 26964 KB Output is correct
116 Correct 592 ms 11552 KB Output is correct
117 Correct 560 ms 11560 KB Output is correct
118 Correct 930 ms 11780 KB Output is correct
119 Correct 1022 ms 11836 KB Output is correct
120 Correct 1297 ms 11868 KB Output is correct
121 Correct 2009 ms 11868 KB Output is correct
122 Correct 2594 ms 11952 KB Output is correct
123 Correct 4240 ms 11920 KB Output is correct
124 Correct 6791 ms 13812 KB Output is correct
125 Execution timed out 7050 ms 13720 KB Time limit exceeded
126 Halted 0 ms 0 KB -