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
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
题意: :给定一棵树,树上每个节点都有一个权值,问两点之间路径上第K大值 。
分析 : 树上的第k大值,跟区间第k大有些不同,区间第k大每个值在前一个值的基础上新建一棵树,而树上第k大则是在父亲节点的基础上新建一棵树 ,子节点继承父节点。查询的时候,因为具有可加减性,答案就是root[v] + root[u] - root[lca(v, u)] - root[fa[lca(v,u)]]上的第k大
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)
const int MAXN = 1e5+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
int root[MAXN<<5],lson[MAXN<<5],rson[MAXN<<5],sum[MAXN<<5],sz;
void Build(int& rt,int le,int ri){
rt=++sz; sum[rt]=0;
if(le==ri) return ;
int mid=(le+ri)>>1;
Build(lson[rt],le,mid);
Build(rson[rt],mid+1,ri);
}
void Update(int pre,int& rt,int le,int ri,int val){
rt=++sz;
sum[rt]=sum[pre]+1,lson[rt]=lson[pre],rson[rt]=rson[pre];
if(le==ri) return ;
int mid =(le+ri)>>1;
if(val<=mid) Update(lson[pre],lson[rt],le,mid,val);
else Update(rson[pre],rson[rt],mid+1,ri,val);
}
int Query(int st,int ed,int lca,int falca,int le,int ri,int k){
if(le==ri) return le;
int mid=(le+ri)>>1;
int t=sum[lson[ed]]+sum[lson[st]]-sum[lson[lca]]-sum[lson[falca]]; // 具有加减性
if(t>=k) Query(lson[st],lson[ed],lson[lca],lson[falca],le,mid,k);
else Query(rson[st],rson[ed],rson[lca],rson[falca],mid+1,ri,k-t);
}
int n,m;
int arr[MAXN],X[MAXN],cnt;
vector<int>ve[MAXN];
int p[MAXN][25],dep[MAXN];
void dfs(int now,int pre,int de){
dep[now]=de; p[now][0]=pre;
Update(root[pre],root[now],1,cnt,arr[now]); // 子节点继承父节点
for(int i=0;i<ve[now].size();i++){
int v=ve[now][i];
if(v==pre) continue;
dfs(v,now,dep[now]+1);
}
}
void lca_init(){
for(int j=1;(1<<j)<=n;j++) {
for(int i=1;i<=n;i++)
p[i][j]=p[p[i][j-1]][j-1];
}
}
int LCA(int v,int u){
if(dep[v]<dep[u]) swap(v,u);
int d=dep[v]-dep[u];
for(int i=0;(d>>i)!=0;i++)
if((d>>i)&1) v = p[v][i];
if(v==u) return v;
for(int i=20;i>=0;i--)
if(p[v][i]!=p[u][i]) v=p[v][i],u=p[u][i];
return p[v][0];
}
int main(){
CLOSE();
// fread();
// fwrite();
while(scanf("%d%d",&n,&m)!=EOF){
cnt=0;
for(int i=1;i<=n;i++){
scanf("%d",&arr[i]);
X[cnt++]=arr[i];
}
sort(X,X+cnt); cnt=unique(X,X+cnt)-X;
for(int i=1;i<=n;i++) arr[i]=lower_bound(X,X+cnt,arr[i])-X+1; // 离散化
for(int i=1;i<=n;i++) ve[i].clear();
for(int i=1;i<n;i++){
int x,y;scanf("%d%d",&x,&y);
ve[x].push_back(y);
ve[y].push_back(x);
}
sz=0; Build(root[0],1,cnt); // 建空树
dfs(1,0,0); lca_init(); //lca
while(m--){
int x,y,k;scanf("%d%d%d",&x,&y,&k);
int lca=LCA(x,y);
int ans=Query(root[x],root[y],root[lca],root[p[lca][0]],1,cnt,k);
printf("%d\n",X[ans-1]);
}
}
return 0;
}