题目链接

https://nanti.jisuanke.com/t/38229

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 11to 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≤105,1≤m≤105)

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≤109)

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≤109)

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

题意:给一个树,以及各边的权值。问结点u,v路径中权值小于k的个数有多少。

跟第k大差不多吧,第k大的查询里面改一下就是小于k的个数

上篇文章讲到树上的点权第k大,这篇就讲边权的第k大,其实,操作都差不多。

答案就是 root[v] + root[u] - 2*root[lca(v, u)]

两种AC代码:个人感觉第一种更快

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int inf=1e9+10;
vector<int>G[N];
int cnt;
int a[N],b[N],sz;
int ls[N*40],rs[40*N],sum[40*N],rt[N],dep[N],fa[N][30];
int n,m; 
int getid(int x)
{
	return lower_bound(b+1,b+1+sz,x)-b;
}
void up(int pre,int &o,int l,int r,int val)
{
	o=++cnt;
	ls[o]=ls[pre];
	rs[o]=rs[pre];
	sum[o]=sum[pre]+1;
	if(l==r) return ;
	int mid=l+r>>1;
	if(val<=mid) up(ls[pre],ls[o],l,mid,val);
	else up(rs[pre],rs[o],mid+1,r,val);
}
int qu(int pre,int o,int lca,int falca,int l,int r,int k)
{
	if(l==r) return l;
	int mid=l+r>>1;
	int cmp=sum[ls[pre]]+sum[ls[o]]-sum[ls[lca]]-sum[ls[falca]];
	if(cmp>=k) return qu(ls[pre],ls[o],ls[lca],ls[falca],l,mid,k);
	else return qu(rs[pre],rs[o],rs[lca],rs[falca],mid+1,r,k-cmp); 
}
void dfs(int pre,int u,int f,int d)
{
	fa[u][0]=f;
	dep[u]=d;
	up(pre,rt[u],1,sz,getid(a[u]));
	for(int i=0;i<G[u].size();i++)
	{
		int v=G[u][i];
		if(v==f) continue;
		dfs(rt[u],v,u,d+1);
	}
}
void init()
{
	dfs(0,1,0,1);
	for(int k=1;k<=23;k++)
	for(int i=1;i<=n;i++)
	fa[i][k]=fa[fa[i][k-1]][k-1];
}
int lca(int u,int v)
{
	if(dep[u]>dep[v]) swap(u,v);
	for(int k=20;k>=0;k--)
	if(dep[v]-dep[u]>=(1<<k))
	v=fa[v][k];
	if(u==v) return u;
	for(int k=20;k>=0;k--)
	if(fa[u][k]!=fa[v][k])
	u=fa[u][k],v=fa[v][k];
	return fa[u][0];
}
int main()
{
	int u,v,k;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) 
	{
		scanf("%d",&a[i]);
		b[i]=a[i];
	}
	sort(b+1,b+1+n);
	sz=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	init();
	int ans=0;
	while(m--)
	{
		scanf("%d%d%d",&u,&v,&k);
		u=u^ans;
		int comlca=lca(u,v);
		ans=b[qu(rt[u],rt[v],rt[comlca],rt[fa[comlca][0]],1,sz,k)];
		printf("%d\n",ans);
	}
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
const ll inf=1e9+10;
vector<int>G[N],dis[N];
int cnt;
int a[N],b[N],sz;
int ls[N*40],rs[40*N],sum[40*N],rt[N],dep[N],fa[N][30];
int n,m; 

void up(int pre,int &o,ll l,ll r,ll val)
{
	o=++cnt;
	ls[o]=ls[pre];
	rs[o]=rs[pre];
	sum[o]=sum[pre]+1;
	if(l==r) return ;
	ll mid=(l+r)>>1;
	if(val<=mid) up(ls[pre],ls[o],l,mid,val);
	else up(rs[pre],rs[o],mid+1,r,val);
}
int qu(int o,ll l,ll r,ll k)
{
	if(l==r) return sum[o];
	ll mid=(l+r)/2;
	if(mid>=k) return qu(ls[o],l,mid,k);
	else return sum[ls[o]]+qu(rs[o],mid+1,r,k); 
}
void dfs(int u,int f,int d)
{
	fa[u][0]=f;
	dep[u]=d;
	for(int i=0;i<G[u].size();i++)
	{
		int v=G[u][i];
		if(v==f) continue;
		up(rt[u],rt[v],0,inf,dis[u][i]);
		dfs(v,u,d+1);
	}
}
void init()
{
	dfs(1,0,1);
	for(int k=1;k<=20;k++)
	for(int i=1;i<=n;i++)
	fa[i][k]=fa[fa[i][k-1]][k-1];
}
int lca(int u,int v)
{
	if(dep[u]>dep[v]) swap(u,v);
	for(int k=20;k>=0;k--)
	if(dep[v]-dep[u]>=(1<<k))
	v=fa[v][k];
	if(u==v) return u;
	for(int k=20;k>=0;k--)
	if(fa[u][k]!=fa[v][k])
	u=fa[u][k],v=fa[v][k];
	return fa[u][0];
}
int main()
{
	int u,v,w,ans;
	ll k;
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++) 
	{
		scanf("%d%d%d",&u,&v,&w);
		G[u].push_back(v);
		G[v].push_back(u);
		dis[u].push_back(w);
		dis[v].push_back(w);
	}
	init();
	while(m--)
	{
		scanf("%d%d%lld",&u,&v,&k);
		int comlca=lca(u,v);
		//ans=qu(rt[u],rt[v],rt[comlca],0,inf,k)
		ans=qu(rt[u],0,inf,k)+qu(rt[v],0,inf,k)-qu(rt[comlca],0,inf,k)*2;
		printf("%d\n",ans);
	}
}