Submission #576869

# Submission time Handle Problem Language Result Execution time Memory
576869 2022-06-13T16:45:12 Z amunduzbaev Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 4844 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
#define ll int64_t

const int N = 3e5 + 5;
const int Q = 5e4 + 5;
const int B = 1150;
int a[N], l[Q], r[Q];
int cnt[N];
ll res[Q], dp[N];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, q; cin>>n>>q;
	vector<int> p;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=1;i<N;i++){
		dp[i] = dp[i-1] + (i * 1ll * (i - 1) / 2);
	}
	for(int i=0;i<q;i++){
		cin>>l[i]>>r[i];
		l[i]--;
		p.push_back(i);
	}
	sort(p.begin(), p.end(), [&](int i, int j){
		if(l[i] / B != l[j] / B){
			return l[i] < l[j];
		} 
		return r[i] < r[j];
	});
	map<int, int> mm;
	auto add = [&](int i){
		if(cnt[a[i]]){
			mm[cnt[a[i]]]--;
			if(!mm[cnt[a[i]]]) mm.erase(cnt[a[i]]);
		} 
		cnt[a[i]]++;
		mm[cnt[a[i]]]++;
	};
	auto del = [&](int i){
		assert(cnt[a[i]]);
		mm[cnt[a[i]]]--;
		if(!mm[cnt[a[i]]]) mm.erase(cnt[a[i]]);
		cnt[a[i]]--;
		if(cnt[a[i]]) mm[cnt[a[i]]]++;
	};
	
	int lx = 0, rx = 0;
	for(auto i : p){
		while(rx > r[i]){ 
			del(--rx);
		}
		while(rx < r[i]){
			add(rx++);
		}
		while(lx > l[i]){
			add(--lx);
		}
		while(lx < l[i]){
			del(lx++);
		}
		
		int ls = 0, rs = 0;
		vector<ar<ll, 2>> L, R;
		for(auto [v, c] : mm){
			int b = (c + 1) / 2, s = c / 2;
			if(ls > rs) swap(b, s);
			if(b) L.push_back({v, b}), ls += b;
			if(s) R.push_back({v, s}), rs += s;
		}
		reverse(R.begin(), R.end());
		L.insert(L.end(), R.begin(), R.end());
		ll sum = 0, sumj = 0, p = 0;
		for(auto [v, c] : L){
			ll C = p * c + c * (c - 1) / 2;
			res[i] = res[i] + v * C * sum - v * c * sumj;
			res[i] += dp[c] * v * v;
			sumj += v * C;
			sum += v * c;
			p += c;
		}
		
		res[i] += sum * (sum + 1) / 2;
	}
	
	for(int i=0;i<q;i++) cout<<res[i]<<"\n";
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 16 ms 3092 KB Output is correct
5 Correct 35 ms 3476 KB Output is correct
6 Correct 47 ms 3844 KB Output is correct
7 Correct 41 ms 3844 KB Output is correct
8 Correct 46 ms 3764 KB Output is correct
9 Correct 54 ms 3844 KB Output is correct
10 Correct 47 ms 3772 KB Output is correct
11 Correct 52 ms 3772 KB Output is correct
12 Correct 47 ms 3796 KB Output is correct
13 Correct 47 ms 3788 KB Output is correct
14 Correct 60 ms 3844 KB Output is correct
15 Correct 44 ms 3840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 16 ms 3092 KB Output is correct
5 Correct 35 ms 3476 KB Output is correct
6 Correct 47 ms 3844 KB Output is correct
7 Correct 41 ms 3844 KB Output is correct
8 Correct 46 ms 3764 KB Output is correct
9 Correct 54 ms 3844 KB Output is correct
10 Correct 47 ms 3772 KB Output is correct
11 Correct 52 ms 3772 KB Output is correct
12 Correct 47 ms 3796 KB Output is correct
13 Correct 47 ms 3788 KB Output is correct
14 Correct 60 ms 3844 KB Output is correct
15 Correct 44 ms 3840 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 5 ms 2656 KB Output is correct
19 Correct 19 ms 3092 KB Output is correct
20 Correct 37 ms 3456 KB Output is correct
21 Correct 63 ms 3852 KB Output is correct
22 Correct 56 ms 3844 KB Output is correct
23 Correct 61 ms 3796 KB Output is correct
24 Correct 58 ms 3800 KB Output is correct
25 Correct 58 ms 3848 KB Output is correct
26 Correct 58 ms 3848 KB Output is correct
27 Correct 67 ms 3796 KB Output is correct
28 Correct 55 ms 3796 KB Output is correct
29 Correct 53 ms 3788 KB Output is correct
30 Correct 56 ms 3804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 16 ms 3092 KB Output is correct
5 Correct 35 ms 3476 KB Output is correct
6 Correct 47 ms 3844 KB Output is correct
7 Correct 41 ms 3844 KB Output is correct
8 Correct 46 ms 3764 KB Output is correct
9 Correct 54 ms 3844 KB Output is correct
10 Correct 47 ms 3772 KB Output is correct
11 Correct 52 ms 3772 KB Output is correct
12 Correct 47 ms 3796 KB Output is correct
13 Correct 47 ms 3788 KB Output is correct
14 Correct 60 ms 3844 KB Output is correct
15 Correct 44 ms 3840 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 5 ms 2656 KB Output is correct
19 Correct 19 ms 3092 KB Output is correct
20 Correct 37 ms 3456 KB Output is correct
21 Correct 63 ms 3852 KB Output is correct
22 Correct 56 ms 3844 KB Output is correct
23 Correct 61 ms 3796 KB Output is correct
24 Correct 58 ms 3800 KB Output is correct
25 Correct 58 ms 3848 KB Output is correct
26 Correct 58 ms 3848 KB Output is correct
27 Correct 67 ms 3796 KB Output is correct
28 Correct 55 ms 3796 KB Output is correct
29 Correct 53 ms 3788 KB Output is correct
30 Correct 56 ms 3804 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 3 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 5 ms 2716 KB Output is correct
35 Correct 4 ms 2640 KB Output is correct
36 Correct 3 ms 2644 KB Output is correct
37 Correct 11 ms 2896 KB Output is correct
38 Correct 9 ms 2900 KB Output is correct
39 Correct 25 ms 3084 KB Output is correct
40 Correct 45 ms 3492 KB Output is correct
41 Correct 74 ms 3868 KB Output is correct
42 Correct 68 ms 3872 KB Output is correct
43 Correct 81 ms 3872 KB Output is correct
44 Correct 70 ms 3856 KB Output is correct
45 Correct 76 ms 3964 KB Output is correct
46 Correct 69 ms 3872 KB Output is correct
47 Correct 74 ms 3864 KB Output is correct
48 Correct 72 ms 3872 KB Output is correct
49 Correct 70 ms 3868 KB Output is correct
50 Correct 74 ms 3808 KB Output is correct
51 Correct 71 ms 3916 KB Output is correct
52 Correct 70 ms 3868 KB Output is correct
53 Correct 73 ms 3824 KB Output is correct
54 Correct 69 ms 3872 KB Output is correct
55 Correct 66 ms 3876 KB Output is correct
56 Correct 72 ms 3884 KB Output is correct
57 Correct 87 ms 3788 KB Output is correct
58 Correct 68 ms 3872 KB Output is correct
59 Correct 71 ms 3788 KB Output is correct
60 Correct 70 ms 3868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 16 ms 3092 KB Output is correct
15 Correct 35 ms 3476 KB Output is correct
16 Correct 47 ms 3844 KB Output is correct
17 Correct 41 ms 3844 KB Output is correct
18 Correct 46 ms 3764 KB Output is correct
19 Correct 54 ms 3844 KB Output is correct
20 Correct 47 ms 3772 KB Output is correct
21 Correct 52 ms 3772 KB Output is correct
22 Correct 47 ms 3796 KB Output is correct
23 Correct 47 ms 3788 KB Output is correct
24 Correct 60 ms 3844 KB Output is correct
25 Correct 44 ms 3840 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2656 KB Output is correct
29 Correct 19 ms 3092 KB Output is correct
30 Correct 37 ms 3456 KB Output is correct
31 Correct 63 ms 3852 KB Output is correct
32 Correct 56 ms 3844 KB Output is correct
33 Correct 61 ms 3796 KB Output is correct
34 Correct 58 ms 3800 KB Output is correct
35 Correct 58 ms 3848 KB Output is correct
36 Correct 58 ms 3848 KB Output is correct
37 Correct 67 ms 3796 KB Output is correct
38 Correct 55 ms 3796 KB Output is correct
39 Correct 53 ms 3788 KB Output is correct
40 Correct 56 ms 3804 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 3 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 5 ms 2716 KB Output is correct
45 Correct 4 ms 2640 KB Output is correct
46 Correct 3 ms 2644 KB Output is correct
47 Correct 11 ms 2896 KB Output is correct
48 Correct 9 ms 2900 KB Output is correct
49 Correct 25 ms 3084 KB Output is correct
50 Correct 45 ms 3492 KB Output is correct
51 Correct 74 ms 3868 KB Output is correct
52 Correct 68 ms 3872 KB Output is correct
53 Correct 81 ms 3872 KB Output is correct
54 Correct 70 ms 3856 KB Output is correct
55 Correct 76 ms 3964 KB Output is correct
56 Correct 69 ms 3872 KB Output is correct
57 Correct 74 ms 3864 KB Output is correct
58 Correct 72 ms 3872 KB Output is correct
59 Correct 70 ms 3868 KB Output is correct
60 Correct 74 ms 3808 KB Output is correct
61 Correct 71 ms 3916 KB Output is correct
62 Correct 70 ms 3868 KB Output is correct
63 Correct 73 ms 3824 KB Output is correct
64 Correct 69 ms 3872 KB Output is correct
65 Correct 66 ms 3876 KB Output is correct
66 Correct 72 ms 3884 KB Output is correct
67 Correct 87 ms 3788 KB Output is correct
68 Correct 68 ms 3872 KB Output is correct
69 Correct 71 ms 3788 KB Output is correct
70 Correct 70 ms 3868 KB Output is correct
71 Correct 10 ms 2824 KB Output is correct
72 Correct 10 ms 2916 KB Output is correct
73 Correct 11 ms 2900 KB Output is correct
74 Correct 8 ms 2900 KB Output is correct
75 Correct 10 ms 2900 KB Output is correct
76 Correct 14 ms 3028 KB Output is correct
77 Correct 18 ms 3104 KB Output is correct
78 Correct 13 ms 3108 KB Output is correct
79 Correct 16 ms 3112 KB Output is correct
80 Correct 15 ms 3028 KB Output is correct
81 Correct 30 ms 3512 KB Output is correct
82 Correct 24 ms 3512 KB Output is correct
83 Correct 33 ms 3500 KB Output is correct
84 Correct 25 ms 3436 KB Output is correct
85 Correct 27 ms 3624 KB Output is correct
86 Correct 24 ms 3536 KB Output is correct
87 Correct 25 ms 3488 KB Output is correct
88 Correct 23 ms 3540 KB Output is correct
89 Correct 30 ms 3444 KB Output is correct
90 Correct 26 ms 3580 KB Output is correct
91 Correct 29 ms 4220 KB Output is correct
92 Correct 29 ms 4192 KB Output is correct
93 Correct 36 ms 4132 KB Output is correct
94 Correct 37 ms 4272 KB Output is correct
95 Correct 30 ms 4132 KB Output is correct
96 Correct 40 ms 4560 KB Output is correct
97 Correct 31 ms 4516 KB Output is correct
98 Correct 32 ms 4544 KB Output is correct
99 Correct 30 ms 4576 KB Output is correct
100 Correct 33 ms 4568 KB Output is correct
101 Correct 30 ms 4540 KB Output is correct
102 Correct 35 ms 4576 KB Output is correct
103 Correct 31 ms 4564 KB Output is correct
104 Correct 33 ms 4636 KB Output is correct
105 Correct 30 ms 4564 KB Output is correct
106 Correct 27 ms 4808 KB Output is correct
107 Correct 29 ms 4784 KB Output is correct
108 Correct 27 ms 4816 KB Output is correct
109 Correct 28 ms 4820 KB Output is correct
110 Correct 28 ms 4820 KB Output is correct
111 Correct 28 ms 4820 KB Output is correct
112 Correct 31 ms 4820 KB Output is correct
113 Correct 28 ms 4704 KB Output is correct
114 Correct 31 ms 4844 KB Output is correct
115 Correct 29 ms 4804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 16 ms 3092 KB Output is correct
15 Correct 35 ms 3476 KB Output is correct
16 Correct 47 ms 3844 KB Output is correct
17 Correct 41 ms 3844 KB Output is correct
18 Correct 46 ms 3764 KB Output is correct
19 Correct 54 ms 3844 KB Output is correct
20 Correct 47 ms 3772 KB Output is correct
21 Correct 52 ms 3772 KB Output is correct
22 Correct 47 ms 3796 KB Output is correct
23 Correct 47 ms 3788 KB Output is correct
24 Correct 60 ms 3844 KB Output is correct
25 Correct 44 ms 3840 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2656 KB Output is correct
29 Correct 19 ms 3092 KB Output is correct
30 Correct 37 ms 3456 KB Output is correct
31 Correct 63 ms 3852 KB Output is correct
32 Correct 56 ms 3844 KB Output is correct
33 Correct 61 ms 3796 KB Output is correct
34 Correct 58 ms 3800 KB Output is correct
35 Correct 58 ms 3848 KB Output is correct
36 Correct 58 ms 3848 KB Output is correct
37 Correct 67 ms 3796 KB Output is correct
38 Correct 55 ms 3796 KB Output is correct
39 Correct 53 ms 3788 KB Output is correct
40 Correct 56 ms 3804 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 3 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 5 ms 2716 KB Output is correct
45 Correct 4 ms 2640 KB Output is correct
46 Correct 3 ms 2644 KB Output is correct
47 Correct 11 ms 2896 KB Output is correct
48 Correct 9 ms 2900 KB Output is correct
49 Correct 25 ms 3084 KB Output is correct
50 Correct 45 ms 3492 KB Output is correct
51 Correct 74 ms 3868 KB Output is correct
52 Correct 68 ms 3872 KB Output is correct
53 Correct 81 ms 3872 KB Output is correct
54 Correct 70 ms 3856 KB Output is correct
55 Correct 76 ms 3964 KB Output is correct
56 Correct 69 ms 3872 KB Output is correct
57 Correct 74 ms 3864 KB Output is correct
58 Correct 72 ms 3872 KB Output is correct
59 Correct 70 ms 3868 KB Output is correct
60 Correct 74 ms 3808 KB Output is correct
61 Correct 71 ms 3916 KB Output is correct
62 Correct 70 ms 3868 KB Output is correct
63 Correct 73 ms 3824 KB Output is correct
64 Correct 69 ms 3872 KB Output is correct
65 Correct 66 ms 3876 KB Output is correct
66 Correct 72 ms 3884 KB Output is correct
67 Correct 87 ms 3788 KB Output is correct
68 Correct 68 ms 3872 KB Output is correct
69 Correct 71 ms 3788 KB Output is correct
70 Correct 70 ms 3868 KB Output is correct
71 Correct 10 ms 2824 KB Output is correct
72 Correct 10 ms 2916 KB Output is correct
73 Correct 11 ms 2900 KB Output is correct
74 Correct 8 ms 2900 KB Output is correct
75 Correct 10 ms 2900 KB Output is correct
76 Correct 14 ms 3028 KB Output is correct
77 Correct 18 ms 3104 KB Output is correct
78 Correct 13 ms 3108 KB Output is correct
79 Correct 16 ms 3112 KB Output is correct
80 Correct 15 ms 3028 KB Output is correct
81 Correct 30 ms 3512 KB Output is correct
82 Correct 24 ms 3512 KB Output is correct
83 Correct 33 ms 3500 KB Output is correct
84 Correct 25 ms 3436 KB Output is correct
85 Correct 27 ms 3624 KB Output is correct
86 Correct 24 ms 3536 KB Output is correct
87 Correct 25 ms 3488 KB Output is correct
88 Correct 23 ms 3540 KB Output is correct
89 Correct 30 ms 3444 KB Output is correct
90 Correct 26 ms 3580 KB Output is correct
91 Correct 29 ms 4220 KB Output is correct
92 Correct 29 ms 4192 KB Output is correct
93 Correct 36 ms 4132 KB Output is correct
94 Correct 37 ms 4272 KB Output is correct
95 Correct 30 ms 4132 KB Output is correct
96 Correct 40 ms 4560 KB Output is correct
97 Correct 31 ms 4516 KB Output is correct
98 Correct 32 ms 4544 KB Output is correct
99 Correct 30 ms 4576 KB Output is correct
100 Correct 33 ms 4568 KB Output is correct
101 Correct 30 ms 4540 KB Output is correct
102 Correct 35 ms 4576 KB Output is correct
103 Correct 31 ms 4564 KB Output is correct
104 Correct 33 ms 4636 KB Output is correct
105 Correct 30 ms 4564 KB Output is correct
106 Correct 27 ms 4808 KB Output is correct
107 Correct 29 ms 4784 KB Output is correct
108 Correct 27 ms 4816 KB Output is correct
109 Correct 28 ms 4820 KB Output is correct
110 Correct 28 ms 4820 KB Output is correct
111 Correct 28 ms 4820 KB Output is correct
112 Correct 31 ms 4820 KB Output is correct
113 Correct 28 ms 4704 KB Output is correct
114 Correct 31 ms 4844 KB Output is correct
115 Correct 29 ms 4804 KB Output is correct
116 Correct 2768 ms 4240 KB Output is correct
117 Correct 2558 ms 4212 KB Output is correct
118 Correct 3964 ms 4500 KB Output is correct
119 Correct 2598 ms 4500 KB Output is correct
120 Correct 1791 ms 4496 KB Output is correct
121 Correct 3293 ms 4692 KB Output is correct
122 Correct 2303 ms 4700 KB Output is correct
123 Execution timed out 7045 ms 4484 KB Time limit exceeded
124 Halted 0 ms 0 KB -