답안 #572479

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

using namespace std;

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

		for (int z = 0; z < y; ++z) {
			bad += (sum + z * 1ll * x) * (sum + z * 1ll * x + 1);
		}
		// bad += sum * sum * y;
		// bad += sum * x * y * (y - 1);
		// bad += sum * y;
		// bad += x * 1ll * y * (y - 1);
		// 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];

		for (int z = 0; z < y; ++z) {
			bad += (sum + z * 1ll * x) * (sum + z * 1ll * x + 1);
		}
		// bad += sum * sum * y;
		// bad += sum * x * y * (y - 1);
		// bad += sum * y;
		// bad += x * 1ll * y * (y - 1);
		// 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;

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;
}
# 결과 실행 시간 메모리 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 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 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 1 ms 340 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 3 ms 468 KB Output is correct
4 Correct 14 ms 980 KB Output is correct
5 Correct 26 ms 1816 KB Output is correct
6 Correct 42 ms 2312 KB Output is correct
7 Correct 36 ms 2672 KB Output is correct
8 Correct 42 ms 2240 KB Output is correct
9 Correct 42 ms 2268 KB Output is correct
10 Correct 40 ms 2500 KB Output is correct
11 Correct 42 ms 2476 KB Output is correct
12 Correct 45 ms 2380 KB Output is correct
13 Correct 42 ms 2248 KB Output is correct
14 Correct 41 ms 2404 KB Output is correct
15 Correct 41 ms 2320 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 3 ms 468 KB Output is correct
4 Correct 14 ms 980 KB Output is correct
5 Correct 26 ms 1816 KB Output is correct
6 Correct 42 ms 2312 KB Output is correct
7 Correct 36 ms 2672 KB Output is correct
8 Correct 42 ms 2240 KB Output is correct
9 Correct 42 ms 2268 KB Output is correct
10 Correct 40 ms 2500 KB Output is correct
11 Correct 42 ms 2476 KB Output is correct
12 Correct 45 ms 2380 KB Output is correct
13 Correct 42 ms 2248 KB Output is correct
14 Correct 41 ms 2404 KB Output is correct
15 Correct 41 ms 2320 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 360 KB Output is correct
19 Correct 16 ms 972 KB Output is correct
20 Correct 34 ms 1704 KB Output is correct
21 Correct 46 ms 2540 KB Output is correct
22 Correct 49 ms 2432 KB Output is correct
23 Correct 47 ms 2400 KB Output is correct
24 Correct 46 ms 2420 KB Output is correct
25 Correct 47 ms 2412 KB Output is correct
26 Correct 47 ms 2380 KB Output is correct
27 Correct 48 ms 2396 KB Output is correct
28 Correct 46 ms 2508 KB Output is correct
29 Correct 48 ms 2384 KB Output is correct
30 Correct 47 ms 2396 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 3 ms 468 KB Output is correct
4 Correct 14 ms 980 KB Output is correct
5 Correct 26 ms 1816 KB Output is correct
6 Correct 42 ms 2312 KB Output is correct
7 Correct 36 ms 2672 KB Output is correct
8 Correct 42 ms 2240 KB Output is correct
9 Correct 42 ms 2268 KB Output is correct
10 Correct 40 ms 2500 KB Output is correct
11 Correct 42 ms 2476 KB Output is correct
12 Correct 45 ms 2380 KB Output is correct
13 Correct 42 ms 2248 KB Output is correct
14 Correct 41 ms 2404 KB Output is correct
15 Correct 41 ms 2320 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 360 KB Output is correct
19 Correct 16 ms 972 KB Output is correct
20 Correct 34 ms 1704 KB Output is correct
21 Correct 46 ms 2540 KB Output is correct
22 Correct 49 ms 2432 KB Output is correct
23 Correct 47 ms 2400 KB Output is correct
24 Correct 46 ms 2420 KB Output is correct
25 Correct 47 ms 2412 KB Output is correct
26 Correct 47 ms 2380 KB Output is correct
27 Correct 48 ms 2396 KB Output is correct
28 Correct 46 ms 2508 KB Output is correct
29 Correct 48 ms 2384 KB Output is correct
30 Correct 47 ms 2396 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 3 ms 304 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 2 ms 356 KB Output is correct
37 Correct 7 ms 724 KB Output is correct
38 Correct 6 ms 748 KB Output is correct
39 Correct 11 ms 1080 KB Output is correct
40 Correct 23 ms 1876 KB Output is correct
41 Correct 41 ms 2636 KB Output is correct
42 Correct 37 ms 2636 KB Output is correct
43 Correct 38 ms 2652 KB Output is correct
44 Correct 40 ms 2652 KB Output is correct
45 Correct 39 ms 2696 KB Output is correct
46 Correct 38 ms 2684 KB Output is correct
47 Correct 37 ms 2620 KB Output is correct
48 Correct 38 ms 2664 KB Output is correct
49 Correct 39 ms 2644 KB Output is correct
50 Correct 39 ms 2684 KB Output is correct
51 Correct 40 ms 2692 KB Output is correct
52 Correct 39 ms 2764 KB Output is correct
53 Correct 39 ms 2696 KB Output is correct
54 Correct 39 ms 2672 KB Output is correct
55 Correct 38 ms 2620 KB Output is correct
56 Correct 37 ms 2604 KB Output is correct
57 Correct 37 ms 2616 KB Output is correct
58 Correct 38 ms 2640 KB Output is correct
59 Correct 39 ms 2636 KB Output is correct
60 Correct 39 ms 2636 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 2 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 1 ms 308 KB Output is correct
7 Correct 1 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 1 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 3 ms 468 KB Output is correct
14 Correct 14 ms 980 KB Output is correct
15 Correct 26 ms 1816 KB Output is correct
16 Correct 42 ms 2312 KB Output is correct
17 Correct 36 ms 2672 KB Output is correct
18 Correct 42 ms 2240 KB Output is correct
19 Correct 42 ms 2268 KB Output is correct
20 Correct 40 ms 2500 KB Output is correct
21 Correct 42 ms 2476 KB Output is correct
22 Correct 45 ms 2380 KB Output is correct
23 Correct 42 ms 2248 KB Output is correct
24 Correct 41 ms 2404 KB Output is correct
25 Correct 41 ms 2320 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 3 ms 360 KB Output is correct
29 Correct 16 ms 972 KB Output is correct
30 Correct 34 ms 1704 KB Output is correct
31 Correct 46 ms 2540 KB Output is correct
32 Correct 49 ms 2432 KB Output is correct
33 Correct 47 ms 2400 KB Output is correct
34 Correct 46 ms 2420 KB Output is correct
35 Correct 47 ms 2412 KB Output is correct
36 Correct 47 ms 2380 KB Output is correct
37 Correct 48 ms 2396 KB Output is correct
38 Correct 46 ms 2508 KB Output is correct
39 Correct 48 ms 2384 KB Output is correct
40 Correct 47 ms 2396 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 304 KB Output is correct
45 Correct 3 ms 340 KB Output is correct
46 Correct 2 ms 356 KB Output is correct
47 Correct 7 ms 724 KB Output is correct
48 Correct 6 ms 748 KB Output is correct
49 Correct 11 ms 1080 KB Output is correct
50 Correct 23 ms 1876 KB Output is correct
51 Correct 41 ms 2636 KB Output is correct
52 Correct 37 ms 2636 KB Output is correct
53 Correct 38 ms 2652 KB Output is correct
54 Correct 40 ms 2652 KB Output is correct
55 Correct 39 ms 2696 KB Output is correct
56 Correct 38 ms 2684 KB Output is correct
57 Correct 37 ms 2620 KB Output is correct
58 Correct 38 ms 2664 KB Output is correct
59 Correct 39 ms 2644 KB Output is correct
60 Correct 39 ms 2684 KB Output is correct
61 Correct 40 ms 2692 KB Output is correct
62 Correct 39 ms 2764 KB Output is correct
63 Correct 39 ms 2696 KB Output is correct
64 Correct 39 ms 2672 KB Output is correct
65 Correct 38 ms 2620 KB Output is correct
66 Correct 37 ms 2604 KB Output is correct
67 Correct 37 ms 2616 KB Output is correct
68 Correct 38 ms 2640 KB Output is correct
69 Correct 39 ms 2636 KB Output is correct
70 Correct 39 ms 2636 KB Output is correct
71 Correct 5 ms 740 KB Output is correct
72 Correct 6 ms 724 KB Output is correct
73 Correct 5 ms 744 KB Output is correct
74 Correct 6 ms 740 KB Output is correct
75 Correct 5 ms 692 KB Output is correct
76 Correct 10 ms 1236 KB Output is correct
77 Correct 9 ms 1252 KB Output is correct
78 Correct 9 ms 1228 KB Output is correct
79 Correct 9 ms 1124 KB Output is correct
80 Correct 10 ms 1236 KB Output is correct
81 Correct 18 ms 2124 KB Output is correct
82 Correct 16 ms 2124 KB Output is correct
83 Correct 16 ms 2020 KB Output is correct
84 Correct 17 ms 2132 KB Output is correct
85 Correct 18 ms 2076 KB Output is correct
86 Correct 18 ms 2236 KB Output is correct
87 Correct 21 ms 2216 KB Output is correct
88 Correct 18 ms 2284 KB Output is correct
89 Correct 18 ms 2220 KB Output is correct
90 Correct 19 ms 2264 KB Output is correct
91 Correct 31 ms 3512 KB Output is correct
92 Correct 28 ms 3532 KB Output is correct
93 Correct 27 ms 3596 KB Output is correct
94 Correct 26 ms 3532 KB Output is correct
95 Correct 27 ms 3628 KB Output is correct
96 Correct 31 ms 4168 KB Output is correct
97 Correct 30 ms 4124 KB Output is correct
98 Correct 33 ms 4116 KB Output is correct
99 Correct 33 ms 4044 KB Output is correct
100 Correct 31 ms 4176 KB Output is correct
101 Correct 31 ms 4136 KB Output is correct
102 Correct 34 ms 4076 KB Output is correct
103 Correct 30 ms 4068 KB Output is correct
104 Correct 32 ms 4132 KB Output is correct
105 Correct 31 ms 4172 KB Output is correct
106 Correct 33 ms 4628 KB Output is correct
107 Correct 35 ms 4360 KB Output is correct
108 Correct 31 ms 4300 KB Output is correct
109 Correct 31 ms 4316 KB Output is correct
110 Correct 31 ms 4276 KB Output is correct
111 Correct 31 ms 4420 KB Output is correct
112 Correct 31 ms 4384 KB Output is correct
113 Correct 31 ms 4384 KB Output is correct
114 Correct 30 ms 4360 KB Output is correct
115 Correct 33 ms 4356 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 2 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 1 ms 308 KB Output is correct
7 Correct 1 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 1 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 3 ms 468 KB Output is correct
14 Correct 14 ms 980 KB Output is correct
15 Correct 26 ms 1816 KB Output is correct
16 Correct 42 ms 2312 KB Output is correct
17 Correct 36 ms 2672 KB Output is correct
18 Correct 42 ms 2240 KB Output is correct
19 Correct 42 ms 2268 KB Output is correct
20 Correct 40 ms 2500 KB Output is correct
21 Correct 42 ms 2476 KB Output is correct
22 Correct 45 ms 2380 KB Output is correct
23 Correct 42 ms 2248 KB Output is correct
24 Correct 41 ms 2404 KB Output is correct
25 Correct 41 ms 2320 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 3 ms 360 KB Output is correct
29 Correct 16 ms 972 KB Output is correct
30 Correct 34 ms 1704 KB Output is correct
31 Correct 46 ms 2540 KB Output is correct
32 Correct 49 ms 2432 KB Output is correct
33 Correct 47 ms 2400 KB Output is correct
34 Correct 46 ms 2420 KB Output is correct
35 Correct 47 ms 2412 KB Output is correct
36 Correct 47 ms 2380 KB Output is correct
37 Correct 48 ms 2396 KB Output is correct
38 Correct 46 ms 2508 KB Output is correct
39 Correct 48 ms 2384 KB Output is correct
40 Correct 47 ms 2396 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 304 KB Output is correct
45 Correct 3 ms 340 KB Output is correct
46 Correct 2 ms 356 KB Output is correct
47 Correct 7 ms 724 KB Output is correct
48 Correct 6 ms 748 KB Output is correct
49 Correct 11 ms 1080 KB Output is correct
50 Correct 23 ms 1876 KB Output is correct
51 Correct 41 ms 2636 KB Output is correct
52 Correct 37 ms 2636 KB Output is correct
53 Correct 38 ms 2652 KB Output is correct
54 Correct 40 ms 2652 KB Output is correct
55 Correct 39 ms 2696 KB Output is correct
56 Correct 38 ms 2684 KB Output is correct
57 Correct 37 ms 2620 KB Output is correct
58 Correct 38 ms 2664 KB Output is correct
59 Correct 39 ms 2644 KB Output is correct
60 Correct 39 ms 2684 KB Output is correct
61 Correct 40 ms 2692 KB Output is correct
62 Correct 39 ms 2764 KB Output is correct
63 Correct 39 ms 2696 KB Output is correct
64 Correct 39 ms 2672 KB Output is correct
65 Correct 38 ms 2620 KB Output is correct
66 Correct 37 ms 2604 KB Output is correct
67 Correct 37 ms 2616 KB Output is correct
68 Correct 38 ms 2640 KB Output is correct
69 Correct 39 ms 2636 KB Output is correct
70 Correct 39 ms 2636 KB Output is correct
71 Correct 5 ms 740 KB Output is correct
72 Correct 6 ms 724 KB Output is correct
73 Correct 5 ms 744 KB Output is correct
74 Correct 6 ms 740 KB Output is correct
75 Correct 5 ms 692 KB Output is correct
76 Correct 10 ms 1236 KB Output is correct
77 Correct 9 ms 1252 KB Output is correct
78 Correct 9 ms 1228 KB Output is correct
79 Correct 9 ms 1124 KB Output is correct
80 Correct 10 ms 1236 KB Output is correct
81 Correct 18 ms 2124 KB Output is correct
82 Correct 16 ms 2124 KB Output is correct
83 Correct 16 ms 2020 KB Output is correct
84 Correct 17 ms 2132 KB Output is correct
85 Correct 18 ms 2076 KB Output is correct
86 Correct 18 ms 2236 KB Output is correct
87 Correct 21 ms 2216 KB Output is correct
88 Correct 18 ms 2284 KB Output is correct
89 Correct 18 ms 2220 KB Output is correct
90 Correct 19 ms 2264 KB Output is correct
91 Correct 31 ms 3512 KB Output is correct
92 Correct 28 ms 3532 KB Output is correct
93 Correct 27 ms 3596 KB Output is correct
94 Correct 26 ms 3532 KB Output is correct
95 Correct 27 ms 3628 KB Output is correct
96 Correct 31 ms 4168 KB Output is correct
97 Correct 30 ms 4124 KB Output is correct
98 Correct 33 ms 4116 KB Output is correct
99 Correct 33 ms 4044 KB Output is correct
100 Correct 31 ms 4176 KB Output is correct
101 Correct 31 ms 4136 KB Output is correct
102 Correct 34 ms 4076 KB Output is correct
103 Correct 30 ms 4068 KB Output is correct
104 Correct 32 ms 4132 KB Output is correct
105 Correct 31 ms 4172 KB Output is correct
106 Correct 33 ms 4628 KB Output is correct
107 Correct 35 ms 4360 KB Output is correct
108 Correct 31 ms 4300 KB Output is correct
109 Correct 31 ms 4316 KB Output is correct
110 Correct 31 ms 4276 KB Output is correct
111 Correct 31 ms 4420 KB Output is correct
112 Correct 31 ms 4384 KB Output is correct
113 Correct 31 ms 4384 KB Output is correct
114 Correct 30 ms 4360 KB Output is correct
115 Correct 33 ms 4356 KB Output is correct
116 Correct 849 ms 2260 KB Output is correct
117 Correct 672 ms 2392 KB Output is correct
118 Correct 1545 ms 2764 KB Output is correct
119 Correct 810 ms 2824 KB Output is correct
120 Correct 546 ms 2832 KB Output is correct
121 Correct 1236 ms 3244 KB Output is correct
122 Correct 843 ms 3288 KB Output is correct
123 Correct 4286 ms 4208 KB Output is correct
124 Correct 1614 ms 4376 KB Output is correct
125 Correct 1626 ms 4412 KB Output is correct
126 Execution timed out 7041 ms 4528 KB Time limit exceeded
127 Halted 0 ms 0 KB -