This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
constexpr int max_A = int(3E5) + 5;
int cnt[max_A];
i64 calc_slow(std::vector<i64> x) {
int n = int(x.size());
i64 ans = 0;
for (int i = 0; i < n; ++i) {
ans += x[i] * (x[i] + 1) / 2;
i64 s = 0;
for (int j = i + 1; j < n; ++j) {
s += x[j] * (j - i + 1);
}
debug(i, s);
ans += x[i] * s;
}
return ans;
}
i64 calc(std::vector<i64> x) {
int n = int(x.size());
i64 ans = 0;
std::vector<i64> sum(n + 1), f(n + 1);
for (int i = n - 1; i >= 0; --i) {
sum[i] = sum[i + 1] + x[i];
}
for (int i = n - 1; i >= 0; --i) {
f[i] = f[i + 1] + sum[i];
}
debug(sum, f);
for (int i = 0; i < n; ++i) {
ans += x[i] * (x[i] + 1) / 2;
debug(i, f[i + 1] + sum[i + 1]);
ans += x[i] * (f[i + 1] + sum[i + 1]);
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
std::vector<int> L(Q), R(Q);
for (int i = 0; i < Q; ++i) {
std::cin >> L[i] >> R[i];
}
for (int i = 0; i < N; ++i) {
++cnt[A[i]];
}
std::vector<i64> x;
for (int i = 0; i < max_A; ++i) {
if (cnt[i] != 0) {
x.emplace_back(cnt[i]);
}
}
int n = int(x.size());
std::sort(x.begin(), x.end());
std::vector<i64> w(n);
int l = 0, r = n - 1;
for (int i = 0; i < n; ++i) {
if (~i & 1) w[l++] = x[i];
else w[r--] = x[i];
}
debug(w);
i64 ans = calc(w);
debug(calc_slow(w));
std::cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |