답안 #577358

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
577358 2022-06-14T15:23:05 Z rembocoder Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 7552 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], cnt[N];
        int is[N], mm[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];
        	});
        	
        	set<int> ss;
        	auto add = [&](int i){
              	int& cur = cnt[a[i]];
        		if(cur){
        			mm[cur]--;
        			if(!mm[cur]) ss.erase(cur);
        		} 
        		cur++;
        		if(!mm[cur]) ss.insert(cur);
        		mm[cur]++;
        	};
        	auto del = [&](int i){
        		//~ assert(cnt[a[i]]);
              	int& cur = cnt[a[i]];
        		mm[cur]--;
        		if(!mm[cur]) ss.erase(cur);
        		cur--;
        		if(cur){
        			if(!mm[cur]) ss.insert(cur);
        			mm[cur]++;
        		}
        	};
        	
        	int lx = 0, rx = 0;
        	for(auto i : p){
        		if(rx > r[i]){ 
        			ss.clear();
        			lx = rx = l[i];
        			memset(cnt, 0, sizeof cnt);
        			memset(mm, 0, sizeof mm);
        		}
        		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 : ss){
        			int c = mm[v];
        			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";
        }
         
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 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 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 17 ms 3140 KB Output is correct
5 Correct 30 ms 3748 KB Output is correct
6 Correct 45 ms 4040 KB Output is correct
7 Correct 43 ms 4480 KB Output is correct
8 Correct 46 ms 3936 KB Output is correct
9 Correct 58 ms 3984 KB Output is correct
10 Correct 60 ms 4148 KB Output is correct
11 Correct 49 ms 4044 KB Output is correct
12 Correct 45 ms 3940 KB Output is correct
13 Correct 44 ms 3928 KB Output is correct
14 Correct 44 ms 4116 KB Output is correct
15 Correct 49 ms 4044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 17 ms 3140 KB Output is correct
5 Correct 30 ms 3748 KB Output is correct
6 Correct 45 ms 4040 KB Output is correct
7 Correct 43 ms 4480 KB Output is correct
8 Correct 46 ms 3936 KB Output is correct
9 Correct 58 ms 3984 KB Output is correct
10 Correct 60 ms 4148 KB Output is correct
11 Correct 49 ms 4044 KB Output is correct
12 Correct 45 ms 3940 KB Output is correct
13 Correct 44 ms 3928 KB Output is correct
14 Correct 44 ms 4116 KB Output is correct
15 Correct 49 ms 4044 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 3 ms 2644 KB Output is correct
18 Correct 5 ms 2644 KB Output is correct
19 Correct 19 ms 3156 KB Output is correct
20 Correct 36 ms 3564 KB Output is correct
21 Correct 46 ms 3988 KB Output is correct
22 Correct 54 ms 4052 KB Output is correct
23 Correct 48 ms 4012 KB Output is correct
24 Correct 76 ms 4072 KB Output is correct
25 Correct 65 ms 3916 KB Output is correct
26 Correct 54 ms 3900 KB Output is correct
27 Correct 65 ms 3952 KB Output is correct
28 Correct 47 ms 4052 KB Output is correct
29 Correct 49 ms 4012 KB Output is correct
30 Correct 47 ms 3944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 17 ms 3140 KB Output is correct
5 Correct 30 ms 3748 KB Output is correct
6 Correct 45 ms 4040 KB Output is correct
7 Correct 43 ms 4480 KB Output is correct
8 Correct 46 ms 3936 KB Output is correct
9 Correct 58 ms 3984 KB Output is correct
10 Correct 60 ms 4148 KB Output is correct
11 Correct 49 ms 4044 KB Output is correct
12 Correct 45 ms 3940 KB Output is correct
13 Correct 44 ms 3928 KB Output is correct
14 Correct 44 ms 4116 KB Output is correct
15 Correct 49 ms 4044 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 3 ms 2644 KB Output is correct
18 Correct 5 ms 2644 KB Output is correct
19 Correct 19 ms 3156 KB Output is correct
20 Correct 36 ms 3564 KB Output is correct
21 Correct 46 ms 3988 KB Output is correct
22 Correct 54 ms 4052 KB Output is correct
23 Correct 48 ms 4012 KB Output is correct
24 Correct 76 ms 4072 KB Output is correct
25 Correct 65 ms 3916 KB Output is correct
26 Correct 54 ms 3900 KB Output is correct
27 Correct 65 ms 3952 KB Output is correct
28 Correct 47 ms 4052 KB Output is correct
29 Correct 49 ms 4012 KB Output is correct
30 Correct 47 ms 3944 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 4 ms 2644 KB Output is correct
35 Correct 3 ms 2644 KB Output is correct
36 Correct 4 ms 2644 KB Output is correct
37 Correct 12 ms 2900 KB Output is correct
38 Correct 7 ms 2916 KB Output is correct
39 Correct 12 ms 3124 KB Output is correct
40 Correct 24 ms 3540 KB Output is correct
41 Correct 38 ms 3880 KB Output is correct
42 Correct 39 ms 3924 KB Output is correct
43 Correct 46 ms 3872 KB Output is correct
44 Correct 39 ms 4012 KB Output is correct
45 Correct 40 ms 3900 KB Output is correct
46 Correct 37 ms 3888 KB Output is correct
47 Correct 37 ms 3888 KB Output is correct
48 Correct 40 ms 3916 KB Output is correct
49 Correct 38 ms 3916 KB Output is correct
50 Correct 40 ms 3888 KB Output is correct
51 Correct 42 ms 3884 KB Output is correct
52 Correct 45 ms 3976 KB Output is correct
53 Correct 63 ms 3868 KB Output is correct
54 Correct 45 ms 3916 KB Output is correct
55 Correct 38 ms 3920 KB Output is correct
56 Correct 37 ms 3856 KB Output is correct
57 Correct 51 ms 3820 KB Output is correct
58 Correct 46 ms 3932 KB Output is correct
59 Correct 41 ms 3884 KB Output is correct
60 Correct 40 ms 3844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 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 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 17 ms 3140 KB Output is correct
15 Correct 30 ms 3748 KB Output is correct
16 Correct 45 ms 4040 KB Output is correct
17 Correct 43 ms 4480 KB Output is correct
18 Correct 46 ms 3936 KB Output is correct
19 Correct 58 ms 3984 KB Output is correct
20 Correct 60 ms 4148 KB Output is correct
21 Correct 49 ms 4044 KB Output is correct
22 Correct 45 ms 3940 KB Output is correct
23 Correct 44 ms 3928 KB Output is correct
24 Correct 44 ms 4116 KB Output is correct
25 Correct 49 ms 4044 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2644 KB Output is correct
28 Correct 5 ms 2644 KB Output is correct
29 Correct 19 ms 3156 KB Output is correct
30 Correct 36 ms 3564 KB Output is correct
31 Correct 46 ms 3988 KB Output is correct
32 Correct 54 ms 4052 KB Output is correct
33 Correct 48 ms 4012 KB Output is correct
34 Correct 76 ms 4072 KB Output is correct
35 Correct 65 ms 3916 KB Output is correct
36 Correct 54 ms 3900 KB Output is correct
37 Correct 65 ms 3952 KB Output is correct
38 Correct 47 ms 4052 KB Output is correct
39 Correct 49 ms 4012 KB Output is correct
40 Correct 47 ms 3944 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 4 ms 2644 KB Output is correct
45 Correct 3 ms 2644 KB Output is correct
46 Correct 4 ms 2644 KB Output is correct
47 Correct 12 ms 2900 KB Output is correct
48 Correct 7 ms 2916 KB Output is correct
49 Correct 12 ms 3124 KB Output is correct
50 Correct 24 ms 3540 KB Output is correct
51 Correct 38 ms 3880 KB Output is correct
52 Correct 39 ms 3924 KB Output is correct
53 Correct 46 ms 3872 KB Output is correct
54 Correct 39 ms 4012 KB Output is correct
55 Correct 40 ms 3900 KB Output is correct
56 Correct 37 ms 3888 KB Output is correct
57 Correct 37 ms 3888 KB Output is correct
58 Correct 40 ms 3916 KB Output is correct
59 Correct 38 ms 3916 KB Output is correct
60 Correct 40 ms 3888 KB Output is correct
61 Correct 42 ms 3884 KB Output is correct
62 Correct 45 ms 3976 KB Output is correct
63 Correct 63 ms 3868 KB Output is correct
64 Correct 45 ms 3916 KB Output is correct
65 Correct 38 ms 3920 KB Output is correct
66 Correct 37 ms 3856 KB Output is correct
67 Correct 51 ms 3820 KB Output is correct
68 Correct 46 ms 3932 KB Output is correct
69 Correct 41 ms 3884 KB Output is correct
70 Correct 40 ms 3844 KB Output is correct
71 Correct 6 ms 2848 KB Output is correct
72 Correct 6 ms 2840 KB Output is correct
73 Correct 6 ms 2900 KB Output is correct
74 Correct 6 ms 2808 KB Output is correct
75 Correct 6 ms 2900 KB Output is correct
76 Correct 12 ms 3028 KB Output is correct
77 Correct 13 ms 3028 KB Output is correct
78 Correct 13 ms 3028 KB Output is correct
79 Correct 14 ms 3120 KB Output is correct
80 Correct 13 ms 3096 KB Output is correct
81 Correct 24 ms 3412 KB Output is correct
82 Correct 25 ms 3424 KB Output is correct
83 Correct 17 ms 3540 KB Output is correct
84 Correct 23 ms 3496 KB Output is correct
85 Correct 17 ms 3512 KB Output is correct
86 Correct 23 ms 3556 KB Output is correct
87 Correct 19 ms 3544 KB Output is correct
88 Correct 19 ms 3472 KB Output is correct
89 Correct 17 ms 3500 KB Output is correct
90 Correct 18 ms 3552 KB Output is correct
91 Correct 28 ms 4244 KB Output is correct
92 Correct 28 ms 4164 KB Output is correct
93 Correct 27 ms 4196 KB Output is correct
94 Correct 27 ms 4272 KB Output is correct
95 Correct 29 ms 4204 KB Output is correct
96 Correct 39 ms 4620 KB Output is correct
97 Correct 40 ms 4620 KB Output is correct
98 Correct 30 ms 4564 KB Output is correct
99 Correct 30 ms 4624 KB Output is correct
100 Correct 30 ms 4540 KB Output is correct
101 Correct 32 ms 4632 KB Output is correct
102 Correct 32 ms 4564 KB Output is correct
103 Correct 35 ms 4652 KB Output is correct
104 Correct 34 ms 4552 KB Output is correct
105 Correct 32 ms 4556 KB Output is correct
106 Correct 30 ms 4752 KB Output is correct
107 Correct 30 ms 4852 KB Output is correct
108 Correct 32 ms 4812 KB Output is correct
109 Correct 29 ms 4728 KB Output is correct
110 Correct 37 ms 4772 KB Output is correct
111 Correct 33 ms 4840 KB Output is correct
112 Correct 33 ms 4720 KB Output is correct
113 Correct 32 ms 4820 KB Output is correct
114 Correct 43 ms 4812 KB Output is correct
115 Correct 36 ms 4768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 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 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 17 ms 3140 KB Output is correct
15 Correct 30 ms 3748 KB Output is correct
16 Correct 45 ms 4040 KB Output is correct
17 Correct 43 ms 4480 KB Output is correct
18 Correct 46 ms 3936 KB Output is correct
19 Correct 58 ms 3984 KB Output is correct
20 Correct 60 ms 4148 KB Output is correct
21 Correct 49 ms 4044 KB Output is correct
22 Correct 45 ms 3940 KB Output is correct
23 Correct 44 ms 3928 KB Output is correct
24 Correct 44 ms 4116 KB Output is correct
25 Correct 49 ms 4044 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2644 KB Output is correct
28 Correct 5 ms 2644 KB Output is correct
29 Correct 19 ms 3156 KB Output is correct
30 Correct 36 ms 3564 KB Output is correct
31 Correct 46 ms 3988 KB Output is correct
32 Correct 54 ms 4052 KB Output is correct
33 Correct 48 ms 4012 KB Output is correct
34 Correct 76 ms 4072 KB Output is correct
35 Correct 65 ms 3916 KB Output is correct
36 Correct 54 ms 3900 KB Output is correct
37 Correct 65 ms 3952 KB Output is correct
38 Correct 47 ms 4052 KB Output is correct
39 Correct 49 ms 4012 KB Output is correct
40 Correct 47 ms 3944 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 4 ms 2644 KB Output is correct
45 Correct 3 ms 2644 KB Output is correct
46 Correct 4 ms 2644 KB Output is correct
47 Correct 12 ms 2900 KB Output is correct
48 Correct 7 ms 2916 KB Output is correct
49 Correct 12 ms 3124 KB Output is correct
50 Correct 24 ms 3540 KB Output is correct
51 Correct 38 ms 3880 KB Output is correct
52 Correct 39 ms 3924 KB Output is correct
53 Correct 46 ms 3872 KB Output is correct
54 Correct 39 ms 4012 KB Output is correct
55 Correct 40 ms 3900 KB Output is correct
56 Correct 37 ms 3888 KB Output is correct
57 Correct 37 ms 3888 KB Output is correct
58 Correct 40 ms 3916 KB Output is correct
59 Correct 38 ms 3916 KB Output is correct
60 Correct 40 ms 3888 KB Output is correct
61 Correct 42 ms 3884 KB Output is correct
62 Correct 45 ms 3976 KB Output is correct
63 Correct 63 ms 3868 KB Output is correct
64 Correct 45 ms 3916 KB Output is correct
65 Correct 38 ms 3920 KB Output is correct
66 Correct 37 ms 3856 KB Output is correct
67 Correct 51 ms 3820 KB Output is correct
68 Correct 46 ms 3932 KB Output is correct
69 Correct 41 ms 3884 KB Output is correct
70 Correct 40 ms 3844 KB Output is correct
71 Correct 6 ms 2848 KB Output is correct
72 Correct 6 ms 2840 KB Output is correct
73 Correct 6 ms 2900 KB Output is correct
74 Correct 6 ms 2808 KB Output is correct
75 Correct 6 ms 2900 KB Output is correct
76 Correct 12 ms 3028 KB Output is correct
77 Correct 13 ms 3028 KB Output is correct
78 Correct 13 ms 3028 KB Output is correct
79 Correct 14 ms 3120 KB Output is correct
80 Correct 13 ms 3096 KB Output is correct
81 Correct 24 ms 3412 KB Output is correct
82 Correct 25 ms 3424 KB Output is correct
83 Correct 17 ms 3540 KB Output is correct
84 Correct 23 ms 3496 KB Output is correct
85 Correct 17 ms 3512 KB Output is correct
86 Correct 23 ms 3556 KB Output is correct
87 Correct 19 ms 3544 KB Output is correct
88 Correct 19 ms 3472 KB Output is correct
89 Correct 17 ms 3500 KB Output is correct
90 Correct 18 ms 3552 KB Output is correct
91 Correct 28 ms 4244 KB Output is correct
92 Correct 28 ms 4164 KB Output is correct
93 Correct 27 ms 4196 KB Output is correct
94 Correct 27 ms 4272 KB Output is correct
95 Correct 29 ms 4204 KB Output is correct
96 Correct 39 ms 4620 KB Output is correct
97 Correct 40 ms 4620 KB Output is correct
98 Correct 30 ms 4564 KB Output is correct
99 Correct 30 ms 4624 KB Output is correct
100 Correct 30 ms 4540 KB Output is correct
101 Correct 32 ms 4632 KB Output is correct
102 Correct 32 ms 4564 KB Output is correct
103 Correct 35 ms 4652 KB Output is correct
104 Correct 34 ms 4552 KB Output is correct
105 Correct 32 ms 4556 KB Output is correct
106 Correct 30 ms 4752 KB Output is correct
107 Correct 30 ms 4852 KB Output is correct
108 Correct 32 ms 4812 KB Output is correct
109 Correct 29 ms 4728 KB Output is correct
110 Correct 37 ms 4772 KB Output is correct
111 Correct 33 ms 4840 KB Output is correct
112 Correct 33 ms 4720 KB Output is correct
113 Correct 32 ms 4820 KB Output is correct
114 Correct 43 ms 4812 KB Output is correct
115 Correct 36 ms 4768 KB Output is correct
116 Correct 2493 ms 6556 KB Output is correct
117 Correct 1708 ms 6560 KB Output is correct
118 Correct 3763 ms 6840 KB Output is correct
119 Correct 1089 ms 6840 KB Output is correct
120 Correct 603 ms 6840 KB Output is correct
121 Correct 1018 ms 7036 KB Output is correct
122 Correct 716 ms 7036 KB Output is correct
123 Correct 2185 ms 7444 KB Output is correct
124 Correct 591 ms 7552 KB Output is correct
125 Correct 458 ms 7548 KB Output is correct
126 Execution timed out 7029 ms 7248 KB Time limit exceeded
127 Halted 0 ms 0 KB -