Description given a tree of n nodes, each point has a weight, for M queries (U,V,K), you need to answer U xor Lastans and v these two nodes K small point right. Where Lastans is the answer to the previous query, starting with 0, that is, the first query of U is clear text.
Input the first row of two integer n,m. The second line has n integers, where the I integer represents the weight of the point I. The following N-1 line has two integers (x, y) per line, indicating that the point x points y has an edge. The last m row of two integers per line (u,v,k) represents a set of queries. The Output m line, which represents the answer to each query. Sample Input8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output2
8
9
105
7
hintn,m<=100000
Violent self-esteem ... Analysis: Can be persisted line tree, the Father version of the foundation to create a son version, combined with LCA can be. (The last one to give me the format error QAQ) code:
1#include <cstdio>2#include <algorithm>3 4 Const intMAXN =500000;5 Const intMAXM =2000000;6 7 intET[MAXN], EP[MAXN], last[maxn], en;8 intch[maxm][2], COUNT[MAXM];9 intROOT[MAXM], size;Ten intN, M, U, V, W, LCA, ans; One intDEEP[MAXN], fa[maxn][ -]; A intNUM[MAXN], ID[MAXN], BACK[MAXN]; - - voidINS (intAintb) the { -en++; -Ep[en] =Last[a]; -Last[a] =en; +Et[en] =b; - } + A #defineMID (left + (right-left >> 1)) at - - intModify (intLeftintRightintPosintprev) - { - inti = + +size; - if(Left <Right ) in { - intc = pos >MID; toCH[I][!C] = ch[prev][!c]; +C? left = MID +1: right =MID; -CH[I][C] =Modify (left, right, POS, ch[prev][c]); theCount[i] = count[ch[i][0]] + count[ch[i][1]]; *}ElseCount[i] =1; $ returni;Panax Notoginseng } - the #defineLeftsize (Count[ch[l1][0]] [count[ch[l2][0]]-count[ch[e1][0]]-count[ch[e2][0]) + A intQuery (intLeftintRightintE1,intE2,intL1,intL2,intk) the { + if(left = right)returnNum[id[left]]; - intc = k >leftsize; $C? left = MID +1: right =MID; $ returnQuery (left, right, Ch[e1][c], ch[e2][c], ch[l1][c], ch[l2][c], c? K-leftsize:k); - } - the BOOLCMP (intAintb) - {Wuyi returnNum[a] <Num[b]; the } - Wu voidDFS (intIintp) - { AboutDeep[i] = Deep[p] +1; $fa[i][0] =p; - for(intb =0; FA[I][B]; b++) -Fa[i][b +1] =Fa[fa[i][b]][b]; -Root[i] = Modify (1, N, Back[i], root[p]); A for(inte = Last[i]; E E =Ep[e]) + if(Deep[et[e]] = =0) DFS (et[e], i); the } - $ intFind (intIintj) the { the intK, B; the if(Deep[i] >Deep[j]) theI ^= J, J ^= I, I ^=J; - for(k = deep[j]-Deep[i], B =0; K K >>=1, b++) inK &1? j = Fa[j][b]:1; the if(i = = j)returni; the for(b =0; FA[I][B]! = Fa[j][b]; b++); About for(b--; ~b; b--)if(fa[i][b]! = fa[j][b]) i = fa[i][b], j =Fa[j][b]; the returnfa[i][0]; the } the + intMain () - { thescanf ("%d%d", &n, &m);Bayi for(inti =1; I <= N; i++) thescanf ("%d", &num[i]), id[i] =i; theStd::sort (ID +1, ID + n +1, CMP); - for(inti =1; I <= N; i++) -Back[id[i]] =i; the for(inti =1; I < n; i++) the { thescanf ("%d%d", &u, &v); the intFlag = last[id[1]] >0; - ins (U, v); Ins (v, u); the } theDFS (1,0); theAns =0;94 for(inti =0; I < m; i++) the { thescanf (" %d%d%d", &u, &v, &W); theU ^=ans;98LCA =Find (U, v); AboutAns = Query (1, N, root[fa[lca][0] ], Root[lca], Root[u], Root[v], W); - if(i) printf ("\ n");101printf ("%d", ans);102 }103}
"Bzoj 2588" Count on a tree