传送门:主席树
解题报告:
就非常板子昂$QwQ$
直接dfs,在dfs的过程中建树
然后就直接查询就好
其实我jio得就是个主席树板子题套在树上,,,?
然后具体处理也不难想?就考虑树上差分,形式大概就是tr[r]+tr[l]-tr[lca]-tr[lca.fa]
然后其他做法就都和主席树的板子一样辣辣辣!
快乐地发现有个双倍经验嘻嘻
#include<bits/stdc++.h>
using namespace std;
#define il inline
#define fr first
#define sc second
#define rg register
#define gc getchar()
#define mp make_pair
#define t(i) edge[i].to
#define rp(i,x,y) for(rg int i=x;i<=y;++i)
#define my(i,x,y) for(rg int i=x;i>=y;--i)
#define e(i,x) for(rg int i=head[x];i;i=edge[i].nxt) const int N=+;
int n,m,a[N],st[N],rt[N],cnt,n_lsh,head[N],ed_cnt,as,fa[N][],dep[N],poww[]={};
struct node{int ls,rs,l,r,val;}tr[N*];
struct ed{int to,nxt;}edge[N<<]; il int read()
{
rg char ch=gc;rg int x=;rg bool y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
il void ad(int x,int y){edge[++ed_cnt]=(ed){x,head[y]};head[y]=ed_cnt;}
il void build(int x,int l,int r)
{
++cnt;tr[x].l=l,tr[x].r=r;if(l==r)return;
int mid=(l+r)>>;tr[x].ls=cnt+;build(tr[x].ls,l,mid);tr[x].rs=cnt+;build(tr[x].rs,mid+,r);return;
}
il void updat(int x,int dat)
{
++cnt;tr[cnt]=tr[x];++tr[cnt].val;if(tr[cnt].l==tr[cnt].r)return;
int mid=(tr[cnt].l+tr[cnt].r)>>;
if(dat<=mid){tr[cnt].ls=cnt+;updat(tr[x].ls,dat);}else{tr[cnt].rs=cnt+;updat(tr[x].rs,dat);}
}
il int query(int x,int xx,int x1,int x2,int dat)
{
if(tr[x1].l==tr[x1].r)return tr[x1].l;int ret=tr[tr[x2].ls].val+tr[tr[x1].ls].val-tr[tr[x].ls].val-tr[tr[xx].ls].val;
if(ret<dat)return query(tr[x].rs,tr[xx].rs,tr[x1].rs,tr[x2].rs,dat-ret);return query(tr[x].ls,tr[xx].ls,tr[x1].ls,tr[x2].ls,dat);
}
il void dfs(int x,int fat){fa[x][]=fat;dep[x]=dep[fat]+;rp(i,,)fa[x][i]=fa[fa[x][i-]][i-];e(i,x)if(t(i)^fat)rt[t(i)]=cnt+,updat(rt[x],a[t(i)]),dfs(t(i),x);}
il int lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
my(i,,)if(dep[fa[x][i]]>=dep[y])x=fa[x][i];if(x==y)return x;
my(i,,)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
return fa[x][];
} int main()
{
n=read();m=read();rp(i,,n)a[i]=st[i]=read();sort(st+,st++n);n_lsh=unique(st+,st++n)-st-;rp(i,,n)a[i]=lower_bound(st+,st+n_lsh+,a[i])-st;
rt[]=;build(,,n_lsh);rt[]=cnt+;updat(,a[]);
rp(i,,n-){int x=read(),y=read();ad(x,y);ad(y,x);}dfs(,);
rp(i,,)poww[i]=poww[i-]<<;
rp(i,,m){int l=read()^as,r=read(),x=read(),lcaa=lca(l,r);printf("%d\n",as=st[query(rt[fa[lcaa][]],rt[lcaa],rt[l],rt[r],x)]);}
return ;
}
放下代码就好辣!