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;
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);
ll n, q;
cin >> n >> q;
vector<ll> arr(n);
for (auto &ref: arr) cin >> ref;
while (q--) {
ll left, right;
cin >> left >> right;
left--;
std::sort(arr.begin(), arr.end());
vector<ll> lens;
{
vector<ll> raw_lens;
ll last = -1;
for (ll 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<ll> first, second;
ll 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 (ll l: first)
lens.push_back(l);
for (auto it = second.rbegin(); it != second.rend(); it++)
lens.push_back(*it);
}
struct ptr {
ll in;
ll cnt;
};
auto advance = [&](ptr &ptr) {
if (ptr.cnt + 1 == lens[ptr.in]) {
ptr.cnt = 0;
ptr.in++;
} else ptr.cnt++;
};
ll total = 0;
{
ll bias = 0;
vector<ll> uniques;
{
ptr first{0, 0};
ptr second{0, 0};
for (ll sublen = 1; sublen <= n; sublen++) {
uniques.push_back(second.in - first.in + 1);
advance(second);
}
}
ll sum = accumulate(uniques.begin(), uniques.end(), 0ll);
total = sum;
for (ll i = 1; i < uniques.size(); i++) {
sum -= (uniques[i - 1] - bias);
if ((uniques[i] - bias) == 2) {
bias++;
sum -= uniques.size() - i;
}
total += sum;
}
}
cout << total << "\n";
}
}
Compilation message (stderr)
diversity.cpp: In function 'int main()':
diversity.cpp:52:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long 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: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if (i < raw_lens.size()) {
| ~~^~~~~~~~~~~~~~~~~
diversity.cpp:96:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (ll i = 1; i < uniques.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# | 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... |