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 <stdio.h>
#include <string.h>
#define N 300000
#define A 300000
long long f(int n, int n_) {
return (long long) n * (n + 1) / 2 + (long long) (n_ - n) * (n_ - n + 1) / 2;
}
int aa[N];
long long solve(int l, int r) {
static int kk[A + 1], cc[N + 1];
int n, i, j, a, k;
long long x;
memset(kk, 0, (A + 1) * sizeof *kk);
for (i = l; i <= r; i++)
kk[aa[i]]++;
memset(cc, 0, (N + 1) * sizeof *cc);
n = 0;
for (a = 0; a <= A; a++)
if (kk[a] > 0)
cc[kk[a]]++, n++;
x = (long long) (r - l + 1) * (r - l + 2) / 2 * n;
for (k = 1, i = 0, j = r - l + 1; k <= N; k++)
while (cc[k] > 0 && j - i > k) {
if (i < r - l + 1 - j)
x -= f(i += k, r - l + 1);
else
x -= f(j -= k, r - l + 1);
cc[k]--;
}
return x;
}
int main() {
int n, q, i;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
while (q--) {
int l, r;
scanf("%d%d", &l, &r), l--, r--;
printf("%lld\n", solve(l, r));
}
return 0;
}
Compilation message (stderr)
diversity.c: In function 'main':
diversity.c:41:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
diversity.c:43:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
diversity.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d", &l, &r), l--, r--;
| ^~~~~~~~~~~~~~~~~~~~~| # | 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... |