标签:hup main update nod ret ons else 输出 lan
如果有3个点,权值都为0,尽管异或为0,但0是出现了奇数次,应该输出0....
树剖模板
const int N = 100005;
int n, q, tot, cnt;
int head[N], pa[N], dep[N], sz[N], son[N], top[N], id[N], rk[N];
struct node { int to, next; } e[N * 2];
void Inite() {
cnt = tot = 0;
mem(head, -1);
}
void addedge(int u, int v) {
e[tot].to = v, e[tot].next = head[u], head[u] = tot++;
}
void DFS1(int u, int p, int deep) {
pa[u] = p;
sz[u] = 1;
dep[u] = deep;
for (int i = head[u]; ~i; i = e[i].next) if (e[i].to != p) {
int v = e[i].to;
DFS1(v, u, deep + 1);
sz[u] += sz[v];
if (son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v;
}
}
void DFS2(int u, int st) {
top[u] = st;
id[u] = ++cnt;
rk[cnt] = u;
if (son[u] == -1) return;
DFS2(son[u], st);
for (int i = head[u]; ~i; i = e[i].next) if (e[i].to != son[u] && e[i].to != pa[u]) DFS2(e[i].to, e[i].to);
}
int a[N], sum[4 * N];
void Pushup(int root) {
sum[root] = sum[lson] ^ sum[rson];
}
void Build(int l, int r, int root) {
if (l == r) {
sum[root] = a[rk[++cnt]] + 1;
return;
}
int mid = (l + r) >> 1;
Build(l, mid, lson);
Build(mid + 1, r, rson);
Pushup(root);
}
void Update(int l, int r, int root, int pos, int x) {
if (l == r) {
sum[root] = x;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) Update(l, mid, lson, pos, x);
else Update(mid + 1, r, rson, pos, x);
Pushup(root);
}
int Query(int l, int r, int root, int L, int R) {
if (l > R || r < L) return 0;
if (L <= l && r <= R) return sum[root];
int mid = (l + r) >> 1;
int ans = 0;
ans ^= Query(l, mid, lson, L, R);
ans ^= Query(mid + 1, r, rson, L, R);
return ans;
}
int check(int u, int v) {
int ans = 0;
int pu = top[u], pv = top[v];
while(pu != pv) {
if (dep[pu] > dep[pv]) {
ans ^= Query(1, n, 1, id[pu], id[u]);
u = pa[pu];
}
else {
ans ^= Query(1, n, 1, id[pv], id[v]);
v = pa[pv];
}
pu = top[u], pv = top[v];
}
if (id[u] <= id[v]) ans ^= Query(1, n, 1, id[u], id[v]);
else ans ^= Query(1, n, 1, id[v], id[u]);
return ans;
}
int main()
{
BEGIN() {
sc(n), sc(q);
Inite();
Rep(i, 2, n) {
int u, v;
sc(u), sc(v);
addedge(u, v);
addedge(v, u);
}
Rep(i, 1, n) sc(a[i]);
mem(son, -1);
DFS1(1, 1, 0);
DFS2(1, 1);
cnt = 0;
mem(sum, 0);
Build(1, n, 1);
while(q--) {
int op;
sc(op);
if (op == 0) {
int x, y;
sc(x), sc(y);
Update(1, n, 1, id[x], y + 1);
}
else {
int u, v;
sc(u), sc(v);
pr(check(u, v) - 1);
}
}
}
return 0;
}
标签:hup main update nod ret ons else 输出 lan
原文地址:https://www.cnblogs.com/zgglj-com/p/9822969.html