【BZOJ2819】Nim

【题目链接】

【思路要点】

  • 补档博客,无题解。

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN	500005
#define MAXLOG	20
struct Binary_Index_Tree {
	int value[MAXN], n;
	void modify(int x, int y, int v) {
		for (int i = x; i <= n; i += i & -i)
			value[i] ^= v;
		for (int i = y + 1; i <= n; i += i & -i)
			value[i] ^= v;
	}
	int query(int x) {
		int ans = 0;
		for (int i = x; i; i -= i & -i)
			ans ^= value[i];
		return ans;
	}
	void init(int x) {
		n = x;
	}
} BIT;
vector <int> a[MAXN];
int value[MAXN], depth[MAXN];
int num[MAXN], rit[MAXN];
int father[MAXN][MAXLOG];
int n, timer;
void work(int pos, int fa) {
	depth[pos] = depth[fa] + 1;
	father[pos][0] = fa;
	for (int i = 1; i < MAXLOG; i++)
		father[pos][i] = father[father[pos][i - 1]][i - 1];
	num[pos] = ++timer;
	for (unsigned i = 0; i < a[pos].size(); i++)
		if (a[pos][i] != fa) work(a[pos][i], pos);
	rit[pos] = timer;
}
int lca(int x, int y) {
	if (depth[x] < depth[y]) swap(x, y);
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (depth[father[x][i]] >= depth[y]) x = father[x][i];
	if (x == y) return x;
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (father[x][i] != father[y][i]) {
			x = father[x][i];
			y = father[y][i];
		}
	return father[x][0];
}
int main() {
	scanf("%d", &n);
	BIT.init(n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &value[i]);
	for (int i = 1; i < n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	work(1, 0);
	for (int i = 1; i <= n; i++)
		BIT.modify(num[i], rit[i], value[i]);
	int q; scanf("%d", &q);
	while (q--) {
		char c;
		int x, y;
		scanf("\n%c%d%d", &c, &x, &y);
		if (c == 'C') {
			BIT.modify(num[x], rit[x], value[x] ^ y);
			value[x] = y;
		} else {
			int ans = BIT.query(num[x]) ^ BIT.query(num[y]);
			ans ^= value[lca(x, y)];
			if (ans) printf("Yes\n");
			else printf("No\n");
		}
	}
	return 0;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/79964370
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页

关闭
关闭
关闭