Submission #494197

#TimeUsernameProblemLanguageResultExecution timeMemory
494197rainboyDiversity (CEOI21_diversity)C11
64 / 100
7075 ms2988 KiB
#include <stdio.h>
#include <string.h>

#define N	300000
#define Q	50000
#define A	300000
#define B	1341

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 cc[N + 1], kk[A + 1], l, r, d;

void add(int a) {
	if (cc[kk[a]] > 0)
		d--;
	cc[kk[a]]--, kk[a]++, cc[kk[a]]++;
	if (cc[kk[a]] > 0)
		d++;
}

void delete(int a) {
	if (cc[kk[a]] > 0)
		d--;
	cc[kk[a]]--, kk[a]--, cc[kk[a]]++;
	if (cc[kk[a]] > 0)
		d++;
}

long long solve() {
	int h, i, j, k;
	long long x;

	x = (long long) (r - l + 1) * (r - l + 2) / 2 * d;
	for (k = 1, i = 0, j = r - l + 1; k <= N; 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);
	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:86:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
diversity.c:88:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
diversity.c:90:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   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...