Submission #858367

# Submission time Handle Problem Language Result Execution time Memory
858367 2023-10-08T09:01:19 Z danikoynov Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 20860 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 q, a[maxn], freq[maxn], comp[maxn];
ll n;
unordered_map < int, int > act;
void update(int val, int c)
{
    ///cout << "update " << val << " " << c << endl;
    if (freq[val] != 0)
    {
        act[freq[val]] --;
        if (act[freq[val]] == 0)
            act.erase(freq[val]);
    }

    ///comp[freq[val]] --;
    freq[val] += c;
    ///comp[freq[val]] ++;
    if (freq[val] != 0)
    {
        act[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(ll len)
{
    vector < pair < ll, ll >  > values;
    for (pair < int, int > cur : act)
    {
        values.push_back(cur);
    }
    sort(values.begin(), values.end());

    ll sz = values.size(), cp = 0;
    deque < pair < ll, ll > > dq;
    ll left_add = 0, right_add = 0;
    values.back().second --;
    cp ++;
    dq.push_back({values.back().first, 1});
    for (int i = sz - 1; i >= 0; i --)
    {
        ///cout << "here " << i << " " << values[i].first << " " << values[i].second << endl;
        ll p1 = (values[i].second + 1) / 2;
        ll p2 = (values[i].second) / 2;
        if (right_add > left_add)
            swap(p1, p2);
        if(p1 != 0)
            dq.push_back({values[i].first, p1});
        if (p2 != 0)
            dq.push_front({values[i].first, p2});

        cp += values[i].second;
        left_add += p2;
        right_add += p1;
    }

    int pt = 0;
    for (int i = 0; i < dq.size(); i ++)
    {
        ///cout << "pair " << dq[i].first << " " << dq[i].second << endl;
        for (int j = 0; j < dq[i].second; j ++)
            arr[++ pt] = dq[i].first;
    }
    /**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 <= cp; i ++)
        cout << arr[i] << " ";
    cout << endl;*/

    /**for (int i = 1; i <= n; i ++)
        cout << comp[i] << " ";
    cout << endl;*/
    ll tot = 0, pref = 0;
    for (int i = 1; i < cp; i ++)
    {
        pref += arr[i];
        tot = tot + pref * pref;
    }
    ll suff = 0;
    for (int i = cp; i > 1; i --)
    {
        suff = suff + arr[i];
        tot = tot + suff * suff;
    }

    ///cout << tot << endl;
    tot = (cp * len * (len + 1) - len * (cp - 1) - tot) / 2;
    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(task[i].r - task[i].l + 1);
    }

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

}

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

Compilation message

diversity.cpp: In function 'll calc_function(ll)':
diversity.cpp:124:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |     for (int i = 0; i < dq.size(); i ++)
      |                     ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 14 ms 8536 KB Output is correct
5 Correct 26 ms 8532 KB Output is correct
6 Correct 41 ms 9048 KB Output is correct
7 Correct 38 ms 9052 KB Output is correct
8 Correct 42 ms 9044 KB Output is correct
9 Correct 40 ms 9052 KB Output is correct
10 Correct 40 ms 9040 KB Output is correct
11 Correct 44 ms 9088 KB Output is correct
12 Correct 43 ms 9100 KB Output is correct
13 Correct 41 ms 9044 KB Output is correct
14 Correct 40 ms 9044 KB Output is correct
15 Correct 43 ms 9044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 14 ms 8536 KB Output is correct
5 Correct 26 ms 8532 KB Output is correct
6 Correct 41 ms 9048 KB Output is correct
7 Correct 38 ms 9052 KB Output is correct
8 Correct 42 ms 9044 KB Output is correct
9 Correct 40 ms 9052 KB Output is correct
10 Correct 40 ms 9040 KB Output is correct
11 Correct 44 ms 9088 KB Output is correct
12 Correct 43 ms 9100 KB Output is correct
13 Correct 41 ms 9044 KB Output is correct
14 Correct 40 ms 9044 KB Output is correct
15 Correct 43 ms 9044 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 14 ms 8668 KB Output is correct
20 Correct 28 ms 8540 KB Output is correct
21 Correct 42 ms 9092 KB Output is correct
22 Correct 42 ms 9040 KB Output is correct
23 Correct 43 ms 9088 KB Output is correct
24 Correct 42 ms 8912 KB Output is correct
25 Correct 43 ms 9040 KB Output is correct
26 Correct 43 ms 9040 KB Output is correct
27 Correct 44 ms 8920 KB Output is correct
28 Correct 50 ms 9100 KB Output is correct
29 Correct 49 ms 9044 KB Output is correct
30 Correct 43 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 14 ms 8536 KB Output is correct
5 Correct 26 ms 8532 KB Output is correct
6 Correct 41 ms 9048 KB Output is correct
7 Correct 38 ms 9052 KB Output is correct
8 Correct 42 ms 9044 KB Output is correct
9 Correct 40 ms 9052 KB Output is correct
10 Correct 40 ms 9040 KB Output is correct
11 Correct 44 ms 9088 KB Output is correct
12 Correct 43 ms 9100 KB Output is correct
13 Correct 41 ms 9044 KB Output is correct
14 Correct 40 ms 9044 KB Output is correct
15 Correct 43 ms 9044 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 14 ms 8668 KB Output is correct
20 Correct 28 ms 8540 KB Output is correct
21 Correct 42 ms 9092 KB Output is correct
22 Correct 42 ms 9040 KB Output is correct
23 Correct 43 ms 9088 KB Output is correct
24 Correct 42 ms 8912 KB Output is correct
25 Correct 43 ms 9040 KB Output is correct
26 Correct 43 ms 9040 KB Output is correct
27 Correct 44 ms 8920 KB Output is correct
28 Correct 50 ms 9100 KB Output is correct
29 Correct 49 ms 9044 KB Output is correct
30 Correct 43 ms 8916 KB Output is correct
31 Correct 2 ms 8540 KB Output is correct
32 Correct 1 ms 8540 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 2 ms 8540 KB Output is correct
35 Correct 2 ms 8672 KB Output is correct
36 Correct 3 ms 8540 KB Output is correct
37 Correct 9 ms 8692 KB Output is correct
38 Correct 10 ms 8540 KB Output is correct
39 Correct 19 ms 8716 KB Output is correct
40 Correct 36 ms 8532 KB Output is correct
41 Correct 55 ms 9148 KB Output is correct
42 Correct 53 ms 9044 KB Output is correct
43 Correct 54 ms 9140 KB Output is correct
44 Correct 53 ms 9052 KB Output is correct
45 Correct 53 ms 9044 KB Output is correct
46 Correct 58 ms 9044 KB Output is correct
47 Correct 53 ms 9152 KB Output is correct
48 Correct 59 ms 9156 KB Output is correct
49 Correct 53 ms 9044 KB Output is correct
50 Correct 54 ms 9136 KB Output is correct
51 Correct 52 ms 9044 KB Output is correct
52 Correct 54 ms 9040 KB Output is correct
53 Correct 54 ms 9172 KB Output is correct
54 Correct 54 ms 9128 KB Output is correct
55 Correct 52 ms 9040 KB Output is correct
56 Correct 53 ms 9044 KB Output is correct
57 Correct 53 ms 9136 KB Output is correct
58 Correct 54 ms 9044 KB Output is correct
59 Correct 52 ms 9044 KB Output is correct
60 Correct 52 ms 9148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 3 ms 8540 KB Output is correct
14 Correct 14 ms 8536 KB Output is correct
15 Correct 26 ms 8532 KB Output is correct
16 Correct 41 ms 9048 KB Output is correct
17 Correct 38 ms 9052 KB Output is correct
18 Correct 42 ms 9044 KB Output is correct
19 Correct 40 ms 9052 KB Output is correct
20 Correct 40 ms 9040 KB Output is correct
21 Correct 44 ms 9088 KB Output is correct
22 Correct 43 ms 9100 KB Output is correct
23 Correct 41 ms 9044 KB Output is correct
24 Correct 40 ms 9044 KB Output is correct
25 Correct 43 ms 9044 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 14 ms 8668 KB Output is correct
30 Correct 28 ms 8540 KB Output is correct
31 Correct 42 ms 9092 KB Output is correct
32 Correct 42 ms 9040 KB Output is correct
33 Correct 43 ms 9088 KB Output is correct
34 Correct 42 ms 8912 KB Output is correct
35 Correct 43 ms 9040 KB Output is correct
36 Correct 43 ms 9040 KB Output is correct
37 Correct 44 ms 8920 KB Output is correct
38 Correct 50 ms 9100 KB Output is correct
39 Correct 49 ms 9044 KB Output is correct
40 Correct 43 ms 8916 KB Output is correct
41 Correct 2 ms 8540 KB Output is correct
42 Correct 1 ms 8540 KB Output is correct
43 Correct 1 ms 8540 KB Output is correct
44 Correct 2 ms 8540 KB Output is correct
45 Correct 2 ms 8672 KB Output is correct
46 Correct 3 ms 8540 KB Output is correct
47 Correct 9 ms 8692 KB Output is correct
48 Correct 10 ms 8540 KB Output is correct
49 Correct 19 ms 8716 KB Output is correct
50 Correct 36 ms 8532 KB Output is correct
51 Correct 55 ms 9148 KB Output is correct
52 Correct 53 ms 9044 KB Output is correct
53 Correct 54 ms 9140 KB Output is correct
54 Correct 53 ms 9052 KB Output is correct
55 Correct 53 ms 9044 KB Output is correct
56 Correct 58 ms 9044 KB Output is correct
57 Correct 53 ms 9152 KB Output is correct
58 Correct 59 ms 9156 KB Output is correct
59 Correct 53 ms 9044 KB Output is correct
60 Correct 54 ms 9136 KB Output is correct
61 Correct 52 ms 9044 KB Output is correct
62 Correct 54 ms 9040 KB Output is correct
63 Correct 54 ms 9172 KB Output is correct
64 Correct 54 ms 9128 KB Output is correct
65 Correct 52 ms 9040 KB Output is correct
66 Correct 53 ms 9044 KB Output is correct
67 Correct 53 ms 9136 KB Output is correct
68 Correct 54 ms 9044 KB Output is correct
69 Correct 52 ms 9044 KB Output is correct
70 Correct 52 ms 9148 KB Output is correct
71 Correct 10 ms 8536 KB Output is correct
72 Correct 10 ms 8540 KB Output is correct
73 Correct 11 ms 8540 KB Output is correct
74 Correct 10 ms 8740 KB Output is correct
75 Correct 11 ms 8540 KB Output is correct
76 Correct 22 ms 8884 KB Output is correct
77 Correct 25 ms 8796 KB Output is correct
78 Correct 25 ms 8884 KB Output is correct
79 Correct 22 ms 8896 KB Output is correct
80 Correct 21 ms 8796 KB Output is correct
81 Correct 45 ms 9044 KB Output is correct
82 Correct 46 ms 9168 KB Output is correct
83 Correct 45 ms 9152 KB Output is correct
84 Correct 45 ms 9172 KB Output is correct
85 Correct 46 ms 9160 KB Output is correct
86 Correct 57 ms 9556 KB Output is correct
87 Correct 53 ms 9556 KB Output is correct
88 Correct 59 ms 9608 KB Output is correct
89 Correct 56 ms 9824 KB Output is correct
90 Correct 53 ms 9472 KB Output is correct
91 Correct 110 ms 13792 KB Output is correct
92 Correct 112 ms 13652 KB Output is correct
93 Correct 109 ms 13780 KB Output is correct
94 Correct 115 ms 13904 KB Output is correct
95 Correct 110 ms 13648 KB Output is correct
96 Correct 155 ms 18480 KB Output is correct
97 Correct 146 ms 18476 KB Output is correct
98 Correct 143 ms 18460 KB Output is correct
99 Correct 135 ms 18260 KB Output is correct
100 Correct 139 ms 18300 KB Output is correct
101 Correct 145 ms 18460 KB Output is correct
102 Correct 163 ms 18448 KB Output is correct
103 Correct 138 ms 18256 KB Output is correct
104 Correct 149 ms 18656 KB Output is correct
105 Correct 169 ms 18460 KB Output is correct
106 Correct 154 ms 20820 KB Output is correct
107 Correct 147 ms 20820 KB Output is correct
108 Correct 154 ms 20668 KB Output is correct
109 Correct 150 ms 20780 KB Output is correct
110 Correct 183 ms 20820 KB Output is correct
111 Correct 193 ms 20860 KB Output is correct
112 Correct 152 ms 20616 KB Output is correct
113 Correct 166 ms 20820 KB Output is correct
114 Correct 152 ms 20820 KB Output is correct
115 Correct 155 ms 20680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 3 ms 8540 KB Output is correct
14 Correct 14 ms 8536 KB Output is correct
15 Correct 26 ms 8532 KB Output is correct
16 Correct 41 ms 9048 KB Output is correct
17 Correct 38 ms 9052 KB Output is correct
18 Correct 42 ms 9044 KB Output is correct
19 Correct 40 ms 9052 KB Output is correct
20 Correct 40 ms 9040 KB Output is correct
21 Correct 44 ms 9088 KB Output is correct
22 Correct 43 ms 9100 KB Output is correct
23 Correct 41 ms 9044 KB Output is correct
24 Correct 40 ms 9044 KB Output is correct
25 Correct 43 ms 9044 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 14 ms 8668 KB Output is correct
30 Correct 28 ms 8540 KB Output is correct
31 Correct 42 ms 9092 KB Output is correct
32 Correct 42 ms 9040 KB Output is correct
33 Correct 43 ms 9088 KB Output is correct
34 Correct 42 ms 8912 KB Output is correct
35 Correct 43 ms 9040 KB Output is correct
36 Correct 43 ms 9040 KB Output is correct
37 Correct 44 ms 8920 KB Output is correct
38 Correct 50 ms 9100 KB Output is correct
39 Correct 49 ms 9044 KB Output is correct
40 Correct 43 ms 8916 KB Output is correct
41 Correct 2 ms 8540 KB Output is correct
42 Correct 1 ms 8540 KB Output is correct
43 Correct 1 ms 8540 KB Output is correct
44 Correct 2 ms 8540 KB Output is correct
45 Correct 2 ms 8672 KB Output is correct
46 Correct 3 ms 8540 KB Output is correct
47 Correct 9 ms 8692 KB Output is correct
48 Correct 10 ms 8540 KB Output is correct
49 Correct 19 ms 8716 KB Output is correct
50 Correct 36 ms 8532 KB Output is correct
51 Correct 55 ms 9148 KB Output is correct
52 Correct 53 ms 9044 KB Output is correct
53 Correct 54 ms 9140 KB Output is correct
54 Correct 53 ms 9052 KB Output is correct
55 Correct 53 ms 9044 KB Output is correct
56 Correct 58 ms 9044 KB Output is correct
57 Correct 53 ms 9152 KB Output is correct
58 Correct 59 ms 9156 KB Output is correct
59 Correct 53 ms 9044 KB Output is correct
60 Correct 54 ms 9136 KB Output is correct
61 Correct 52 ms 9044 KB Output is correct
62 Correct 54 ms 9040 KB Output is correct
63 Correct 54 ms 9172 KB Output is correct
64 Correct 54 ms 9128 KB Output is correct
65 Correct 52 ms 9040 KB Output is correct
66 Correct 53 ms 9044 KB Output is correct
67 Correct 53 ms 9136 KB Output is correct
68 Correct 54 ms 9044 KB Output is correct
69 Correct 52 ms 9044 KB Output is correct
70 Correct 52 ms 9148 KB Output is correct
71 Correct 10 ms 8536 KB Output is correct
72 Correct 10 ms 8540 KB Output is correct
73 Correct 11 ms 8540 KB Output is correct
74 Correct 10 ms 8740 KB Output is correct
75 Correct 11 ms 8540 KB Output is correct
76 Correct 22 ms 8884 KB Output is correct
77 Correct 25 ms 8796 KB Output is correct
78 Correct 25 ms 8884 KB Output is correct
79 Correct 22 ms 8896 KB Output is correct
80 Correct 21 ms 8796 KB Output is correct
81 Correct 45 ms 9044 KB Output is correct
82 Correct 46 ms 9168 KB Output is correct
83 Correct 45 ms 9152 KB Output is correct
84 Correct 45 ms 9172 KB Output is correct
85 Correct 46 ms 9160 KB Output is correct
86 Correct 57 ms 9556 KB Output is correct
87 Correct 53 ms 9556 KB Output is correct
88 Correct 59 ms 9608 KB Output is correct
89 Correct 56 ms 9824 KB Output is correct
90 Correct 53 ms 9472 KB Output is correct
91 Correct 110 ms 13792 KB Output is correct
92 Correct 112 ms 13652 KB Output is correct
93 Correct 109 ms 13780 KB Output is correct
94 Correct 115 ms 13904 KB Output is correct
95 Correct 110 ms 13648 KB Output is correct
96 Correct 155 ms 18480 KB Output is correct
97 Correct 146 ms 18476 KB Output is correct
98 Correct 143 ms 18460 KB Output is correct
99 Correct 135 ms 18260 KB Output is correct
100 Correct 139 ms 18300 KB Output is correct
101 Correct 145 ms 18460 KB Output is correct
102 Correct 163 ms 18448 KB Output is correct
103 Correct 138 ms 18256 KB Output is correct
104 Correct 149 ms 18656 KB Output is correct
105 Correct 169 ms 18460 KB Output is correct
106 Correct 154 ms 20820 KB Output is correct
107 Correct 147 ms 20820 KB Output is correct
108 Correct 154 ms 20668 KB Output is correct
109 Correct 150 ms 20780 KB Output is correct
110 Correct 183 ms 20820 KB Output is correct
111 Correct 193 ms 20860 KB Output is correct
112 Correct 152 ms 20616 KB Output is correct
113 Correct 166 ms 20820 KB Output is correct
114 Correct 152 ms 20820 KB Output is correct
115 Correct 155 ms 20680 KB Output is correct
116 Correct 604 ms 9552 KB Output is correct
117 Correct 495 ms 9500 KB Output is correct
118 Correct 992 ms 9760 KB Output is correct
119 Correct 734 ms 9776 KB Output is correct
120 Correct 609 ms 9804 KB Output is correct
121 Correct 1196 ms 9796 KB Output is correct
122 Correct 1002 ms 9848 KB Output is correct
123 Correct 3043 ms 9856 KB Output is correct
124 Correct 2174 ms 10040 KB Output is correct
125 Correct 2362 ms 10276 KB Output is correct
126 Execution timed out 7073 ms 11348 KB Time limit exceeded
127 Halted 0 ms 0 KB -