#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> arr(n);
for (auto &ref: arr) cin >> ref;
while (q--) {
int left, right;
cin >> left >> right;
left--;
std::sort(arr.begin(), arr.end());
vector<int> lens;
{
vector<int> raw_lens;
int last = -1;
for (int i = 0; i < n; i++) {
if (last == -1) {
last = i;
continue;
}
if (arr[i] == arr[last]) {}
else {
raw_lens.push_back(i - last);
last = i;
}
}
raw_lens.push_back(n - last);
std::sort(raw_lens.begin(), raw_lens.end());
vector<int> first, second;
int i = 0;
while (true) {
if (i < raw_lens.size()) {
first.push_back(raw_lens[i]);
i++;
} else break;
if (i < raw_lens.size()) {
second.push_back(raw_lens[i]);
i++;
} else break;
}
for (int l: first)
lens.push_back(l);
for (auto it = second.rbegin(); it != second.rend(); it++)
lens.push_back(*it);
}
struct ptr {
int in;
int cnt;
};
auto advance = [&](ptr &ptr) {
if (ptr.cnt + 1 == lens[ptr.in]) {
ptr.cnt = 0;
ptr.in++;
} else ptr.cnt++;
};
int total = 0;
for (int sublen = 1; sublen <= n; sublen++) {
ptr first{0, 0};
ptr second{0, 0};
for (int _ = 0; _ < sublen - 1; _++) advance(second);
while (second.in < lens.size()) {
total += second.in - first.in + 1;
advance(first);
advance(second);
}
}
cout << total << "\n";
}
}
Compilation message
diversity.cpp: In function 'int main()':
diversity.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | if (i < raw_lens.size()) {
| ~~^~~~~~~~~~~~~~~~~
diversity.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if (i < raw_lens.size()) {
| ~~^~~~~~~~~~~~~~~~~
diversity.cpp:87:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while (second.in < lens.size()) {
| ~~~~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
104 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
7064 ms |
856 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
104 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
7064 ms |
856 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
104 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
7064 ms |
856 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
104 ms |
348 KB |
Output is correct |
14 |
Execution timed out |
7064 ms |
856 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
104 ms |
348 KB |
Output is correct |
14 |
Execution timed out |
7064 ms |
856 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |