Submission #592628

# Submission time Handle Problem Language Result Execution time Memory
592628 2022-07-09T11:32:35 Z SeDunion Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 7776 KB
#include <iostream>
#include <cassert>
#include <iomanip>
#include <algorithm>
#include <string>
#include <bitset> 
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#ifndef LOCAL
	#include <bits/stdc++.h>
	#define cerr if(false)cerr
#endif
 
using namespace std;
using ll = long long;
 
const int N = 3e5 + 66;
 
ll a[N], n;
 
ll pref[N];

/*
summa(a[i]*(a[i]+1)/2 + a[i]*pref[i-1]*(i+1) - a[i]*(pref[n-1]-pref[i])*i)
summa((a[i]^2+a[i])/2 + a[i]*pref[i-1]*i+a[i]*pref[i-1] - a[i]*(pref[n-1]-pref[i])*i)
summa((a[i]^2+a[i])/2 + a[i]*pref[i]*i-a[i]^2*i + a[i]*pref[i-1] - a[i]*(pref[n-1]-pref[i])*i)
summa((a[i]^2+a[i])/2 + a[i]*pref[i]*i-a[i]^2*i + a[i]*pref[i]-a[i]^2 - a[i]*(pref[n-1]-pref[i])*i)
summa((a[i]^2+a[i])/2 + a[i]*pref[i]*i-a[i]^2*i + a[i]*pref[i]-a[i]^2 - a[i]*pref[n-1]*i - a[i]*pref[i]*i)
summa((a[i]^2+a[i])/2 + a[i]*pref[i]*i - a[i]^2*i + a[i]*pref[i]-a[i]^2 - a[i]*pref[n-1]*i - a[i]*pref[i]*i)
summa(a[i]/2 + a[i]*pref[i]*(i+1) - a[i]^2*i - a[i]^2/2 - a[i]*pref[n-1]*i - a[i]*pref[i]*i)
summa(a[i]/2 + a[i]*pref[i] - a[i]^2*i - a[i]^2/2 - a[i]*pref[n-1]*i)
summa(a[i]/2 + a[i]*pref[i] - a[i]^2*i - a[i]^2/2 - a[i]*pref[n-1]*i)
 */

ll f(ll s) { return s * (s + 1) / 2; }

//summa(f(pref[i - 1]) + f(pref[n - 1] - pref[i]))

ll solve() {
	ll ans = 0;
	for (int i = 0 ; i < n ; ++ i) {
		pref[i] = a[i];
		if (i) pref[i] += pref[i - 1];
	}
	for (int i = 0 ; i < n ; ++ i) {
		if (i) ans += f(pref[i - 1]);
		ans += f(pref[n - 1] - pref[i]);
	}
	ll C = n * f(pref[n-1]);
	// C = len * f(sum)
	return C-ans;
}

int comp[N], freq[N];

void del(int v) {
	v = a[v];
	cerr << "del " << v << endl;
	comp[freq[v]]--;
	freq[v]--;
	comp[freq[v]]++;
}
void add(int v) {
	v = a[v];
	cerr << "add " << v << endl;
	comp[freq[v]]--;
	freq[v]++;
	comp[freq[v]]++;
}
deque<ll>d;
int L, R;
ll get() {
	ll q = 0;
	d.clear();
	for (int i = n ; i >= 1 ; -- i) {
		for (int _ = 0 ; _ < comp[i] ; ++ _) {
			if ((int)d.size() & 1) d.push_back(i);
			else d.push_front(i);
		}
	}
	int m = d.size(); ll pref = 0;
	cerr << "m ";
	for (int i = 0 ; i < m ; ++ i) cerr << d[i] << " ";
	cerr << endl;
	for (int i = 0 ; i < m ; ++ i) {
		q -= f(pref) + f(R-L+1 - pref - d[i]);
		pref += d[i];
	}
	q += m * f(R-L+1);
	cerr << "get " << q << endl;
	return q;
}

ll ans[N];

int ql[N], qr[N];

vector<tuple<int,int,int>>MO;

