Submission #572485

# Submission time Handle Problem Language Result Execution time Memory
572485 2022-06-04T13:24:44 Z valerikk Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 2824 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 7;
const int Q = 5e4 + 11;
const int B = 450;

long long solve(vector<pair<int, int>> c) {
	reverse(c.begin(), c.end());

	int n = 0, k = 0;
	
	deque<pair<int, int>> q;

	int t = 0;
	for (const auto &[x, y] : c) {
		n += x * y;
		k += y;
		if (t) {
			q.push_back({x, (y + 1) / 2});	
			if (y > 1)
				q.push_front({x, y / 2});
		} else {
			q.push_front({x, (y + 1) / 2});
			if (y > 1)
				q.push_back({x, y / 2});
		}

		t = (t + y) & 1;
	}

	vector<pair<int, int>> z(q.begin(), q.end());

	long long all = n * 1ll * (n + 1) / 2 * k;

	long long bad = 0;

	long long sum = 0;
	for (int i = 0; i < (int) z.size(); ++i) {
		const auto &[x, y] = z[i];

		bad += sum * sum * y;
		bad += sum * x * y * (y - 1);
		bad += sum * y;
		bad += x * 1ll * y * (y - 1) / 2;
		bad += (y - 1) * 1ll * y * (2 * (y - 1) + 1) / 6 * x * x;

		sum += x * y;
	}

	sum = 0;
	for (int i = (int) z.size() - 1; i >= 0; --i) {
		const auto &[x, y] = z[i];

		bad += sum * sum * y;
		bad += sum * x * y * (y - 1);
		bad += sum * y;
		bad += x * 1ll * y * (y - 1) / 2;
		bad += (y - 1) * 1ll * y * (2 * (y - 1) + 1) / 6 * x * x;

		sum += x * y;
	}

	bad /= 2;

	// cout << all << " " << bad << "\n";

	long long res = all - bad;

	return res;
}

int n, q;
int a[N];
int l[Q], r[Q];
int ord[Q];

struct Cnt {
	int cnt[N];
	int scnt[N];
	set<int> s;

	void add(int x) {
		if (--scnt[cnt[x]] == 0)
			s.erase(cnt[x]);
		++cnt[x];
		if (++scnt[cnt[x]] == 1)
			s.insert(cnt[x]);
	}

	void remove(int x) {
		if (--scnt[cnt[x]] == 0)
			s.erase(cnt[x]);
		--cnt[x];
		if (++scnt[cnt[x]] == 1)
			s.insert(cnt[x]);
	}

	vector<pair<int, int>> get_cnts() {
		vector<pair<int, int>> res;
		res.reserve(s.size());
		for (const auto &i : s) {
			if (i != 0) 
				res.push_back({i, scnt[i]});
		}
		return res;
	}

