DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.

The experienced and knowledgeable teacher had known about him even before the first class. However, she didn’t wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.

This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n-1n−1 edges…" DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. “” A tree with nn nodes, which is numbered from 11 to nn. Edge between each two adjacent vertexes uu and vv has a value w, you’re asked to answer the number of edge whose value is no more than kk during the path between uu and vv."" If you can’t solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

The problem seems quite easy for DSM. However, it can hardly be solved in a break. It’s such a disgrace if DSM can’t solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?

Input
In the first line there are two integers n,mn,m, represent the number of vertexes on the tree and queries(2 \le n \le 10^5,1 \le m \le 10^52≤n≤10
5
,1≤m≤10
5
)

The next n-1n−1 lines, each line contains three integers u,v,wu,v,w, indicates there is an undirected edge between nodes uu and vv with value ww. (1 \le u,v \le n,1 \le w \le 10^91≤u,v≤n,1≤w≤10
9
)

The next mm lines, each line contains three integers u,v,ku,v,k , be consistent with the problem given by the teacher above. (1 \le u,v \le n,0 \le k \le 10^9)(1≤u,v≤n,0≤k≤10
9
)

Output
For each query, just print a single line contains the number of edges which meet the condition.

样例输入1复制
3 3
1 3 2
2 3 7
1 3 0
1 2 4
1 2 7
样例输出1复制
0
1
2
样例输入2复制
5 2
1 2 1000000000
1 3 1000000000
2 4 1000000000
3 5 1000000000
2 3 1000000000
4 5 1000000000
样例输出2复制
2
4


lca树上差分+主席树。

题目大意:求两个点之间的路径上,有多少边的权值小于给定的k。

树上边差分,我们需要把边的权值赋值给边下面的点。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,m,q,rt[N],cnt,a[N],b[N],f[N][20],h[N],u[N],v[N],lg[N];
int head[N],nex[N<<1],to[N<<1],w[N<<1],tot;
struct node{int l,r,sum;}t[N*20];
inline void add(int a,int b,int c){
	to[++tot]=b; nex[tot]=head[a]; w[tot]=c; head[a]=tot;
}
void change(int l,int r,int &x,int y,int k){
	x=++cnt; t[x]=t[y]; t[x].sum++;
	if(l==r)	return ; int mid=l+r>>1;
	if(k<=mid)	change(l,mid,t[x].l,t[y].l,k);
	else	change(mid+1,r,t[x].r,t[y].r,k);	
}
int ask(int l,int r,int s1,int s2,int s3,int k){
	if(l==r)	return t[s1].sum+t[s2].sum-2*t[s3].sum;
	int mid=l+r>>1; int s=t[t[s1].l].sum+t[t[s2].l].sum-2*t[t[s3].l].sum;
	if(k<=mid)	return ask(l,mid,t[s1].l,t[s2].l,t[s3].l,k);
	else	return s+ask(mid+1,r,t[s1].r,t[s2].r,t[s3].r,k);
}
void dfs(int x,int fa){
	h[x]=h[fa]+1;	f[x][0]=fa;
	for(int i=1;(1<<i)<=h[x];i++)	f[x][i]=f[f[x][i-1]][i-1];
	for(int i=head[x];i;i=nex[i]){
		if(to[i]==fa)	continue;
		change(1,m,rt[to[i]],rt[x],w[i]);	dfs(to[i],x);
	}
}
inline int lca(int x,int y){
	if(h[x]<h[y])	swap(x,y);
	while(h[x]>h[y])	x=f[x][lg[h[x]-h[y]]-1];
	if(x==y)	return x;
	for(int i=lg[h[x]]-1;i>=0;i--)
		if(f[x][i]!=f[y][i])
			x=f[x][i],y=f[y][i];
	return f[x][0];
}
signed main(){
	cin>>n>>q;
	for(int i=1;i<=n;i++)	lg[i]=lg[i-1]+(1<<lg[i-1]==i);
	for(int i=1;i<n;i++)	scanf("%d %d %d",&u[i],&v[i],&a[i]),b[i]=a[i];
	sort(b+1,b+1+n);	m=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<n;i++)	a[i]=lower_bound(b+1,b+1+m,a[i])-b;
	for(int i=1;i<n;i++)	add(u[i],v[i],a[i]),add(v[i],u[i],a[i]);
	dfs(1,0);
	while(q--){
		int u,v,k;	scanf("%d %d %d",&u,&v,&k);	int fa=lca(u,v); int t=k;
		k=lower_bound(b+1,b+1+m,k)-b;	if(b[k]!=t)	k--;
		printf("%d\n",ask(1,m,rt[u],rt[v],rt[fa],k));	
	}
	return 0;
}