답안 #545842

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
545842 2022-04-05T14:59:20 Z rainboy 우주 해적 (JOI14_space_pirate) C
0 / 100
472 ms 78536 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int *ej[N], eo[N], pp[N], pp_[L + 1][N], ii[N + 1], tt[N], tt_[N], n; long long cc[N], bb[N], cc_[L + 1][N], k, time;

void upd(int i, int b) {
	cc[i] -= b * time, bb[i] += b;
}

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;
}

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

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

		dfs1(j, t);
	}
}

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

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

int qu[N * 2 + 1], h1, t1, t2;

void dfs2(int p, int i, int cnt, int h_, int h0) {
	int o;

	time++;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			int h0_ = tt[j] != -1 ? cnt : h0, h;

			qu[cnt] = j;
			for (h = h0; h >= h_; h--)
				upd(qu[cnt - 1 - (k + cnt - (h <= h1 ? t1 : t2) - 1) % (cnt - h)], -1);
			for (h = h0_; h >= h_; h--)
				upd(qu[cnt - (k + cnt - (h <= h1 ? t1 : t2)) % (cnt - h + 1)], 1);
			dfs2(i, j, cnt + 1, h_, h0_);
			for (h = h0_; h >= h_; h--)
				upd(qu[cnt - (k + cnt - (h <= h1 ? t1 : t2)) % (cnt - h + 1)], -1);
			for (h = h0; h >= h_; h--)
				upd(qu[cnt - 1 - (k + cnt - (h <= h1 ? t1 : t2) - 1) % (cnt - h)], 1);
		}
	}
}

int main() {
	int g, h, h_, i, i_, j, t, l, d, cnt;
	long long 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);
	}
	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]];
	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, dfs1(i, t);
	ii[t] = i_ = i;
	for (i = 0; i < n; i++)
		if (tt_[i] == -1)
			tt_[i] = t;
	j = i_, k_ = (k - t) % (t - tt[i_]);
	while (k_--)
		j = pp[j];
	for (i = 0; i < n; i++)
		if (tt[i] == -1)
			cc[j] += n;
	for (i = 0; i < n; i++)
		update(i, k, 1), update(i, k - tt_[i], -1);
	for (l = L; l > 0; l--)
		for (i = 0; i < n; i++)
			cc_[l - 1][i] += cc_[l][i], cc_[l - 1][pp_[l - 1][i]] += cc_[l][i];
	for (i = 0; i < n; i++)
		cc[i] += cc_[0][i];
	cnt = 0;
	for (h = t - 1; h > tt[i_]; h--)
		qu[cnt++] = ii[h];
	qu[cnt] = i_;
	h1 = t - tt[i_] - 1, t1 = tt[qu[h1]] + h1, t2 = tt[qu[cnt]] + cnt, h_ = cnt - (t - tt[i_]);
	for (h = t; h > tt[i_]; h--) {
		qu[cnt] = ii[h];
		if (h == t)
			for (g = cnt; g >= 0; g--)
				upd(qu[cnt - (k + cnt - (g <= h1 ? t1 : t2)) % (cnt - g + 1)], 1);
		else if (h == t - 1) {
			for (g = cnt - 1; g >= h_; g--)
				upd(qu[cnt - 1 - (k + cnt - (g <= h1 ? t1 : t2) - 1) % (cnt - g)], -1);
			t2 = tt[qu[cnt]] + cnt, h_++;
			for (g = cnt; g >= h_; g--)
				upd(qu[cnt - (k + cnt - (g <= h1 ? t1 : t2)) % (cnt - g + 1)], 1);
		} else {
			for (d = 1; d <= cnt - h_; d++)
				upd(qu[cnt - 1 - (k + cnt - (cnt - d <= h1 ? t1 : t2) - 1) % d], -1);
			h_++;
			for (d = 1, g = cnt; d <= cnt + 1 - h_; d++, g--)
				upd(qu[cnt - (k + cnt - (cnt + 1 - d <= h1 ? t1 : t2)) % d], 1);
		}
		dfs2(ii[h - 1], ii[h], cnt + 1, cnt - (t - tt[i_]) + 1, cnt);
		cnt++;
	}
	for (i = 0; i < n; i++)
		upd(i, -bb[i]);
	for (i = 0; i < n; i++)
		printf("%lld\n", cc[i]);
	return 0;
}

Compilation message

space_pirate.c: In function 'append':
space_pirate.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   17 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
space_pirate.c: In function 'main':
space_pirate.c:73:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |  scanf("%d%lld", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~~~
space_pirate.c:77:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |   scanf("%d", &pp[i]), pp[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Incorrect 1 ms 980 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Incorrect 1 ms 980 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 472 ms 78536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Incorrect 1 ms 980 KB Output isn't correct
3 Halted 0 ms 0 KB -