답안 #576861

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
576861 2022-06-13T16:35:29 Z amunduzbaev Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 4624 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
#define int int64_t

const int N = 3e5 + 5;
const int Q = 5e4 + 5;
const int B = 550;
int a[N], l[Q], r[Q];
int cnt[N], 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){
		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;
		deque<ar<int, 2>> L, R;
		assert(mm.size() < 2 * B);
		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_front({v, s}), rs += s;
		}
		
		L.insert(L.end(), R.begin(), R.end());
		int 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";
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 264 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 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 224 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 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 17 ms 1108 KB Output is correct
5 Correct 34 ms 1900 KB Output is correct
6 Correct 48 ms 2600 KB Output is correct
7 Correct 50 ms 2680 KB Output is correct
8 Correct 63 ms 2672 KB Output is correct
9 Correct 56 ms 2672 KB Output is correct
10 Correct 54 ms 2676 KB Output is correct
11 Correct 48 ms 2708 KB Output is correct
12 Correct 49 ms 2668 KB Output is correct
13 Correct 55 ms 2672 KB Output is correct
14 Correct 47 ms 2588 KB Output is correct
15 Correct 51 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 17 ms 1108 KB Output is correct
5 Correct 34 ms 1900 KB Output is correct
6 Correct 48 ms 2600 KB Output is correct
7 Correct 50 ms 2680 KB Output is correct
8 Correct 63 ms 2672 KB Output is correct
9 Correct 56 ms 2672 KB Output is correct
10 Correct 54 ms 2676 KB Output is correct
11 Correct 48 ms 2708 KB Output is correct
12 Correct 49 ms 2668 KB Output is correct
13 Correct 55 ms 2672 KB Output is correct
14 Correct 47 ms 2588 KB Output is correct
15 Correct 51 ms 2636 KB Output is correct
16 Correct 1 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 19 ms 1120 KB Output is correct
20 Correct 37 ms 1824 KB Output is correct
21 Correct 56 ms 2644 KB Output is correct
22 Correct 57 ms 2668 KB Output is correct
23 Correct 63 ms 2672 KB Output is correct
24 Correct 61 ms 2672 KB Output is correct
25 Correct 61 ms 2644 KB Output is correct
26 Correct 63 ms 2672 KB Output is correct
27 Correct 62 ms 2672 KB Output is correct
28 Correct 54 ms 2668 KB Output is correct
29 Correct 57 ms 2672 KB Output is correct
30 Correct 59 ms 2624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 17 ms 1108 KB Output is correct
5 Correct 34 ms 1900 KB Output is correct
6 Correct 48 ms 2600 KB Output is correct
7 Correct 50 ms 2680 KB Output is correct
8 Correct 63 ms 2672 KB Output is correct
9 Correct 56 ms 2672 KB Output is correct
10 Correct 54 ms 2676 KB Output is correct
11 Correct 48 ms 2708 KB Output is correct
12 Correct 49 ms 2668 KB Output is correct
13 Correct 55 ms 2672 KB Output is correct
14 Correct 47 ms 2588 KB Output is correct
15 Correct 51 ms 2636 KB Output is correct
16 Correct 1 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 19 ms 1120 KB Output is correct
20 Correct 37 ms 1824 KB Output is correct
21 Correct 56 ms 2644 KB Output is correct
22 Correct 57 ms 2668 KB Output is correct
23 Correct 63 ms 2672 KB Output is correct
24 Correct 61 ms 2672 KB Output is correct
25 Correct 61 ms 2644 KB Output is correct
26 Correct 63 ms 2672 KB Output is correct
27 Correct 62 ms 2672 KB Output is correct
28 Correct 54 ms 2668 KB Output is correct
29 Correct 57 ms 2672 KB Output is correct
30 Correct 59 ms 2624 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 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 2 ms 340 KB Output is correct
37 Correct 12 ms 724 KB Output is correct
38 Correct 11 ms 720 KB Output is correct
39 Correct 20 ms 1108 KB Output is correct
40 Correct 47 ms 1912 KB Output is correct
41 Correct 71 ms 2656 KB Output is correct
42 Correct 69 ms 2704 KB Output is correct
43 Correct 71 ms 2712 KB Output is correct
44 Correct 68 ms 2704 KB Output is correct
45 Correct 89 ms 2680 KB Output is correct
46 Correct 66 ms 2696 KB Output is correct
47 Correct 66 ms 2684 KB Output is correct
48 Correct 67 ms 2708 KB Output is correct
49 Correct 69 ms 2696 KB Output is correct
50 Correct 68 ms 2704 KB Output is correct
51 Correct 66 ms 2636 KB Output is correct
52 Correct 78 ms 2708 KB Output is correct
53 Correct 67 ms 2676 KB Output is correct
54 Correct 71 ms 2704 KB Output is correct
55 Correct 79 ms 2604 KB Output is correct
56 Correct 69 ms 2704 KB Output is correct
57 Correct 67 ms 2636 KB Output is correct
58 Correct 70 ms 2688 KB Output is correct
59 Correct 67 ms 2696 KB Output is correct
60 Correct 66 ms 2708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 264 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 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 224 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 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 17 ms 1108 KB Output is correct
15 Correct 34 ms 1900 KB Output is correct
16 Correct 48 ms 2600 KB Output is correct
17 Correct 50 ms 2680 KB Output is correct
18 Correct 63 ms 2672 KB Output is correct
19 Correct 56 ms 2672 KB Output is correct
20 Correct 54 ms 2676 KB Output is correct
21 Correct 48 ms 2708 KB Output is correct
22 Correct 49 ms 2668 KB Output is correct
23 Correct 55 ms 2672 KB Output is correct
24 Correct 47 ms 2588 KB Output is correct
25 Correct 51 ms 2636 KB Output is correct
26 Correct 1 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 19 ms 1120 KB Output is correct
30 Correct 37 ms 1824 KB Output is correct
31 Correct 56 ms 2644 KB Output is correct
32 Correct 57 ms 2668 KB Output is correct
33 Correct 63 ms 2672 KB Output is correct
34 Correct 61 ms 2672 KB Output is correct
35 Correct 61 ms 2644 KB Output is correct
36 Correct 63 ms 2672 KB Output is correct
37 Correct 62 ms 2672 KB Output is correct
38 Correct 54 ms 2668 KB Output is correct
39 Correct 57 ms 2672 KB Output is correct
40 Correct 59 ms 2624 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 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 2 ms 340 KB Output is correct
47 Correct 12 ms 724 KB Output is correct
48 Correct 11 ms 720 KB Output is correct
49 Correct 20 ms 1108 KB Output is correct
50 Correct 47 ms 1912 KB Output is correct
51 Correct 71 ms 2656 KB Output is correct
52 Correct 69 ms 2704 KB Output is correct
53 Correct 71 ms 2712 KB Output is correct
54 Correct 68 ms 2704 KB Output is correct
55 Correct 89 ms 2680 KB Output is correct
56 Correct 66 ms 2696 KB Output is correct
57 Correct 66 ms 2684 KB Output is correct
58 Correct 67 ms 2708 KB Output is correct
59 Correct 69 ms 2696 KB Output is correct
60 Correct 68 ms 2704 KB Output is correct
61 Correct 66 ms 2636 KB Output is correct
62 Correct 78 ms 2708 KB Output is correct
63 Correct 67 ms 2676 KB Output is correct
64 Correct 71 ms 2704 KB Output is correct
65 Correct 79 ms 2604 KB Output is correct
66 Correct 69 ms 2704 KB Output is correct
67 Correct 67 ms 2636 KB Output is correct
68 Correct 70 ms 2688 KB Output is correct
69 Correct 67 ms 2696 KB Output is correct
70 Correct 66 ms 2708 KB Output is correct
71 Correct 6 ms 724 KB Output is correct
72 Correct 7 ms 724 KB Output is correct
73 Correct 7 ms 724 KB Output is correct
74 Correct 7 ms 724 KB Output is correct
75 Correct 7 ms 724 KB Output is correct
76 Correct 13 ms 1180 KB Output is correct
77 Correct 12 ms 1168 KB Output is correct
78 Correct 12 ms 1108 KB Output is correct
79 Correct 13 ms 1168 KB Output is correct
80 Correct 12 ms 1108 KB Output is correct
81 Correct 23 ms 1944 KB Output is correct
82 Correct 23 ms 1968 KB Output is correct
83 Correct 23 ms 1892 KB Output is correct
84 Correct 23 ms 1976 KB Output is correct
85 Correct 24 ms 1980 KB Output is correct
86 Correct 22 ms 2060 KB Output is correct
87 Correct 22 ms 1988 KB Output is correct
88 Correct 27 ms 2004 KB Output is correct
89 Correct 24 ms 2068 KB Output is correct
90 Correct 22 ms 2068 KB Output is correct
91 Correct 28 ms 3384 KB Output is correct
92 Correct 28 ms 3336 KB Output is correct
93 Correct 31 ms 3460 KB Output is correct
94 Correct 29 ms 3448 KB Output is correct
95 Correct 28 ms 3412 KB Output is correct
96 Correct 30 ms 4172 KB Output is correct
97 Correct 28 ms 4156 KB Output is correct
98 Correct 28 ms 4184 KB Output is correct
99 Correct 29 ms 4200 KB Output is correct
100 Correct 29 ms 4132 KB Output is correct
101 Correct 29 ms 4188 KB Output is correct
102 Correct 29 ms 4180 KB Output is correct
103 Correct 28 ms 4168 KB Output is correct
104 Correct 34 ms 4172 KB Output is correct
105 Correct 29 ms 4196 KB Output is correct
106 Correct 26 ms 4620 KB Output is correct
107 Correct 26 ms 4564 KB Output is correct
108 Correct 26 ms 4612 KB Output is correct
109 Correct 28 ms 4556 KB Output is correct
110 Correct 27 ms 4556 KB Output is correct
111 Correct 26 ms 4564 KB Output is correct
112 Correct 27 ms 4568 KB Output is correct
113 Correct 27 ms 4620 KB Output is correct
114 Correct 28 ms 4624 KB Output is correct
115 Correct 27 ms 4616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 264 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 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 224 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 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 17 ms 1108 KB Output is correct
15 Correct 34 ms 1900 KB Output is correct
16 Correct 48 ms 2600 KB Output is correct
17 Correct 50 ms 2680 KB Output is correct
18 Correct 63 ms 2672 KB Output is correct
19 Correct 56 ms 2672 KB Output is correct
20 Correct 54 ms 2676 KB Output is correct
21 Correct 48 ms 2708 KB Output is correct
22 Correct 49 ms 2668 KB Output is correct
23 Correct 55 ms 2672 KB Output is correct
24 Correct 47 ms 2588 KB Output is correct
25 Correct 51 ms 2636 KB Output is correct
26 Correct 1 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 19 ms 1120 KB Output is correct
30 Correct 37 ms 1824 KB Output is correct
31 Correct 56 ms 2644 KB Output is correct
32 Correct 57 ms 2668 KB Output is correct
33 Correct 63 ms 2672 KB Output is correct
34 Correct 61 ms 2672 KB Output is correct
35 Correct 61 ms 2644 KB Output is correct
36 Correct 63 ms 2672 KB Output is correct
37 Correct 62 ms 2672 KB Output is correct
38 Correct 54 ms 2668 KB Output is correct
39 Correct 57 ms 2672 KB Output is correct
40 Correct 59 ms 2624 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 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 2 ms 340 KB Output is correct
47 Correct 12 ms 724 KB Output is correct
48 Correct 11 ms 720 KB Output is correct
49 Correct 20 ms 1108 KB Output is correct
50 Correct 47 ms 1912 KB Output is correct
51 Correct 71 ms 2656 KB Output is correct
52 Correct 69 ms 2704 KB Output is correct
53 Correct 71 ms 2712 KB Output is correct
54 Correct 68 ms 2704 KB Output is correct
55 Correct 89 ms 2680 KB Output is correct
56 Correct 66 ms 2696 KB Output is correct
57 Correct 66 ms 2684 KB Output is correct
58 Correct 67 ms 2708 KB Output is correct
59 Correct 69 ms 2696 KB Output is correct
60 Correct 68 ms 2704 KB Output is correct
61 Correct 66 ms 2636 KB Output is correct
62 Correct 78 ms 2708 KB Output is correct
63 Correct 67 ms 2676 KB Output is correct
64 Correct 71 ms 2704 KB Output is correct
65 Correct 79 ms 2604 KB Output is correct
66 Correct 69 ms 2704 KB Output is correct
67 Correct 67 ms 2636 KB Output is correct
68 Correct 70 ms 2688 KB Output is correct
69 Correct 67 ms 2696 KB Output is correct
70 Correct 66 ms 2708 KB Output is correct
71 Correct 6 ms 724 KB Output is correct
72 Correct 7 ms 724 KB Output is correct
73 Correct 7 ms 724 KB Output is correct
74 Correct 7 ms 724 KB Output is correct
75 Correct 7 ms 724 KB Output is correct
76 Correct 13 ms 1180 KB Output is correct
77 Correct 12 ms 1168 KB Output is correct
78 Correct 12 ms 1108 KB Output is correct
79 Correct 13 ms 1168 KB Output is correct
80 Correct 12 ms 1108 KB Output is correct
81 Correct 23 ms 1944 KB Output is correct
82 Correct 23 ms 1968 KB Output is correct
83 Correct 23 ms 1892 KB Output is correct
84 Correct 23 ms 1976 KB Output is correct
85 Correct 24 ms 1980 KB Output is correct
86 Correct 22 ms 2060 KB Output is correct
87 Correct 22 ms 1988 KB Output is correct
88 Correct 27 ms 2004 KB Output is correct
89 Correct 24 ms 2068 KB Output is correct
90 Correct 22 ms 2068 KB Output is correct
91 Correct 28 ms 3384 KB Output is correct
92 Correct 28 ms 3336 KB Output is correct
93 Correct 31 ms 3460 KB Output is correct
94 Correct 29 ms 3448 KB Output is correct
95 Correct 28 ms 3412 KB Output is correct
96 Correct 30 ms 4172 KB Output is correct
97 Correct 28 ms 4156 KB Output is correct
98 Correct 28 ms 4184 KB Output is correct
99 Correct 29 ms 4200 KB Output is correct
100 Correct 29 ms 4132 KB Output is correct
101 Correct 29 ms 4188 KB Output is correct
102 Correct 29 ms 4180 KB Output is correct
103 Correct 28 ms 4168 KB Output is correct
104 Correct 34 ms 4172 KB Output is correct
105 Correct 29 ms 4196 KB Output is correct
106 Correct 26 ms 4620 KB Output is correct
107 Correct 26 ms 4564 KB Output is correct
108 Correct 26 ms 4612 KB Output is correct
109 Correct 28 ms 4556 KB Output is correct
110 Correct 27 ms 4556 KB Output is correct
111 Correct 26 ms 4564 KB Output is correct
112 Correct 27 ms 4568 KB Output is correct
113 Correct 27 ms 4620 KB Output is correct
114 Correct 28 ms 4624 KB Output is correct
115 Correct 27 ms 4616 KB Output is correct
116 Correct 1375 ms 2488 KB Output is correct
117 Correct 1220 ms 2488 KB Output is correct
118 Correct 2522 ms 2936 KB Output is correct
119 Correct 1725 ms 3016 KB Output is correct
120 Correct 1182 ms 2956 KB Output is correct
121 Correct 3113 ms 3404 KB Output is correct
122 Correct 2202 ms 3340 KB Output is correct
123 Execution timed out 7047 ms 3532 KB Time limit exceeded
124 Halted 0 ms 0 KB -