可持久化trie可行吗?
RT
我dfs序+可持久化trie写炸了,但我想应该可以这么做。
写炸的代码
#include <bits/stdc++.h> #define N 100005 using namespace std; int n, a[N]; int head[N], to[N], nxt[N], top = 1; int trie[N * 17][2], ed[N * 17], sum[N * 17], root[N], cnt = 2; int L[N], R[N], maxx[N], tim; void inedge(int u, int v) { to[top] = v; nxt[top] = head[u]; head[u] = top++; } int intrie(int x, int id) { int now = root[id] = ++cnt; int pre = root[id - 1]; for(int i = 17;~i;i--) { int ch = (x & (1 << i)) ? 1 : 0; trie[now][0] = trie[pre][0]; trie[now][1] = trie[pre][1]; sum[now] = sum[pre] + 1; trie[now][ch] = cnt++; now = trie[now][ch]; pre = trie[pre][ch]; } ed[now] = id; } int find(int pt, int nt, int v) { for(int i = 17;~i;i--) { int ch = (v & (1 << i)) ? 1 : 0; if(sum[trie[nt][ch ^ 1]] - sum[trie[pt][ch ^ 1]]) { nt = trie[nt][ch ^ 1]; pt = trie[pt][ch ^ 1]; } else { nt = trie[nt][ch]; pt = trie[pt][ch]; } } return ed[nt]; } int dfs(int x) { int ls; intrie(maxx[x] = a[x], L[x] = ++tim); for(int i = head[x];i;i = nxt[i]) if((ls = dfs(to[i])) > maxx[x]) maxx[x] = ls; R[x] = tim; return maxx[x]; } void init() { scanf("%d", &n); int u, v; for(int i = 1;i < n;i++) { scanf("%d%d", &u, &v); inedge(u, v); } for(int i = 1;i <= n;i++) scanf("%d", a + i); dfs(1); } void work() { int m, p; scanf("%d", &m); while(m--) { scanf("%d", &p); int ans = maxx[p], pre = 0; while(ans > pre) { pre = ans; ans ^= a[find(root[L[p] - 1], root[R[p]], pre)]; } printf("%d\n", pre); } } int main() { init(); work(); return 0; }
爆0可还行,我要死了QAQ