Submission #814229

# Submission time Handle Problem Language Result Execution time Memory
814229 2023-08-08T06:28:14 Z enerelt14 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 33764 KB
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
 
#define ff first
#define ss second
#define ll long long
 
const int MX = 3e5 + 5;
const int MX1 = 5e4 + 5;
const int B = 500;
 
int N, Q;
int a[MX], num[MX], l[MX], r[MX];
ll ans[MX1];
pair<pair<int, int>, pair<int, int> > q[MX1];
ll val;
 
struct T{
	ll sum, left_sum, right_sum;
	T() {
		sum = left_sum = right_sum = 0;
	}
} tree[4 * MX];
 
void show(T x) {
	cout << x.left_sum << " " << x.right_sum << " " << x.sum << "\n";
}
 
void update(int id, int l, int r, int pos, int x) {
	if(l > pos || pos > r) return;
	tree[id].sum += x;
	tree[id].left_sum += x * (pos - l + 1);
	tree[id].right_sum += x * (r - pos + 1);
	if(l == r) return;
	int mid = (l + r) / 2;
	update(id * 2 + 1, l, mid, pos, x);
	update(id * 2 + 2, mid + 1, r, pos, x);
}
 
T query(int id, int l, int r, int L, int R) {
	T res;
	if(L > r || l > R) return res;
	if(L <= l && r <= R) return tree[id];
	int mid = (l + r) / 2;
	if(L > mid) return query(id * 2 + 2, mid + 1, r, L, R);
	if(R <= mid) return query(id * 2 + 1, l, mid, L, R);
	T x = query(id * 2 + 1, l, mid, L, R), y = query(id * 2 + 2, mid + 1, r, L, R);
	res.sum = x.sum + y.sum;
	res.left_sum = x.left_sum + y.left_sum + y.sum * (mid - L + 1);
	res.right_sum = x.right_sum + y.right_sum + x.sum * (R - mid);
	return res;
}
 
int prv(int x) {
	if(x <= (N - 1) / 2) return N - x;
	else return N - x - 1;
}
 
int nxt(int x) {
	if(x <= N / 2) return N - x - 1;
	else return N - x;
}
 
void add(int x) {
	num[x]++;
	l[num[x]] = r[num[x] - 1];
	if(r[num[x]] == -1) r[num[x]] = l[num[x]];
	if(r[num[x] - 1] == l[num[x] - 1]) r[num[x] - 1] = l[num[x] - 1] = -1;
	else r[num[x] - 1] = prv(r[num[x] - 1]);
	update(0, 0, N - 1, l[num[x]], 1);
	T y, z;
	if(l[num[x]] != 0) y = query(0, 0, N - 1, 0, l[num[x]] - 1);
	if(l[num[x]] != N - 1) z = query(0, 0, N - 1, l[num[x]] + 1, N - 1);
	val += y.right_sum + y.sum + z.left_sum + z.sum;
	val += num[x];
	// show(y);
	// show(z);
	// cout << val << "\n---------\n";
}
 
void remove(int x) {
	num[x]--;
	r[num[x]] = l[num[x] + 1];
	if(l[num[x]] == -1) l[num[x]] = r[num[x]];
	if(r[num[x] + 1] == l[num[x] + 1]) r[num[x] + 1] = l[num[x] + 1] = -1;
	else l[num[x] + 1] = nxt(l[num[x] + 1]);
	update(0, 0, N - 1, r[num[x]], -1);
	T y, z;
	if(r[num[x]] != 0) y = query(0, 0, N - 1, 0, r[num[x]] - 1);
	if(r[num[x]] != N - 1) z = query(0, 0, N - 1, r[num[x]] + 1, N - 1);
	val -= y.right_sum + y.sum + z.left_sum + z.sum;
	val -= (num[x] + 1);
}
 