const int K = 400;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int q;
	cin >> n >> q;
	for (int i = 0 ; i < n ; ++ i) cin >> a[i];
	for (int i = 0 ; i < q ; ++ i) {
		cin >> ql[i] >> qr[i];
		ql[i]--, qr[i]--;
		MO.emplace_back(ql[i]/K, qr[i], i);
	}
	sort(MO.begin(), MO.end());
	L = 1, R = 0;
	for (int $ = 0 ; $ < q ; ++ $) {
		int i = get<2>(MO[$]);
		int l = ql[i], r = qr[i];
		while (L > l) add(--L);
		while (R < r) add(++R);
		while (L < l) del(L++);
		while (R > r) del(R--);
		ans[i] = get();
	}
	for (int i = 0 ; i < q ; ++ i) {
		cout << ans[i] << "\n";
	}
	return 0;
	map<int,int>mp;
	for (int i = 0 ; i < n ; ++ i) mp[a[i]]++;
	vector<int>freq;
	for (auto [f, s] : mp) freq.emplace_back(s);
	sort(freq.rbegin(), freq.rend());
	deque<int>p;
	for (int i : freq) {
		if ((int)p.size() & 1) p.push_back(i);
		else p.push_front(i);
	}
	n = p.size();
	for (int i = 0 ; i < n ; ++ i) a[i] = p[i];
	cout << solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 7 ms 1364 KB Output is correct
