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;
const long long N = 1e1 + 5;
long long t[N][N * 4], a[N];
vector <long long> ans, res;
void build (long long v, long long tl, long long tr) {
if (tl == tr) {
for (int i = 0 ; i < N; i++) t[i][v] = 0;
t[a[tl]][v] = 1;
return;
} long long tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
for (int i = 1; i < N; i++) t[i][v] = t[i][v * 2] + t[i][v * 2 + 1];
}
void get (int v, int tl, int tr, int l, int r) {
if (tl >= l && tr <= r) {
for (int i = 0; i < N; i++)
for (int j = 0; j < t[i][v]; j++) ans.push_back(i);
return;
} if (tl > r || tr < l) return;
int tm = (tl + tr) / 2;
get(v * 2, tl, tm, l, r);
get(v * 2 + 1, tm + 1, tr, l, r);
}
long long cont (long long r) {
return (r + 1) * (r) / 2;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q; cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
while (q--) {
int l, r; cin >> l >> r;
ans.clear();
res.clear();
get(1, 1, n, l, r);
sort(ans.begin(), ans.end());
for (int i = 0 ; i < ans.size();) {
int cnt = 1, j = i + 1;
while (j < ans.size()) {
if (ans[j] == ans[i]) cnt++, j++;
else break;
} i = j;
res.push_back(cnt);
}
long long x = 0;
for (int i = 0; i < res.size(); i++) {
long long nw = 0, k = 2, nwres = res[i];
for (int j = i + 1; j < res.size(); j++) {
nw += k, nwres += k * res[j], k++;
} x += nwres * res[i] - cont(res[i] - 1);
} cout << x << '\n';
// for (auto it : res) cout << it << ' ' ;
// cout << '\n';
}
return 0;
}
Compilation message (stderr)
diversity.cpp: In function 'int main()':
diversity.cpp:41:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0 ; i < ans.size();) {
| ~~^~~~~~~~~~~~
diversity.cpp:43:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | while (j < ans.size()) {
| ~~^~~~~~~~~~~~
diversity.cpp:50:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 0; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
diversity.cpp:52:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int j = i + 1; j < res.size(); j++) {
| ~~^~~~~~~~~~~~
# | 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... |