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 Q 50000
#define A 300000
#define B 1729
#define INF 0x3f3f3f3f
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long sum(int n) {
return (long long) n * (n + 1) / 2;
}
long long sum2(int n) {
return (long long) n * (n + 1) * (n * 2 + 1) / 6;
}
long long prog(int a, int c, int d) {
return (long long) a * c + (long long) d * sum(c - 1);
}
long long prog2(int a, int c, int d) {
return (long long) a * a * c + (long long) a * d * 2 * sum(c - 1) + (long long) d * d * sum2(c - 1);
}
long long fprog(int a, int b, int c, int d) {
return (long long) b * (b + 1) / 2 * c + prog2(a, c, d) - (long long) prog(a, c, d) * b;
}
int aa[N];
int ll[Q], rr[Q];
int compare(int h1, int h2) {
return ll[h1] / B != ll[h2] / B ? ll[h1] / B - ll[h2] / B : rr[h1] - rr[h2];
}
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(hh[j], h);
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
l = k;
}
}
int prev[N + 1], next[N + 1];
int cc[N + 1], kk[A + 1], l, r, d;
void add(int a) {
int k = kk[a]++, p = prev[k], q = next[k];
if (k == 0)
d++;
if (cc[k] == 1 && cc[k + 1] > 0) {
p = prev[k], q = next[k];
if (p != -1)
next[p] = q;
if (q != -1)
prev[q] = p;
prev[k] = next[k] = -1;
} else if (cc[k] > 1 && cc[k + 1] == 0) {
next[k] = k + 1, prev[k + 1] = k;
next[k + 1] = q;
if (q != -1)
prev[q] = k + 1;
} else if (cc[k] == 1 && cc[k + 1] == 0) {
p = prev[k], q = next[k];
prev[k + 1] = p;
if (p != -1)
next[p] = k + 1;
next[k + 1] = q;
if (q != -1)
prev[q] = k + 1;
prev[k] = next[k] = -1;
}
cc[k]--, cc[k + 1]++;
}
void delete(int a) {
int k = kk[a]--, p = prev[k], q = next[k];
if (k == 1)
d--;
if (cc[k] == 1 && cc[k - 1] > 0) {
p = prev[k], q = next[k];
if (p != -1)
next[p] = q;
if (q != -1)
prev[q] = p;
prev[k] = next[k] = -1;
} else if (cc[k] > 1 && cc[k - 1] == 0) {
prev[k] = k - 1, next[k - 1] = k;
prev[k - 1] = p;
if (p != -1)
next[p] = k - 1;
} else if (cc[k] == 1 && cc[k - 1] == 0) {
p = prev[k], q = next[k];
prev[k - 1] = p;
if (p != -1)
next[p] = k - 1;
next[k - 1] = q;
if (q != -1)
prev[q] = k - 1;
prev[k] = next[k] = -1;
}
cc[k]--, cc[k - 1]++;
}
long long solve() {
int i, j, k;
long long x;
x = (long long) (r - l + 1) * (r - l + 2) / 2 * d;
for (k = next[0], i = 0, j = r - l + 1; k != -1; k = next[k]) {
int c = j - i == k * cc[k] ? cc[k] - 1 : cc[k];
if (i < r - l + 1 - j) {
x -= fprog(i + k, r - l + 1, (c + 1) / 2, k), i += k * ((c + 1) / 2);
j -= k * (c / 2), x -= fprog(j, r - l + 1, c / 2, k);
} else {
x -= fprog(i + k, r - l + 1, c / 2, k), i += k * (c / 2);
j -= k * ((c + 1) / 2), x -= fprog(j, r - l + 1, (c + 1) / 2, k);
}
}
return x;
}
int main() {
static long long ans[Q];
static int hh[Q];
int n, q, h, i;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (h = 0; h < q; h++) {
scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
hh[h] = h;
}
sort(hh, 0, q);
cc[0] = INF, prev[0] = next[0] = -1;
for (h = 0, l = 0, r = -1; h < q; h++) {
while (r < rr[hh[h]])
add(aa[++r]);
while (l > ll[hh[h]])
add(aa[--l]);
while (r > rr[hh[h]])
delete(aa[r--]);
while (l < ll[hh[h]])
delete(aa[l++]);
ans[hh[h]] = solve();
}
for (h = 0; h < q; h++)
printf("%lld\n", ans[h]);
return 0;
}
Compilation message (stderr)
diversity.c: In function 'main':
diversity.c:152:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
diversity.c:154:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
154 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
diversity.c:156:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |