답안 #971701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971701 2024-04-29T07:56:33 Z Szil Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 8112 KB
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
 
const int MAXN = 300'001;
const int BLOCK = 650;

struct Query {
    int l, r, idx;
 
    bool operator<(Query x) const {
        if (l / BLOCK == x.l / BLOCK) return (l / BLOCK) & 1 ? r > x.r : r < x.r;
        return l < x.l;
    }
};
 
int a[MAXN], b[MAXN]; 
map<int, int> c;
ll ans[MAXN], negyzet[MAXN];
 
void add(int i) {
    int v = a[i];
    if (b[v]) {
        c[b[v]]--;
        if (c[b[v]] == 0) c.erase(b[v]);
    }
    b[v]++;
    c[b[v]]++;
}
 
void remove(int i) {
    int v = a[i];
    c[b[v]]--;
    if (c[b[v]] == 0) c.erase(b[v]);
    b[v]--;
    if (b[v]) {
        c[b[v]]++;
    }
}
 
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, q; cin >> n >> q;
    for (ll i = 1; i <= n; i++) {
        negyzet[i] = negyzet[i-1] + i*i;
    }
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    vector<Query> qrys(q);
    for (int i = 0; i < q; i++) {
        cin >> qrys[i].l >> qrys[i].r;
        qrys[i].idx = i+1;
    }
    sort(qrys.begin(), qrys.end());
    int l = 1, r = 0;
    for (auto [nl, nr, idx] : qrys) {
        while (r < nr) add(++r);
        while (r > nr) remove(r--);
        while (l < nl) remove(l++);
        while (l > nl) add(--l);
 
        deque<pair<ll, ll>> dq;
        int d = 0;
 
        auto push = [&](int a, int b) {
            if (d) dq.emplace_back(a, b);
            else dq.emplace_front(a, b);
            d ^= 1;
        };


        for (auto it = c.rbegin(); it != c.rend(); it++) {
            auto [len, cnt] = *it;
            if (cnt & 1) {
                push(len, 1);
                cnt--;
            }
            push(len, cnt/2);
            push(len, cnt/2);
        }
 
        auto sum_arit = [&](ll start, ll d, ll cnt) {
            return start*cnt + d * (cnt - 1) * cnt / 2;
        };
 
        ll pref = 0;
        int x = nr-nl+1;
        for (auto [len, cnt] : dq) {
            ans[idx] += cnt * (len*(len+1)/2);
            ans[idx] += cnt * (len*(x-len));
            ans[idx] += (x - len) * sum_arit(pref, len, cnt);
            ans[idx] -= pref * pref * cnt;
            ans[idx] -= pref * sum_arit(0, 2*len, cnt);
            ans[idx] -= len * len * negyzet[cnt-1];
            pref += len*cnt;
        }
    }
 
    for (int i = 1; i <= q; i++) {
        cout << ans[i] << "\n";
    }
    return 0;
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:58:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |     for (auto [nl, nr, idx] : qrys) {
      |               ^
diversity.cpp:75:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   75 |             auto [len, cnt] = *it;
      |                  ^
diversity.cpp:90:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   90 |         for (auto [len, cnt] : dq) {
      |                   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2480 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 16 ms 4952 KB Output is correct
5 Correct 25 ms 4972 KB Output is correct
6 Correct 40 ms 7260 KB Output is correct
7 Correct 36 ms 7256 KB Output is correct
8 Correct 40 ms 7260 KB Output is correct
9 Correct 40 ms 7264 KB Output is correct
10 Correct 43 ms 7260 KB Output is correct
11 Correct 39 ms 7256 KB Output is correct
12 Correct 40 ms 7260 KB Output is correct
13 Correct 40 ms 7260 KB Output is correct
14 Correct 39 ms 7260 KB Output is correct
15 Correct 41 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 16 ms 4952 KB Output is correct
5 Correct 25 ms 4972 KB Output is correct
6 Correct 40 ms 7260 KB Output is correct
7 Correct 36 ms 7256 KB Output is correct
8 Correct 40 ms 7260 KB Output is correct
9 Correct 40 ms 7264 KB Output is correct
10 Correct 43 ms 7260 KB Output is correct
11 Correct 39 ms 7256 KB Output is correct
12 Correct 40 ms 7260 KB Output is correct
13 Correct 40 ms 7260 KB Output is correct
14 Correct 39 ms 7260 KB Output is correct
15 Correct 41 ms 7260 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 16 ms 4736 KB Output is correct
20 Correct 31 ms 4956 KB Output is correct
21 Correct 45 ms 7356 KB Output is correct
22 Correct 47 ms 7260 KB Output is correct
23 Correct 46 ms 7256 KB Output is correct
24 Correct 47 ms 7300 KB Output is correct
25 Correct 48 ms 7260 KB Output is correct
26 Correct 49 ms 7764 KB Output is correct
27 Correct 46 ms 7356 KB Output is correct
28 Correct 46 ms 7260 KB Output is correct
29 Correct 51 ms 7256 KB Output is correct
30 Correct 47 ms 7256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 16 ms 4952 KB Output is correct
5 Correct 25 ms 4972 KB Output is correct
6 Correct 40 ms 7260 KB Output is correct
7 Correct 36 ms 7256 KB Output is correct
8 Correct 40 ms 7260 KB Output is correct
9 Correct 40 ms 7264 KB Output is correct
10 Correct 43 ms 7260 KB Output is correct
11 Correct 39 ms 7256 KB Output is correct
12 Correct 40 ms 7260 KB Output is correct
13 Correct 40 ms 7260 KB Output is correct
14 Correct 39 ms 7260 KB Output is correct
15 Correct 41 ms 7260 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 16 ms 4736 KB Output is correct
20 Correct 31 ms 4956 KB Output is correct
21 Correct 45 ms 7356 KB Output is correct
22 Correct 47 ms 7260 KB Output is correct
23 Correct 46 ms 7256 KB Output is correct
24 Correct 47 ms 7300 KB Output is correct
25 Correct 48 ms 7260 KB Output is correct
26 Correct 49 ms 7764 KB Output is correct
27 Correct 46 ms 7356 KB Output is correct
28 Correct 46 ms 7260 KB Output is correct
29 Correct 51 ms 7256 KB Output is correct
30 Correct 47 ms 7256 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 3 ms 2396 KB Output is correct
35 Correct 2 ms 2396 KB Output is correct
36 Correct 2 ms 2396 KB Output is correct
37 Correct 9 ms 4696 KB Output is correct
38 Correct 8 ms 4696 KB Output is correct
39 Correct 17 ms 4700 KB Output is correct
40 Correct 39 ms 5068 KB Output is correct
41 Correct 62 ms 7516 KB Output is correct
42 Correct 68 ms 7516 KB Output is correct
43 Correct 64 ms 7504 KB Output is correct
44 Correct 63 ms 7516 KB Output is correct
45 Correct 62 ms 7512 KB Output is correct
46 Correct 60 ms 7504 KB Output is correct
47 Correct 62 ms 7528 KB Output is correct
48 Correct 61 ms 7504 KB Output is correct
49 Correct 65 ms 7508 KB Output is correct
50 Correct 62 ms 7512 KB Output is correct
51 Correct 62 ms 7508 KB Output is correct
52 Correct 61 ms 7508 KB Output is correct
53 Correct 62 ms 7508 KB Output is correct
54 Correct 63 ms 7512 KB Output is correct
55 Correct 62 ms 7516 KB Output is correct
56 Correct 60 ms 7504 KB Output is correct
57 Correct 68 ms 7756 KB Output is correct
58 Correct 65 ms 7516 KB Output is correct
59 Correct 63 ms 7508 KB Output is correct
60 Correct 62 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2480 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 16 ms 4952 KB Output is correct
15 Correct 25 ms 4972 KB Output is correct
16 Correct 40 ms 7260 KB Output is correct
17 Correct 36 ms 7256 KB Output is correct
18 Correct 40 ms 7260 KB Output is correct
19 Correct 40 ms 7264 KB Output is correct
20 Correct 43 ms 7260 KB Output is correct
21 Correct 39 ms 7256 KB Output is correct
22 Correct 40 ms 7260 KB Output is correct
23 Correct 40 ms 7260 KB Output is correct
24 Correct 39 ms 7260 KB Output is correct
25 Correct 41 ms 7260 KB Output is correct
26 Correct 1 ms 2392 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 16 ms 4736 KB Output is correct
30 Correct 31 ms 4956 KB Output is correct
31 Correct 45 ms 7356 KB Output is correct
32 Correct 47 ms 7260 KB Output is correct
33 Correct 46 ms 7256 KB Output is correct
34 Correct 47 ms 7300 KB Output is correct
35 Correct 48 ms 7260 KB Output is correct
36 Correct 49 ms 7764 KB Output is correct
37 Correct 46 ms 7356 KB Output is correct
38 Correct 46 ms 7260 KB Output is correct
39 Correct 51 ms 7256 KB Output is correct
40 Correct 47 ms 7256 KB Output is correct
41 Correct 1 ms 2396 KB Output is correct
42 Correct 1 ms 2396 KB Output is correct
43 Correct 1 ms 2396 KB Output is correct
44 Correct 3 ms 2396 KB Output is correct
45 Correct 2 ms 2396 KB Output is correct
46 Correct 2 ms 2396 KB Output is correct
47 Correct 9 ms 4696 KB Output is correct
48 Correct 8 ms 4696 KB Output is correct
49 Correct 17 ms 4700 KB Output is correct
50 Correct 39 ms 5068 KB Output is correct
51 Correct 62 ms 7516 KB Output is correct
52 Correct 68 ms 7516 KB Output is correct
53 Correct 64 ms 7504 KB Output is correct
54 Correct 63 ms 7516 KB Output is correct
55 Correct 62 ms 7512 KB Output is correct
56 Correct 60 ms 7504 KB Output is correct
57 Correct 62 ms 7528 KB Output is correct
58 Correct 61 ms 7504 KB Output is correct
59 Correct 65 ms 7508 KB Output is correct
60 Correct 62 ms 7512 KB Output is correct
61 Correct 62 ms 7508 KB Output is correct
62 Correct 61 ms 7508 KB Output is correct
63 Correct 62 ms 7508 KB Output is correct
64 Correct 63 ms 7512 KB Output is correct
65 Correct 62 ms 7516 KB Output is correct
66 Correct 60 ms 7504 KB Output is correct
67 Correct 68 ms 7756 KB Output is correct
68 Correct 65 ms 7516 KB Output is correct
69 Correct 63 ms 7508 KB Output is correct
70 Correct 62 ms 7516 KB Output is correct
71 Correct 7 ms 4696 KB Output is correct
72 Correct 8 ms 4696 KB Output is correct
73 Correct 7 ms 4696 KB Output is correct
74 Correct 7 ms 4700 KB Output is correct
75 Correct 7 ms 4700 KB Output is correct
76 Correct 11 ms 4700 KB Output is correct
77 Correct 11 ms 4696 KB Output is correct
78 Correct 12 ms 4700 KB Output is correct
79 Correct 11 ms 4700 KB Output is correct
80 Correct 11 ms 4732 KB Output is correct
81 Correct 22 ms 4956 KB Output is correct
82 Correct 23 ms 4992 KB Output is correct
83 Correct 22 ms 5064 KB Output is correct
84 Correct 22 ms 4952 KB Output is correct
85 Correct 22 ms 4956 KB Output is correct
86 Correct 27 ms 4900 KB Output is correct
87 Correct 20 ms 4952 KB Output is correct
88 Correct 21 ms 4956 KB Output is correct
89 Correct 21 ms 4956 KB Output is correct
90 Correct 21 ms 5064 KB Output is correct
91 Correct 25 ms 7260 KB Output is correct
92 Correct 25 ms 7260 KB Output is correct
93 Correct 25 ms 7260 KB Output is correct
94 Correct 25 ms 7260 KB Output is correct
95 Correct 29 ms 7772 KB Output is correct
96 Correct 24 ms 7260 KB Output is correct
97 Correct 26 ms 7500 KB Output is correct
98 Correct 25 ms 7468 KB Output is correct
99 Correct 26 ms 7260 KB Output is correct
100 Correct 27 ms 7260 KB Output is correct
101 Correct 25 ms 7444 KB Output is correct
102 Correct 25 ms 7260 KB Output is correct
103 Correct 25 ms 7300 KB Output is correct
104 Correct 25 ms 7508 KB Output is correct
105 Correct 25 ms 7260 KB Output is correct
106 Correct 24 ms 7260 KB Output is correct
107 Correct 24 ms 7496 KB Output is correct
108 Correct 24 ms 7300 KB Output is correct
109 Correct 23 ms 7292 KB Output is correct
110 Correct 24 ms 7260 KB Output is correct
111 Correct 24 ms 7260 KB Output is correct
112 Correct 23 ms 7260 KB Output is correct
113 Correct 24 ms 7304 KB Output is correct
114 Correct 24 ms 7256 KB Output is correct
115 Correct 24 ms 7420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2480 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 16 ms 4952 KB Output is correct
15 Correct 25 ms 4972 KB Output is correct
16 Correct 40 ms 7260 KB Output is correct
17 Correct 36 ms 7256 KB Output is correct
18 Correct 40 ms 7260 KB Output is correct
19 Correct 40 ms 7264 KB Output is correct
20 Correct 43 ms 7260 KB Output is correct
21 Correct 39 ms 7256 KB Output is correct
22 Correct 40 ms 7260 KB Output is correct
23 Correct 40 ms 7260 KB Output is correct
24 Correct 39 ms 7260 KB Output is correct
25 Correct 41 ms 7260 KB Output is correct
26 Correct 1 ms 2392 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 16 ms 4736 KB Output is correct
30 Correct 31 ms 4956 KB Output is correct
31 Correct 45 ms 7356 KB Output is correct
32 Correct 47 ms 7260 KB Output is correct
33 Correct 46 ms 7256 KB Output is correct
34 Correct 47 ms 7300 KB Output is correct
35 Correct 48 ms 7260 KB Output is correct
36 Correct 49 ms 7764 KB Output is correct
37 Correct 46 ms 7356 KB Output is correct
38 Correct 46 ms 7260 KB Output is correct
39 Correct 51 ms 7256 KB Output is correct
40 Correct 47 ms 7256 KB Output is correct
41 Correct 1 ms 2396 KB Output is correct
42 Correct 1 ms 2396 KB Output is correct
43 Correct 1 ms 2396 KB Output is correct
44 Correct 3 ms 2396 KB Output is correct
45 Correct 2 ms 2396 KB Output is correct
46 Correct 2 ms 2396 KB Output is correct
47 Correct 9 ms 4696 KB Output is correct
48 Correct 8 ms 4696 KB Output is correct
49 Correct 17 ms 4700 KB Output is correct
50 Correct 39 ms 5068 KB Output is correct
51 Correct 62 ms 7516 KB Output is correct
52 Correct 68 ms 7516 KB Output is correct
53 Correct 64 ms 7504 KB Output is correct
54 Correct 63 ms 7516 KB Output is correct
55 Correct 62 ms 7512 KB Output is correct
56 Correct 60 ms 7504 KB Output is correct
57 Correct 62 ms 7528 KB Output is correct
58 Correct 61 ms 7504 KB Output is correct
59 Correct 65 ms 7508 KB Output is correct
60 Correct 62 ms 7512 KB Output is correct
61 Correct 62 ms 7508 KB Output is correct
62 Correct 61 ms 7508 KB Output is correct
63 Correct 62 ms 7508 KB Output is correct
64 Correct 63 ms 7512 KB Output is correct
65 Correct 62 ms 7516 KB Output is correct
66 Correct 60 ms 7504 KB Output is correct
67 Correct 68 ms 7756 KB Output is correct
68 Correct 65 ms 7516 KB Output is correct
69 Correct 63 ms 7508 KB Output is correct
70 Correct 62 ms 7516 KB Output is correct
71 Correct 7 ms 4696 KB Output is correct
72 Correct 8 ms 4696 KB Output is correct
73 Correct 7 ms 4696 KB Output is correct
74 Correct 7 ms 4700 KB Output is correct
75 Correct 7 ms 4700 KB Output is correct
76 Correct 11 ms 4700 KB Output is correct
77 Correct 11 ms 4696 KB Output is correct
78 Correct 12 ms 4700 KB Output is correct
79 Correct 11 ms 4700 KB Output is correct
80 Correct 11 ms 4732 KB Output is correct
81 Correct 22 ms 4956 KB Output is correct
82 Correct 23 ms 4992 KB Output is correct
83 Correct 22 ms 5064 KB Output is correct
84 Correct 22 ms 4952 KB Output is correct
85 Correct 22 ms 4956 KB Output is correct
86 Correct 27 ms 4900 KB Output is correct
87 Correct 20 ms 4952 KB Output is correct
88 Correct 21 ms 4956 KB Output is correct
89 Correct 21 ms 4956 KB Output is correct
90 Correct 21 ms 5064 KB Output is correct
91 Correct 25 ms 7260 KB Output is correct
92 Correct 25 ms 7260 KB Output is correct
93 Correct 25 ms 7260 KB Output is correct
94 Correct 25 ms 7260 KB Output is correct
95 Correct 29 ms 7772 KB Output is correct
96 Correct 24 ms 7260 KB Output is correct
97 Correct 26 ms 7500 KB Output is correct
98 Correct 25 ms 7468 KB Output is correct
99 Correct 26 ms 7260 KB Output is correct
100 Correct 27 ms 7260 KB Output is correct
101 Correct 25 ms 7444 KB Output is correct
102 Correct 25 ms 7260 KB Output is correct
103 Correct 25 ms 7300 KB Output is correct
104 Correct 25 ms 7508 KB Output is correct
105 Correct 25 ms 7260 KB Output is correct
106 Correct 24 ms 7260 KB Output is correct
107 Correct 24 ms 7496 KB Output is correct
108 Correct 24 ms 7300 KB Output is correct
109 Correct 23 ms 7292 KB Output is correct
110 Correct 24 ms 7260 KB Output is correct
111 Correct 24 ms 7260 KB Output is correct
112 Correct 23 ms 7260 KB Output is correct
113 Correct 24 ms 7304 KB Output is correct
114 Correct 24 ms 7256 KB Output is correct
115 Correct 24 ms 7420 KB Output is correct
116 Correct 1274 ms 3824 KB Output is correct
117 Correct 1153 ms 3920 KB Output is correct
118 Correct 1955 ms 6232 KB Output is correct
119 Correct 1410 ms 6044 KB Output is correct
120 Correct 923 ms 6240 KB Output is correct
121 Correct 1999 ms 6252 KB Output is correct
122 Correct 1380 ms 6240 KB Output is correct
123 Correct 5559 ms 6592 KB Output is correct
124 Correct 2612 ms 6568 KB Output is correct
125 Correct 1834 ms 6560 KB Output is correct
126 Execution timed out 7017 ms 8112 KB Time limit exceeded
127 Halted 0 ms 0 KB -