#include <bits/stdc++.h>
using i64 = long long;
struct Query {
int l;
int r;
};
int naive(std::vector<int> A) {
const int N = (int)A.size();
std::map<int, int> countS;
for (const auto e : A) {
++countS[e];
}
A.clear();
for (auto &[key, val] : countS) {
A.push_back(val);
}
std::sort(A.begin(), A.end());
std::vector<int> B;
for (int i = 0; i < ((int)A.size() + 1) / 2; ++i) {
while (A[i]--) {
B.push_back(i);
}
const int x = (int)A.size() - i - 1;
if (x <= i) {
break;
}
while (A[x]--) {
B.push_back(x);
}
}
A = B;
i64 ans = 0;
for (int i = 0; i < N; ++i) {
for (int j = 1; j <= N; ++j) {
std::set<int> s;
for (int k = i; k < j; ++k) {
s.insert(A[k]);
}
ans += (int)s.size();
}
}
return ans;
}
int main() {
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (auto &e : A) {
std::cin >> e;
}
std::vector<Query> queries(Q);
for (auto &[l, r] : queries) {
std::cin >> l >> r;
--l;
}
for (int i = 0; i < Q; ++i) {
std::vector<int> vec;
std::copy(A.begin() + queries[i].l, A.begin() + queries[i].r, std::back_inserter(vec));
std::cout << naive(vec) << std::endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |