Submission #576028

# Submission time Handle Problem Language Result Execution time Memory
576028 2022-06-12T05:30:00 Z 8e7 Diversity (CEOI21_diversity) C++17
18 / 100
27 ms 8908 KB
//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 300005
#define mod 998244353
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int bs = 1100;
struct query{
	int l, r, id;
	query(){l = r = id = 0;}	
	query(int a, int b, int c){l = a, r = b, id = c;}
};
int a[maxn], c[maxn], occ[maxn];
int lef[maxn], rig[maxn];
ll ans[maxn];

int distinct = 0;
void add(int col) {
	int &p = c[col];
	if (p == 0) distinct++;
	if (occ[p] == 1) {
		if (occ[p+1]) {
			rig[lef[p]] = rig[p];
			lef[rig[p]] = lef[p];
		} else {
			rig[lef[p]] = p+1;
			lef[rig[p]] = p+1;
			rig[p+1] = rig[p];
			lef[p+1] = lef[p];
		}
		lef[p] = 0, rig[p] = maxn-1;	
	} else if (occ[p+1]==0) {
		rig[p+1] = rig[p];
		lef[p+1] = p;

		lef[rig[p]] = p+1;
		rig[p] = p+1;
	}
	occ[p]--, occ[p+1]++;
	p++;
}
void del(int col) {
	int &p = c[col];
	if (p == 1) distinct--;
	if (occ[p] == 1) {
		if (occ[p-1]) {
			rig[lef[p]] = rig[p];
			lef[rig[p]] = lef[p];
		} else {
			rig[lef[p]] = p-1;
			lef[rig[p]] = p-1;
			rig[p-1] = rig[p];
			lef[p-1] = lef[p];
		}
		lef[p] = 0, rig[p] = maxn-1;	
	} else if (occ[p-1]==0) {
		rig[p-1] = p;
		lef[p-1] = lef[p];

		rig[lef[p]] = p-1;
		lef[p] = p-1;
	}

	occ[p]--, occ[p-1]++;
	p--;
}
int tot;

