답안 #905226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
905226 2024-01-12T19:29:32 Z rainboy Counterspells (CPSPC17_counterspells) C
0 / 100
121 ms 18036 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200001
#define N_	(1 << 18)	/* N_ = pow2(ceil(log2(N + 1))) */

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 dd[N], pp[N], qq[N], ta[N], tb[N];

int dfs1(int i, int d) {
	int o, s, k_, j_;

	dd[i] = d;
	s = 1, k_ = 0, j_ = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o], k = dfs1(j, d + 1);

		s += k;
		if (k_ < k)
			k_ = k, j_ = j;
	}
	qq[i] = j_;
	return s;
}

void dfs2(int i, int q) {
	static int time;
	int o, j_;

	ta[i] = time++;
	j_ = qq[i], qq[i] = q;
	if (j_ != -1)
		dfs2(j_, q);
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != j_)
			dfs2(j, j);
	}
	tb[i] = time;
}

int ft[N];

void ft_update(int i, int n) {
	while (i < n) {
		ft[i] ^= 1;
		i |= i + 1;
	}
}

int ft_query(int i) {
	int x = 0;

	while (i >= 0) {
		x ^= ft[i];
		i &= i + 1, i--;
	}
	return x;
}

int st[N_ * 2], n_;

void pul(int i) {
	st[i] = st[i << 1 | 0] | st[i << 1 | 1];
}

void st_update(int i, int x) {
	st[i += n_] = x;
	while (i > 1)
		pul(i >>= 1);
}

int st_query(int r) {
	int l = 0;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((r & 1) == 0) {
			if (st[r]) {
				while (r < n_)
					r = st[r << 1 | 1] ? r << 1 | 1 : r << 1 | 0;
				return r - n_;
			}
			r--;
		}
	return -1;
}

int main() {
	static int pp[N], kk[N];
	int n, i, j, a, flip;

	scanf("%d", &n), n++;
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (i = 1; i < n; i++) {
		scanf("%d", &pp[i]);
		append(pp[i], i);
	}
	dfs1(0, 0), dfs2(0, 0);
	n_ = 1;
	while (n_ <= n)
		n_ <<= 1;
	for (i = 1; i < n; i++) {
		j = i;
		while (j != -1) {
			a = st_query(ta[j] - 1);
			if (a >= ta[qq[j]]) {
				ft_update(a + 1, n), ft_update(ta[j] + 1, n);
				printf("%d\n", dd[i] - dd[j] + ta[j] - a - 1);
				break;
			}
			ft_update(ta[qq[j]], n), ft_update(ta[j] + 1, n);
			j = qq[j];
			flip = 0;
			if (pp[j] != -1) {
				if ((dd[i] - dd[j]) % 2 == 0) {
					if (kk[pp[j]]++ == 0) {
						st_update(ta[pp[j]], 1);
						flip = ft_query(ta[pp[j]]);
					}
				} else
					if (--kk[pp[j]] == 0) {
						st_update(ta[pp[j]], 0);
						flip = !ft_query(ta[pp[j]]) && (ta[pp[j]] + 1 == n || !ft_query(ta[pp[j]] + 1));
					}
			}
			if (!flip) {
				printf("%d\n", dd[i] - dd[j]);
				break;
			}
			j = pp[j];
		}
	}
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:102:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |  scanf("%d", &n), n++;
      |  ^~~~~~~~~~~~~~~
Main.c:106:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |   scanf("%d", &pp[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 6492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 6492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 6492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 121 ms 18036 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 6492 KB Output isn't correct
2 Halted 0 ms 0 KB -