답안 #858397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
858397 2023-10-08T11:01:26 Z danikoynov Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 23404 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;
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], prec[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 --)
    {
        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;
    }


    ll tot = 0, pref = 0;
    for (int i = 0; i < dq.size(); i ++)
    {
        ll l = dq[i].second;
        tot = tot + l * pref * pref;
        tot = tot + l * (l + 1) * pref * dq[i].first;
        tot = tot + prec[l] * dq[i].first * dq[i].first;
        pref = pref + dq[i].first * l;
        ///cout << pref << " " << tot << " " << dq[i].first << " " << dq[i].second << endl;
    }
    tot = tot - pref * pref;

    ll suff = 0;
    for (int i = dq.size() - 1; i >= 0; i --)
    {
        ll l = dq[i].second;
        tot = tot + l * suff * suff;
        tot = tot + l * (l + 1) * suff * dq[i].first;
        tot = tot + prec[l] * dq[i].first * dq[i].first;
        suff = suff + dq[i].first * l;
    }
    tot = tot - suff * suff;

    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];
    for (int i = 1; i <= n; i ++)
    {
        prec[i] = prec[i - 1] + (ll)(i) * (ll)(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 ++)
      |                     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 15 ms 8840 KB Output is correct
5 Correct 27 ms 9052 KB Output is correct
6 Correct 46 ms 11352 KB Output is correct
7 Correct 39 ms 11596 KB Output is correct
8 Correct 45 ms 11596 KB Output is correct
9 Correct 45 ms 11604 KB Output is correct
10 Correct 43 ms 11600 KB Output is correct
11 Correct 44 ms 11596 KB Output is correct
12 Correct 46 ms 11596 KB Output is correct
13 Correct 44 ms 11604 KB Output is correct
14 Correct 43 ms 11596 KB Output is correct
15 Correct 44 ms 11600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 15 ms 8840 KB Output is correct
5 Correct 27 ms 9052 KB Output is correct
6 Correct 46 ms 11352 KB Output is correct
7 Correct 39 ms 11596 KB Output is correct
8 Correct 45 ms 11596 KB Output is correct
9 Correct 45 ms 11604 KB Output is correct
10 Correct 43 ms 11600 KB Output is correct
11 Correct 44 ms 11596 KB Output is correct
12 Correct 46 ms 11596 KB Output is correct
13 Correct 44 ms 11604 KB Output is correct
14 Correct 43 ms 11596 KB Output is correct
15 Correct 44 ms 11600 KB Output is correct
16 Correct 1 ms 6488 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 3 ms 6492 KB Output is correct
19 Correct 18 ms 8840 KB Output is correct
20 Correct 35 ms 8916 KB Output is correct
21 Correct 53 ms 11600 KB Output is correct
22 Correct 55 ms 11600 KB Output is correct
23 Correct 54 ms 11600 KB Output is correct
24 Correct 55 ms 11600 KB Output is correct
25 Correct 54 ms 11604 KB Output is correct
26 Correct 55 ms 11352 KB Output is correct
27 Correct 53 ms 11352 KB Output is correct
28 Correct 51 ms 11600 KB Output is correct
29 Correct 52 ms 11596 KB Output is correct
30 Correct 55 ms 11600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 15 ms 8840 KB Output is correct
5 Correct 27 ms 9052 KB Output is correct
6 Correct 46 ms 11352 KB Output is correct
7 Correct 39 ms 11596 KB Output is correct
8 Correct 45 ms 11596 KB Output is correct
9 Correct 45 ms 11604 KB Output is correct
10 Correct 43 ms 11600 KB Output is correct
11 Correct 44 ms 11596 KB Output is correct
12 Correct 46 ms 11596 KB Output is correct
13 Correct 44 ms 11604 KB Output is correct
14 Correct 43 ms 11596 KB Output is correct
15 Correct 44 ms 11600 KB Output is correct
16 Correct 1 ms 6488 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 3 ms 6492 KB Output is correct
19 Correct 18 ms 8840 KB Output is correct
20 Correct 35 ms 8916 KB Output is correct
21 Correct 53 ms 11600 KB Output is correct
22 Correct 55 ms 11600 KB Output is correct
23 Correct 54 ms 11600 KB Output is correct
24 Correct 55 ms 11600 KB Output is correct
25 Correct 54 ms 11604 KB Output is correct
26 Correct 55 ms 11352 KB Output is correct
27 Correct 53 ms 11352 KB Output is correct
28 Correct 51 ms 11600 KB Output is correct
29 Correct 52 ms 11596 KB Output is correct
30 Correct 55 ms 11600 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 3 ms 6492 KB Output is correct
35 Correct 3 ms 6492 KB Output is correct
36 Correct 3 ms 6748 KB Output is correct
37 Correct 15 ms 8796 KB Output is correct
38 Correct 13 ms 8796 KB Output is correct
39 Correct 28 ms 8796 KB Output is correct
40 Correct 59 ms 9216 KB Output is correct
41 Correct 98 ms 11456 KB Output is correct
42 Correct 97 ms 11644 KB Output is correct
43 Correct 93 ms 11604 KB Output is correct
44 Correct 93 ms 11648 KB Output is correct
45 Correct 90 ms 11640 KB Output is correct
46 Correct 89 ms 11520 KB Output is correct
47 Correct 92 ms 11604 KB Output is correct
48 Correct 92 ms 11632 KB Output is correct
49 Correct 94 ms 11604 KB Output is correct
50 Correct 95 ms 11608 KB Output is correct
51 Correct 91 ms 11644 KB Output is correct
52 Correct 90 ms 11604 KB Output is correct
53 Correct 93 ms 11652 KB Output is correct
54 Correct 94 ms 11640 KB Output is correct
55 Correct 91 ms 11656 KB Output is correct
56 Correct 94 ms 11644 KB Output is correct
57 Correct 93 ms 11600 KB Output is correct
58 Correct 90 ms 11640 KB Output is correct
59 Correct 90 ms 11640 KB Output is correct
60 Correct 90 ms 11600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 3 ms 6492 KB Output is correct
14 Correct 15 ms 8840 KB Output is correct
15 Correct 27 ms 9052 KB Output is correct
16 Correct 46 ms 11352 KB Output is correct
17 Correct 39 ms 11596 KB Output is correct
18 Correct 45 ms 11596 KB Output is correct
19 Correct 45 ms 11604 KB Output is correct
20 Correct 43 ms 11600 KB Output is correct
21 Correct 44 ms 11596 KB Output is correct
22 Correct 46 ms 11596 KB Output is correct
23 Correct 44 ms 11604 KB Output is correct
24 Correct 43 ms 11596 KB Output is correct
25 Correct 44 ms 11600 KB Output is correct
26 Correct 1 ms 6488 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 3 ms 6492 KB Output is correct
29 Correct 18 ms 8840 KB Output is correct
30 Correct 35 ms 8916 KB Output is correct
31 Correct 53 ms 11600 KB Output is correct
32 Correct 55 ms 11600 KB Output is correct
33 Correct 54 ms 11600 KB Output is correct
34 Correct 55 ms 11600 KB Output is correct
35 Correct 54 ms 11604 KB Output is correct
36 Correct 55 ms 11352 KB Output is correct
37 Correct 53 ms 11352 KB Output is correct
38 Correct 51 ms 11600 KB Output is correct
39 Correct 52 ms 11596 KB Output is correct
40 Correct 55 ms 11600 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 3 ms 6492 KB Output is correct
45 Correct 3 ms 6492 KB Output is correct
46 Correct 3 ms 6748 KB Output is correct
47 Correct 15 ms 8796 KB Output is correct
48 Correct 13 ms 8796 KB Output is correct
49 Correct 28 ms 8796 KB Output is correct
50 Correct 59 ms 9216 KB Output is correct
51 Correct 98 ms 11456 KB Output is correct
52 Correct 97 ms 11644 KB Output is correct
53 Correct 93 ms 11604 KB Output is correct
54 Correct 93 ms 11648 KB Output is correct
55 Correct 90 ms 11640 KB Output is correct
56 Correct 89 ms 11520 KB Output is correct
57 Correct 92 ms 11604 KB Output is correct
58 Correct 92 ms 11632 KB Output is correct
59 Correct 94 ms 11604 KB Output is correct
60 Correct 95 ms 11608 KB Output is correct
61 Correct 91 ms 11644 KB Output is correct
62 Correct 90 ms 11604 KB Output is correct
63 Correct 93 ms 11652 KB Output is correct
64 Correct 94 ms 11640 KB Output is correct
65 Correct 91 ms 11656 KB Output is correct
66 Correct 94 ms 11644 KB Output is correct
67 Correct 93 ms 11600 KB Output is correct
68 Correct 90 ms 11640 KB Output is correct
69 Correct 90 ms 11640 KB Output is correct
70 Correct 90 ms 11600 KB Output is correct
71 Correct 14 ms 8796 KB Output is correct
72 Correct 13 ms 8924 KB Output is correct
73 Correct 13 ms 8796 KB Output is correct
74 Correct 13 ms 8796 KB Output is correct
75 Correct 14 ms 8796 KB Output is correct
76 Correct 26 ms 9052 KB Output is correct
77 Correct 26 ms 9052 KB Output is correct
78 Correct 27 ms 9052 KB Output is correct
79 Correct 27 ms 9044 KB Output is correct
80 Correct 26 ms 9044 KB Output is correct
81 Correct 55 ms 9552 KB Output is correct
82 Correct 54 ms 9552 KB Output is correct
83 Correct 54 ms 9552 KB Output is correct
84 Correct 56 ms 9552 KB Output is correct
85 Correct 54 ms 9552 KB Output is correct
86 Correct 59 ms 10000 KB Output is correct
87 Correct 58 ms 9992 KB Output is correct
88 Correct 63 ms 10020 KB Output is correct
89 Correct 59 ms 10064 KB Output is correct
90 Correct 60 ms 10064 KB Output is correct
91 Correct 113 ms 16208 KB Output is correct
92 Correct 114 ms 16096 KB Output is correct
93 Correct 115 ms 16204 KB Output is correct
94 Correct 113 ms 16296 KB Output is correct
95 Correct 127 ms 16236 KB Output is correct
96 Correct 142 ms 20904 KB Output is correct
97 Correct 145 ms 20816 KB Output is correct
98 Correct 149 ms 20796 KB Output is correct
99 Correct 144 ms 20824 KB Output is correct
100 Correct 153 ms 20956 KB Output is correct
101 Correct 158 ms 20796 KB Output is correct
102 Correct 144 ms 20776 KB Output is correct
103 Correct 145 ms 20816 KB Output is correct
104 Correct 178 ms 20948 KB Output is correct
105 Correct 144 ms 20816 KB Output is correct
106 Correct 156 ms 23128 KB Output is correct
107 Correct 172 ms 23404 KB Output is correct
108 Correct 158 ms 23336 KB Output is correct
109 Correct 168 ms 23180 KB Output is correct
110 Correct 160 ms 23168 KB Output is correct
111 Correct 181 ms 23120 KB Output is correct
112 Correct 180 ms 23120 KB Output is correct
113 Correct 159 ms 23120 KB Output is correct
114 Correct 162 ms 23200 KB Output is correct
115 Correct 157 ms 23244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 3 ms 6492 KB Output is correct
14 Correct 15 ms 8840 KB Output is correct
15 Correct 27 ms 9052 KB Output is correct
16 Correct 46 ms 11352 KB Output is correct
17 Correct 39 ms 11596 KB Output is correct
18 Correct 45 ms 11596 KB Output is correct
19 Correct 45 ms 11604 KB Output is correct
20 Correct 43 ms 11600 KB Output is correct
21 Correct 44 ms 11596 KB Output is correct
22 Correct 46 ms 11596 KB Output is correct
23 Correct 44 ms 11604 KB Output is correct
24 Correct 43 ms 11596 KB Output is correct
25 Correct 44 ms 11600 KB Output is correct
26 Correct 1 ms 6488 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 3 ms 6492 KB Output is correct
29 Correct 18 ms 8840 KB Output is correct
30 Correct 35 ms 8916 KB Output is correct
31 Correct 53 ms 11600 KB Output is correct
32 Correct 55 ms 11600 KB Output is correct
33 Correct 54 ms 11600 KB Output is correct
34 Correct 55 ms 11600 KB Output is correct
35 Correct 54 ms 11604 KB Output is correct
36 Correct 55 ms 11352 KB Output is correct
37 Correct 53 ms 11352 KB Output is correct
38 Correct 51 ms 11600 KB Output is correct
39 Correct 52 ms 11596 KB Output is correct
40 Correct 55 ms 11600 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 3 ms 6492 KB Output is correct
45 Correct 3 ms 6492 KB Output is correct
46 Correct 3 ms 6748 KB Output is correct
47 Correct 15 ms 8796 KB Output is correct
48 Correct 13 ms 8796 KB Output is correct
49 Correct 28 ms 8796 KB Output is correct
50 Correct 59 ms 9216 KB Output is correct
51 Correct 98 ms 11456 KB Output is correct
52 Correct 97 ms 11644 KB Output is correct
53 Correct 93 ms 11604 KB Output is correct
54 Correct 93 ms 11648 KB Output is correct
55 Correct 90 ms 11640 KB Output is correct
56 Correct 89 ms 11520 KB Output is correct
57 Correct 92 ms 11604 KB Output is correct
58 Correct 92 ms 11632 KB Output is correct
59 Correct 94 ms 11604 KB Output is correct
60 Correct 95 ms 11608 KB Output is correct
61 Correct 91 ms 11644 KB Output is correct
62 Correct 90 ms 11604 KB Output is correct
63 Correct 93 ms 11652 KB Output is correct
64 Correct 94 ms 11640 KB Output is correct
65 Correct 91 ms 11656 KB Output is correct
66 Correct 94 ms 11644 KB Output is correct
67 Correct 93 ms 11600 KB Output is correct
68 Correct 90 ms 11640 KB Output is correct
69 Correct 90 ms 11640 KB Output is correct
70 Correct 90 ms 11600 KB Output is correct
71 Correct 14 ms 8796 KB Output is correct
72 Correct 13 ms 8924 KB Output is correct
73 Correct 13 ms 8796 KB Output is correct
74 Correct 13 ms 8796 KB Output is correct
75 Correct 14 ms 8796 KB Output is correct
76 Correct 26 ms 9052 KB Output is correct
77 Correct 26 ms 9052 KB Output is correct
78 Correct 27 ms 9052 KB Output is correct
79 Correct 27 ms 9044 KB Output is correct
80 Correct 26 ms 9044 KB Output is correct
81 Correct 55 ms 9552 KB Output is correct
82 Correct 54 ms 9552 KB Output is correct
83 Correct 54 ms 9552 KB Output is correct
84 Correct 56 ms 9552 KB Output is correct
85 Correct 54 ms 9552 KB Output is correct
86 Correct 59 ms 10000 KB Output is correct
87 Correct 58 ms 9992 KB Output is correct
88 Correct 63 ms 10020 KB Output is correct
89 Correct 59 ms 10064 KB Output is correct
90 Correct 60 ms 10064 KB Output is correct
91 Correct 113 ms 16208 KB Output is correct
92 Correct 114 ms 16096 KB Output is correct
93 Correct 115 ms 16204 KB Output is correct
94 Correct 113 ms 16296 KB Output is correct
95 Correct 127 ms 16236 KB Output is correct
96 Correct 142 ms 20904 KB Output is correct
97 Correct 145 ms 20816 KB Output is correct
98 Correct 149 ms 20796 KB Output is correct
99 Correct 144 ms 20824 KB Output is correct
100 Correct 153 ms 20956 KB Output is correct
101 Correct 158 ms 20796 KB Output is correct
102 Correct 144 ms 20776 KB Output is correct
103 Correct 145 ms 20816 KB Output is correct
104 Correct 178 ms 20948 KB Output is correct
105 Correct 144 ms 20816 KB Output is correct
106 Correct 156 ms 23128 KB Output is correct
107 Correct 172 ms 23404 KB Output is correct
108 Correct 158 ms 23336 KB Output is correct
109 Correct 168 ms 23180 KB Output is correct
110 Correct 160 ms 23168 KB Output is correct
111 Correct 181 ms 23120 KB Output is correct
112 Correct 180 ms 23120 KB Output is correct
113 Correct 159 ms 23120 KB Output is correct
114 Correct 162 ms 23200 KB Output is correct
115 Correct 157 ms 23244 KB Output is correct
116 Correct 1044 ms 7532 KB Output is correct
117 Correct 952 ms 7540 KB Output is correct
118 Correct 1936 ms 9920 KB Output is correct
119 Correct 1385 ms 10052 KB Output is correct
120 Correct 965 ms 9812 KB Output is correct
121 Correct 2678 ms 9816 KB Output is correct
122 Correct 1881 ms 10004 KB Output is correct
123 Execution timed out 7063 ms 9556 KB Time limit exceeded
124 Halted 0 ms 0 KB -