HZHWCMHF said, the title is the chairman of the tree, and then I said that according to the tree chain split, was not dropped, and later found that is the DFS sequence, although there is no big difference, but think carefully, the tree is completely redundant. I was too weak in the past.
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <
Cstring> #define INF 0x7fffffff #define LL Long long #define N 100005 #define M 2000005 using namespace std;
inline ll read () {ll x=0,f=1;char Ch=getchar (); while (ch> ' 9 ' | |
ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;
} int n,m,tot,siz,cnt,ind,last;
int num[n],pos[n];
int v[n],tmp[n],hash[n],root[n];
int ls[m],rs[m],sum[m];
int deep[n],fa[n][17]; struct Data{int to,next;}
E[200005];int Head[n]; void ins (int u,int v) {e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;} void Insert (int u,int v) {ins (u,v); ins (v,u);} int
Find (int x) {int l=1,r=tot;
while (l<=r) {int mid= (L+R) >>1;
if (hash[mid]<x) l=mid+1;
else if (hash[mid]==x) return mid;
else r=mid-1;
}} void Dfs (int x) {ind++;num[ind]=x;pos[x]=ind; for (int i=1;i<=16;i++) if ((1<<i) <=deep[x]) fa[x][i]=fa[fa[x][i-1]][i-1];
else break;
for (int i=head[x];i;i=e[i].next) if (fa[x][0]!=e[i].to) {deep[e[i].to]=deep[x]+1;
Fa[e[i].to][0]=x;
DFS (E[I].TO);
}} int LCA (int x,int y) {if (Deep[x]<deep[y]) swap (x, y);
int t=deep[x]-deep[y];
for (int i=0;i<=16;i++) if ((1<<i) &t) x=fa[x][i];
for (int i=16;i>=0;i--) if (Fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
if (x==y) return x;
return fa[x][0];
} void Update (int l,int r,int x,int &y,int val) {siz++;
Y=siz;
sum[y]=sum[x]+1;
if (l==r) return;
LS[Y]=LS[X];RS[Y]=RS[X];
int mid= (L+R) >>1;
if (val<=mid) update (l,mid,ls[x],ls[y],val);
else update (mid+1,r,rs[x],rs[y],val);
} int que (int x,int y,int k) {int A=x,b=y,c=lca (x, y), d=fa[c][0];
A=ROOT[POS[A]],B=ROOT[POS[B]],C=ROOT[POS[C]],D=ROOT[POS[D]];
int L=1,r=tot; while (l<R) {int mid= (L+R) >>1;
int Tmp=sum[ls[a]]+sum[ls[b]]-sum[ls[c]]-sum[ls[d]];
if (tmp>=k) {r=mid;
A=LS[A];B=LS[B];C=LS[C];d =ls[d];
} else {k-=tmp;
l=mid+1;
A=RS[A];B=RS[B];C=RS[C];d =rs[d];
}} return hash[l];
} int main () {N=read (), M=read ();
for (int i=1;i<=n;i++) V[i]=read (), tmp[i]=v[i];
Sort (tmp+1,tmp+n+1);
HASH[++TOT]=TMP[1];
for (int i=2;i<=n;i++) if (tmp[i]!=tmp[i-1]) hash[++tot]=tmp[i];
for (int i=1;i<=n;i++) V[i]=find (V[i]);
for (int i=1;i<n;i++) {int u=read (), V=read ();
Insert (U,V);
} dfs (1);
for (int i=1;i<=n;i++) {update (1,tot,root[pos[fa[num[i]][0]]],root[i],v[num[i]]);
} for (int i=1;i<=m;i++) {int x=read (), Y=read (), Rk=read ();
X^=last;
Last=que (X,Y,RK);
printf ("%d", last); if (i!=m) printf ("\ n");
} return 0; }