Submission #956679

#TimeUsernameProblemLanguageResultExecution timeMemory
956679TAhmed33Diversity (CEOI21_diversity)C++98
64 / 100
745 ms8792 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 3e5 + 25; const int B = 500; int a[MAXN], n, q; array <int, 3> queries[MAXN]; ll ans[MAXN]; int freq[MAXN], freq2[MAXN]; set <int> t; void add (int x) { if (x < 1 || x > n) exit(0); if (a[x] >= MAXN || a[x] < 0); if (freq2[freq[a[x]]] == 1) t.erase(freq[a[x]]); freq2[freq[a[x]]]--; freq[a[x]]++; freq2[freq[a[x]]]++; if (freq2[freq[a[x]]] == 1) t.insert(freq[a[x]]); } void rem (int x) { if (x < 1 || x > n) exit(0); if (a[x] >= MAXN || a[x] < 0); if (freq2[freq[a[x]]] == 1) t.erase(freq[a[x]]); freq2[freq[a[x]]]--; freq[a[x]]--; freq2[freq[a[x]]]++; if (freq2[freq[a[x]]] == 1) t.insert(freq[a[x]]); } int main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> q; freq2[0] = MAXN - 1; t.insert(0); for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; queries[i] = {l, r, i}; } sort(queries + 1, queries + q + 1, [&] (array <int, 3> &x, array <int, 3> &y) { return x[0] / B == y[0] / B ? x[1] < y[1] : x[0] / B < y[0] / B; }); int L = queries[0][1] + 1, R = queries[0][1]; for (int i = 1; i <= q; i++) { while (L > queries[i][0]) add(--L); while (L < queries[i][0]) rem(L++); while (R > queries[i][1]) rem(R--); while (R < queries[i][1]) add(++R); ll dis = 0; ll n = R - L + 1; vector <pair <ll, ll>> pp[2]; int c = 0; for (auto j : t) { if (j) { if (j > MAXN || j < 0) exit(0); if (c < 0 || c > 1) exit(0); dis += freq2[j]; int x = j, y = freq2[j]; if (y == 1) { pp[c].push_back({x, 1}); c ^= 1; continue; } int l = (y + 1) / 2, r = y / 2; pp[c].push_back({x, l}); pp[c ^ 1].push_back({x, r}); c ^= (y & 1); } } reverse(pp[1].begin(), pp[1].end()); for (auto j : pp[1]) pp[0].push_back(j); ll cur = 0, cur2 = 0, pref = 0; for (auto [x, y] : pp[0]) { cur2 += pref * pref * y + y * (y - 1) / 2 * 2 * pref * x + x * x * (y - 1) * y * (2 * y - 1) / 6 + y * pref + x * y * (y - 1) / 2; cur += y * n * n - y * n * pref - n * y * (y + 1) / 2 * x + y * n; cur += -y * n * pref + pref * pref * y + pref * y * (y + 1) / 2 * x - y * pref; cur += -n * y * (y + 1) / 2 * x + y * (y + 1) / 2 * x * pref + x * x * y * (y + 1) * (2 * y + 1) / 6 - y * (y + 1) / 2 * x; pref += x * y; } if (queries[i][2] < 1 || queries[i][2] > q) exit(0); ans[queries[i][2]] = n * (n + 1) / 2 * dis - cur / 2 - cur2 / 2; } for (int i = 1; i <= q; i++) cout << ans[i] << '\n'; }

Compilation message (stderr)

diversity.cpp: In function 'int main()':
diversity.cpp:69:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   69 |   for (auto [x, y] : pp[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...