Submission #971686

#TimeUsernameProblemLanguageResultExecution timeMemory
971686SzilDiversity (CEOI21_diversity)C++17
64 / 100
7091 ms8768 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MAXN = 300'001; const int BLOCK = 400; struct Query { int l, r, idx; bool operator<(Query x) const { if (l / BLOCK == x.l / BLOCK) return (l / BLOCK) & 1 ? r > x.r : r < x.r; return l < x.l; } }; int a[MAXN], b[MAXN]; map<int, int> c; ll ans[MAXN], negyzet[MAXN]; void add(int i) { int v = a[i]; if (b[v]) { c[b[v]]--; if (c[b[v]] == 0) c.erase(b[v]); } b[v]++; c[b[v]]++; } void remove(int i) { int v = a[i]; c[b[v]]--; if (c[b[v]] == 0) c.erase(b[v]); b[v]--; if (b[v]) { c[b[v]]++; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; for (ll i = 1; i <= n; i++) { negyzet[i] = negyzet[i-1] + i*i; } for (int i = 1; i <= n; i++) { cin >> a[i]; } vector<Query> qrys(q); for (int i = 0; i < q; i++) { cin >> qrys[i].l >> qrys[i].r; qrys[i].idx = i+1; } sort(qrys.begin(), qrys.end()); int l = 1, r = 0; for (auto [nl, nr, idx] : qrys) { while (r < nr) add(++r); while (r > nr) remove(r--); while (l < nl) remove(l++); while (l > nl) add(--l); deque<pair<ll, ll>> dq; int d = 0; auto push = [&](int a, int b) { if (d) dq.emplace_back(a, b); else dq.emplace_front(a, b); d ^= 1; }; for (auto it = c.rbegin(); it != c.rend(); it++) { auto [len, cnt] = *it; if (cnt & 1) { push(len, 1); cnt--; } push(len, cnt/2); push(len, cnt/2); } auto sum_arit = [&](ll start, ll d, ll cnt) { return start*cnt + d * (cnt - 1) * cnt / 2; }; ll pref = 0; int x = nr-nl+1; for (auto [len, cnt] : dq) { ans[idx] += cnt * (len*(len+1)/2); ans[idx] += cnt * (len*(x-len)); ans[idx] += (x - len) * sum_arit(pref, len, cnt); ans[idx] -= pref * pref * cnt; ans[idx] -= pref * sum_arit(0, 2*len, cnt); ans[idx] -= len * len * negyzet[cnt-1]; pref += len*cnt; } } for (int i = 1; i <= q; i++) { cout << ans[i] << "\n"; } return 0; }
#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...