	Cnt() {
		for (int i = 0; i < N; ++i) {
			++scnt[cnt[i]];
		}
		s.insert(0);
	}
} cnt;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> q;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}

	for (int i = 0; i < q; ++i) {
		cin >> l[i] >> r[i];
		--l[i];
	}

	iota(ord, ord + q, 0);
	sort(ord, ord + q, [&] (const int &i, const int &j) {
		return make_pair(l[i] / B, r[i]) < make_pair(l[j] / B, r[j]);
	});

	vector<long long> ans(q);

	int cl = 0, cr = 0;
	for (int i = 0; i < q; ++i) {
		while (cl > l[ord[i]]) {
			cnt.add(a[--cl]);
		}
		while (cr < r[ord[i]]) {
			cnt.add(a[cr++]);
		}
		while (cl < l[ord[i]]) {
			cnt.remove(a[cl++]);
		}
		while (cr > r[ord[i]]) {
			cnt.remove(a[--cr]);
		}

		ans[ord[i]] = solve(cnt.get_cnts());
	}

	for (int i = 0; i < q; ++i) {
		cout << ans[i] << "\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 17 ms 756 KB Output is correct
5 Correct 31 ms 1392 KB Output is correct
6 Correct 49 ms 1732 KB Output is correct
7 Correct 36 ms 2104 KB Output is correct
8 Correct 44 ms 1584 KB Output is correct
9 Correct 43 ms 1664 KB Output is correct
10 Correct 41 ms 1812 KB Output is correct
11 Correct 41 ms 1768 KB Output is correct
12 Correct 44 ms 1636 KB Output is correct
13 Correct 42 ms 1636 KB Output is correct
14 Correct 44 ms 1812 KB Output is correct
15 Correct 40 ms 1668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 17 ms 756 KB Output is correct
5 Correct 31 ms 1392 KB Output is correct
6 Correct 49 ms 1732 KB Output is correct
7 Correct 36 ms 2104 KB Output is correct
8 Correct 44 ms 1584 KB Output is correct
9 Correct 43 ms 1664 KB Output is correct
10 Correct 41 ms 1812 KB Output is correct
11 Correct 41 ms 1768 KB Output is correct
12 Correct 44 ms 1636 KB Output is correct
13 Correct 42 ms 1636 KB Output is correct
14 Correct 44 ms 1812 KB Output is correct
15 Correct 40 ms 1668 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 24 ms 780 KB Output is correct
20 Correct 35 ms 1156 KB Output is correct
21 Correct 49 ms 1684 KB Output is correct
22 Correct 54 ms 1632 KB Output is correct
23 Correct 48 ms 1632 KB Output is correct
24 Correct 51 ms 1532 KB Output is correct
25 Correct 53 ms 1656 KB Output is correct
26 Correct 51 ms 1624 KB Output is correct
27 Correct 51 ms 1684 KB Output is correct
28 Correct 45 ms 1692 KB Output is correct
29 Correct 47 ms 1616 KB Output is correct
30 Correct 47 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 17 ms 756 KB Output is correct
5 Correct 31 ms 1392 KB Output is correct
6 Correct 49 ms 1732 KB Output is correct
7 Correct 36 ms 2104 KB Output is correct
8 Correct 44 ms 1584 KB Output is correct
9 Correct 43 ms 1664 KB Output is correct
10 Correct 41 ms 1812 KB Output is correct
11 Correct 41 ms 1768 KB Output is correct
12 Correct 44 ms 1636 KB Output is correct
13 Correct 42 ms 1636 KB Output is correct
14 Correct 44 ms 1812 KB Output is correct
15 Correct 40 ms 1668 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 24 ms 780 KB Output is correct
20 Correct 35 ms 1156 KB Output is correct
21 Correct 49 ms 1684 KB Output is correct
22 Correct 54 ms 1632 KB Output is correct
23 Correct 48 ms 1632 KB Output is correct
24 Correct 51 ms 1532 KB Output is correct
25 Correct 53 ms 1656 KB Output is correct
26 Correct 51 ms 1624 KB Output is correct
27 Correct 51 ms 1684 KB Output is correct
28 Correct 45 ms 1692 KB Output is correct
29 Correct 47 ms 1616 KB Output is correct
30 Correct 47 ms 1612 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 7 ms 468 KB Output is correct
38 Correct 8 ms 468 KB Output is correct
39 Correct 11 ms 724 KB Output is correct
40 Correct 23 ms 1156 KB Output is correct
41 Correct 39 ms 1512 KB Output is correct
42 Correct 41 ms 1540 KB Output is correct
43 Correct 41 ms 1548 KB Output is correct
44 Correct 46 ms 1500 KB Output is correct
45 Correct 40 ms 1576 KB Output is correct
46 Correct 39 ms 1540 KB Output is correct
47 Correct 37 ms 1540 KB Output is correct
48 Correct 39 ms 1552 KB Output is correct
49 Correct 37 ms 1492 KB Output is correct
50 Correct 43 ms 1540 KB Output is correct
51 Correct 54 ms 1544 KB Output is correct
52 Correct 38 ms 1492 KB Output is correct
53 Correct 38 ms 1484 KB Output is correct
54 Correct 38 ms 1536 KB Output is correct
55 Correct 39 ms 1452 KB Output is correct
56 Correct 36 ms 1536 KB Output is correct
57 Correct 36 ms 1552 KB Output is correct
58 Correct 39 ms 1436 KB Output is correct
59 Correct 44 ms 1484 KB Output is correct
60 Correct 47 ms 1560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 17 ms 756 KB Output is correct
15 Correct 31 ms 1392 KB Output is correct
16 Correct 49 ms 1732 KB Output is correct
17 Correct 36 ms 2104 KB Output is correct
18 Correct 44 ms 1584 KB Output is correct
19 Correct 43 ms 1664 KB Output is correct
20 Correct 41 ms 1812 KB Output is correct
21 Correct 41 ms 1768 KB Output is correct
22 Correct 44 ms 1636 KB Output is correct
23 Correct 42 ms 1636 KB Output is correct
24 Correct 44 ms 1812 KB Output is correct
25 Correct 40 ms 1668 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 24 ms 780 KB Output is correct
30 Correct 35 ms 1156 KB Output is correct
31 Correct 49 ms 1684 KB Output is correct
32 Correct 54 ms 1632 KB Output is correct
33 Correct 48 ms 1632 KB Output is correct
34 Correct 51 ms 1532 KB Output is correct
35 Correct 53 ms 1656 KB Output is correct
36 Correct 51 ms 1624 KB Output is correct
37 Correct 51 ms 1684 KB Output is correct
38 Correct 45 ms 1692 KB Output is correct
39 Correct 47 ms 1616 KB Output is correct
40 Correct 47 ms 1612 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 4 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 7 ms 468 KB Output is correct
48 Correct 8 ms 468 KB Output is correct
49 Correct 11 ms 724 KB Output is correct
50 Correct 23 ms 1156 KB Output is correct
51 Correct 39 ms 1512 KB Output is correct
52 Correct 41 ms 1540 KB Output is correct
53 Correct 41 ms 1548 KB Output is correct
54 Correct 46 ms 1500 KB Output is correct
55 Correct 40 ms 1576 KB Output is correct
56 Correct 39 ms 1540 KB Output is correct
57 Correct 37 ms 1540 KB Output is correct
58 Correct 39 ms 1552 KB Output is correct
59 Correct 37 ms 1492 KB Output is correct
60 Correct 43 ms 1540 KB Output is correct
61 Correct 54 ms 1544 KB Output is correct
62 Correct 38 ms 1492 KB Output is correct
63 Correct 38 ms 1484 KB Output is correct
64 Correct 38 ms 1536 KB Output is correct
65 Correct 39 ms 1452 KB Output is correct
66 Correct 36 ms 1536 KB Output is correct
67 Correct 36 ms 1552 KB Output is correct
68 Correct 39 ms 1436 KB Output is correct
69 Correct 44 ms 1484 KB Output is correct
70 Correct 47 ms 1560 KB Output is correct
71 Correct 5 ms 468 KB Output is correct
72 Correct 5 ms 528 KB Output is correct
73 Correct 5 ms 468 KB Output is correct
74 Correct 5 ms 468 KB Output is correct
75 Correct 7 ms 468 KB Output is correct
76 Correct 8 ms 724 KB Output is correct
77 Correct 9 ms 724 KB Output is correct
78 Correct 8 ms 724 KB Output is correct
79 Correct 8 ms 696 KB Output is correct
80 Correct 9 ms 680 KB Output is correct
81 Correct 17 ms 1084 KB Output is correct
82 Correct 18 ms 1108 KB Output is correct
83 Correct 19 ms 1184 KB Output is correct
84 Correct 17 ms 1108 KB Output is correct
85 Correct 22 ms 1108 KB Output is correct
86 Correct 18 ms 1116 KB Output is correct
87 Correct 18 ms 1108 KB Output is correct
88 Correct 18 ms 1108 KB Output is correct
89 Correct 18 ms 1280 KB Output is correct
90 Correct 17 ms 1236 KB Output is correct
91 Correct 26 ms 1812 KB Output is correct
92 Correct 27 ms 1840 KB Output is correct
93 Correct 29 ms 1820 KB Output is correct
94 Correct 26 ms 1908 KB Output is correct
95 Correct 28 ms 1824 KB Output is correct
96 Correct 32 ms 2284 KB Output is correct
97 Correct 28 ms 2260 KB Output is correct
98 Correct 29 ms 2212 KB Output is correct
99 Correct 29 ms 2268 KB Output is correct
100 Correct 33 ms 2196 KB Output is correct
101 Correct 29 ms 2212 KB Output is correct
102 Correct 30 ms 2288 KB Output is correct
103 Correct 29 ms 2256 KB Output is correct
104 Correct 33 ms 2216 KB Output is correct
105 Correct 30 ms 2260 KB Output is correct
106 Correct 31 ms 2388 KB Output is correct
107 Correct 39 ms 2412 KB Output is correct
108 Correct 30 ms 2380 KB Output is correct
109 Correct 29 ms 2464 KB Output is correct
110 Correct 30 ms 2460 KB Output is correct
111 Correct 31 ms 2420 KB Output is correct
112 Correct 31 ms 2380 KB Output is correct
113 Correct 35 ms 2484 KB Output is correct
114 Correct 30 ms 2392 KB Output is correct
115 Correct 29 ms 2368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 17 ms 756 KB Output is correct
15 Correct 31 ms 1392 KB Output is correct
16 Correct 49 ms 1732 KB Output is correct
17 Correct 36 ms 2104 KB Output is correct
18 Correct 44 ms 1584 KB Output is correct
19 Correct 43 ms 1664 KB Output is correct
20 Correct 41 ms 1812 KB Output is correct
21 Correct 41 ms 1768 KB Output is correct
22 Correct 44 ms 1636 KB Output is correct
23 Correct 42 ms 1636 KB Output is correct
24 Correct 44 ms 1812 KB Output is correct
25 Correct 40 ms 1668 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 24 ms 780 KB Output is correct
30 Correct 35 ms 1156 KB Output is correct
31 Correct 49 ms 1684 KB Output is correct
32 Correct 54 ms 1632 KB Output is correct
33 Correct 48 ms 1632 KB Output is correct
34 Correct 51 ms 1532 KB Output is correct
35 Correct 53 ms 1656 KB Output is correct
36 Correct 51 ms 1624 KB Output is correct
37 Correct 51 ms 1684 KB Output is correct
38 Correct 45 ms 1692 KB Output is correct
39 Correct 47 ms 1616 KB Output is correct
40 Correct 47 ms 1612 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 4 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 7 ms 468 KB Output is correct
48 Correct 8 ms 468 KB Output is correct
49 Correct 11 ms 724 KB Output is correct
50 Correct 23 ms 1156 KB Output is correct
51 Correct 39 ms 1512 KB Output is correct
52 Correct 41 ms 1540 KB Output is correct
53 Correct 41 ms 1548 KB Output is correct
54 Correct 46 ms 1500 KB Output is correct
55 Correct 40 ms 1576 KB Output is correct
56 Correct 39 ms 1540 KB Output is correct
57 Correct 37 ms 1540 KB Output is correct
58 Correct 39 ms 1552 KB Output is correct
59 Correct 37 ms 1492 KB Output is correct
60 Correct 43 ms 1540 KB Output is correct
61 Correct 54 ms 1544 KB Output is correct
62 Correct 38 ms 1492 KB Output is correct
63 Correct 38 ms 1484 KB Output is correct
64 Correct 38 ms 1536 KB Output is correct
65 Correct 39 ms 1452 KB Output is correct
66 Correct 36 ms 1536 KB Output is correct
67 Correct 36 ms 1552 KB Output is correct
68 Correct 39 ms 1436 KB Output is correct
69 Correct 44 ms 1484 KB Output is correct
70 Correct 47 ms 1560 KB Output is correct
71 Correct 5 ms 468 KB Output is correct
72 Correct 5 ms 528 KB Output is correct
73 Correct 5 ms 468 KB Output is correct
74 Correct 5 ms 468 KB Output is correct
75 Correct 7 ms 468 KB Output is correct
76 Correct 8 ms 724 KB Output is correct
77 Correct 9 ms 724 KB Output is correct
78 Correct 8 ms 724 KB Output is correct
79 Correct 8 ms 696 KB Output is correct
80 Correct 9 ms 680 KB Output is correct
81 Correct 17 ms 1084 KB Output is correct
82 Correct 18 ms 1108 KB Output is correct
83 Correct 19 ms 1184 KB Output is correct
84 Correct 17 ms 1108 KB Output is correct
85 Correct 22 ms 1108 KB Output is correct
86 Correct 18 ms 1116 KB Output is correct
87 Correct 18 ms 1108 KB Output is correct
88 Correct 18 ms 1108 KB Output is correct
89 Correct 18 ms 1280 KB Output is correct
90 Correct 17 ms 1236 KB Output is correct
91 Correct 26 ms 1812 KB Output is correct
92 Correct 27 ms 1840 KB Output is correct
93 Correct 29 ms 1820 KB Output is correct
94 Correct 26 ms 1908 KB Output is correct
95 Correct 28 ms 1824 KB Output is correct
96 Correct 32 ms 2284 KB Output is correct
97 Correct 28 ms 2260 KB Output is correct
98 Correct 29 ms 2212 KB Output is correct
99 Correct 29 ms 2268 KB Output is correct
100 Correct 33 ms 2196 KB Output is correct
101 Correct 29 ms 2212 KB Output is correct
102 Correct 30 ms 2288 KB Output is correct
103 Correct 29 ms 2256 KB Output is correct
104 Correct 33 ms 2216 KB Output is correct
105 Correct 30 ms 2260 KB Output is correct
106 Correct 31 ms 2388 KB Output is correct
107 Correct 39 ms 2412 KB Output is correct
108 Correct 30 ms 2380 KB Output is correct
109 Correct 29 ms 2464 KB Output is correct
110 Correct 30 ms 2460 KB Output is correct
111 Correct 31 ms 2420 KB Output is correct
112 Correct 31 ms 2380 KB Output is correct
113 Correct 35 ms 2484 KB Output is correct
114 Correct 30 ms 2392 KB Output is correct
115 Correct 29 ms 2368 KB Output is correct
116 Correct 848 ms 1756 KB Output is correct
117 Correct 664 ms 1760 KB Output is correct
118 Correct 1603 ms 2056 KB Output is correct
119 Correct 722 ms 2092 KB Output is correct
120 Correct 463 ms 2072 KB Output is correct
121 Correct 1220 ms 2284 KB Output is correct
122 Correct 719 ms 2288 KB Output is correct
123 Correct 4681 ms 2824 KB Output is correct
124 Correct 1386 ms 2812 KB Output is correct
125 Correct 1132 ms 2816 KB Output is correct
126 Execution timed out 7018 ms 2676 KB Time limit exceeded
127 Halted 0 ms 0 KB -