Submission #502892

# Submission time Handle Problem Language Result Execution time Memory
502892 2022-01-06T17:47:35 Z rainboy Tortoise (CEOI21_tortoise) C
100 / 100
278 ms 31504 KB
/* https://hsin.hr/ceoi/competition/ceoi2021_day2_editorial.pdf */
#include <stdio.h>

#define N	500000
#define N_	(1 << 19)	/* N_ = pow2(ceil(log2(N))) */
#define INF	0x3f3f3f3f

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 tt[N], dd[N];

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 (dd[ii[j]] == dd[i_])
				j++;
			else if (dd[ii[j]] < dd[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 sum[N_ * 2], pref[N_ * 2], ss[N_], kk[N_], n_;

void pul(int i) {
	int l = i << 1, r = l | 1;

	sum[i] = sum[l] + sum[r];
	pref[i] = min(pref[l], sum[l] + pref[r]);
}

void build(int n) {
	int i;

	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	for (i = 1; i < n; i++)
		ss[i] = 1;
	for (i = 0; i < n_; i++)
		sum[n_ + i] = ss[i], pref[n_ + i] = kk[i] == 0 ? INF : ss[i];
	for (i = n_ - 1; i > 0; i--)
		pul(i);
}

void update(int i, int s, int k) {
	ss[i] += s, kk[i] += k;
	sum[n_ + i] = ss[i], pref[n_ + i] = kk[i] == 0 ? INF : ss[i];
	i += n_;
	while (i > 1)
		pul(i >>= 1);
}

int main() {
	static int aa[N], ii[N], ll[N], rr[N], ll_[N], rr_[N], pp[N];
	int n, n_, i, j;
	long long ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (i = 0; i < n; i++)
		ll[i] = aa[i] == -1 ? i : (i == 0 ? -INF : ll[i - 1]);
	for (i = n - 1; i >= 0; i--)
		rr[i] = aa[i] == -1 ? i : (i + 1 == n ? INF : rr[i + 1]);
	n_ = 0;
	for (i = 0; i < n; i++)
		if (aa[i] > 0) {
			if (i - ll[i] <= rr[i] - i)
				tt[i] = -1, dd[i] = i - ll[i];
			else
				tt[i] = 1, dd[i] = rr[i] - i;
			ii[n_++] = i;
		}
	ans = 0;
	for (i = 0; i < n; i++)
		if (aa[i] > 0)
			ans += aa[i];
	build(n);
	for (j = 0; j < n; j++)
		if (aa[j] == -1) {
			i = j - 1;
			while (i >= 0 && aa[i] == 0)
				i--;
			ll_[j] = -INF, rr_[j] = INF;
			if (i >= 0 && aa[i] > 0)
				pp[j] = i, update(i, 0, 1), ans--;
			else
				pp[j] = -1;
		}
	sort(ii, 0, n_);
	for (i = 0; i < n_; i++) {
		int i_ = ii[i], r = rr[i_], p;

		while (kk[i_] <= aa[i_])
			if (tt[i_] == -1) {
				if (kk[i_] == aa[i_])
					break;
				if (kk[i_] == 0) {
					update(i_, 0, 1);
					if (i_ + 1 < n)
						update(i_ + 1, -dd[i_] * 2, 0);
				} else
					update(i_, -dd[i_] * 2, 1);
				if (pref[1] >= 0) {
					if (r != INF)
						ll_[r] = i_;
					ans--;
				} else {
					if (kk[i_] == 1) {
						update(i_, 0, -1);
						if (i_ + 1 < n)
							update(i_ + 1, dd[i_] * 2, 0);
					} else
						update(i_, dd[i_] * 2, -1);
					break;
				}
			} else {
				p = pp[r];
				if (i_ != p)
					break;
				while (p >= 0 && kk[p] == aa[p])
					p--;
				if (p < 0 || aa[p] == -1 || p < ll_[r] || p > rr_[r])
					break;
				update(p, 0, 1), update(i_, -dd[i_] * 2, 0);
				if (pref[1] >= 0)
					rr_[r] = i_, pp[r] = p, ans--;
				else {
					update(p, 0, -1), update(i_, dd[i_] * 2, 0);
					break;
				}
			}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

tortoise.c: In function 'main':
tortoise.c:74:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
tortoise.c:76:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 276 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 288 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 0 ms 284 KB Output is correct
37 Correct 0 ms 280 KB Output is correct
38 Correct 1 ms 284 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 276 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 288 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 0 ms 284 KB Output is correct
37 Correct 0 ms 280 KB Output is correct
38 Correct 1 ms 284 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 0 ms 332 KB Output is correct
42 Correct 0 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 0 ms 288 KB Output is correct
45 Correct 0 ms 332 KB Output is correct
46 Correct 0 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 0 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 284 KB Output is correct
52 Correct 0 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 0 ms 332 KB Output is correct
56 Correct 0 ms 332 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 0 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 0 ms 332 KB Output is correct
62 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 276 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 288 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 0 ms 284 KB Output is correct
37 Correct 0 ms 280 KB Output is correct
38 Correct 1 ms 284 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 0 ms 332 KB Output is correct
42 Correct 0 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 0 ms 288 KB Output is correct
45 Correct 0 ms 332 KB Output is correct
46 Correct 0 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 0 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 284 KB Output is correct
52 Correct 0 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 0 ms 332 KB Output is correct
56 Correct 0 ms 332 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 0 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 0 ms 332 KB Output is correct
62 Correct 0 ms 332 KB Output is correct
63 Correct 1 ms 588 KB Output is correct
64 Correct 2 ms 680 KB Output is correct
65 Correct 1 ms 716 KB Output is correct
66 Correct 2 ms 588 KB Output is correct
67 Correct 2 ms 668 KB Output is correct
68 Correct 2 ms 680 KB Output is correct
69 Correct 2 ms 684 KB Output is correct
70 Correct 2 ms 716 KB Output is correct
71 Correct 2 ms 716 KB Output is correct
72 Correct 2 ms 672 KB Output is correct
73 Correct 2 ms 716 KB Output is correct
74 Correct 3 ms 736 KB Output is correct
75 Correct 1 ms 588 KB Output is correct
76 Correct 2 ms 716 KB Output is correct
77 Correct 2 ms 716 KB Output is correct
78 Correct 2 ms 716 KB Output is correct
79 Correct 2 ms 716 KB Output is correct
80 Correct 2 ms 588 KB Output is correct
81 Correct 2 ms 712 KB Output is correct
82 Correct 2 ms 680 KB Output is correct
83 Correct 2 ms 588 KB Output is correct
84 Correct 2 ms 716 KB Output is correct
85 Correct 2 ms 716 KB Output is correct
86 Correct 2 ms 708 KB Output is correct
87 Correct 3 ms 716 KB Output is correct
88 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 276 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 288 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 0 ms 284 KB Output is correct
37 Correct 0 ms 280 KB Output is correct
38 Correct 1 ms 284 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 0 ms 332 KB Output is correct
42 Correct 0 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 0 ms 288 KB Output is correct
45 Correct 0 ms 332 KB Output is correct
46 Correct 0 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 0 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 284 KB Output is correct
52 Correct 0 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 0 ms 332 KB Output is correct
56 Correct 0 ms 332 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 0 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 0 ms 332 KB Output is correct
62 Correct 0 ms 332 KB Output is correct
63 Correct 1 ms 588 KB Output is correct
64 Correct 2 ms 680 KB Output is correct
65 Correct 1 ms 716 KB Output is correct
66 Correct 2 ms 588 KB Output is correct
67 Correct 2 ms 668 KB Output is correct
68 Correct 2 ms 680 KB Output is correct
69 Correct 2 ms 684 KB Output is correct
70 Correct 2 ms 716 KB Output is correct
71 Correct 2 ms 716 KB Output is correct
72 Correct 2 ms 672 KB Output is correct
73 Correct 2 ms 716 KB Output is correct
74 Correct 3 ms 736 KB Output is correct
75 Correct 1 ms 588 KB Output is correct
76 Correct 2 ms 716 KB Output is correct
77 Correct 2 ms 716 KB Output is correct
78 Correct 2 ms 716 KB Output is correct
79 Correct 2 ms 716 KB Output is correct
80 Correct 2 ms 588 KB Output is correct
81 Correct 2 ms 712 KB Output is correct
82 Correct 2 ms 680 KB Output is correct
83 Correct 2 ms 588 KB Output is correct
84 Correct 2 ms 716 KB Output is correct
85 Correct 2 ms 716 KB Output is correct
86 Correct 2 ms 708 KB Output is correct
87 Correct 3 ms 716 KB Output is correct
88 Correct 2 ms 716 KB Output is correct
89 Correct 95 ms 29260 KB Output is correct
90 Correct 90 ms 29244 KB Output is correct
91 Correct 93 ms 29276 KB Output is correct
92 Correct 134 ms 29516 KB Output is correct
93 Correct 134 ms 29652 KB Output is correct
94 Correct 134 ms 29512 KB Output is correct
95 Correct 105 ms 29496 KB Output is correct
96 Correct 108 ms 29416 KB Output is correct
97 Correct 148 ms 29708 KB Output is correct
98 Correct 149 ms 29892 KB Output is correct
99 Correct 278 ms 31464 KB Output is correct
100 Correct 269 ms 31152 KB Output is correct
101 Correct 278 ms 31504 KB Output is correct
102 Correct 271 ms 31456 KB Output is correct
103 Correct 52 ms 17176 KB Output is correct
104 Correct 10 ms 2092 KB Output is correct
105 Correct 9 ms 2092 KB Output is correct
106 Correct 9 ms 2116 KB Output is correct
107 Correct 9 ms 2124 KB Output is correct
108 Correct 9 ms 2124 KB Output is correct
109 Correct 69 ms 17220 KB Output is correct