2019 icpc南昌邀请赛网络赛 J - Distance on the tree

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 uuand 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

题意就是 带权树上任意两点之间边权值不超过k的边个数

树上dfs建主席树,记录根结点到当前结点的边权,查询就是在root[u]和root[v]之间0~w的权值和 加起来,减去u和v的LCA的2倍   (这个其实类似求树上两点之间的距离,u的深度+v的深度-2*lca(u,v)的深度) 

#include 
using namespace std;
typedef long long ll;
const int maxn =1e5+10;
const int INF=0x3f3f3f3f;
int dep[maxn],Fa[maxn][22],root[maxn],sum[maxn*40],ls[maxn*40],rs[maxn*40],cnt,n,m;
struct  node
{
	int p,w;
};
std::vector G[maxn];
void update(int pre,int &o,int l,int r,int k)
{
	o=++cnt;ls[o]=ls[pre],rs[o]=rs[pre];sum[o]=sum[pre]+1;
	if(l==r) return ;
	int m=(1ll*l+r)>>1;
	if(k<=m) update(ls[pre],ls[o],l,m,k);
	else update(rs[pre],rs[o],m+1,r,k);
}
int query(int root,int l,int r,int k)
{
	if(l==r) return sum[root];
	int m=(1ll*l+r)>>1;
	if(k<=m) return query(ls[root],l,m,k);
	else return sum[ls[root]]+query(rs[root],m+1,r,k);
}

void dfs(int u,int fa)
{
	dep[u]=dep[fa]+1;
	Fa[u][0]=fa;
	for(int i=1;(1<=0;i--) if((1<=0;i--) if(Fa[x][i]!=Fa[y][i]) x=Fa[x][i],y=Fa[y][i];
    return Fa[x][0];
}
int main(int argc, char const *argv[])
{
	scanf("%d%d",&n,&m);
	for(int i=1;i

 

你可能感兴趣的:(LCA,线段树)