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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <iostream> #include <stdio.h> #include <stdio.h> using namespace std;
const int maxn = 600020; const int K = 31; int fa[maxn][20],deep[maxn],vis[maxn]; int root_arr[maxn],root_tree[maxn]; struct node{ int lc,rc,cnt,val; }T[maxn * 32]; struct _edge{ int to,next; }edge[maxn]; int root[maxn],head[maxn],tot,cnt,m,n; void addedge(int u,int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt ++; } void init(int n){ cnt = 0; for(int i = 0;i<=n;i++){ head[i] = -1; } } int dsz; int in[maxn],out[maxn],id[maxn],v[maxn]; int xxor[maxn]; int lca(int u,int v) { if(deep[u] < deep[v])swap(u,v); int dis = deep[u] - deep[v]; for(int i = 0;i<20;i++){ if((1<<i)&dis)u = fa[u][i]; } if(u == v)return u; for(int i = 19;i>=0;i--){ if(fa[u][i] != fa[v][i]){ u = fa[u][i]; v = fa[v][i]; } } return fa[u][0]; } void update(int y,int &x,int val,int k) { x = ++tot;T[x] = T[y];T[x].cnt ++; if(k < 0){ T[x].val = val; return; } int c = (val >> k) & 1; if(!c){ update(T[y].lc,T[x].lc,val,k-1); }else{ update(T[y].rc,T[x].rc,val,k-1); } } void dfs(int now) { vis[now] = true; in[now] = ++dsz; id[dsz] = now; for(int i = 1;i<20;i++){ if(deep[now] < (1<<i))break; fa[now][i] = fa[fa[now][i-1]][i-1]; } for(int i = head[now];~i;i=edge[i].next){ int to = edge[i].to; if(!vis[to]){ fa[to][0] = now; deep[to] = deep[now] + 1; update(root_tree[now],root_tree[to],v[to],K); dfs(to); } } out[now] = dsz; } int query_arr(int y,int x,int val,int k,int ret=0) { if(k < 0)return T[x].val ^ val; int c = (val >> k) & 1; if(c){ int cnt = T[T[x].lc].cnt - T[T[y].lc].cnt; if(cnt > 0)return query_arr(T[y].lc,T[x].lc,val,k-1,ret+(1<<k)); else return query_arr(T[y].rc,T[x].rc,val,k-1,ret); }else{ int cnt = T[T[x].rc].cnt - T[T[y].rc].cnt; if(cnt > 0)return query_arr(T[y].rc,T[x].rc,val,k-1,ret+(1<<k)); else return query_arr(T[y].lc,T[x].lc,val,k-1,ret); } } int query_path(int y,int x,int f,int ff,int val,int k,int ret=0) { if(k < 0){return ret;} int c = (val >> k) & 1; if(c){ int cnt = T[T[x].lc].cnt + T[T[y].lc].cnt - T[T[f].lc].cnt - T[T[ff].lc].cnt; if(cnt > 0)return query_path(T[y].lc,T[x].lc,T[f].lc,T[ff].lc,val,k-1,ret+(1<<k)); else return query_path(T[y].rc,T[x].rc,T[f].rc,T[ff].rc,val,k-1,ret); }else{ int cnt = T[T[x].rc].cnt + T[T[y].rc].cnt - T[T[f].rc].cnt - T[T[ff].rc].cnt; if(cnt > 0)return query_path(T[y].rc,T[x].rc,T[f].rc,T[ff].rc,val,k-1,ret+(1<<k)); else return query_path(T[y].lc,T[x].lc,T[f].lc,T[ff].lc,val,k-1,ret); } } void build(int &x,int k) { cout << k << endl; x = ++tot;T[x].cnt = 0; if(k<0)return; build(T[x].lc,k-1); build(T[x].rc,k-1); } int main() { scanf("%d%d",&n,&m); init(n); for(int i = 1;i<=n;i++)scanf("%d",&v[i]); for(int i = 0;i<n-1;i++){ int xx,yy;scanf("%d%d",&xx,&yy); addedge(xx,yy);addedge(yy,xx); } update(root_tree[0],root_tree[1],v[1],K); dfs(1); for(int i = 1;i<=dsz;i++){ xxor[i] = v[id[i]]; update(root_arr[i-1],root_arr[i],xxor[i],K); } int op,qx,qy,qz; while(m--){ scanf("%d",&op); if(op == 1){ scanf("%d%d",&qx,&qy); printf("%d\n",query_arr(root_arr[in[qx] - 1] , root_arr[out[qx]],qy,K)); }else{ scanf("%d%d%d",&qx,&qy,&qz); int fx = lca(qx,qy); int ffx = fa[fx][0]; printf("%d\n",query_path(root_tree[qx],root_tree[qy],root_tree[fx],root_tree[ffx],qz,K)); } } }
|