You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight.
We will ask you to perform the following operation:
u v k : ask for the kth minimum weight on the path from node u to node v
Input
In the first line there are two integers N and M.(N,M<=100000)
In the second line there are N integers.The ith integer denotes the weight of the ith node.
In the next N-1 lines,each line contains two integers u v,which describes an edge (u,v).
In the next M lines,each line contains three integers u v k,which means an operation asking for the kth minimum weight on the path from node u to node v.
Output
For each operation,print its result.
Example
Input:
8 5
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
2 5 2
2 5 3
2 5 4
7 8 2
Output:
2
8
9
105
7
题意:给你一棵由n个节点,n-1条边组成的树。问u,v之间第k小的是什么。
题解:第k小,首先想到主席树,我们建一棵由根开始的线段树。则u,v之间的关系就可以表示成 root[u]+root[v]-root[lca(u,v)]-root[fa[lca(u,v)]].因为需要知道lca,所以再套一个lca。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int cnt,u,v,k,tot,n,m,mx;
int dep[N],fa[N],siz[N],son[N];
int a[N],root[N];
struct node{int to,next;}edge[N<<2];
struct nodee{int l,r,sum;}T[N*40];
int head[N<<2];
vector<int>p;
int getid(int x){return lower_bound(p.begin(),p.end(),x)-p.begin()+1;}
void add(int u,int v)
{
edge[cnt].to=v,edge[cnt].next=head[u],head[u]=cnt++;
edge[cnt].to=u,edge[cnt].next=head[v],head[v]=cnt++;
}
void update(int l,int r,int &x,int y,int pos)
{
T[++cnt]=T[y],x=cnt,T[x].sum++;
if(l==r) return ;
int mid=(r+l)>>1;
if(pos<=mid)
update(l,mid,T[x].l,T[y].l,pos);
else
update(mid+1,r,T[x].r,T[y].r,pos);
}
int query(int l,int r,int x,int y,int z,int rr,int k)
{
if(l>=r) return l;
int mid=(r+l)>>1;
int sum=T[T[y].l].sum+T[T[x].l].sum-T[T[z].l].sum-T[T[rr].l].sum;
if(k<=sum) query(l,mid,T[x].l,T[y].l,T[z].l,T[rr].l,k);
else query(mid+1,r,T[x].r,T[y].r,T[z].r,T[rr].r,k-sum);
}
void dfs1(int u,int f,int d)
{
dep[u]=d,fa[u]=f,siz[u]=1,son[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(v==f)continue;
dfs1(v,u,d+1);
siz[u]+=siz[v];
if(siz[son[u]]<siz[v]) son[u]=v;
}
}
int top[N],pre[N],tree[N];
void dfs2(int u,int tp)
{
top[u]=tp,tree[u]=++tot,pre[tree[u]]=u;
update(1,mx,root[u],root[fa[u]],getid(a[u]));
if(!son[u]) return ;
dfs2(son[u],tp);
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa[u]||v==son[u]) continue;
dfs2(v,v);
}
}
int lca(int x,int y)
{
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(dep[fx]<dep[fy]) swap(x,y),swap(fx,fy);
x=fa[fx],fx=top[x];
}
if(dep[x]>dep[y]) swap(x,y);
return x;
}
void init()
{
tot=0;
cnt=0;
memset(head,-1,sizeof(head));
}
int main()
{
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
p.push_back(a[i]);
}
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
}
cnt=0;
sort(p.begin(),p.end()),p.erase(unique(p.begin(),p.end()),p.end());
mx=p.size();
dfs1(1,0,1),dfs2(1,1);
while(m--)
{
scanf("%d%d%d",&u,&v,&k);
int lc=lca(u,v);
printf("%d\n",p[query(1,mx,root[u],root[v],root[lc],root[fa[lc]],k)-1]);
}
}