Submission #545484

# Submission time Handle Problem Language Result Execution time Memory
545484 2022-04-04T15:26:06 Z rainboy Space Pirate (JOI14_space_pirate) C
10 / 100
2000 ms 78300 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define L	60	/* L = ceil(log2(10^18)) */

int pp[N], pp_[L + 1][N]; long long cnt[N], cnt_[L + 1][N];

void mark(int *pp, int n, long long k) {
	static int tt[N];
	int i, t;

	memset(tt, 0, n * sizeof *tt);
	i = 0, t = 0;
	while (!tt[i])
		tt[i] = t++, i = pp[i];
	k = (k - t) % (t - tt[i]);
	while (k--)
		i = pp[i];
	cnt[i]++;
}

int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int tt[N], tt_[N];

void dfs(int i, int t) {
	int o;

	if (tt_[i] != -1)
		return;
	tt_[i] = t;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs(j, t);
	}
}

void update(int i, long long k, int x) {
	int l;

	for (l = L; l >= 0; l--)
		if (k >= 1LL << l)
			cnt_[l][i] += x, k -= 1LL << l, i = pp_[l][i];
}

int main() {
	static int ii[N + 1];
	int n, h, i, j, t, l;
	long long k, k_;

	scanf("%d%lld", &n, &k);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (i = 0; i < n; i++) {
		scanf("%d", &pp[i]), pp[i]--;
		append(pp[i], i);
	}
	memset(tt, -1, n * sizeof *tt), memset(tt_, -1, n * sizeof *tt_);
	for (i = 0, t = 0; tt[i] == -1; i = pp[i], t++)
		ii[tt[i] = t] = i, dfs(i, t);
	ii[t] = i;
	for (i = 0; i < n; i++)
		if (tt_[i] == -1)
			tt_[i] = t;
	j = ii[t], k_ = (k - t) % (t - tt[ii[t]]);
	while (k_--)
		j = pp[j];
	for (i = 0; i < n; i++)
		if (tt[i] == -1)
			cnt[j] += n;
	for (i = 0; i < n; i++)
		pp_[0][i] = pp[i];
	for (l = 1; l <= L; l++)
		for (i = 0; i < n; i++)
			pp_[l][i] = pp_[l - 1][pp_[l - 1][i]];
	for (i = 0; i < n; i++) {
		update(i, k, 1), update(i, k - tt_[i], -1);
		for (h = tt_[i]; h < t; h++)
			pp[ii[h]] = i, mark(pp, n, k), pp[ii[h]] = ii[h + 1];
	}
	for (l = L; l > 0; l--)
		for (i = 0; i < n; i++)
			cnt_[l - 1][i] += cnt_[l][i], cnt_[l - 1][pp_[l - 1][i]] += cnt_[l][i];
	for (i = 0; i < n; i++)
		cnt[i] += cnt_[0][i];
	for (i = 0; i < n; i++)
		printf("%lld\n", cnt[i]);
	return 0;
}

Compilation message

space_pirate.c: In function 'append':
space_pirate.c:29:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   29 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
space_pirate.c: In function 'main':
space_pirate.c:62:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |  scanf("%d%lld", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~~~
space_pirate.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |   scanf("%d", &pp[i]), pp[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 932 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 3 ms 980 KB Output is correct
5 Correct 1 ms 932 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 2 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 936 KB Output is correct
12 Correct 3 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 932 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 3 ms 980 KB Output is correct
5 Correct 1 ms 932 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 2 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 936 KB Output is correct
12 Correct 3 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 67 ms 3200 KB Output is correct
16 Correct 9 ms 3284 KB Output is correct
17 Correct 122 ms 3196 KB Output is correct
18 Execution timed out 2047 ms 2692 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1976 ms 78300 KB Output is correct
2 Execution timed out 2084 ms 38156 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 932 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 3 ms 980 KB Output is correct
5 Correct 1 ms 932 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 2 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 936 KB Output is correct
12 Correct 3 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 67 ms 3200 KB Output is correct
16 Correct 9 ms 3284 KB Output is correct
17 Correct 122 ms 3196 KB Output is correct
18 Execution timed out 2047 ms 2692 KB Time limit exceeded
19 Halted 0 ms 0 KB -