int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	memset(l, -1, sizeof(l));
	memset(r, -1, sizeof(r));
	cin >> N >> Q;
	l[0] = 0;
	r[0] = N / 2;
	for(int i = 0; i < N; i++) cin >> a[i];
	for(int i = 0; i < Q; i++) {
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		q[i] = {{l / B, r}, {l, i}};
	}
	sort(q, q + Q);
	for(int i = 0; i < Q; i++) {
		if(i == 0) {
			for(int j = 0; j <= q[i].ff.ss; j++) add(a[j]);
			for(int j = 0; j < q[i].ss.ff; j++) remove(a[j]);
			ans[q[i].ss.ss] = val;
			continue;
		}
		for(int j = q[i - 1].ff.ss + 1; j <= q[i].ff.ss; j++) add(a[j]);
		for(int j = q[i - 1].ff.ss; j > q[i].ff.ss; j--) remove(a[j]);
		for(int j = q[i - 1].ss.ff; j < q[i].ss.ff; j++) remove(a[j]);
		for(int j = q[i - 1].ss.ff - 1; j >= q[i].ss.ff; j--) add(a[j]);
		ans[q[i].ss.ss] = val;
	}
	for(int i = 0; i < Q; i++) {
		cout << ans[i] << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 30804 KB Output is correct
2 Correct 12 ms 30784 KB Output is correct
3 Correct 13 ms 30864 KB Output is correct
4 Correct 15 ms 30804 KB Output is correct
5 Correct 13 ms 30836 KB Output is correct
6 Correct 12 ms 30808 KB Output is correct
7 Correct 13 ms 30752 KB Output is correct
8 Correct 13 ms 30812 KB Output is correct
9 Correct 13 ms 30792 KB Output is correct
10 Correct 13 ms 30804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 30804 KB Output is correct
2 Correct 13 ms 30764 KB Output is correct
3 Correct 15 ms 30852 KB Output is correct
4 Correct 53 ms 31220 KB Output is correct
5 Correct 75 ms 31624 KB Output is correct
6 Correct 113 ms 32692 KB Output is correct
7 Correct 126 ms 32624 KB Output is correct
8 Correct 110 ms 32640 KB Output is correct
9 Correct 120 ms 32536 KB Output is correct
10 Correct 110 ms 32676 KB Output is correct
11 Correct 110 ms 32668 KB Output is correct
12 Correct 112 ms 32688 KB Output is correct
13 Correct 111 ms 32660 KB Output is correct
14 Correct 125 ms 32656 KB Output is correct
15 Correct 110 ms 32644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 30804 KB Output is correct
2 Correct 13 ms 30764 KB Output is correct
3 Correct 15 ms 30852 KB Output is correct
4 Correct 53 ms 31220 KB Output is correct
5 Correct 75 ms 31624 KB Output is correct
6 Correct 113 ms 32692 KB Output is correct
7 Correct 126 ms 32624 KB Output is correct
8 Correct 110 ms 32640 KB Output is correct
9 Correct 120 ms 32536 KB Output is correct
10 Correct 110 ms 32676 KB Output is correct
11 Correct 110 ms 32668 KB Output is correct
12 Correct 112 ms 32688 KB Output is correct
13 Correct 111 ms 32660 KB Output is correct
14 Correct 125 ms 32656 KB Output is correct
15 Correct 110 ms 32644 KB Output is correct
16 Correct 12 ms 30864 KB Output is correct
17 Correct 13 ms 30804 KB Output is correct
18 Correct 16 ms 30828 KB Output is correct
19 Correct 42 ms 31532 KB Output is correct
20 Correct 80 ms 32144 KB Output is correct
21 Correct 117 ms 32784 KB Output is correct
22 Correct 111 ms 32780 KB Output is correct
23 Correct 112 ms 32788 KB Output is correct
24 Correct 113 ms 32784 KB Output is correct
25 Correct 126 ms 32780 KB Output is correct
26 Correct 123 ms 32772 KB Output is correct
27 Correct 120 ms 32676 KB Output is correct
28 Correct 114 ms 32744 KB Output is correct
29 Correct 111 ms 32756 KB Output is correct
30 Correct 118 ms 32716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 30804 KB Output is correct
2 Correct 13 ms 30764 KB Output is correct
3 Correct 15 ms 30852 KB Output is correct
4 Correct 53 ms 31220 KB Output is correct
5 Correct 75 ms 31624 KB Output is correct
6 Correct 113 ms 32692 KB Output is correct
7 Correct 126 ms 32624 KB Output is correct
8 Correct 110 ms 32640 KB Output is correct
9 Correct 120 ms 32536 KB Output is correct
10 Correct 110 ms 32676 KB Output is correct
11 Correct 110 ms 32668 KB Output is correct
12 Correct 112 ms 32688 KB Output is correct
13 Correct 111 ms 32660 KB Output is correct
14 Correct 125 ms 32656 KB Output is correct
15 Correct 110 ms 32644 KB Output is correct
16 Correct 12 ms 30864 KB Output is correct
17 Correct 13 ms 30804 KB Output is correct
18 Correct 16 ms 30828 KB Output is correct
19 Correct 42 ms 31532 KB Output is correct
20 Correct 80 ms 32144 KB Output is correct
21 Correct 117 ms 32784 KB Output is correct
22 Correct 111 ms 32780 KB Output is correct
23 Correct 112 ms 32788 KB Output is correct
24 Correct 113 ms 32784 KB Output is correct
25 Correct 126 ms 32780 KB Output is correct
26 Correct 123 ms 32772 KB Output is correct
27 Correct 120 ms 32676 KB Output is correct
28 Correct 114 ms 32744 KB Output is correct
29 Correct 111 ms 32756 KB Output is correct
30 Correct 118 ms 32716 KB Output is correct
31 Correct 12 ms 30804 KB Output is correct
32 Correct 13 ms 30796 KB Output is correct
33 Correct 13 ms 30864 KB Output is correct
34 Correct 14 ms 30872 KB Output is correct
35 Correct 15 ms 30912 KB Output is correct
36 Correct 19 ms 31016 KB Output is correct
37 Correct 33 ms 31172 KB Output is correct
38 Correct 31 ms 31128 KB Output is correct
39 Correct 58 ms 31620 KB Output is correct
40 Correct 111 ms 32412 KB Output is correct
41 Correct 156 ms 32784 KB Output is correct
42 Correct 146 ms 32828 KB Output is correct
43 Correct 153 ms 32792 KB Output is correct
44 Correct 144 ms 32788 KB Output is correct
45 Correct 153 ms 32784 KB Output is correct
46 Correct 152 ms 32756 KB Output is correct
47 Correct 138 ms 32792 KB Output is correct
48 Correct 154 ms 32784 KB Output is correct
49 Correct 158 ms 32792 KB Output is correct
50 Correct 145 ms 32788 KB Output is correct
51 Correct 142 ms 32788 KB Output is correct
52 Correct 140 ms 32688 KB Output is correct
53 Correct 139 ms 32792 KB Output is correct
54 Correct 145 ms 32784 KB Output is correct
55 Correct 143 ms 32696 KB Output is correct
56 Correct 138 ms 32784 KB Output is correct
57 Correct 138 ms 32780 KB Output is correct
58 Correct 147 ms 32792 KB Output is correct
59 Correct 149 ms 32792 KB Output is correct
60 Correct 139 ms 32788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 30804 KB Output is correct
2 Correct 12 ms 30784 KB Output is correct
3 Correct 13 ms 30864 KB Output is correct
4 Correct 15 ms 30804 KB Output is correct
5 Correct 13 ms 30836 KB Output is correct
6 Correct 12 ms 30808 KB Output is correct
7 Correct 13 ms 30752 KB Output is correct
8 Correct 13 ms 30812 KB Output is correct
9 Correct 13 ms 30792 KB Output is correct
10 Correct 13 ms 30804 KB Output is correct
11 Correct 16 ms 30804 KB Output is correct
12 Correct 13 ms 30764 KB Output is correct
13 Correct 15 ms 30852 KB Output is correct
14 Correct 53 ms 31220 KB Output is correct
15 Correct 75 ms 31624 KB Output is correct
16 Correct 113 ms 32692 KB Output is correct
17 Correct 126 ms 32624 KB Output is correct
18 Correct 110 ms 32640 KB Output is correct
19 Correct 120 ms 32536 KB Output is correct
20 Correct 110 ms 32676 KB Output is correct
21 Correct 110 ms 32668 KB Output is correct
22 Correct 112 ms 32688 KB Output is correct
23 Correct 111 ms 32660 KB Output is correct
24 Correct 125 ms 32656 KB Output is correct
25 Correct 110 ms 32644 KB Output is correct
26 Correct 12 ms 30864 KB Output is correct
27 Correct 13 ms 30804 KB Output is correct
28 Correct 16 ms 30828 KB Output is correct
29 Correct 42 ms 31532 KB Output is correct
30 Correct 80 ms 32144 KB Output is correct
31 Correct 117 ms 32784 KB Output is correct
32 Correct 111 ms 32780 KB Output is correct
33 Correct 112 ms 32788 KB Output is correct
34 Correct 113 ms 32784 KB Output is correct
35 Correct 126 ms 32780 KB Output is correct
36 Correct 123 ms 32772 KB Output is correct
37 Correct 120 ms 32676 KB Output is correct
38 Correct 114 ms 32744 KB Output is correct
39 Correct 111 ms 32756 KB Output is correct
40 Correct 118 ms 32716 KB Output is correct
41 Correct 12 ms 30804 KB Output is correct
42 Correct 13 ms 30796 KB Output is correct
43 Correct 13 ms 30864 KB Output is correct
44 Correct 14 ms 30872 KB Output is correct
45 Correct 15 ms 30912 KB Output is correct
46 Correct 19 ms 31016 KB Output is correct
47 Correct 33 ms 31172 KB Output is correct
48 Correct 31 ms 31128 KB Output is correct
49 Correct 58 ms 31620 KB Output is correct
50 Correct 111 ms 32412 KB Output is correct
51 Correct 156 ms 32784 KB Output is correct
52 Correct 146 ms 32828 KB Output is correct
53 Correct 153 ms 32792 KB Output is correct
54 Correct 144 ms 32788 KB Output is correct
55 Correct 153 ms 32784 KB Output is correct
56 Correct 152 ms 32756 KB Output is correct
57 Correct 138 ms 32792 KB Output is correct
58 Correct 154 ms 32784 KB Output is correct
59 Correct 158 ms 32792 KB Output is correct
60 Correct 145 ms 32788 KB Output is correct
61 Correct 142 ms 32788 KB Output is correct
62 Correct 140 ms 32688 KB Output is correct
63 Correct 139 ms 32792 KB Output is correct
64 Correct 145 ms 32784 KB Output is correct
65 Correct 143 ms 32696 KB Output is correct
66 Correct 138 ms 32784 KB Output is correct
67 Correct 138 ms 32780 KB Output is correct
68 Correct 147 ms 32792 KB Output is correct
69 Correct 149 ms 32792 KB Output is correct
70 Correct 139 ms 32788 KB Output is correct
71 Correct 33 ms 31264 KB Output is correct
72 Correct 33 ms 31192 KB Output is correct
73 Correct 32 ms 31236 KB Output is correct
74 Correct 33 ms 31256 KB Output is correct
75 Correct 33 ms 31188 KB Output is correct
76 Correct 55 ms 31700 KB Output is correct
77 Correct 72 ms 31724 KB Output is correct
78 Correct 55 ms 31724 KB Output is correct
79 Correct 55 ms 31732 KB Output is correct
80 Correct 58 ms 31724 KB Output is correct
81 Correct 106 ms 32436 KB Output is correct
82 Correct 103 ms 32396 KB Output is correct
83 Correct 105 ms 32436 KB Output is correct
84 Correct 102 ms 32436 KB Output is correct
85 Correct 109 ms 32460 KB Output is correct
86 Correct 109 ms 32472 KB Output is correct
87 Correct 104 ms 32380 KB Output is correct
88 Correct 103 ms 32480 KB Output is correct
89 Correct 111 ms 32484 KB Output is correct
90 Correct 123 ms 32460 KB Output is correct
91 Correct 154 ms 33176 KB Output is correct
92 Correct 158 ms 33172 KB Output is correct
93 Correct 168 ms 33176 KB Output is correct
94 Correct 153 ms 33092 KB Output is correct
95 Correct 153 ms 33100 KB Output is correct
96 Correct 171 ms 33644 KB Output is correct
97 Correct 162 ms 33484 KB Output is correct
98 Correct 158 ms 33572 KB Output is correct
99 Correct 153 ms 33556 KB Output is correct
100 Correct 155 ms 33568 KB Output is correct
101 Correct 163 ms 33564 KB Output is correct
102 Correct 155 ms 33540 KB Output is correct
103 Correct 153 ms 33568 KB Output is correct
104 Correct 156 ms 33568 KB Output is correct
105 Correct 156 ms 33564 KB Output is correct
106 Correct 158 ms 33740 KB Output is correct
107 Correct 149 ms 33684 KB Output is correct
108 Correct 151 ms 33760 KB Output is correct
109 Correct 155 ms 33764 KB Output is correct
110 Correct 152 ms 33764 KB Output is correct
111 Correct 166 ms 33756 KB Output is correct
112 Correct 152 ms 33760 KB Output is correct
113 Correct 159 ms 33764 KB Output is correct
114 Correct 155 ms 33760 KB Output is correct
115 Correct 149 ms 33656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 30804 KB Output is correct
2 Correct 12 ms 30784 KB Output is correct
3 Correct 13 ms 30864 KB Output is correct
4 Correct 15 ms 30804 KB Output is correct
5 Correct 13 ms 30836 KB Output is correct
6 Correct 12 ms 30808 KB Output is correct
7 Correct 13 ms 30752 KB Output is correct
8 Correct 13 ms 30812 KB Output is correct
9 Correct 13 ms 30792 KB Output is correct
10 Correct 13 ms 30804 KB Output is correct
11 Correct 16 ms 30804 KB Output is correct
12 Correct 13 ms 30764 KB Output is correct
13 Correct 15 ms 30852 KB Output is correct
14 Correct 53 ms 31220 KB Output is correct
15 Correct 75 ms 31624 KB Output is correct
16 Correct 113 ms 32692 KB Output is correct
17 Correct 126 ms 32624 KB Output is correct
18 Correct 110 ms 32640 KB Output is correct
19 Correct 120 ms 32536 KB Output is correct
20 Correct 110 ms 32676 KB Output is correct
21 Correct 110 ms 32668 KB Output is correct
22 Correct 112 ms 32688 KB Output is correct
23 Correct 111 ms 32660 KB Output is correct
24 Correct 125 ms 32656 KB Output is correct
25 Correct 110 ms 32644 KB Output is correct
26 Correct 12 ms 30864 KB Output is correct
27 Correct 13 ms 30804 KB Output is correct
28 Correct 16 ms 30828 KB Output is correct
29 Correct 42 ms 31532 KB Output is correct
30 Correct 80 ms 32144 KB Output is correct
31 Correct 117 ms 32784 KB Output is correct
32 Correct 111 ms 32780 KB Output is correct
33 Correct 112 ms 32788 KB Output is correct
34 Correct 113 ms 32784 KB Output is correct
35 Correct 126 ms 32780 KB Output is correct
36 Correct 123 ms 32772 KB Output is correct
37 Correct 120 ms 32676 KB Output is correct
38 Correct 114 ms 32744 KB Output is correct
39 Correct 111 ms 32756 KB Output is correct
40 Correct 118 ms 32716 KB Output is correct
41 Correct 12 ms 30804 KB Output is correct
42 Correct 13 ms 30796 KB Output is correct
43 Correct 13 ms 30864 KB Output is correct
44 Correct 14 ms 30872 KB Output is correct
45 Correct 15 ms 30912 KB Output is correct
46 Correct 19 ms 31016 KB Output is correct
47 Correct 33 ms 31172 KB Output is correct
48 Correct 31 ms 31128 KB Output is correct
49 Correct 58 ms 31620 KB Output is correct
50 Correct 111 ms 32412 KB Output is correct
51 Correct 156 ms 32784 KB Output is correct
52 Correct 146 ms 32828 KB Output is correct
53 Correct 153 ms 32792 KB Output is correct
54 Correct 144 ms 32788 KB Output is correct
55 Correct 153 ms 32784 KB Output is correct
56 Correct 152 ms 32756 KB Output is correct
57 Correct 138 ms 32792 KB Output is correct
58 Correct 154 ms 32784 KB Output is correct
59 Correct 158 ms 32792 KB Output is correct
60 Correct 145 ms 32788 KB Output is correct
61 Correct 142 ms 32788 KB Output is correct
62 Correct 140 ms 32688 KB Output is correct
63 Correct 139 ms 32792 KB Output is correct
64 Correct 145 ms 32784 KB Output is correct
65 Correct 143 ms 32696 KB Output is correct
66 Correct 138 ms 32784 KB Output is correct
67 Correct 138 ms 32780 KB Output is correct
68 Correct 147 ms 32792 KB Output is correct
69 Correct 149 ms 32792 KB Output is correct
70 Correct 139 ms 32788 KB Output is correct
71 Correct 33 ms 31264 KB Output is correct
72 Correct 33 ms 31192 KB Output is correct
73 Correct 32 ms 31236 KB Output is correct
74 Correct 33 ms 31256 KB Output is correct
75 Correct 33 ms 31188 KB Output is correct
76 Correct 55 ms 31700 KB Output is correct
77 Correct 72 ms 31724 KB Output is correct
78 Correct 55 ms 31724 KB Output is correct
79 Correct 55 ms 31732 KB Output is correct
80 Correct 58 ms 31724 KB Output is correct
81 Correct 106 ms 32436 KB Output is correct
82 Correct 103 ms 32396 KB Output is correct
83 Correct 105 ms 32436 KB Output is correct
84 Correct 102 ms 32436 KB Output is correct
85 Correct 109 ms 32460 KB Output is correct
86 Correct 109 ms 32472 KB Output is correct
87 Correct 104 ms 32380 KB Output is correct
88 Correct 103 ms 32480 KB Output is correct
89 Correct 111 ms 32484 KB Output is correct
90 Correct 123 ms 32460 KB Output is correct
91 Correct 154 ms 33176 KB Output is correct
92 Correct 158 ms 33172 KB Output is correct
93 Correct 168 ms 33176 KB Output is correct
94 Correct 153 ms 33092 KB Output is correct
95 Correct 153 ms 33100 KB Output is correct
96 Correct 171 ms 33644 KB Output is correct
97 Correct 162 ms 33484 KB Output is correct
98 Correct 158 ms 33572 KB Output is correct
99 Correct 153 ms 33556 KB Output is correct
100 Correct 155 ms 33568 KB Output is correct
101 Correct 163 ms 33564 KB Output is correct
102 Correct 155 ms 33540 KB Output is correct
103 Correct 153 ms 33568 KB Output is correct
104 Correct 156 ms 33568 KB Output is correct
105 Correct 156 ms 33564 KB Output is correct
106 Correct 158 ms 33740 KB Output is correct
107 Correct 149 ms 33684 KB Output is correct
108 Correct 151 ms 33760 KB Output is correct
109 Correct 155 ms 33764 KB Output is correct
110 Correct 152 ms 33764 KB Output is correct
111 Correct 166 ms 33756 KB Output is correct
112 Correct 152 ms 33760 KB Output is correct
113 Correct 159 ms 33764 KB Output is correct
114 Correct 155 ms 33760 KB Output is correct
115 Correct 149 ms 33656 KB Output is correct
116 Correct 1884 ms 32944 KB Output is correct
117 Correct 1931 ms 32948 KB Output is correct
118 Correct 3807 ms 33440 KB Output is correct
119 Correct 4259 ms 33480 KB Output is correct
120 Correct 4473 ms 33484 KB Output is correct
121 Execution timed out 7081 ms 33180 KB Time limit exceeded
122 Halted 0 ms 0 KB -