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 namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> yy = a;
sort(yy.begin(), yy.end());
yy.resize(unique(yy.begin(), yy.end()) - yy.begin());
for (int &x : a) {
x = lower_bound(yy.begin(), yy.end(), x) - yy.begin();
}
const int m = yy.size();
int L, R;
cin >> L >> R;
assert(q == 1 && L == 1 && R == n);
vector<int> cnt(m);
for (int x : a) {
cnt[x] += 1;
}
ll ans = 0;
for (int x : cnt) {
ans += 1LL * x * (x + 1) / 2 + 1LL * x * (n - x);
}
sort(cnt.begin(), cnt.end());
int l = 0, r = 0;
for (int x : cnt) {
if (l < r) {
ans += 1LL * l * (n - x - l);
l += x;
} else {
ans += 1LL * r * (n - x - r);
r += x;
}
}
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... |