2588:spoj 10628. Count on a tree time limit:12 Sec Memory limit:128 MB
submit:5394 solved:1272
[Submit] [Status] [Discuss] 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
6 S
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
HINT
HINT:
n,m<=100000
Violent self-esteem ...
Source
Acknowledgement Seter
[Submit] [Status] [Discuss]
Tree of the Chairman tree
1#include <bits/stdc++.h>2 Const intSiz =200005, Tre =7000005;3 intN, M, Q, Val[siz], Map[siz], tot, Hd[siz], To[siz], Nt[siz], Edge, Root[siz], Ls[tre], Rs[tre], sm[tre], CNT, Vis[siz], FA [Siz] [ +], Dep[siz], Qa[siz], qb[siz], tail;4 voidInsertint&t,intFintLintRintp) {5t = ++cnt; LS[T] = Ls[f], rs[t] = Rs[f], sm[t] = sm[f] +1;6 if(l! = r) {intMid = (L + r) >>1; (P <= Mid Insert (Ls[t], ls[f], L, Mid, p): Insert (Rs[t], rs[f], Mid +1, R, p)); }7 }8 voidDfsintUintf) {Vis[u] =1; 9Dep[u] = Dep[f] +1, fa[u][0] =F;TenInsert (Root[u], root[f],1, Tot, val[u]); One for(inti =1; I <= -; ++i) Fa[u][i] = fa[fa[u][i-1]][i-1]; A for(inti = Hd[u]; I i = Nt[i])if(To[i]! =f) Dfs (to[i], u); - } -InlineintLcaintAintb) { the if(Dep[a] < dep[b]) a ^= b ^= a ^=b; - for(inti = -; ~i; I.)if(Dep[fa[a][i]] >= dep[b]) A =Fa[a][i]; - for(inti = -; ~i; I.)if(Fa[a][i] ^ fa[b][i]) A = Fa[a][i], B =Fa[b][i]; - returnA = = b? a:fa[a][0]; + } -InlinevoidAddintAintb) {Qa[tail] = Root[a], qb[tail++] =b;} + intQryintLintRintK) {if(L = = r)returnLints =0, Mid = (L + r) >>1; A for(inti =0; i < tail; ++i) s + = qb[i] *Sm[ls[qa[i]]; at for(inti =0; i < tail; ++i) Qa[i] = s < k?Rs[qa[i]]: ls[qa[i]; - returnS < k? Qry (Mid +1, R, K-s): Qry (L, Mid, k); - } -InlinevoidAdde (intXinty) { -Nt[++edge] = Hd[x], To[edge] = y, hd[x] =Edge, -Nt[++edge] = Hd[y], To[edge] = x, hd[y] =Edge; in } -Signed Main (void) { toscanf"%d%d", &n, &q); m = n-1; + for(inti =1; I <= N; ++i) scanf ("%d", Val + i), map[++tot] =Val[i]; -Std::sort (map +1, map + tot +1); tot = std::unique (map +1, map + tot +1) -map; the for(inti =1; I <= N; ++i) Val[i] = Std::lower_bound (map +1, map + tot, val[i])-map; * for(inti =1, X, y; I <= m; ++i) scanf ("%d%d", &x, &y), Adde (x, y); $ for(inti =1; I <= N; ++i)if(!vis[i]) DFS (i,0);Panax Notoginseng for(inti =1, ans =0, X, Y, K, t; I <= Q; ++i) { -scanf"%d%d%d", &x, &y, &k), x ^= ans, tail =0, theAdd (x,1), add (Y,1), add (t = LCA (x, y),-1), Add (fa[t][0], -1), printf ("%d", ans = map[qry (1, Tot, K)]);if(i! = q) puts (""); + } A}
@Author: Yousiki
Bzoj 2588:spoj 10628. Count on a tree