ll quad[maxn], cube[maxn];
ll getval(ll A, ll P, ll K) { //sum of A^2 + (A+P)^2 + ... + (A+(K-1)P)^2 + A + (A+P) + ... + (A + (K-1)P)
	return K * (A * (A+1) + (tot - K*P-A)*(tot-K*P-A+1)) + quad[K] * (2*(tot-K*P)+1) * P + cube[K] * P * P;
}
ll getans() {
	int cur = rig[0];
	ll ret = 0;
	ll lc = 0, rc = 0;
	while (cur <= tot) {
		if (lc > rc) swap(lc, rc);
		ll p = cur;
		//debug(cur, occ[p]);
		ll pl = (occ[p] + 1)/2, pr = occ[p] / 2;
		ret += getval(lc, p, pl) + getval(rc, p, pr);

		lc += pl * p, rc += pr * p;
		cur = rig[cur];
	}
	return ret / 2;
}
int main() {
	io
	for (ll i = 1;i < maxn;i++) {
		quad[i] = i * (i-1) / 2;	
		cube[i] = (i-1) * i * (2 * i - 1) / 6;
	}

	ll n, q;
	cin >> n >> q;
	for (int i = 0;i < n;i++) {
		cin >> a[i];
	}
	for (int i = 0;i <= n;i++) rig[i] = maxn-1;
	occ[0] = maxn-1;
	vector<query> que;
	for (int i = 0;i < q;i++) {
		int l, r;
		cin >> l >> r;
		l--, r--;
		que.push_back(query(l, r, i));
	}
	sort(que.begin(), que.end(), [&](query x, query y){
		return x.l / bs == y.l / bs ? ((x.l / bs & 1) ? x.r < y.r : x.r > y.r) : (x.l < y.l);});
	int cl = 0, cr = -1;
	for (auto [l, r, id]:que) {
		while (cr < r) add(a[++cr]);
		while (cl > l) add(a[--cl]);
		while (cr > r) del(a[cr--]);
		while (cl < l) del(a[cl++]);
		ans[id] = quad[r - l + 2] * distinct;
		tot = r - l + 1;
		ans[id] -= getans();
	}
	for (int i = 0;i < q;i++) cout << ans[i] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 4 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 4 ms 5036 KB Output is correct
4 Correct 8 ms 6192 KB Output is correct
5 Correct 16 ms 7468 KB Output is correct
6 Correct 21 ms 8144 KB Output is correct
7 Correct 21 ms 8908 KB Output is correct
8 Correct 21 ms 8140 KB Output is correct
9 Correct 22 ms 8092 KB Output is correct
10 Correct 27 ms 8472 KB Output is correct
11 Correct 21 ms 8276 KB Output is correct
12 Correct 22 ms 8168 KB Output is correct
13 Correct 20 ms 8084 KB Output is correct
14 Correct 21 ms 8276 KB Output is correct
15 Correct 22 ms 8260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 4 ms 5036 KB Output is correct
4 Correct 8 ms 6192 KB Output is correct
5 Correct 16 ms 7468 KB Output is correct
6 Correct 21 ms 8144 KB Output is correct
7 Correct 21 ms 8908 KB Output is correct
8 Correct 21 ms 8140 KB Output is correct
9 Correct 22 ms 8092 KB Output is correct
10 Correct 27 ms 8472 KB Output is correct
11 Correct 21 ms 8276 KB Output is correct
12 Correct 22 ms 8168 KB Output is correct
13 Correct 20 ms 8084 KB Output is correct
14 Correct 21 ms 8276 KB Output is correct
15 Correct 22 ms 8260 KB Output is correct
16 Correct 3 ms 5052 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5076 KB Output is correct
19 Correct 10 ms 6184 KB Output is correct
20 Correct 20 ms 7192 KB Output is correct
21 Correct 23 ms 8232 KB Output is correct
22 Correct 24 ms 8012 KB Output is correct
23 Correct 23 ms 8112 KB Output is correct
24 Correct 23 ms 7960 KB Output is correct
25 Correct 25 ms 8080 KB Output is correct
26 Correct 22 ms 8020 KB Output is correct
27 Correct 25 ms 8012 KB Output is correct
28 Correct 25 ms 8360 KB Output is correct
29 Correct 22 ms 7976 KB Output is correct
30 Correct 22 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 4 ms 5036 KB Output is correct
4 Correct 8 ms 6192 KB Output is correct
5 Correct 16 ms 7468 KB Output is correct
6 Correct 21 ms 8144 KB Output is correct
7 Correct 21 ms 8908 KB Output is correct
8 Correct 21 ms 8140 KB Output is correct
9 Correct 22 ms 8092 KB Output is correct
10 Correct 27 ms 8472 KB Output is correct
11 Correct 21 ms 8276 KB Output is correct
12 Correct 22 ms 8168 KB Output is correct
13 Correct 20 ms 8084 KB Output is correct
14 Correct 21 ms 8276 KB Output is correct
15 Correct 22 ms 8260 KB Output is correct
16 Correct 3 ms 5052 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5076 KB Output is correct
19 Correct 10 ms 6184 KB Output is correct
20 Correct 20 ms 7192 KB Output is correct
21 Correct 23 ms 8232 KB Output is correct
22 Correct 24 ms 8012 KB Output is correct
23 Correct 23 ms 8112 KB Output is correct
24 Correct 23 ms 7960 KB Output is correct
25 Correct 25 ms 8080 KB Output is correct
26 Correct 22 ms 8020 KB Output is correct
27 Correct 25 ms 8012 KB Output is correct
28 Correct 25 ms 8360 KB Output is correct
29 Correct 22 ms 7976 KB Output is correct
30 Correct 22 ms 8020 KB Output is correct
31 Incorrect 3 ms 5076 KB Output isn't correct
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 4 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 4 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -