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;
ll cost(vector<int> v) {
ll ans = 0;
map<ll,ll> last;
for (ll i = 0; i<v.size(); ++i) {
int bef = last[v[i]];
ans += (i-bef+1)*(v.size()-i);
last[v[i]] = i+1;
}
return ans;
}
int main() {
/*vector<int> v = {1,2,2,3,3,3,5,5,5,6,6,4};
cout << cost(v);*/
//ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n,q; cin >> n >> q;
vector<int> vv(n); for (int i = 0; i<n; ++i) cin >> vv[i];
while (q--) {
int L, R;
cin >> L >> R;
--L; --R;
vector<int> vvv(R-L+1); for (int i = 0; i<R-L+1; ++i) vvv[i] = vv[i+L];
map<ll,ll> m; for (int &i : vvv) ++m[i];
vector<int> a; for (auto p : m) a.push_back(p.second);
sort(a.begin(), a.end());
vector<int> v(R-L+1); ll l = 0, r = R-L;
for (int i = 0; i<a.size(); ++i) {
if (i%2) {
for (int L = 0; L<a[i]; ++L) v[l+L] = i;
l += a[i];
}
else {
for (int R = 0; R<a[i]; ++R) v[r-R] = i;
r -= a[i];
}
}
ll fin = cost(v);
cout << fin << '\n';
}
return 0;
}
/*
1 2 2 3 3 3 5 5 5 6 6 4
*/
Compilation message (stderr)
diversity.cpp: In function 'll cost(std::vector<int>)':
diversity.cpp:8:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | for (ll i = 0; i<v.size(); ++i) {
| ~^~~~~~~~~
diversity.cpp: In function 'int main()':
diversity.cpp:32:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i<a.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... |