답안 #572482

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
572482 2022-06-04T13:19:51 Z valerikk Diversity (CEOI21_diversity) C++17
18 / 100
49 ms 2700 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];

		bad += sum * sum * y;
		bad += sum * x * y * (y - 1) / 2;
		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];

		bad += sum * sum * y;
		bad += sum * x * y * (y - 1) / 2;
		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 308 KB Output is correct
2 Correct 1 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 Incorrect 2 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 980 KB Output is correct
5 Correct 27 ms 1760 KB Output is correct
6 Correct 46 ms 2392 KB Output is correct
7 Correct 39 ms 2700 KB Output is correct
8 Correct 42 ms 2276 KB Output is correct
9 Correct 43 ms 2288 KB Output is correct
10 Correct 42 ms 2548 KB Output is correct
11 Correct 40 ms 2396 KB Output is correct
12 Correct 41 ms 2272 KB Output is correct
13 Correct 43 ms 2444 KB Output is correct
14 Correct 41 ms 2444 KB Output is correct
15 Correct 43 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 980 KB Output is correct
5 Correct 27 ms 1760 KB Output is correct
6 Correct 46 ms 2392 KB Output is correct
7 Correct 39 ms 2700 KB Output is correct
8 Correct 42 ms 2276 KB Output is correct
9 Correct 43 ms 2288 KB Output is correct
10 Correct 42 ms 2548 KB Output is correct
11 Correct 40 ms 2396 KB Output is correct
12 Correct 41 ms 2272 KB Output is correct
13 Correct 43 ms 2444 KB Output is correct
14 Correct 41 ms 2444 KB Output is correct
15 Correct 43 ms 2384 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 356 KB Output is correct
19 Correct 20 ms 996 KB Output is correct
20 Correct 32 ms 1696 KB Output is correct
21 Correct 46 ms 2560 KB Output is correct
22 Correct 49 ms 2452 KB Output is correct
23 Correct 46 ms 2492 KB Output is correct
24 Correct 48 ms 2432 KB Output is correct
25 Correct 47 ms 2420 KB Output is correct
26 Correct 47 ms 2404 KB Output is correct
27 Correct 48 ms 2380 KB Output is correct
28 Correct 48 ms 2528 KB Output is correct
29 Correct 48 ms 2380 KB Output is correct
30 Correct 47 ms 2372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 980 KB Output is correct
5 Correct 27 ms 1760 KB Output is correct
6 Correct 46 ms 2392 KB Output is correct
7 Correct 39 ms 2700 KB Output is correct
8 Correct 42 ms 2276 KB Output is correct
9 Correct 43 ms 2288 KB Output is correct
10 Correct 42 ms 2548 KB Output is correct
11 Correct 40 ms 2396 KB Output is correct
12 Correct 41 ms 2272 KB Output is correct
13 Correct 43 ms 2444 KB Output is correct
14 Correct 41 ms 2444 KB Output is correct
15 Correct 43 ms 2384 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 356 KB Output is correct
19 Correct 20 ms 996 KB Output is correct
20 Correct 32 ms 1696 KB Output is correct
21 Correct 46 ms 2560 KB Output is correct
22 Correct 49 ms 2452 KB Output is correct
23 Correct 46 ms 2492 KB Output is correct
24 Correct 48 ms 2432 KB Output is correct
25 Correct 47 ms 2420 KB Output is correct
26 Correct 47 ms 2404 KB Output is correct
27 Correct 48 ms 2380 KB Output is correct
28 Correct 48 ms 2528 KB Output is correct
29 Correct 48 ms 2380 KB Output is correct
30 Correct 47 ms 2372 KB Output is correct
31 Incorrect 2 ms 340 KB Output isn't correct
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 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 Incorrect 2 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 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 Incorrect 2 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -