1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| ##include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef long double ld; typedef pair<int, int> pdd;
##define INF 0x3f3f3f3f ##define lowbit(x) x & (-x) ##define mem(a, b) memset(a , b , sizeof(a)) ##define FOR(i, x, n) for(int i = x;i <= n; i++)
const int maxn = 1e5 + 10;
struct Edge { int v, next; }e[maxn << 1]; int head[maxn], cnt;
inline void add(int u, int v) { e[++cnt].v = v; e[cnt].next = head[u]; head[u] = cnt; }
int fa[maxn], dep[maxn], siz[maxn], son[maxn];
void dfs1(int u, int par) { dep[u] = dep[fa[u] = par] + (siz[u] = 1); for(int i = head[u]; i ; i = e[i].next) { int v = e[i].v; if(v == par) continue; dfs1(v, u); siz[u] += siz[v]; if(!son[u] siz[v] > siz[son[u]]) { son[u] = v; } } }
int tim, dfn[maxn], top[maxn], nodeof[maxn];
void dfs2(int u, int topf) { nodeof[dfn[u] = ++tim] = u; top[u] = topf; if(!son[u]) return ; dfs2(son[u], topf); for(int i = head[u]; i; i = e[i].next) { int v = e[i].v; if(v == son[u] v == fa[u]) continue; dfs2(v, v); } }
int w[maxn], n;
##define lowbit(x) x & (-x)
int t[maxn];
void modify(int x, int val) { while(x <= n) { t[x] ^= val; x += lowbit(x); } }
int query(int x) { int ans = 0; while(x) { ans ^= t[x]; x -= lowbit(x); } return ans; }
int query_chain(int x, int y) { int ans = 0; while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); ans ^= (query(dfn[top[x]] - 1) ^ query(dfn[x])); x = fa[top[x]]; } if(dep[x] > dep[y]) swap(x, y); ans ^= (query(x - 1) ^ query(y)); return ans; }
void solve() { int m; cin >> n >> m; for(int i = 1;i <= n - 1; i++) { int u, v; cin >> u >> v; add(u, v); add(v, u); } dfs1(1, 0); dfs2(1, 1); for(int i = 1;i <= n; i++) { cin >> w[i]; w[i]++; modify(dfn[i], w[i]); } while(m--) { int opt, x, y; cin >> opt >> x >> y; if(opt == 0) { modify(dfn[x], w[x]); modify(dfn[x], w[x] = y + 1); } else { cout << query_chain(x, y) - 1 << endl; } } }
int main() { solve(); }
|