Submission #576874

# Submission time Handle Problem Language Result Execution time Memory
576874 2022-06-13T16:54:15 Z amunduzbaev Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 4028 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 = 1350;
int a[N], l[Q], r[Q];
int cnt[N];
ll res[Q];
 
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=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){
		if(rx > r[i]){ 
			lx = rx = l[i];
			memset(cnt, 0, sizeof cnt);
			mm.clear();
		}
		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){
			res[i] += v * (p * c + c * (c - 1) / 2) * sum;
			res[i] -= v * c * sumj;
			res[i] += (c * c * (c + 1) / 2 - c * (c + 1) * (2 * c + 1) / 6) * v * v;
			sumj += v * (p * c + c * (c - 1) / 2);
			p += c, sum += v * 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 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 16 ms 740 KB Output is correct
5 Correct 27 ms 1068 KB Output is correct
6 Correct 44 ms 1496 KB Output is correct
7 Correct 38 ms 1504 KB Output is correct
8 Correct 47 ms 1500 KB Output is correct
9 Correct 43 ms 1492 KB Output is correct
10 Correct 41 ms 1492 KB Output is correct
11 Correct 43 ms 1508 KB Output is correct
12 Correct 44 ms 1492 KB Output is correct
13 Correct 44 ms 1492 KB Output is correct
14 Correct 43 ms 1384 KB Output is correct
15 Correct 43 ms 1496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 16 ms 740 KB Output is correct
5 Correct 27 ms 1068 KB Output is correct
6 Correct 44 ms 1496 KB Output is correct
7 Correct 38 ms 1504 KB Output is correct
8 Correct 47 ms 1500 KB Output is correct
9 Correct 43 ms 1492 KB Output is correct
10 Correct 41 ms 1492 KB Output is correct
11 Correct 43 ms 1508 KB Output is correct
12 Correct 44 ms 1492 KB Output is correct
13 Correct 44 ms 1492 KB Output is correct
14 Correct 43 ms 1384 KB Output is correct
15 Correct 43 ms 1496 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 18 ms 724 KB Output is correct
20 Correct 35 ms 1108 KB Output is correct
21 Correct 54 ms 1500 KB Output is correct
22 Correct 54 ms 1540 KB Output is correct
23 Correct 52 ms 1416 KB Output is correct
24 Correct 52 ms 1492 KB Output is correct
25 Correct 53 ms 1496 KB Output is correct
26 Correct 56 ms 1504 KB Output is correct
27 Correct 55 ms 1492 KB Output is correct
28 Correct 51 ms 1512 KB Output is correct
29 Correct 52 ms 1484 KB Output is correct
30 Correct 54 ms 1512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 16 ms 740 KB Output is correct
5 Correct 27 ms 1068 KB Output is correct
6 Correct 44 ms 1496 KB Output is correct
7 Correct 38 ms 1504 KB Output is correct
8 Correct 47 ms 1500 KB Output is correct
9 Correct 43 ms 1492 KB Output is correct
10 Correct 41 ms 1492 KB Output is correct
11 Correct 43 ms 1508 KB Output is correct
12 Correct 44 ms 1492 KB Output is correct
13 Correct 44 ms 1492 KB Output is correct
14 Correct 43 ms 1384 KB Output is correct
15 Correct 43 ms 1496 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 18 ms 724 KB Output is correct
20 Correct 35 ms 1108 KB Output is correct
21 Correct 54 ms 1500 KB Output is correct
22 Correct 54 ms 1540 KB Output is correct
23 Correct 52 ms 1416 KB Output is correct
24 Correct 52 ms 1492 KB Output is correct
25 Correct 53 ms 1496 KB Output is correct
26 Correct 56 ms 1504 KB Output is correct
27 Correct 55 ms 1492 KB Output is correct
28 Correct 51 ms 1512 KB Output is correct
29 Correct 52 ms 1484 KB Output is correct
30 Correct 54 ms 1512 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 10 ms 468 KB Output is correct
38 Correct 8 ms 468 KB Output is correct
39 Correct 18 ms 752 KB Output is correct
40 Correct 43 ms 1104 KB Output is correct
41 Correct 67 ms 1516 KB Output is correct
42 Correct 67 ms 1536 KB Output is correct
43 Correct 69 ms 1648 KB Output is correct
44 Correct 68 ms 1524 KB Output is correct
45 Correct 68 ms 1556 KB Output is correct
46 Correct 69 ms 1492 KB Output is correct
47 Correct 67 ms 1524 KB Output is correct
48 Correct 67 ms 1512 KB Output is correct
49 Correct 70 ms 1492 KB Output is correct
50 Correct 68 ms 1524 KB Output is correct
51 Correct 69 ms 1632 KB Output is correct
52 Correct 67 ms 1528 KB Output is correct
53 Correct 67 ms 1532 KB Output is correct
54 Correct 70 ms 1612 KB Output is correct
55 Correct 67 ms 1532 KB Output is correct
56 Correct 67 ms 1520 KB Output is correct
57 Correct 66 ms 1452 KB Output is correct
58 Correct 68 ms 1520 KB Output is correct
59 Correct 68 ms 1512 KB Output is correct
60 Correct 69 ms 1516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 16 ms 740 KB Output is correct
15 Correct 27 ms 1068 KB Output is correct
16 Correct 44 ms 1496 KB Output is correct
17 Correct 38 ms 1504 KB Output is correct
18 Correct 47 ms 1500 KB Output is correct
19 Correct 43 ms 1492 KB Output is correct
20 Correct 41 ms 1492 KB Output is correct
21 Correct 43 ms 1508 KB Output is correct
22 Correct 44 ms 1492 KB Output is correct
23 Correct 44 ms 1492 KB Output is correct
24 Correct 43 ms 1384 KB Output is correct
25 Correct 43 ms 1496 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 18 ms 724 KB Output is correct
30 Correct 35 ms 1108 KB Output is correct
31 Correct 54 ms 1500 KB Output is correct
32 Correct 54 ms 1540 KB Output is correct
33 Correct 52 ms 1416 KB Output is correct
34 Correct 52 ms 1492 KB Output is correct
35 Correct 53 ms 1496 KB Output is correct
36 Correct 56 ms 1504 KB Output is correct
37 Correct 55 ms 1492 KB Output is correct
38 Correct 51 ms 1512 KB Output is correct
39 Correct 52 ms 1484 KB Output is correct
40 Correct 54 ms 1512 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 10 ms 468 KB Output is correct
48 Correct 8 ms 468 KB Output is correct
49 Correct 18 ms 752 KB Output is correct
50 Correct 43 ms 1104 KB Output is correct
51 Correct 67 ms 1516 KB Output is correct
52 Correct 67 ms 1536 KB Output is correct
53 Correct 69 ms 1648 KB Output is correct
54 Correct 68 ms 1524 KB Output is correct
55 Correct 68 ms 1556 KB Output is correct
56 Correct 69 ms 1492 KB Output is correct
57 Correct 67 ms 1524 KB Output is correct
58 Correct 67 ms 1512 KB Output is correct
59 Correct 70 ms 1492 KB Output is correct
60 Correct 68 ms 1524 KB Output is correct
61 Correct 69 ms 1632 KB Output is correct
62 Correct 67 ms 1528 KB Output is correct
63 Correct 67 ms 1532 KB Output is correct
64 Correct 70 ms 1612 KB Output is correct
65 Correct 67 ms 1532 KB Output is correct
66 Correct 67 ms 1520 KB Output is correct
67 Correct 66 ms 1452 KB Output is correct
68 Correct 68 ms 1520 KB Output is correct
69 Correct 68 ms 1512 KB Output is correct
70 Correct 69 ms 1516 KB Output is correct
71 Correct 8 ms 524 KB Output is correct
72 Correct 7 ms 468 KB Output is correct
73 Correct 7 ms 468 KB Output is correct
74 Correct 7 ms 468 KB Output is correct
75 Correct 7 ms 468 KB Output is correct
76 Correct 12 ms 756 KB Output is correct
77 Correct 13 ms 724 KB Output is correct
78 Correct 13 ms 852 KB Output is correct
79 Correct 11 ms 724 KB Output is correct
80 Correct 14 ms 760 KB Output is correct
81 Correct 25 ms 1160 KB Output is correct
82 Correct 23 ms 1168 KB Output is correct
83 Correct 23 ms 1108 KB Output is correct
84 Correct 25 ms 1084 KB Output is correct
85 Correct 23 ms 1164 KB Output is correct
86 Correct 21 ms 1112 KB Output is correct
87 Correct 21 ms 1196 KB Output is correct
88 Correct 22 ms 1124 KB Output is correct
89 Correct 21 ms 1200 KB Output is correct
90 Correct 21 ms 1100 KB Output is correct
91 Correct 27 ms 1884 KB Output is correct
92 Correct 29 ms 1804 KB Output is correct
93 Correct 36 ms 1844 KB Output is correct
94 Correct 28 ms 1768 KB Output is correct
95 Correct 27 ms 1840 KB Output is correct
96 Correct 27 ms 2304 KB Output is correct
97 Correct 29 ms 2196 KB Output is correct
98 Correct 29 ms 2276 KB Output is correct
99 Correct 28 ms 2244 KB Output is correct
100 Correct 28 ms 2284 KB Output is correct
101 Correct 27 ms 2272 KB Output is correct
102 Correct 27 ms 2296 KB Output is correct
103 Correct 27 ms 2276 KB Output is correct
104 Correct 27 ms 2252 KB Output is correct
105 Correct 28 ms 2276 KB Output is correct
106 Correct 25 ms 2368 KB Output is correct
107 Correct 25 ms 2380 KB Output is correct
108 Correct 26 ms 2452 KB Output is correct
109 Correct 25 ms 2480 KB Output is correct
110 Correct 26 ms 2388 KB Output is correct
111 Correct 27 ms 2388 KB Output is correct
112 Correct 28 ms 2428 KB Output is correct
113 Correct 26 ms 2432 KB Output is correct
114 Correct 27 ms 2400 KB Output is correct
115 Correct 26 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 16 ms 740 KB Output is correct
15 Correct 27 ms 1068 KB Output is correct
16 Correct 44 ms 1496 KB Output is correct
17 Correct 38 ms 1504 KB Output is correct
18 Correct 47 ms 1500 KB Output is correct
19 Correct 43 ms 1492 KB Output is correct
20 Correct 41 ms 1492 KB Output is correct
21 Correct 43 ms 1508 KB Output is correct
22 Correct 44 ms 1492 KB Output is correct
23 Correct 44 ms 1492 KB Output is correct
24 Correct 43 ms 1384 KB Output is correct
25 Correct 43 ms 1496 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 18 ms 724 KB Output is correct
30 Correct 35 ms 1108 KB Output is correct
31 Correct 54 ms 1500 KB Output is correct
32 Correct 54 ms 1540 KB Output is correct
33 Correct 52 ms 1416 KB Output is correct
34 Correct 52 ms 1492 KB Output is correct
35 Correct 53 ms 1496 KB Output is correct
36 Correct 56 ms 1504 KB Output is correct
37 Correct 55 ms 1492 KB Output is correct
38 Correct 51 ms 1512 KB Output is correct
39 Correct 52 ms 1484 KB Output is correct
40 Correct 54 ms 1512 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 10 ms 468 KB Output is correct
48 Correct 8 ms 468 KB Output is correct
49 Correct 18 ms 752 KB Output is correct
50 Correct 43 ms 1104 KB Output is correct
51 Correct 67 ms 1516 KB Output is correct
52 Correct 67 ms 1536 KB Output is correct
53 Correct 69 ms 1648 KB Output is correct
54 Correct 68 ms 1524 KB Output is correct
55 Correct 68 ms 1556 KB Output is correct
56 Correct 69 ms 1492 KB Output is correct
57 Correct 67 ms 1524 KB Output is correct
58 Correct 67 ms 1512 KB Output is correct
59 Correct 70 ms 1492 KB Output is correct
60 Correct 68 ms 1524 KB Output is correct
61 Correct 69 ms 1632 KB Output is correct
62 Correct 67 ms 1528 KB Output is correct
63 Correct 67 ms 1532 KB Output is correct
64 Correct 70 ms 1612 KB Output is correct
65 Correct 67 ms 1532 KB Output is correct
66 Correct 67 ms 1520 KB Output is correct
67 Correct 66 ms 1452 KB Output is correct
68 Correct 68 ms 1520 KB Output is correct
69 Correct 68 ms 1512 KB Output is correct
70 Correct 69 ms 1516 KB Output is correct
71 Correct 8 ms 524 KB Output is correct
72 Correct 7 ms 468 KB Output is correct
73 Correct 7 ms 468 KB Output is correct
74 Correct 7 ms 468 KB Output is correct
75 Correct 7 ms 468 KB Output is correct
76 Correct 12 ms 756 KB Output is correct
77 Correct 13 ms 724 KB Output is correct
78 Correct 13 ms 852 KB Output is correct
79 Correct 11 ms 724 KB Output is correct
80 Correct 14 ms 760 KB Output is correct
81 Correct 25 ms 1160 KB Output is correct
82 Correct 23 ms 1168 KB Output is correct
83 Correct 23 ms 1108 KB Output is correct
84 Correct 25 ms 1084 KB Output is correct
85 Correct 23 ms 1164 KB Output is correct
86 Correct 21 ms 1112 KB Output is correct
87 Correct 21 ms 1196 KB Output is correct
88 Correct 22 ms 1124 KB Output is correct
89 Correct 21 ms 1200 KB Output is correct
90 Correct 21 ms 1100 KB Output is correct
91 Correct 27 ms 1884 KB Output is correct
92 Correct 29 ms 1804 KB Output is correct
93 Correct 36 ms 1844 KB Output is correct
94 Correct 28 ms 1768 KB Output is correct
95 Correct 27 ms 1840 KB Output is correct
96 Correct 27 ms 2304 KB Output is correct
97 Correct 29 ms 2196 KB Output is correct
98 Correct 29 ms 2276 KB Output is correct
99 Correct 28 ms 2244 KB Output is correct
100 Correct 28 ms 2284 KB Output is correct
101 Correct 27 ms 2272 KB Output is correct
102 Correct 27 ms 2296 KB Output is correct
103 Correct 27 ms 2276 KB Output is correct
104 Correct 27 ms 2252 KB Output is correct
105 Correct 28 ms 2276 KB Output is correct
106 Correct 25 ms 2368 KB Output is correct
107 Correct 25 ms 2380 KB Output is correct
108 Correct 26 ms 2452 KB Output is correct
109 Correct 25 ms 2480 KB Output is correct
110 Correct 26 ms 2388 KB Output is correct
111 Correct 27 ms 2388 KB Output is correct
112 Correct 28 ms 2428 KB Output is correct
113 Correct 26 ms 2432 KB Output is correct
114 Correct 27 ms 2400 KB Output is correct
115 Correct 26 ms 2388 KB Output is correct
116 Correct 3118 ms 3028 KB Output is correct
117 Correct 2885 ms 3032 KB Output is correct
118 Correct 4187 ms 3280 KB Output is correct
119 Correct 2745 ms 3312 KB Output is correct
120 Correct 1878 ms 3424 KB Output is correct
121 Correct 3041 ms 3508 KB Output is correct
122 Correct 2136 ms 3504 KB Output is correct
123 Correct 5606 ms 4020 KB Output is correct
124 Correct 2632 ms 4024 KB Output is correct
125 Correct 1858 ms 4028 KB Output is correct
126 Execution timed out 7017 ms 3712 KB Time limit exceeded
127 Halted 0 ms 0 KB -