就是主席树维护树上的链,主席树部分是从过了的主席树板子那边复制过来的,RE是SIGGEV,应该是段错误
求助啊呜呜呜
#include<bits/stdc++.h>
using namespace std;
inline int read(){
register bool f=true;int x=0;
register char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=false;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
if(f) return x;
return ~(--x);
}
int n,m,a[100005],uni[100005],N,tot,cts[100005];
int dep[100005],fa[100005][50];
vector<int>to[100005];
struct Seg{
int v,ls,rs;
#define ls(o) c[o].ls
#define rs(o) c[o].rs
#define mid ((l+r)>>1)
#define v(o) c[o].v
}c[20000005];
inline void build(int o,int l,int r){
if(l>=r)
return;
ls(o)=++tot;
build(tot,l,mid);
rs(o)=++tot;
build(tot,mid+1,r);
}
inline void update(int o,int ox,int l,int r,int x,int v){
if(l==x&&r==x){
v(o)=v(ox)+v;
return;
}
if(x<=mid){
ls(o)=++tot;
rs(o)=rs(ox);
update(ls(o),ls(ox),l,mid,x,v);
}else {
ls(o)=ls(ox);
rs(o)=++tot;
update(rs(o),rs(ox),mid+1,r,x,v);
}
v(o)=v(ls(o))+v(rs(o));
}
#define Schwarzkopf_Henkal (v(ls(o1))+v(ls(o2))-v(ls(o3))-v(ls(o4)))
inline int query(int o1,int o2,int o3,int o4,int l,int r,int x){
if(l>=r)
return uni[l];
if(Schwarzkopf_Henkal>=x){
return query(ls(o1),ls(o2),ls(o3),ls(o4),l,mid,x);
}else return query(rs(o1),rs(o2),rs(o3),rs(o4),mid+1,r,x-Schwarzkopf_Henkal);
}
void dfs(int p,int f){
fa[p][0]=f;
for(int i=1;fa[fa[p][i-1]][i-1];i++)
fa[p][i]=fa[fa[p][i-1]][i-1];
cts[p]=++tot;
a[p]=lower_bound(uni+1,uni+N+1,a[p])-uni;
update(cts[p],cts[f],1,N,a[p],1);
for(int i=0;i<to[p].size();i++)
if(to[p][i]!=f){
dep[to[p][i]]=dep[p]+1;
dfs(to[p][i],p);
}
}
inline int LCA(int u,int v){
if(dep[u]<dep[v])
swap(u,v);
for(int i=20;i>=0;i--)
if((1<<i)<=dep[u]-dep[v])
u=fa[u][i];
if(u==v)
return u;
for(int i=20;i>=0;i--)
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];
v=fa[v][i];
}
return fa[u][0];
}
int main(){
n=read();
m=read();
for(int i=1;i<=n;i++){
a[i]=read();
uni[i]=a[i];
}
for(int i=1,u,v;i<n;i++){
u=read();
v=read();
to[u].push_back(v);
to[v].push_back(u);
}
sort(uni+1,uni+n+1);
N=unique(uni+1,uni+n+1)-uni;
cts[0]=++tot;
build(tot,1,N);
dfs(1,0);
while(m--){
int u,V,k,asc;
u=read();
V=read();
k=read();
asc=LCA(u,V);
printf("%d\n",query(cts[u],cts[V],cts[asc],cts[fa[asc][0]],1,N,k));
}
}