Submission #823229

#TimeUsernameProblemLanguageResultExecution timeMemory
823229rainboyReal Mountains (CCO23_day1problem2)C11
18 / 25
303 ms26376 KiB
#include <stdio.h>

#define N	1000000
#define INF	0x3f3f3f3f
#define MD	1000003
#define A	1000000

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int aa[N + 2];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (aa[ii[j]] == aa[i_])
				j++;
			else if (aa[ii[j]] < aa[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

int main() {
	static int ii[N], pp[N + 2], qq[N + 2], bb[N + 2];
	int n, h, i, p, q, a, l, r, k, ans;

	scanf("%d", &n);
	for (i = 1; i <= n; i++) {
		scanf("%d", &aa[i]);
		ii[i - 1] = i;
	}
	sort(ii, 0, n);
	for (i = 0; i <= n + 1; i++)
		pp[i] = i - 1, qq[i] = i + 1;
	ans = 0;
	for (a = A, h = n - 1, l = n + 2, r = -1; a > 0; a--) {
		while (h >= 0 && aa[i = ii[h]] > a) {
			l = min(l, i), r = max(r, i);
			p = pp[i], q = qq[i];
			qq[p] = q, pp[q] = p, bb[p] = aa[i];
			h--;
		}
		if (h == n - 1)
			continue;
		k = r - l + 1 - (n - 1 - h);
		if (k == 0)
			continue;
		ans = (ans + (long long) (a * 3 + 2) * k) % MD;
		if (k == 1)
			ans = (ans + bb[l - 1] + bb[pp[r + 1]] - (a + 1) * 2) % MD;
		else
			ans = (ans + bb[l - 1] + bb[pp[r + 1]] + aa[ii[h + 1]] - (a + 1) * 3) % MD;
	}
	printf("%d\n", ans);
	return 0;
}

Compilation message (stderr)

Main.c: In function 'main':
Main.c:42:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...