Reprinted from: http://www.cnblogs.com/zxhl/p/7459240.html
Problem Description
Monkey a lives on a tree, he is always plays on the This tree.
One day, monkey A learned about one of the Bit-operations, XOR. He was keen of this interesting operation and wanted to practise it at once.
Monkey a gave a value to each node on the tree. And he is curious about a problem.
The problem is what large the XOR result of number x and one node value of label Y can be, when giving you a non-negative i Nteger x and a node label u indicates that node y are in the subtree whose root are u (Y can be equal to u).
Can you help him?
Input
There is no more than 6 test cases.
For all test case there is positive integers n and q indicate that the tree have n nodes and you need to answer Q qu Eries.
Then the lines follow.
The first line contains n non-negative integers v1,v2,⋯,vn, indicating the value of node I.
The second line contains n-1 non-negative integers f1,f2,⋯fn−1, Fi means the father of node i+1.
And then Q lines follow.
In the i-th line, there is, integers u and x, indicating that node you pick should is in the subtree of U, and x H As been described in the problem.
2≤n,q≤105
0≤vi≤109
1≤fi≤n, the root of the tree is node 1.
1≤u≤n,0≤x≤109
Output
For each query, just print an integer in a line indicating the largest result.
Sample Input
2 2 1 2 1 1 3 2 1
Sample Output
2 3
Problem:
Each number exists in the respective trie tree, n points This is a tree, and then from the bottom up the tri-Tree merged together
Query is to query a merged trie tree, using from high to low, greedy fetch
#include <bits/stdc++.h> inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;}
using namespace Std;
#define LL Long Long const int N = 2E5;
Vector<int > G[n];
int n, q, x, U, a[n];
int ch[n*45][2], root[n],sz;
void inserts (int u,int x) {Root[u] = ++sz;
int tmp = SZ;
int y = sz;
for (int i = +; I >= 0; i) {int tmps = (x>>i) &1;
if (!ch[y][tmps]) ch[y][tmps] = ++sz;
y = Ch[y][tmps];
}} int merges (int u,int to) {if (U = = 0) return to;
if (to = = 0) return u;
int t = ++sz;
Ch[t][0] = merges (ch[u][0],ch[to][0]);
CH[T][1] = merges (ch[u][1],ch[to][1]);
return t;
} void Dfs (int u) {inserts (U,a[u]);
for (auto To:g[u]) {DFS (to);
Root[u] = merges (Root[u],root[to]);
}} LL query (int u,int x) {int y = Root[u];
LL ret = 0; for (int i= 30; I >= 0;
-i) {int tmps = (x>>i) &1;
if (ch[y][tmps^1]) ret + = (1<<i), y = ch[y][tmps^1];
Else y = ch[y][tmps];
} return ret;
} void Init () {for (int i = 0; I <= N; ++i) Root[i] = 0,g[i].clear ();
SZ = 0;
memset (ch,0,sizeof (ch)); } int main (int argc, char * argv[]) {while (scanf ("%d%d", &n,&q)!=eof) {for (int i = 1; I <= N; + +)
i) scanf ("%d", &a[i]);
Init ();
for (int i = 2; I <= n; ++i) {scanf ("%d", &x);
G[x].push_back (i);
} dfs (1);
for (int i = 1; I <= Q; ++i) {scanf ("%d%d", &u,&x);
printf ("%lld\n", Query (u,x));
}} return 0; }