/**
* author: BERNARD B.01
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef B01
#include "deb.h"
#else
#define deb(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
const int N = (int) 3e5 + 1;
vector<int> cnt(N);
for (int i = 0; i < n; i++) {
cnt[a[i]]++;
}
vector<pair<int, int>> freqs;
for (int i = 0; i < N; i++) {
if (cnt[i] > 0) {
freqs.emplace_back(cnt[i], i);
}
}
sort(freqs.rbegin(), freqs.rend());
deque<int> d;
for (int i = 0; i < (int) freqs.size(); i++) {
if (i & 1) {
d.push_front(freqs[i].second);
} else {
d.push_back(freqs[i].first);
}
}
vector<int> pos(n);
for (int i = 0; i < (int) freqs.size(); i++) {
pos[freqs[i].second] = i;
}
sort(a.begin(), a.end(), [&](int i, int j) {
return pos[i] < pos[j];
});
long long ans = 0;
for (int i = 0; i < n; i++) {
if (i == 0 || a[i] != a[i - 1]) {
ans += (long long) (i + 1) * (n - i);
} else {
ans += (n - i);
}
}
while (q--) {
int l, r;
cin >> l >> r;
--l; --r;
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |