답안 #572483

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
572483 2022-06-04T13:22:33 Z valerikk Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 6560 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

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

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;
	}

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

	long long bad = 0;

	long long sum = 0;
	for (int i = 0; i < (int) q.size(); ++i) {
		const auto &[x, y] = q[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) q.size() - 1; i >= 0; --i) {
		const auto &[x, y] = q[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]});
		}
		assert(is_sorted(res.begin(), res.end()));
		return res;
	}

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

signed 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 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 344 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 15 ms 1348 KB Output is correct
5 Correct 28 ms 2484 KB Output is correct
6 Correct 43 ms 3020 KB Output is correct
7 Correct 39 ms 3860 KB Output is correct
8 Correct 43 ms 3060 KB Output is correct
9 Correct 47 ms 3020 KB Output is correct
10 Correct 45 ms 3408 KB Output is correct
11 Correct 46 ms 3148 KB Output is correct
12 Correct 43 ms 3024 KB Output is correct
13 Correct 48 ms 3028 KB Output is correct
14 Correct 41 ms 3164 KB Output is correct
15 Correct 45 ms 3132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 15 ms 1348 KB Output is correct
5 Correct 28 ms 2484 KB Output is correct
6 Correct 43 ms 3020 KB Output is correct
7 Correct 39 ms 3860 KB Output is correct
8 Correct 43 ms 3060 KB Output is correct
9 Correct 47 ms 3020 KB Output is correct
10 Correct 45 ms 3408 KB Output is correct
11 Correct 46 ms 3148 KB Output is correct
12 Correct 43 ms 3024 KB Output is correct
13 Correct 48 ms 3028 KB Output is correct
14 Correct 41 ms 3164 KB Output is correct
15 Correct 45 ms 3132 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 16 ms 1268 KB Output is correct
20 Correct 39 ms 2056 KB Output is correct
21 Correct 56 ms 3116 KB Output is correct
22 Correct 50 ms 2892 KB Output is correct
23 Correct 59 ms 3076 KB Output is correct
24 Correct 52 ms 2944 KB Output is correct
25 Correct 48 ms 3016 KB Output is correct
26 Correct 48 ms 2892 KB Output is correct
27 Correct 47 ms 3024 KB Output is correct
28 Correct 52 ms 3412 KB Output is correct
29 Correct 57 ms 2912 KB Output is correct
30 Correct 51 ms 2904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 15 ms 1348 KB Output is correct
5 Correct 28 ms 2484 KB Output is correct
6 Correct 43 ms 3020 KB Output is correct
7 Correct 39 ms 3860 KB Output is correct
8 Correct 43 ms 3060 KB Output is correct
9 Correct 47 ms 3020 KB Output is correct
10 Correct 45 ms 3408 KB Output is correct
11 Correct 46 ms 3148 KB Output is correct
12 Correct 43 ms 3024 KB Output is correct
13 Correct 48 ms 3028 KB Output is correct
14 Correct 41 ms 3164 KB Output is correct
15 Correct 45 ms 3132 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 16 ms 1268 KB Output is correct
20 Correct 39 ms 2056 KB Output is correct
21 Correct 56 ms 3116 KB Output is correct
22 Correct 50 ms 2892 KB Output is correct
23 Correct 59 ms 3076 KB Output is correct
24 Correct 52 ms 2944 KB Output is correct
25 Correct 48 ms 3016 KB Output is correct
26 Correct 48 ms 2892 KB Output is correct
27 Correct 47 ms 3024 KB Output is correct
28 Correct 52 ms 3412 KB Output is correct
29 Correct 57 ms 2912 KB Output is correct
30 Correct 51 ms 2904 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 352 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 3 ms 364 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 8 ms 868 KB Output is correct
38 Correct 6 ms 852 KB Output is correct
39 Correct 11 ms 1508 KB Output is correct
40 Correct 31 ms 2692 KB Output is correct
41 Correct 40 ms 3892 KB Output is correct
42 Correct 40 ms 3892 KB Output is correct
43 Correct 42 ms 3860 KB Output is correct
44 Correct 40 ms 3788 KB Output is correct
45 Correct 40 ms 3864 KB Output is correct
46 Correct 40 ms 3864 KB Output is correct
47 Correct 41 ms 3880 KB Output is correct
48 Correct 42 ms 3868 KB Output is correct
49 Correct 38 ms 3784 KB Output is correct
50 Correct 40 ms 3860 KB Output is correct
51 Correct 39 ms 3876 KB Output is correct
52 Correct 40 ms 3892 KB Output is correct
53 Correct 47 ms 3880 KB Output is correct
54 Correct 40 ms 3864 KB Output is correct
55 Correct 42 ms 3864 KB Output is correct
56 Correct 39 ms 3824 KB Output is correct
57 Correct 39 ms 3808 KB Output is correct
58 Correct 42 ms 3788 KB Output is correct
59 Correct 40 ms 3872 KB Output is correct
60 Correct 40 ms 3972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 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 344 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 15 ms 1348 KB Output is correct
15 Correct 28 ms 2484 KB Output is correct
16 Correct 43 ms 3020 KB Output is correct
17 Correct 39 ms 3860 KB Output is correct
18 Correct 43 ms 3060 KB Output is correct
19 Correct 47 ms 3020 KB Output is correct
20 Correct 45 ms 3408 KB Output is correct
21 Correct 46 ms 3148 KB Output is correct
22 Correct 43 ms 3024 KB Output is correct
23 Correct 48 ms 3028 KB Output is correct
24 Correct 41 ms 3164 KB Output is correct
25 Correct 45 ms 3132 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 16 ms 1268 KB Output is correct
30 Correct 39 ms 2056 KB Output is correct
31 Correct 56 ms 3116 KB Output is correct
32 Correct 50 ms 2892 KB Output is correct
33 Correct 59 ms 3076 KB Output is correct
34 Correct 52 ms 2944 KB Output is correct
35 Correct 48 ms 3016 KB Output is correct
36 Correct 48 ms 2892 KB Output is correct
37 Correct 47 ms 3024 KB Output is correct
38 Correct 52 ms 3412 KB Output is correct
39 Correct 57 ms 2912 KB Output is correct
40 Correct 51 ms 2904 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 352 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 468 KB Output is correct
45 Correct 3 ms 364 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 8 ms 868 KB Output is correct
48 Correct 6 ms 852 KB Output is correct
49 Correct 11 ms 1508 KB Output is correct
50 Correct 31 ms 2692 KB Output is correct
51 Correct 40 ms 3892 KB Output is correct
52 Correct 40 ms 3892 KB Output is correct
53 Correct 42 ms 3860 KB Output is correct
54 Correct 40 ms 3788 KB Output is correct
55 Correct 40 ms 3864 KB Output is correct
56 Correct 40 ms 3864 KB Output is correct
57 Correct 41 ms 3880 KB Output is correct
58 Correct 42 ms 3868 KB Output is correct
59 Correct 38 ms 3784 KB Output is correct
60 Correct 40 ms 3860 KB Output is correct
61 Correct 39 ms 3876 KB Output is correct
62 Correct 40 ms 3892 KB Output is correct
63 Correct 47 ms 3880 KB Output is correct
64 Correct 40 ms 3864 KB Output is correct
65 Correct 42 ms 3864 KB Output is correct
66 Correct 39 ms 3824 KB Output is correct
67 Correct 39 ms 3808 KB Output is correct
68 Correct 42 ms 3788 KB Output is correct
69 Correct 40 ms 3872 KB Output is correct
70 Correct 40 ms 3972 KB Output is correct
71 Correct 6 ms 980 KB Output is correct
72 Correct 6 ms 988 KB Output is correct
73 Correct 6 ms 952 KB Output is correct
74 Correct 6 ms 980 KB Output is correct
75 Correct 6 ms 996 KB Output is correct
76 Correct 10 ms 1624 KB Output is correct
77 Correct 9 ms 1620 KB Output is correct
78 Correct 11 ms 1616 KB Output is correct
79 Correct 10 ms 1640 KB Output is correct
80 Correct 10 ms 1636 KB Output is correct
81 Correct 18 ms 2876 KB Output is correct
82 Correct 19 ms 2872 KB Output is correct
83 Correct 18 ms 2932 KB Output is correct
84 Correct 17 ms 2900 KB Output is correct
85 Correct 18 ms 3056 KB Output is correct
86 Correct 19 ms 3092 KB Output is correct
87 Correct 20 ms 3076 KB Output is correct
88 Correct 20 ms 3028 KB Output is correct
89 Correct 19 ms 3004 KB Output is correct
90 Correct 18 ms 3044 KB Output is correct
91 Correct 28 ms 5168 KB Output is correct
92 Correct 29 ms 5172 KB Output is correct
93 Correct 28 ms 5068 KB Output is correct
94 Correct 29 ms 5316 KB Output is correct
95 Correct 29 ms 5144 KB Output is correct
96 Correct 32 ms 6096 KB Output is correct
97 Correct 33 ms 6092 KB Output is correct
98 Correct 36 ms 6076 KB Output is correct
99 Correct 33 ms 6092 KB Output is correct
100 Correct 32 ms 6100 KB Output is correct
101 Correct 36 ms 6096 KB Output is correct
102 Correct 34 ms 6108 KB Output is correct
103 Correct 34 ms 6244 KB Output is correct
104 Correct 34 ms 6132 KB Output is correct
105 Correct 33 ms 6012 KB Output is correct
106 Correct 32 ms 6500 KB Output is correct
107 Correct 34 ms 6516 KB Output is correct
108 Correct 32 ms 6444 KB Output is correct
109 Correct 33 ms 6560 KB Output is correct
110 Correct 33 ms 6544 KB Output is correct
111 Correct 34 ms 6496 KB Output is correct
112 Correct 40 ms 6548 KB Output is correct
113 Correct 31 ms 6468 KB Output is correct
114 Correct 38 ms 6528 KB Output is correct
115 Correct 34 ms 6480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 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 344 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 15 ms 1348 KB Output is correct
15 Correct 28 ms 2484 KB Output is correct
16 Correct 43 ms 3020 KB Output is correct
17 Correct 39 ms 3860 KB Output is correct
18 Correct 43 ms 3060 KB Output is correct
19 Correct 47 ms 3020 KB Output is correct
20 Correct 45 ms 3408 KB Output is correct
21 Correct 46 ms 3148 KB Output is correct
22 Correct 43 ms 3024 KB Output is correct
23 Correct 48 ms 3028 KB Output is correct
24 Correct 41 ms 3164 KB Output is correct
25 Correct 45 ms 3132 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 16 ms 1268 KB Output is correct
30 Correct 39 ms 2056 KB Output is correct
31 Correct 56 ms 3116 KB Output is correct
32 Correct 50 ms 2892 KB Output is correct
33 Correct 59 ms 3076 KB Output is correct
34 Correct 52 ms 2944 KB Output is correct
35 Correct 48 ms 3016 KB Output is correct
36 Correct 48 ms 2892 KB Output is correct
37 Correct 47 ms 3024 KB Output is correct
38 Correct 52 ms 3412 KB Output is correct
39 Correct 57 ms 2912 KB Output is correct
40 Correct 51 ms 2904 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 352 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 468 KB Output is correct
45 Correct 3 ms 364 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 8 ms 868 KB Output is correct
48 Correct 6 ms 852 KB Output is correct
49 Correct 11 ms 1508 KB Output is correct
50 Correct 31 ms 2692 KB Output is correct
51 Correct 40 ms 3892 KB Output is correct
52 Correct 40 ms 3892 KB Output is correct
53 Correct 42 ms 3860 KB Output is correct
54 Correct 40 ms 3788 KB Output is correct
55 Correct 40 ms 3864 KB Output is correct
56 Correct 40 ms 3864 KB Output is correct
57 Correct 41 ms 3880 KB Output is correct
58 Correct 42 ms 3868 KB Output is correct
59 Correct 38 ms 3784 KB Output is correct
60 Correct 40 ms 3860 KB Output is correct
61 Correct 39 ms 3876 KB Output is correct
62 Correct 40 ms 3892 KB Output is correct
63 Correct 47 ms 3880 KB Output is correct
64 Correct 40 ms 3864 KB Output is correct
65 Correct 42 ms 3864 KB Output is correct
66 Correct 39 ms 3824 KB Output is correct
67 Correct 39 ms 3808 KB Output is correct
68 Correct 42 ms 3788 KB Output is correct
69 Correct 40 ms 3872 KB Output is correct
70 Correct 40 ms 3972 KB Output is correct
71 Correct 6 ms 980 KB Output is correct
72 Correct 6 ms 988 KB Output is correct
73 Correct 6 ms 952 KB Output is correct
74 Correct 6 ms 980 KB Output is correct
75 Correct 6 ms 996 KB Output is correct
76 Correct 10 ms 1624 KB Output is correct
77 Correct 9 ms 1620 KB Output is correct
78 Correct 11 ms 1616 KB Output is correct
79 Correct 10 ms 1640 KB Output is correct
80 Correct 10 ms 1636 KB Output is correct
81 Correct 18 ms 2876 KB Output is correct
82 Correct 19 ms 2872 KB Output is correct
83 Correct 18 ms 2932 KB Output is correct
84 Correct 17 ms 2900 KB Output is correct
85 Correct 18 ms 3056 KB Output is correct
86 Correct 19 ms 3092 KB Output is correct
87 Correct 20 ms 3076 KB Output is correct
88 Correct 20 ms 3028 KB Output is correct
89 Correct 19 ms 3004 KB Output is correct
90 Correct 18 ms 3044 KB Output is correct
91 Correct 28 ms 5168 KB Output is correct
92 Correct 29 ms 5172 KB Output is correct
93 Correct 28 ms 5068 KB Output is correct
94 Correct 29 ms 5316 KB Output is correct
95 Correct 29 ms 5144 KB Output is correct
96 Correct 32 ms 6096 KB Output is correct
97 Correct 33 ms 6092 KB Output is correct
98 Correct 36 ms 6076 KB Output is correct
99 Correct 33 ms 6092 KB Output is correct
100 Correct 32 ms 6100 KB Output is correct
101 Correct 36 ms 6096 KB Output is correct
102 Correct 34 ms 6108 KB Output is correct
103 Correct 34 ms 6244 KB Output is correct
104 Correct 34 ms 6132 KB Output is correct
105 Correct 33 ms 6012 KB Output is correct
106 Correct 32 ms 6500 KB Output is correct
107 Correct 34 ms 6516 KB Output is correct
108 Correct 32 ms 6444 KB Output is correct
109 Correct 33 ms 6560 KB Output is correct
110 Correct 33 ms 6544 KB Output is correct
111 Correct 34 ms 6496 KB Output is correct
112 Correct 40 ms 6548 KB Output is correct
113 Correct 31 ms 6468 KB Output is correct
114 Correct 38 ms 6528 KB Output is correct
115 Correct 34 ms 6480 KB Output is correct
116 Correct 902 ms 2900 KB Output is correct
117 Correct 704 ms 2912 KB Output is correct
118 Correct 1607 ms 3564 KB Output is correct
119 Correct 761 ms 3624 KB Output is correct
120 Correct 457 ms 3660 KB Output is correct
121 Correct 1159 ms 4240 KB Output is correct
122 Correct 712 ms 4288 KB Output is correct
123 Correct 4277 ms 5596 KB Output is correct
124 Correct 1364 ms 5708 KB Output is correct
125 Correct 1090 ms 5812 KB Output is correct
126 Execution timed out 7069 ms 6200 KB Time limit exceeded
127 Halted 0 ms 0 KB -