結果

問題 No.1641 Tree Xor Query
ユーザー 👑 ygussanyygussany
提出日時 2021-08-06 22:16:30
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 185 ms / 5,000 ms
コード長 1,799 bytes
コンパイル時間 1,332 ms
コンパイル使用メモリ 32,484 KB
実行使用メモリ 16,376 KB
最終ジャッジ日時 2023-10-17 03:40:14
合計ジャッジ時間 2,418 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
14,208 KB
testcase_01 AC 5 ms
14,208 KB
testcase_02 AC 5 ms
14,208 KB
testcase_03 AC 5 ms
14,208 KB
testcase_04 AC 5 ms
14,208 KB
testcase_05 AC 5 ms
14,208 KB
testcase_06 AC 5 ms
14,208 KB
testcase_07 AC 5 ms
14,208 KB
testcase_08 AC 5 ms
14,208 KB
testcase_09 AC 5 ms
14,208 KB
testcase_10 AC 5 ms
14,208 KB
testcase_11 AC 5 ms
14,208 KB
testcase_12 AC 5 ms
14,208 KB
testcase_13 AC 185 ms
16,376 KB
testcase_14 AC 185 ms
16,376 KB
testcase_15 AC 7 ms
14,220 KB
testcase_16 AC 14 ms
14,240 KB
testcase_17 AC 11 ms
14,228 KB
testcase_18 AC 8 ms
14,236 KB
testcase_19 AC 10 ms
14,212 KB
testcase_20 AC 184 ms
16,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next;
	int v;
} list;

const int SIZE = 500;

int main()
{
	int i, N, Q, u, w, C[100001];
	list *adj[100001] = {}, e[200001], *p;
	scanf("%d %d", &N, &Q);
	for (i = 1; i <= N; i++) scanf("%d", &(C[i]));
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d", &u, &w);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	int x, par[100001][20] = {}, depth[100001], q[100001], head, tail, sum[100001];
	q[0] = 1;
	depth[1] = 0;
	depth[0] = -1;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w][0] == 0 && w != 1) {
				par[w][0] = u;
				depth[w] = depth[u] + 1;
				q[tail++] = w;
				for (x = u, i = 0; par[x][i] != 0; x = par[x][i++]) par[w][i+1] = par[x][i];
			}
		}
	}
	for (u = 1; u <= N; u++) sum[u] = 0;
	for (head = tail - 1; head >= 0; head--) {
		u = q[head];
		sum[u] ^= C[u];
		if (head > 0) sum[par[u][0]] ^= sum[u];
	}
	
	int j, k, t[100001], y[100001], z[100001], ans;
	for (i = 0; i < Q; i++) {
		scanf("%d %d %d\n", &(t[i]), &(y[i]), &(z[i]));
		if (t[i] == 1) C[y[i]] ^= z[i];
		else {
			ans = sum[y[i]];
			for (j = i / SIZE * SIZE; j < i; j++) {
				if (t[j] == 2) continue;
				u = y[i];
				w = y[j];
				if (depth[u] > depth[w]) continue;
				while (depth[u] < depth[w]) {
					for (k = 0; depth[par[w][k]] >= depth[u]; k++);
					w = par[w][k-1];
				}
				if (u == w) ans ^= z[j];
			}
			printf("%d\n", ans);
		}
		
		if (i % SIZE == SIZE - 1) {
			for (u = 1; u <= N; u++) sum[u] = 0;
			for (head = tail - 1; head >= 0; head--) {
				u = q[head];
				sum[u] ^= C[u];
				if (head > 0) sum[par[u][0]] ^= sum[u];
			}
		}
	}
	fflush(stdout);
	return 0;
}
0