Submission #494198

#TimeUsernameProblemLanguageResultExecution timeMemory
494198rainboyDiversity (CEOI21_diversity)C11
100 / 100
4731 ms6216 KiB
#include <stdio.h> #include <string.h> #define N 300000 #define Q 50000 #define A 300000 #define B 1341 #define INF 0x3f3f3f3f unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } long long f(int n, int n_) { return (long long) n * (n + 1) / 2 + (long long) (n_ - n) * (n_ - n + 1) / 2; } int cc[N + 1], 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 h, 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]) for (h = 0; h < cc[k] && j - i > k; h++) if (i < r - l + 1 - j) x -= f(i += k, r - l + 1); else x -= f(j -= k, r - l + 1); 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:132:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
diversity.c:134:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
diversity.c:136:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |   scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...