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;
typedef long long ll;
const int MAXN = 3e5 + 25;
int n, a[MAXN], q, n1;
int freq[MAXN];
int main () {
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
while (q--) {
memset(freq, 0, sizeof(freq));
int l, r;
cin >> l >> r;
for (int i = l; i <= r; i++) freq[a[i]]++;
vector <int> x;
for (int i = 1; i < MAXN; i++) {
if (freq[i]) {
x.push_back(freq[i]);
}
}
n1 = x.size();
sort(x.begin(), x.end());
deque <int> y;
for (int i = n1 - 1; i >= 0; i -= 2) y.push_front(x[i]);
for (int i = n1 - 2; i >= 0; i -= 2) y.push_back(x[i]);
ll o = r - l + 1;
ll rr = 0, sum = 0;
for (auto i : y) {
sum += rr * (rr + 1) / 2 + (o - i - rr) * (o - i - rr + 1) / 2;
rr += i;
}
sum = ll(o) * ll((o + 1)) / 2 * ((ll)y.size()) - sum;
cout << sum << '\n';
}
}
# | 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... |