5 Correct 12 ms 2516 KB Output is correct
6 Correct 21 ms 3496 KB Output is correct
7 Correct 19 ms 3924 KB Output is correct
8 Correct 21 ms 3456 KB Output is correct
9 Correct 18 ms 3512 KB Output is correct
10 Correct 18 ms 3668 KB Output is correct
11 Correct 18 ms 3540 KB Output is correct
12 Correct 18 ms 3504 KB Output is correct
13 Correct 19 ms 3424 KB Output is correct
14 Correct 20 ms 3568 KB Output is correct
15 Correct 18 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 7 ms 1364 KB Output is correct
5 Correct 12 ms 2516 KB Output is correct
6 Correct 21 ms 3496 KB Output is correct
7 Correct 19 ms 3924 KB Output is correct
8 Correct 21 ms 3456 KB Output is correct
9 Correct 18 ms 3512 KB Output is correct
10 Correct 18 ms 3668 KB Output is correct
11 Correct 18 ms 3540 KB Output is correct
12 Correct 18 ms 3504 KB Output is correct
13 Correct 19 ms 3424 KB Output is correct
14 Correct 20 ms 3568 KB Output is correct
15 Correct 18 ms 3540 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 400 KB Output is correct
19 Correct 6 ms 1452 KB Output is correct
20 Correct 15 ms 2464 KB Output is correct
21 Correct 21 ms 3684 KB Output is correct
22 Correct 24 ms 3552 KB Output is correct
23 Correct 28 ms 3660 KB Output is correct
24 Correct 19 ms 3560 KB Output is correct
25 Correct 20 ms 3564 KB Output is correct
26 Correct 19 ms 3708 KB Output is correct
27 Correct 19 ms 3564 KB Output is correct
28 Correct 19 ms 3700 KB Output is correct
29 Correct 19 ms 3512 KB Output is correct
30 Correct 25 ms 3788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 7 ms 1364 KB Output is correct
5 Correct 12 ms 2516 KB Output is correct
6 Correct 21 ms 3496 KB Output is correct
7 Correct 19 ms 3924 KB Output is correct
8 Correct 21 ms 3456 KB Output is correct
9 Correct 18 ms 3512 KB Output is correct
10 Correct 18 ms 3668 KB Output is correct
11 Correct 18 ms 3540 KB Output is correct
12 Correct 18 ms 3504 KB Output is correct
13 Correct 19 ms 3424 KB Output is correct
14 Correct 20 ms 3568 KB Output is correct
15 Correct 18 ms 3540 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 400 KB Output is correct
19 Correct 6 ms 1452 KB Output is correct
20 Correct 15 ms 2464 KB Output is correct
21 Correct 21 ms 3684 KB Output is correct
22 Correct 24 ms 3552 KB Output is correct
23 Correct 28 ms 3660 KB Output is correct
24 Correct 19 ms 3560 KB Output is correct
25 Correct 20 ms 3564 KB Output is correct
26 Correct 19 ms 3708 KB Output is correct
27 Correct 19 ms 3564 KB Output is correct
28 Correct 19 ms 3700 KB Output is correct
29 Correct 19 ms 3512 KB Output is correct
30 Correct 25 ms 3788 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 5 ms 868 KB Output is correct
38 Correct 4 ms 852 KB Output is correct
39 Correct 8 ms 1504 KB Output is correct
40 Correct 14 ms 2644 KB Output is correct
41 Correct 20 ms 3764 KB Output is correct
42 Correct 21 ms 3828 KB Output is correct
43 Correct 25 ms 3796 KB Output is correct
44 Correct 21 ms 3764 KB Output is correct
45 Correct 25 ms 3852 KB Output is correct
46 Correct 27 ms 3788 KB Output is correct
47 Correct 22 ms 3760 KB Output is correct
48 Correct 22 ms 3844 KB Output is correct
49 Correct 21 ms 3788 KB Output is correct
50 Correct 21 ms 3872 KB Output is correct
51 Correct 21 ms 3772 KB Output is correct
52 Correct 21 ms 3740 KB Output is correct
53 Correct 22 ms 3796 KB Output is correct
54 Correct 21 ms 3796 KB Output is correct
55 Correct 30 ms 3844 KB Output is correct
56 Correct 23 ms 3848 KB Output is correct
57 Correct 26 ms 3744 KB Output is correct
58 Correct 21 ms 3840 KB Output is correct
59 Correct 21 ms 3784 KB Output is correct
60 Correct 21 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 7 ms 1364 KB Output is correct
15 Correct 12 ms 2516 KB Output is correct
16 Correct 21 ms 3496 KB Output is correct
17 Correct 19 ms 3924 KB Output is correct
18 Correct 21 ms 3456 KB Output is correct
19 Correct 18 ms 3512 KB Output is correct
20 Correct 18 ms 3668 KB Output is correct
21 Correct 18 ms 3540 KB Output is correct
22 Correct 18 ms 3504 KB Output is correct
23 Correct 19 ms 3424 KB Output is correct
24 Correct 20 ms 3568 KB Output is correct
25 Correct 18 ms 3540 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 400 KB Output is correct
29 Correct 6 ms 1452 KB Output is correct
30 Correct 15 ms 2464 KB Output is correct
31 Correct 21 ms 3684 KB Output is correct
32 Correct 24 ms 3552 KB Output is correct
33 Correct 28 ms 3660 KB Output is correct
34 Correct 19 ms 3560 KB Output is correct
35 Correct 20 ms 3564 KB Output is correct
36 Correct 19 ms 3708 KB Output is correct
37 Correct 19 ms 3564 KB Output is correct
38 Correct 19 ms 3700 KB Output is correct
39 Correct 19 ms 3512 KB Output is correct
40 Correct 25 ms 3788 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 5 ms 868 KB Output is correct
48 Correct 4 ms 852 KB Output is correct
49 Correct 8 ms 1504 KB Output is correct
50 Correct 14 ms 2644 KB Output is correct
51 Correct 20 ms 3764 KB Output is correct
52 Correct 21 ms 3828 KB Output is correct
53 Correct 25 ms 3796 KB Output is correct
54 Correct 21 ms 3764 KB Output is correct
55 Correct 25 ms 3852 KB Output is correct
56 Correct 27 ms 3788 KB Output is correct
57 Correct 22 ms 3760 KB Output is correct
58 Correct 22 ms 3844 KB Output is correct
59 Correct 21 ms 3788 KB Output is correct
60 Correct 21 ms 3872 KB Output is correct
61 Correct 21 ms 3772 KB Output is correct
62 Correct 21 ms 3740 KB Output is correct
63 Correct 22 ms 3796 KB Output is correct
64 Correct 21 ms 3796 KB Output is correct
65 Correct 30 ms 3844 KB Output is correct
66 Correct 23 ms 3848 KB Output is correct
67 Correct 26 ms 3744 KB Output is correct
68 Correct 21 ms 3840 KB Output is correct
69 Correct 21 ms 3784 KB Output is correct
70 Correct 21 ms 3796 KB Output is correct
71 Correct 4 ms 984 KB Output is correct
72 Correct 5 ms 980 KB Output is correct
73 Correct 5 ms 980 KB Output is correct
74 Correct 5 ms 980 KB Output is correct
75 Correct 5 ms 980 KB Output is correct
76 Correct 9 ms 1620 KB Output is correct
77 Correct 9 ms 1620 KB Output is correct
78 Correct 9 ms 1628 KB Output is correct
79 Correct 9 ms 1632 KB Output is correct
80 Correct 8 ms 1664 KB Output is correct
81 Correct 16 ms 2984 KB Output is correct
82 Correct 16 ms 2960 KB Output is correct
83 Correct 16 ms 2972 KB Output is correct
84 Correct 16 ms 2924 KB Output is correct
85 Correct 17 ms 2900 KB Output is correct
86 Correct 17 ms 3144 KB Output is correct
87 Correct 23 ms 3144 KB Output is correct
88 Correct 19 ms 3148 KB Output is correct
89 Correct 19 ms 3216 KB Output is correct
90 Correct 20 ms 3216 KB Output is correct
91 Correct 27 ms 5584 KB Output is correct
92 Correct 29 ms 5588 KB Output is correct
93 Correct 28 ms 5712 KB Output is correct
94 Correct 28 ms 5588 KB Output is correct
95 Correct 27 ms 5580 KB Output is correct
96 Correct 31 ms 6964 KB Output is correct
97 Correct 32 ms 6996 KB Output is correct
98 Correct 32 ms 7012 KB Output is correct
99 Correct 31 ms 6980 KB Output is correct
100 Correct 31 ms 7000 KB Output is correct
101 Correct 31 ms 6976 KB Output is correct
102 Correct 32 ms 6892 KB Output is correct
103 Correct 33 ms 7016 KB Output is correct
104 Correct 31 ms 6988 KB Output is correct
105 Correct 32 ms 6940 KB Output is correct
106 Correct 43 ms 7708 KB Output is correct
107 Correct 33 ms 7652 KB Output is correct
108 Correct 33 ms 7548 KB Output is correct
109 Correct 33 ms 7544 KB Output is correct
110 Correct 35 ms 7584 KB Output is correct
111 Correct 42 ms 7604 KB Output is correct
112 Correct 33 ms 7624 KB Output is correct
113 Correct 32 ms 7564 KB Output is correct
114 Correct 44 ms 7592 KB Output is correct
115 Correct 33 ms 7776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 7 ms 1364 KB Output is correct
15 Correct 12 ms 2516 KB Output is correct
16 Correct 21 ms 3496 KB Output is correct
17 Correct 19 ms 3924 KB Output is correct
18 Correct 21 ms 3456 KB Output is correct
19 Correct 18 ms 3512 KB Output is correct
20 Correct 18 ms 3668 KB Output is correct
21 Correct 18 ms 3540 KB Output is correct
22 Correct 18 ms 3504 KB Output is correct
23 Correct 19 ms 3424 KB Output is correct
24 Correct 20 ms 3568 KB Output is correct
25 Correct 18 ms 3540 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 400 KB Output is correct
29 Correct 6 ms 1452 KB Output is correct
30 Correct 15 ms 2464 KB Output is correct
31 Correct 21 ms 3684 KB Output is correct
32 Correct 24 ms 3552 KB Output is correct
33 Correct 28 ms 3660 KB Output is correct
34 Correct 19 ms 3560 KB Output is correct
35 Correct 20 ms 3564 KB Output is correct
36 Correct 19 ms 3708 KB Output is correct
37 Correct 19 ms 3564 KB Output is correct
38 Correct 19 ms 3700 KB Output is correct
39 Correct 19 ms 3512 KB Output is correct
40 Correct 25 ms 3788 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 5 ms 868 KB Output is correct
48 Correct 4 ms 852 KB Output is correct
49 Correct 8 ms 1504 KB Output is correct
50 Correct 14 ms 2644 KB Output is correct
51 Correct 20 ms 3764 KB Output is correct
52 Correct 21 ms 3828 KB Output is correct
53 Correct 25 ms 3796 KB Output is correct
54 Correct 21 ms 3764 KB Output is correct
55 Correct 25 ms 3852 KB Output is correct
56 Correct 27 ms 3788 KB Output is correct
57 Correct 22 ms 3760 KB Output is correct
58 Correct 22 ms 3844 KB Output is correct
59 Correct 21 ms 3788 KB Output is correct
60 Correct 21 ms 3872 KB Output is correct
61 Correct 21 ms 3772 KB Output is correct
62 Correct 21 ms 3740 KB Output is correct
63 Correct 22 ms 3796 KB Output is correct
64 Correct 21 ms 3796 KB Output is correct
65 Correct 30 ms 3844 KB Output is correct
66 Correct 23 ms 3848 KB Output is correct
67 Correct 26 ms 3744 KB Output is correct
68 Correct 21 ms 3840 KB Output is correct
69 Correct 21 ms 3784 KB Output is correct
70 Correct 21 ms 3796 KB Output is correct
71 Correct 4 ms 984 KB Output is correct
72 Correct 5 ms 980 KB Output is correct
73 Correct 5 ms 980 KB Output is correct
74 Correct 5 ms 980 KB Output is correct
75 Correct 5 ms 980 KB Output is correct
76 Correct 9 ms 1620 KB Output is correct
77 Correct 9 ms 1620 KB Output is correct
78 Correct 9 ms 1628 KB Output is correct
79 Correct 9 ms 1632 KB Output is correct
80 Correct 8 ms 1664 KB Output is correct
81 Correct 16 ms 2984 KB Output is correct
82 Correct 16 ms 2960 KB Output is correct
83 Correct 16 ms 2972 KB Output is correct
84 Correct 16 ms 2924 KB Output is correct
85 Correct 17 ms 2900 KB Output is correct
86 Correct 17 ms 3144 KB Output is correct
87 Correct 23 ms 3144 KB Output is correct
88 Correct 19 ms 3148 KB Output is correct
89 Correct 19 ms 3216 KB Output is correct
90 Correct 20 ms 3216 KB Output is correct
91 Correct 27 ms 5584 KB Output is correct
92 Correct 29 ms 5588 KB Output is correct
93 Correct 28 ms 5712 KB Output is correct
94 Correct 28 ms 5588 KB Output is correct
95 Correct 27 ms 5580 KB Output is correct
96 Correct 31 ms 6964 KB Output is correct
97 Correct 32 ms 6996 KB Output is correct
98 Correct 32 ms 7012 KB Output is correct
99 Correct 31 ms 6980 KB Output is correct
100 Correct 31 ms 7000 KB Output is correct
101 Correct 31 ms 6976 KB Output is correct
102 Correct 32 ms 6892 KB Output is correct
103 Correct 33 ms 7016 KB Output is correct
104 Correct 31 ms 6988 KB Output is correct
105 Correct 32 ms 6940 KB Output is correct
106 Correct 43 ms 7708 KB Output is correct
107 Correct 33 ms 7652 KB Output is correct
108 Correct 33 ms 7548 KB Output is correct
109 Correct 33 ms 7544 KB Output is correct
110 Correct 35 ms 7584 KB Output is correct
111 Correct 42 ms 7604 KB Output is correct
112 Correct 33 ms 7624 KB Output is correct
113 Correct 32 ms 7564 KB Output is correct
114 Correct 44 ms 7592 KB Output is correct
115 Correct 33 ms 7776 KB Output is correct
116 Correct 960 ms 2848 KB Output is correct
117 Correct 1004 ms 2968 KB Output is correct
118 Correct 4448 ms 3492 KB Output is correct
119 Correct 4677 ms 3532 KB Output is correct
120 Correct 4670 ms 3640 KB Output is correct
121 Execution timed out 7032 ms 3492 KB Time limit exceeded
122 Halted 0 ms 0 KB -