一开始一看树上的操作,就无脑写了树链剖分+主席树
然后果断T了,因为树链剖分+主席树必然带来两个log的复杂度
而且树链剖分复杂度还比较大……
后来发现其实没必要,在这道题,我们可以直接利用主席树维护
只不过,每个点维护的是它到它的祖先上数值出现的个数
则u,v之间各个数值出现的数值=tree[u]+tree[v]-tree[lca(u,v)]-tree[fa[lca(u,v)]];
这是一个满足区间减法的问题所以可以这么做
总结一下,在静态树上(相对于动态树而言,没有改变树的形态)
如果问题满足区间减法性质,如求k大,那就直接做,或者用dfs序(下篇再写)
如果不满足,那就要套树链剖分了
const maxn=;
type node=record
po,next:longint;
end;
point=record
l,r,s:longint;
end; var tree:array[..maxn*] of point;
w:array[..*maxn] of node;
h,p,c,b,a,q1,q2,rank,sa,fa,d:array[..maxn] of longint;
anc:array[..maxn,..] of longint;
j,t,z,e,i,n,m,k,x,y,len,ans,s:longint; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure update(i:longint);
begin
tree[i].s:=tree[tree[i].l].s+tree[tree[i].r].s;
end; procedure add(x,y:longint);
begin
inc(len);
w[len].po:=y;
w[len].next:=p[x];
p[x]:=len;
end; procedure sort(l,r: longint);
var i,j,x:longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div ];
repeat
while (a[i]<x) do inc(i);
while (x<a[j]) do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
swap(c[i],c[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; function build(l,r:longint):longint;
var q,m:longint;
begin
inc(t);
if l=r then exit(t)
else begin
q:=t;
m:=(l+r) shr ;
tree[q].l:=build(l,m);
tree[q].r:=build(m+,r);
exit(q);
end;
end; function add(last,l,r,x:longint):longint;
var q,m:longint;
begin
inc(t);
if l=r then
begin
tree[t].s:=tree[last].s+;
exit(t);
end
else begin
q:=t;
m:=(l+r) shr ;
if x<=m then
begin
tree[q].r:=tree[last].r;
last:=tree[last].l;
tree[q].l:=add(last,l,m,x);
end
else begin
tree[q].l:=tree[last].l;
last:=tree[last].r;
tree[q].r:=add(last,m+,r,x);
end;
update(q);
exit(q);
end;
end; function getans(l,r,k:longint):longint;
var i,m,s1:longint;
begin
if l=r then
exit(sa[l])
else begin
m:=(l+r) shr ;
s1:=tree[tree[x].l].s+tree[tree[y].l].s-tree[tree[z].l].s-tree[tree[e].l].s;
if s1>=k then
begin
x:=tree[x].l;
y:=tree[y].l;
z:=tree[z].l;
e:=tree[e].l;
exit(getans(l,m,k));
end
else begin
x:=tree[x].r;
y:=tree[y].r;
z:=tree[z].r;
e:=tree[e].r;
k:=k-s1;
exit(getans(m+,r,k));
end;
end;
end; function lca(x,y:longint):longint;
var i,p:longint;
begin
if d[x]<d[y] then swap(x,y);
if x=y then exit(x);
p:=trunc(ln(d[x])/ln());
for i:=p downto do
if d[x]- shl i>=d[y] then x:=anc[x,i];
if x=y then exit(x);
for i:=p downto do
if (anc[x,i]<>anc[y,i]) and (anc[x,i]<>) then
begin
x:=anc[x,i];
y:=anc[y,i];
end;
exit(fa[x]);
end; procedure dfs(x:longint);
var i,y:longint;
begin
h[x]:=add(h[fa[x]],,s,rank[x]);
i:=p[x];
while i<> do
begin
y:=w[i].po;
if fa[x]<>y then
begin
d[y]:=d[x]+;
fa[y]:=x;
dfs(y);
end;
i:=w[i].next;
end;
end; begin
readln(n,m);
for i:= to n do
begin
read(a[i]);
c[i]:=i;
end;
sort(,n);
s:=;
sa[]:=a[];
rank[c[]]:=;
for i:= to n do
begin
if a[i]<>a[i-] then
begin
inc(s);
sa[s]:=a[i];
end;
rank[c[i]]:=s;
end;
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
h[]:=build(,s);
dfs(); for i:= to n do
anc[i,]:=fa[i];
k:=trunc(ln(n)/ln());
for j:= to k do
for i:= to n do
begin
x:=anc[i,j-];
if x<> then anc[i,j]:=anc[x,j-];
end; ans:=;
for i:= to m do
begin
readln(x,y,k);
x:=x xor ans;
z:=lca(x,y);
e:=h[fa[z]];
z:=h[z];
x:=h[x];
y:=h[y];
ans:=getans(,s,k);
write(ans);
if i<>m then writeln;
end;
end.