Submission #572486

# Submission time Handle Problem Language Result Execution time Memory
572486 2022-06-04T13:28:44 Z valerikk Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 2816 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) {
		int blocki = l[i] / B;
		int blockj = l[j] / B;
		return make_pair(blocki, (blocki & 1) ? -r[i] : r[i]) < make_pair(blockj, (blockj & 1) ? -r[j] : 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 1 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 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 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 2 ms 340 KB Output is correct
2 Correct 2 ms 304 KB Output is correct
3 Correct 3 ms 400 KB Output is correct
4 Correct 15 ms 840 KB Output is correct
5 Correct 27 ms 1356 KB Output is correct
6 Correct 44 ms 1632 KB Output is correct
7 Correct 37 ms 2108 KB Output is correct
8 Correct 47 ms 1684 KB Output is correct
9 Correct 45 ms 1732 KB Output is correct
10 Correct 44 ms 1852 KB Output is correct
11 Correct 44 ms 1708 KB Output is correct
12 Correct 56 ms 1644 KB Output is correct
13 Correct 44 ms 1680 KB Output is correct
14 Correct 41 ms 1740 KB Output is correct
15 Correct 42 ms 1772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 304 KB Output is correct
3 Correct 3 ms 400 KB Output is correct
4 Correct 15 ms 840 KB Output is correct
5 Correct 27 ms 1356 KB Output is correct
6 Correct 44 ms 1632 KB Output is correct
7 Correct 37 ms 2108 KB Output is correct
8 Correct 47 ms 1684 KB Output is correct
9 Correct 45 ms 1732 KB Output is correct
10 Correct 44 ms 1852 KB Output is correct
11 Correct 44 ms 1708 KB Output is correct
12 Correct 56 ms 1644 KB Output is correct
13 Correct 44 ms 1680 KB Output is correct
14 Correct 41 ms 1740 KB Output is correct
15 Correct 42 ms 1772 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 21 ms 796 KB Output is correct
20 Correct 40 ms 1288 KB Output is correct
21 Correct 63 ms 1628 KB Output is correct
22 Correct 49 ms 1580 KB Output is correct
23 Correct 47 ms 1604 KB Output is correct
24 Correct 49 ms 1520 KB Output is correct
25 Correct 48 ms 1612 KB Output is correct
26 Correct 53 ms 1588 KB Output is correct
27 Correct 54 ms 1656 KB Output is correct
28 Correct 48 ms 1736 KB Output is correct
29 Correct 50 ms 1552 KB Output is correct
30 Correct 48 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 304 KB Output is correct
3 Correct 3 ms 400 KB Output is correct
4 Correct 15 ms 840 KB Output is correct
5 Correct 27 ms 1356 KB Output is correct
6 Correct 44 ms 1632 KB Output is correct
7 Correct 37 ms 2108 KB Output is correct
8 Correct 47 ms 1684 KB Output is correct
9 Correct 45 ms 1732 KB Output is correct
10 Correct 44 ms 1852 KB Output is correct
11 Correct 44 ms 1708 KB Output is correct
12 Correct 56 ms 1644 KB Output is correct
13 Correct 44 ms 1680 KB Output is correct
14 Correct 41 ms 1740 KB Output is correct
15 Correct 42 ms 1772 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 21 ms 796 KB Output is correct
20 Correct 40 ms 1288 KB Output is correct
21 Correct 63 ms 1628 KB Output is correct
22 Correct 49 ms 1580 KB Output is correct
23 Correct 47 ms 1604 KB Output is correct
24 Correct 49 ms 1520 KB Output is correct
25 Correct 48 ms 1612 KB Output is correct
26 Correct 53 ms 1588 KB Output is correct
27 Correct 54 ms 1656 KB Output is correct
28 Correct 48 ms 1736 KB Output is correct
29 Correct 50 ms 1552 KB Output is correct
30 Correct 48 ms 1628 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 2 ms 340 KB Output is correct
35 Correct 2 ms 308 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 7 ms 468 KB Output is correct
38 Correct 6 ms 560 KB Output is correct
39 Correct 11 ms 724 KB Output is correct
40 Correct 29 ms 1148 KB Output is correct
41 Correct 40 ms 1524 KB Output is correct
42 Correct 37 ms 1540 KB Output is correct
43 Correct 39 ms 1492 KB Output is correct
44 Correct 38 ms 1540 KB Output is correct
45 Correct 38 ms 1524 KB Output is correct
46 Correct 41 ms 1528 KB Output is correct
47 Correct 39 ms 1492 KB Output is correct
48 Correct 37 ms 1536 KB Output is correct
49 Correct 38 ms 1536 KB Output is correct
50 Correct 38 ms 1532 KB Output is correct
51 Correct 39 ms 1468 KB Output is correct
52 Correct 41 ms 1540 KB Output is correct
53 Correct 41 ms 1548 KB Output is correct
54 Correct 43 ms 1528 KB Output is correct
55 Correct 40 ms 1532 KB Output is correct
56 Correct 46 ms 1556 KB Output is correct
57 Correct 38 ms 1492 KB Output is correct
58 Correct 46 ms 1540 KB Output is correct
59 Correct 39 ms 1468 KB Output is correct
60 Correct 37 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 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 2 ms 340 KB Output is correct
12 Correct 2 ms 304 KB Output is correct
13 Correct 3 ms 400 KB Output is correct
14 Correct 15 ms 840 KB Output is correct
15 Correct 27 ms 1356 KB Output is correct
16 Correct 44 ms 1632 KB Output is correct
17 Correct 37 ms 2108 KB Output is correct
18 Correct 47 ms 1684 KB Output is correct
19 Correct 45 ms 1732 KB Output is correct
20 Correct 44 ms 1852 KB Output is correct
21 Correct 44 ms 1708 KB Output is correct
22 Correct 56 ms 1644 KB Output is correct
23 Correct 44 ms 1680 KB Output is correct
24 Correct 41 ms 1740 KB Output is correct
25 Correct 42 ms 1772 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 21 ms 796 KB Output is correct
30 Correct 40 ms 1288 KB Output is correct
31 Correct 63 ms 1628 KB Output is correct
32 Correct 49 ms 1580 KB Output is correct
33 Correct 47 ms 1604 KB Output is correct
34 Correct 49 ms 1520 KB Output is correct
35 Correct 48 ms 1612 KB Output is correct
36 Correct 53 ms 1588 KB Output is correct
37 Correct 54 ms 1656 KB Output is correct
38 Correct 48 ms 1736 KB Output is correct
39 Correct 50 ms 1552 KB Output is correct
40 Correct 48 ms 1628 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 2 ms 340 KB Output is correct
45 Correct 2 ms 308 KB Output is correct
46 Correct 3 ms 340 KB Output is correct
47 Correct 7 ms 468 KB Output is correct
48 Correct 6 ms 560 KB Output is correct
49 Correct 11 ms 724 KB Output is correct
50 Correct 29 ms 1148 KB Output is correct
51 Correct 40 ms 1524 KB Output is correct
52 Correct 37 ms 1540 KB Output is correct
53 Correct 39 ms 1492 KB Output is correct
54 Correct 38 ms 1540 KB Output is correct
55 Correct 38 ms 1524 KB Output is correct
56 Correct 41 ms 1528 KB Output is correct
57 Correct 39 ms 1492 KB Output is correct
58 Correct 37 ms 1536 KB Output is correct
59 Correct 38 ms 1536 KB Output is correct
60 Correct 38 ms 1532 KB Output is correct
61 Correct 39 ms 1468 KB Output is correct
62 Correct 41 ms 1540 KB Output is correct
63 Correct 41 ms 1548 KB Output is correct
64 Correct 43 ms 1528 KB Output is correct
65 Correct 40 ms 1532 KB Output is correct
66 Correct 46 ms 1556 KB Output is correct
67 Correct 38 ms 1492 KB Output is correct
68 Correct 46 ms 1540 KB Output is correct
69 Correct 39 ms 1468 KB Output is correct
70 Correct 37 ms 1492 KB Output is correct
71 Correct 5 ms 468 KB Output is correct
72 Correct 5 ms 468 KB Output is correct
73 Correct 5 ms 468 KB Output is correct
74 Correct 5 ms 484 KB Output is correct
75 Correct 7 ms 560 KB Output is correct
76 Correct 12 ms 852 KB Output is correct
77 Correct 12 ms 720 KB Output is correct
78 Correct 9 ms 724 KB Output is correct
79 Correct 9 ms 724 KB Output is correct
80 Correct 8 ms 724 KB Output is correct
81 Correct 16 ms 1120 KB Output is correct
82 Correct 16 ms 1068 KB Output is correct
83 Correct 16 ms 1108 KB Output is correct
84 Correct 17 ms 1108 KB Output is correct
85 Correct 16 ms 1108 KB Output is correct
86 Correct 18 ms 1108 KB Output is correct
87 Correct 17 ms 1108 KB Output is correct
88 Correct 19 ms 1112 KB Output is correct
89 Correct 19 ms 1216 KB Output is correct
90 Correct 18 ms 1220 KB Output is correct
91 Correct 34 ms 1888 KB Output is correct
92 Correct 27 ms 1876 KB Output is correct
93 Correct 26 ms 1900 KB Output is correct
94 Correct 27 ms 1920 KB Output is correct
95 Correct 26 ms 1804 KB Output is correct
96 Correct 29 ms 2280 KB Output is correct
97 Correct 30 ms 2184 KB Output is correct
98 Correct 31 ms 2276 KB Output is correct
99 Correct 36 ms 2256 KB Output is correct
100 Correct 45 ms 2260 KB Output is correct
101 Correct 41 ms 2268 KB Output is correct
102 Correct 30 ms 2232 KB Output is correct
103 Correct 30 ms 2280 KB Output is correct
104 Correct 30 ms 2272 KB Output is correct
105 Correct 30 ms 2184 KB Output is correct
106 Correct 36 ms 2432 KB Output is correct
107 Correct 34 ms 2388 KB Output is correct
108 Correct 32 ms 2436 KB Output is correct
109 Correct 36 ms 2380 KB Output is correct
110 Correct 36 ms 2380 KB Output is correct
111 Correct 31 ms 2388 KB Output is correct
112 Correct 33 ms 2480 KB Output is correct
113 Correct 30 ms 2388 KB Output is correct
114 Correct 32 ms 2380 KB Output is correct
115 Correct 37 ms 2364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 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 2 ms 340 KB Output is correct
12 Correct 2 ms 304 KB Output is correct
13 Correct 3 ms 400 KB Output is correct
14 Correct 15 ms 840 KB Output is correct
15 Correct 27 ms 1356 KB Output is correct
16 Correct 44 ms 1632 KB Output is correct
17 Correct 37 ms 2108 KB Output is correct
18 Correct 47 ms 1684 KB Output is correct
19 Correct 45 ms 1732 KB Output is correct
20 Correct 44 ms 1852 KB Output is correct
21 Correct 44 ms 1708 KB Output is correct
22 Correct 56 ms 1644 KB Output is correct
23 Correct 44 ms 1680 KB Output is correct
24 Correct 41 ms 1740 KB Output is correct
25 Correct 42 ms 1772 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 21 ms 796 KB Output is correct
30 Correct 40 ms 1288 KB Output is correct
31 Correct 63 ms 1628 KB Output is correct
32 Correct 49 ms 1580 KB Output is correct
33 Correct 47 ms 1604 KB Output is correct
34 Correct 49 ms 1520 KB Output is correct
35 Correct 48 ms 1612 KB Output is correct
36 Correct 53 ms 1588 KB Output is correct
37 Correct 54 ms 1656 KB Output is correct
38 Correct 48 ms 1736 KB Output is correct
39 Correct 50 ms 1552 KB Output is correct
40 Correct 48 ms 1628 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 2 ms 340 KB Output is correct
45 Correct 2 ms 308 KB Output is correct
46 Correct 3 ms 340 KB Output is correct
47 Correct 7 ms 468 KB Output is correct
48 Correct 6 ms 560 KB Output is correct
49 Correct 11 ms 724 KB Output is correct
50 Correct 29 ms 1148 KB Output is correct
51 Correct 40 ms 1524 KB Output is correct
52 Correct 37 ms 1540 KB Output is correct
53 Correct 39 ms 1492 KB Output is correct
54 Correct 38 ms 1540 KB Output is correct
55 Correct 38 ms 1524 KB Output is correct
56 Correct 41 ms 1528 KB Output is correct
57 Correct 39 ms 1492 KB Output is correct
58 Correct 37 ms 1536 KB Output is correct
59 Correct 38 ms 1536 KB Output is correct
60 Correct 38 ms 1532 KB Output is correct
61 Correct 39 ms 1468 KB Output is correct
62 Correct 41 ms 1540 KB Output is correct
63 Correct 41 ms 1548 KB Output is correct
64 Correct 43 ms 1528 KB Output is correct
65 Correct 40 ms 1532 KB Output is correct
66 Correct 46 ms 1556 KB Output is correct
67 Correct 38 ms 1492 KB Output is correct
68 Correct 46 ms 1540 KB Output is correct
69 Correct 39 ms 1468 KB Output is correct
70 Correct 37 ms 1492 KB Output is correct
71 Correct 5 ms 468 KB Output is correct
72 Correct 5 ms 468 KB Output is correct
73 Correct 5 ms 468 KB Output is correct
74 Correct 5 ms 484 KB Output is correct
75 Correct 7 ms 560 KB Output is correct
76 Correct 12 ms 852 KB Output is correct
77 Correct 12 ms 720 KB Output is correct
78 Correct 9 ms 724 KB Output is correct
79 Correct 9 ms 724 KB Output is correct
80 Correct 8 ms 724 KB Output is correct
81 Correct 16 ms 1120 KB Output is correct
82 Correct 16 ms 1068 KB Output is correct
83 Correct 16 ms 1108 KB Output is correct
84 Correct 17 ms 1108 KB Output is correct
85 Correct 16 ms 1108 KB Output is correct
86 Correct 18 ms 1108 KB Output is correct
87 Correct 17 ms 1108 KB Output is correct
88 Correct 19 ms 1112 KB Output is correct
89 Correct 19 ms 1216 KB Output is correct
90 Correct 18 ms 1220 KB Output is correct
91 Correct 34 ms 1888 KB Output is correct
92 Correct 27 ms 1876 KB Output is correct
93 Correct 26 ms 1900 KB Output is correct
94 Correct 27 ms 1920 KB Output is correct
95 Correct 26 ms 1804 KB Output is correct
96 Correct 29 ms 2280 KB Output is correct
97 Correct 30 ms 2184 KB Output is correct
98 Correct 31 ms 2276 KB Output is correct
99 Correct 36 ms 2256 KB Output is correct
100 Correct 45 ms 2260 KB Output is correct
101 Correct 41 ms 2268 KB Output is correct
102 Correct 30 ms 2232 KB Output is correct
103 Correct 30 ms 2280 KB Output is correct
104 Correct 30 ms 2272 KB Output is correct
105 Correct 30 ms 2184 KB Output is correct
106 Correct 36 ms 2432 KB Output is correct
107 Correct 34 ms 2388 KB Output is correct
108 Correct 32 ms 2436 KB Output is correct
109 Correct 36 ms 2380 KB Output is correct
110 Correct 36 ms 2380 KB Output is correct
111 Correct 31 ms 2388 KB Output is correct
112 Correct 33 ms 2480 KB Output is correct
113 Correct 30 ms 2388 KB Output is correct
114 Correct 32 ms 2380 KB Output is correct
115 Correct 37 ms 2364 KB Output is correct
116 Correct 798 ms 1752 KB Output is correct
117 Correct 661 ms 1760 KB Output is correct
118 Correct 1289 ms 2052 KB Output is correct
119 Correct 688 ms 2072 KB Output is correct
120 Correct 437 ms 2068 KB Output is correct
121 Correct 926 ms 2312 KB Output is correct
122 Correct 587 ms 2380 KB Output is correct
123 Correct 3187 ms 2764 KB Output is correct
124 Correct 924 ms 2812 KB Output is correct
125 Correct 842 ms 2816 KB Output is correct
126 Execution timed out 7040 ms 2564 KB Time limit exceeded
127 Halted 0 ms 0 KB -