답안 #956756

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
956756 2024-04-02T12:45:31 Z TAhmed33 Diversity (CEOI21_diversity) C++
64 / 100
7000 ms 10640 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
typedef long long ll;
const int MAXN = 3e5 + 1;
const int B = 700;
int a[MAXN], n, q;
array <int, 3> queries[MAXN];
ll ans[MAXN];
int freq[MAXN], freq2[2 * MAXN + 1];
set <int> t;
void add (int x) {
	if (freq2[freq[a[x]] + MAXN] == 1) t.erase(freq[a[x]]);
	freq2[freq[a[x]] + MAXN]--;
	freq[a[x]]++;
	freq2[freq[a[x]] + MAXN]++;
	if (freq2[freq[a[x]] + MAXN] == 1) t.insert(freq[a[x]]);
}
void rem (int x) {
	if (freq2[freq[a[x]] + MAXN] == 1) t.erase(freq[a[x]]);
	freq2[freq[a[x]] + MAXN]--;
	freq[a[x]]--;
	freq2[freq[a[x]] + MAXN]++;
	if (freq2[freq[a[x]] + MAXN] == 1) t.insert(freq[a[x]]);
}
int main () {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> q;
	freq2[MAXN] = MAXN; t.insert(0); 
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= q; i++) {
		int l, r; cin >> l >> r;
		queries[i] = {l, r, i};
	}
	sort(queries + 1, queries + q + 1, [&] (array <int, 3> &x, array <int, 3> &y) {
		return x[0] / B == y[0] / B ? x[1] < y[1] : x[0]  / B < y[0] / B;
	});
	int L = queries[1][0] + 1, R = queries[1][0];
	for (int i = 1; i <= q; i++) {
		while (L > queries[i][0]) add(--L);
		while (L < queries[i][0]) rem(L++);
		while (R > queries[i][1]) rem(R--);
		while (R < queries[i][1]) add(++R);
		ll dis = 0; ll n = R - L + 1;
		vector <pair <ll, ll>> pp[2];
		int c = 0;
		for (auto j : t) {
			if (!j) continue;
			dis += freq2[j + MAXN];
			int x = j, y = freq2[j + MAXN];
			if (y == 1) {
				pp[c].push_back({x, 1});
				c ^= 1; 
			} else {
				int l = (y + 1) / 2, r = y / 2;
				pp[c].push_back({x, l});
				pp[c ^ 1].push_back({x, r});
				c ^= (y & 1);
			}
		}
		reverse(pp[1].begin(), pp[1].end());
		ll cur = 0, pref = 0;
		for (auto [x, y] : pp[0]) {
			cur += pref * pref * y + y * (y - 1) / 2 * 2 * pref * x + x * x * (y - 1) * y * (2 * y - 1) / 6 + y * pref + x * y * (y - 1) / 2;
			cur += y * n * n - y * n * pref - n * y * (y + 1) / 2 * x + y * n;
			cur += -y * n * pref + pref * pref * y + pref * y * (y + 1) / 2 * x - y * pref;
			cur += -n * y * (y + 1) / 2 * x + y * (y + 1) / 2 * x * pref + x * x * y * (y + 1) * (2 * y + 1) / 6 - y * (y + 1) / 2 * x;
			pref += x * y;
		}	
		for (auto [x, y] : pp[1]) {
			cur += pref * pref * y + y * (y - 1) / 2 * 2 * pref * x + x * x * (y - 1) * y * (2 * y - 1) / 6 + y * pref + x * y * (y - 1) / 2;
			cur += y * n * n - y * n * pref - n * y * (y + 1) / 2 * x + y * n;
			cur += -y * n * pref + pref * pref * y + pref * y * (y + 1) / 2 * x - y * pref;
			cur += -n * y * (y + 1) / 2 * x + y * (y + 1) / 2 * x * pref + x * x * y * (y + 1) * (2 * y + 1) / 6 - y * (y + 1) / 2 * x;
			pref += x * y;
		}	
		ans[queries[i][2]] = n * (n + 1) / 2 * dis - cur / 2;
	}
	for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:63:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   63 |   for (auto [x, y] : pp[0]) {
      |             ^
diversity.cpp:70:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |   for (auto [x, y] : pp[1]) {
      |             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6744 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6604 KB Output is correct
6 Correct 1 ms 6488 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 6616 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 2 ms 6492 KB Output is correct
4 Correct 12 ms 6748 KB Output is correct
5 Correct 22 ms 7004 KB Output is correct
6 Correct 34 ms 7256 KB Output is correct
7 Correct 33 ms 7256 KB Output is correct
8 Correct 34 ms 7484 KB Output is correct
9 Correct 34 ms 7256 KB Output is correct
10 Correct 33 ms 7256 KB Output is correct
11 Correct 41 ms 7260 KB Output is correct
12 Correct 35 ms 7260 KB Output is correct
13 Correct 34 ms 7256 KB Output is correct
14 Correct 39 ms 7380 KB Output is correct
15 Correct 34 ms 7372 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 2 ms 6492 KB Output is correct
4 Correct 12 ms 6748 KB Output is correct
5 Correct 22 ms 7004 KB Output is correct
6 Correct 34 ms 7256 KB Output is correct
7 Correct 33 ms 7256 KB Output is correct
8 Correct 34 ms 7484 KB Output is correct
9 Correct 34 ms 7256 KB Output is correct
10 Correct 33 ms 7256 KB Output is correct
11 Correct 41 ms 7260 KB Output is correct
12 Correct 35 ms 7260 KB Output is correct
13 Correct 34 ms 7256 KB Output is correct
14 Correct 39 ms 7380 KB Output is correct
15 Correct 34 ms 7372 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 14 ms 6900 KB Output is correct
20 Correct 26 ms 7000 KB Output is correct
21 Correct 38 ms 7308 KB Output is correct
22 Correct 39 ms 7256 KB Output is correct
23 Correct 38 ms 7260 KB Output is correct
24 Correct 38 ms 7496 KB Output is correct
25 Correct 39 ms 7260 KB Output is correct
26 Correct 41 ms 7516 KB Output is correct
27 Correct 38 ms 7260 KB Output is correct
28 Correct 37 ms 7256 KB Output is correct
29 Correct 38 ms 7260 KB Output is correct
30 Correct 39 ms 7260 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 2 ms 6492 KB Output is correct
4 Correct 12 ms 6748 KB Output is correct
5 Correct 22 ms 7004 KB Output is correct
6 Correct 34 ms 7256 KB Output is correct
7 Correct 33 ms 7256 KB Output is correct
8 Correct 34 ms 7484 KB Output is correct
9 Correct 34 ms 7256 KB Output is correct
10 Correct 33 ms 7256 KB Output is correct
11 Correct 41 ms 7260 KB Output is correct
12 Correct 35 ms 7260 KB Output is correct
13 Correct 34 ms 7256 KB Output is correct
14 Correct 39 ms 7380 KB Output is correct
15 Correct 34 ms 7372 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 14 ms 6900 KB Output is correct
20 Correct 26 ms 7000 KB Output is correct
21 Correct 38 ms 7308 KB Output is correct
22 Correct 39 ms 7256 KB Output is correct
23 Correct 38 ms 7260 KB Output is correct
24 Correct 38 ms 7496 KB Output is correct
25 Correct 39 ms 7260 KB Output is correct
26 Correct 41 ms 7516 KB Output is correct
27 Correct 38 ms 7260 KB Output is correct
28 Correct 37 ms 7256 KB Output is correct
29 Correct 38 ms 7260 KB Output is correct
30 Correct 39 ms 7260 KB Output is correct
31 Correct 1 ms 6488 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 2 ms 6628 KB Output is correct
35 Correct 2 ms 6492 KB Output is correct
36 Correct 2 ms 6492 KB Output is correct
37 Correct 5 ms 6744 KB Output is correct
38 Correct 4 ms 6744 KB Output is correct
39 Correct 8 ms 7000 KB Output is correct
40 Correct 19 ms 7416 KB Output is correct
41 Correct 32 ms 7516 KB Output is correct
42 Correct 31 ms 7516 KB Output is correct
43 Correct 38 ms 7512 KB Output is correct
44 Correct 31 ms 7520 KB Output is correct
45 Correct 31 ms 7484 KB Output is correct
46 Correct 35 ms 7776 KB Output is correct
47 Correct 31 ms 7524 KB Output is correct
48 Correct 31 ms 8016 KB Output is correct
49 Correct 31 ms 7764 KB Output is correct
50 Correct 32 ms 7508 KB Output is correct
51 Correct 33 ms 7512 KB Output is correct
52 Correct 32 ms 7640 KB Output is correct
53 Correct 32 ms 7504 KB Output is correct
54 Correct 31 ms 7516 KB Output is correct
55 Correct 31 ms 7516 KB Output is correct
56 Correct 31 ms 7508 KB Output is correct
57 Correct 30 ms 7664 KB Output is correct
58 Correct 32 ms 7512 KB Output is correct
59 Correct 31 ms 7504 KB Output is correct
60 Correct 31 ms 7656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6744 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6604 KB Output is correct
6 Correct 1 ms 6488 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 6616 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 12 ms 6748 KB Output is correct
15 Correct 22 ms 7004 KB Output is correct
16 Correct 34 ms 7256 KB Output is correct
17 Correct 33 ms 7256 KB Output is correct
18 Correct 34 ms 7484 KB Output is correct
19 Correct 34 ms 7256 KB Output is correct
20 Correct 33 ms 7256 KB Output is correct
21 Correct 41 ms 7260 KB Output is correct
22 Correct 35 ms 7260 KB Output is correct
23 Correct 34 ms 7256 KB Output is correct
24 Correct 39 ms 7380 KB Output is correct
25 Correct 34 ms 7372 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 14 ms 6900 KB Output is correct
30 Correct 26 ms 7000 KB Output is correct
31 Correct 38 ms 7308 KB Output is correct
32 Correct 39 ms 7256 KB Output is correct
33 Correct 38 ms 7260 KB Output is correct
34 Correct 38 ms 7496 KB Output is correct
35 Correct 39 ms 7260 KB Output is correct
36 Correct 41 ms 7516 KB Output is correct
37 Correct 38 ms 7260 KB Output is correct
38 Correct 37 ms 7256 KB Output is correct
39 Correct 38 ms 7260 KB Output is correct
40 Correct 39 ms 7260 KB Output is correct
41 Correct 1 ms 6488 KB Output is correct
42 Correct 2 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 2 ms 6628 KB Output is correct
45 Correct 2 ms 6492 KB Output is correct
46 Correct 2 ms 6492 KB Output is correct
47 Correct 5 ms 6744 KB Output is correct
48 Correct 4 ms 6744 KB Output is correct
49 Correct 8 ms 7000 KB Output is correct
50 Correct 19 ms 7416 KB Output is correct
51 Correct 32 ms 7516 KB Output is correct
52 Correct 31 ms 7516 KB Output is correct
53 Correct 38 ms 7512 KB Output is correct
54 Correct 31 ms 7520 KB Output is correct
55 Correct 31 ms 7484 KB Output is correct
56 Correct 35 ms 7776 KB Output is correct
57 Correct 31 ms 7524 KB Output is correct
58 Correct 31 ms 8016 KB Output is correct
59 Correct 31 ms 7764 KB Output is correct
60 Correct 32 ms 7508 KB Output is correct
61 Correct 33 ms 7512 KB Output is correct
62 Correct 32 ms 7640 KB Output is correct
63 Correct 32 ms 7504 KB Output is correct
64 Correct 31 ms 7516 KB Output is correct
65 Correct 31 ms 7516 KB Output is correct
66 Correct 31 ms 7508 KB Output is correct
67 Correct 30 ms 7664 KB Output is correct
68 Correct 32 ms 7512 KB Output is correct
69 Correct 31 ms 7504 KB Output is correct
70 Correct 31 ms 7656 KB Output is correct
71 Correct 4 ms 6748 KB Output is correct
72 Correct 4 ms 6844 KB Output is correct
73 Correct 4 ms 6748 KB Output is correct
74 Correct 4 ms 6744 KB Output is correct
75 Correct 4 ms 6748 KB Output is correct
76 Correct 7 ms 6984 KB Output is correct
77 Correct 7 ms 7004 KB Output is correct
78 Correct 7 ms 7000 KB Output is correct
79 Correct 8 ms 7004 KB Output is correct
80 Correct 7 ms 7112 KB Output is correct
81 Correct 12 ms 7260 KB Output is correct
82 Correct 12 ms 7312 KB Output is correct
83 Correct 12 ms 7260 KB Output is correct
84 Correct 12 ms 7256 KB Output is correct
85 Correct 13 ms 7384 KB Output is correct
86 Correct 14 ms 7472 KB Output is correct
87 Correct 16 ms 7516 KB Output is correct
88 Correct 14 ms 7412 KB Output is correct
89 Correct 13 ms 7512 KB Output is correct
90 Correct 14 ms 7248 KB Output is correct
91 Correct 19 ms 8264 KB Output is correct
92 Correct 20 ms 8396 KB Output is correct
93 Correct 20 ms 8284 KB Output is correct
94 Correct 22 ms 8248 KB Output is correct
95 Correct 19 ms 8404 KB Output is correct
96 Correct 23 ms 8796 KB Output is correct
97 Correct 22 ms 8704 KB Output is correct
98 Correct 22 ms 8792 KB Output is correct
99 Correct 28 ms 8636 KB Output is correct
100 Correct 27 ms 8784 KB Output is correct
101 Correct 23 ms 8788 KB Output is correct
102 Correct 22 ms 8796 KB Output is correct
103 Correct 22 ms 8760 KB Output is correct
104 Correct 22 ms 8788 KB Output is correct
105 Correct 22 ms 8604 KB Output is correct
106 Correct 22 ms 8960 KB Output is correct
107 Correct 23 ms 8880 KB Output is correct
108 Correct 22 ms 8792 KB Output is correct
109 Correct 22 ms 8784 KB Output is correct
110 Correct 22 ms 8868 KB Output is correct
111 Correct 22 ms 8768 KB Output is correct
112 Correct 22 ms 8788 KB Output is correct
113 Correct 22 ms 8796 KB Output is correct
114 Correct 26 ms 8912 KB Output is correct
115 Correct 24 ms 9116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6744 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6604 KB Output is correct
6 Correct 1 ms 6488 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 6616 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 12 ms 6748 KB Output is correct
15 Correct 22 ms 7004 KB Output is correct
16 Correct 34 ms 7256 KB Output is correct
17 Correct 33 ms 7256 KB Output is correct
18 Correct 34 ms 7484 KB Output is correct
19 Correct 34 ms 7256 KB Output is correct
20 Correct 33 ms 7256 KB Output is correct
21 Correct 41 ms 7260 KB Output is correct
22 Correct 35 ms 7260 KB Output is correct
23 Correct 34 ms 7256 KB Output is correct
24 Correct 39 ms 7380 KB Output is correct
25 Correct 34 ms 7372 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 14 ms 6900 KB Output is correct
30 Correct 26 ms 7000 KB Output is correct
31 Correct 38 ms 7308 KB Output is correct
32 Correct 39 ms 7256 KB Output is correct
33 Correct 38 ms 7260 KB Output is correct
34 Correct 38 ms 7496 KB Output is correct
35 Correct 39 ms 7260 KB Output is correct
36 Correct 41 ms 7516 KB Output is correct
37 Correct 38 ms 7260 KB Output is correct
38 Correct 37 ms 7256 KB Output is correct
39 Correct 38 ms 7260 KB Output is correct
40 Correct 39 ms 7260 KB Output is correct
41 Correct 1 ms 6488 KB Output is correct
42 Correct 2 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 2 ms 6628 KB Output is correct
45 Correct 2 ms 6492 KB Output is correct
46 Correct 2 ms 6492 KB Output is correct
47 Correct 5 ms 6744 KB Output is correct
48 Correct 4 ms 6744 KB Output is correct
49 Correct 8 ms 7000 KB Output is correct
50 Correct 19 ms 7416 KB Output is correct
51 Correct 32 ms 7516 KB Output is correct
52 Correct 31 ms 7516 KB Output is correct
53 Correct 38 ms 7512 KB Output is correct
54 Correct 31 ms 7520 KB Output is correct
55 Correct 31 ms 7484 KB Output is correct
56 Correct 35 ms 7776 KB Output is correct
57 Correct 31 ms 7524 KB Output is correct
58 Correct 31 ms 8016 KB Output is correct
59 Correct 31 ms 7764 KB Output is correct
60 Correct 32 ms 7508 KB Output is correct
61 Correct 33 ms 7512 KB Output is correct
62 Correct 32 ms 7640 KB Output is correct
63 Correct 32 ms 7504 KB Output is correct
64 Correct 31 ms 7516 KB Output is correct
65 Correct 31 ms 7516 KB Output is correct
66 Correct 31 ms 7508 KB Output is correct
67 Correct 30 ms 7664 KB Output is correct
68 Correct 32 ms 7512 KB Output is correct
69 Correct 31 ms 7504 KB Output is correct
70 Correct 31 ms 7656 KB Output is correct
71 Correct 4 ms 6748 KB Output is correct
72 Correct 4 ms 6844 KB Output is correct
73 Correct 4 ms 6748 KB Output is correct
74 Correct 4 ms 6744 KB Output is correct
75 Correct 4 ms 6748 KB Output is correct
76 Correct 7 ms 6984 KB Output is correct
77 Correct 7 ms 7004 KB Output is correct
78 Correct 7 ms 7000 KB Output is correct
79 Correct 8 ms 7004 KB Output is correct
80 Correct 7 ms 7112 KB Output is correct
81 Correct 12 ms 7260 KB Output is correct
82 Correct 12 ms 7312 KB Output is correct
83 Correct 12 ms 7260 KB Output is correct
84 Correct 12 ms 7256 KB Output is correct
85 Correct 13 ms 7384 KB Output is correct
86 Correct 14 ms 7472 KB Output is correct
87 Correct 16 ms 7516 KB Output is correct
88 Correct 14 ms 7412 KB Output is correct
89 Correct 13 ms 7512 KB Output is correct
90 Correct 14 ms 7248 KB Output is correct
91 Correct 19 ms 8264 KB Output is correct
92 Correct 20 ms 8396 KB Output is correct
93 Correct 20 ms 8284 KB Output is correct
94 Correct 22 ms 8248 KB Output is correct
95 Correct 19 ms 8404 KB Output is correct
96 Correct 23 ms 8796 KB Output is correct
97 Correct 22 ms 8704 KB Output is correct
98 Correct 22 ms 8792 KB Output is correct
99 Correct 28 ms 8636 KB Output is correct
100 Correct 27 ms 8784 KB Output is correct
101 Correct 23 ms 8788 KB Output is correct
102 Correct 22 ms 8796 KB Output is correct
103 Correct 22 ms 8760 KB Output is correct
104 Correct 22 ms 8788 KB Output is correct
105 Correct 22 ms 8604 KB Output is correct
106 Correct 22 ms 8960 KB Output is correct
107 Correct 23 ms 8880 KB Output is correct
108 Correct 22 ms 8792 KB Output is correct
109 Correct 22 ms 8784 KB Output is correct
110 Correct 22 ms 8868 KB Output is correct
111 Correct 22 ms 8768 KB Output is correct
112 Correct 22 ms 8788 KB Output is correct
113 Correct 22 ms 8796 KB Output is correct
114 Correct 26 ms 8912 KB Output is correct
115 Correct 24 ms 9116 KB Output is correct
116 Correct 938 ms 10064 KB Output is correct
117 Correct 767 ms 10064 KB Output is correct
118 Correct 1444 ms 10324 KB Output is correct
119 Correct 624 ms 10320 KB Output is correct
120 Correct 344 ms 10104 KB Output is correct
121 Correct 791 ms 10384 KB Output is correct
122 Correct 438 ms 10364 KB Output is correct
123 Correct 2707 ms 10640 KB Output is correct
124 Correct 617 ms 10636 KB Output is correct
125 Correct 411 ms 10580 KB Output is correct
126 Execution timed out 7100 ms 10316 KB Time limit exceeded
127 Halted 0 ms 0 KB -