Submission #871387

#TimeUsernameProblemLanguageResultExecution timeMemory
871387Trisanu_DasDiversity (CEOI21_diversity)C++17
100 / 100
1893 ms7364 KiB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
 
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
	cerr << to_string(h);
	if(sizeof ...(t)) cerr << ", ";
	DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
 
const int INF = 1e9 + 7, K = 500, N = 3e5 + 7;
 
namespace ds {
	int cnt[N];
	bool inhebe[N];
	vi hebe;
	void add(int x) {
		if(x == 0) return;
		cnt[x]++;
		if(!inhebe[x]) {
			inhebe[x] = true;
			hebe.PB(x);
		}
	}
	void del(int x) {
		if(x == 0) return;
		cnt[x]--;
	}
	ll qry() {
		vi alive;
		V<pi> aux;
		for(int x:hebe) {
			if(cnt[x] == 0) {
				inhebe[x] = false;
			} else {
				alive.PB(x);
				debug(x, cnt[x]);
				aux.EB(x, cnt[x]);
			}
		}
		hebe = alive;
		sort(ALL(aux));
 
		V<pi> even, odd;
		bool f = 0;
		for(auto[x, y]:aux) {
			if(!f) {
				even.EB(x, (y + 1) / 2);
				if(y / 2) odd.EB(x, y / 2);
			} else {
				odd.EB(x, (y + 1) / 2);
				if(y / 2) even.EB(x, y / 2);
			}
			f ^= y & 1;			
		}
		reverse(ALL(odd));
		even.insert(even.end(), ALL(odd));
 
		ll ans = 0, ct = 0, sx = 0, ss = 0;
 
		for(auto[x, y]:even) {
			ans += 1LL * x * (x + 1) * y;
			ll a = (x * (ct + 1) * sx - x * ss) * 2;
			ll b = (x * sx + 1LL * x * x * (ct + 1)) * 2 - 1LL * x * x * (2 * ct - 1);
			ll c = 1LL * x * x;
			ans += a * y;
			ans += b * y * (y - 1) / 2;
			ans += c * (y - 1) * y * (2 * y - 1) / 6;
			ss += x * (2 * ct + y - 1) * y / 2;
			sx += x * y;
			ct += y;
		}
 
		return ans / 2;
	}
}
 
signed main()
{
	IO_OP;
 
	int n, q;
	cin >> n >> q;
	vi a(n);
	for(int i = 0; i < n; i++)
		cin >> a[i];
 
	vi l(q), r(q);
	for(int i = 0; i < q; i++) {
		cin >> l[i] >> r[i];
		l[i]--, r[i]--;
	}
 
	vi order(q); iota(ALL(order), 0);
	sort(ALL(order), [&] (int i, int j) {
		return MP(l[i] / K, r[i]) < MP(l[j] / K, r[j]);
	});
 
	V<ll> ans(q);
	int cnt[N] = {};
 
	auto add = [&] (int x) {
		ds::del(cnt[x]);
		cnt[x]++;
		ds::add(cnt[x]);
	};
	auto del = [&] (int x) {
		ds::del(cnt[x]);
		cnt[x]--;
		ds::add(cnt[x]);
	};
 
	int L = 0, R = 0;
	add(a[0]);
	for(int qi:order) {
		while(R < r[qi]) add(a[++R]);
		while(L > l[qi]) add(a[--L]);
		while(R > r[qi]) del(a[R--]);
		while(L < l[qi]) del(a[L++]);
		ans[qi] = ds::qry();
	}
 
	for(int i = 0; i < q; i++)
		cout << ans[i] << '\n';
 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...