Descriptions:

2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataDSM(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.

2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataThe 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.

2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataThis 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 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_04 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 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_05 to nn. Edge between each two adjacent vertexes 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data_06 and 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#include_07 has a value 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_08, you’re asked to answer the number of edge whose value is no more than 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_09 during the path between 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data_06 and 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#include_07."" If you can’t solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataThe 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

2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataIn the first line there are two integers 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data_14, represent the number of vertexes on the tree and queries2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_15
2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataThe next 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_04 lines, each line contains three integers 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data_06,2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#include_07,2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_08 indicates there is an undirected edge between nodes 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data_06 and 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#include_07 with value 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_08. 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data_24

2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataThe next mm lines, each line contains three integers 2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_26, be consistent with the problem given by the teacher above.2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data_27

Output

2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_DataFor 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
2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_Data题意就是2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_30个点,2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_04条边,然后每条边都有自己的权值,2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_32个询问,每个询问就是从一个点到另外一个点的路径上有多少满足权值不大于2019ICPC南昌邀请赛J题 Distance on the tree(主席树)_#define_09的边有多少条。
这个就是HDU4417的进阶版,加了一个LCA函数。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}

int qpow(int m, int k, int mod)
{
int res = 1, t = m;
while (k)
{
if (k&1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}

ll gcd(ll a,ll b)
{
return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}

const int maxn = 1e5+7;
struct node
{
int l;
int r;
int sum;
} T[maxn*40];
struct edge
{
int to;
int w;
};
int n,m,cnt,root[maxn],x,y,w,k,lg[maxn],fa[maxn][21],dep[maxn],vis[maxn];
vector<int> v;
vector<edge> G[maxn];
int getid(int x)
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
void update(int l,int r,int &now,int pre,int pos)
{
T[++cnt]=T[pre];
T[cnt].sum++;
now=cnt;
if(l==r)
return;
int mid=(l+r)/2;
if(pos<=mid)
update(l,mid,T[now].l,T[pre].l,pos);
else
update(mid+1,r,T[now].r,T[pre].r,pos);
}
int query(int l,int r,int x,int y,int k)
{
if(r<=k)
return T[y].sum-T[x].sum;
int mid=(l+r)/2;
if(k<=mid)
return query(l,mid,T[x].l,T[y].l,k);
else
return T[T[y].l].sum-T[T[x].l].sum+query(mid+1,r,T[x].r,T[y].r,k);
}
void dfs(int now,int last)
{
dep[now]=dep[last]+1;
fa[now][0]=last;
for(int i=1; (1<<i)<=dep[now]; i++)
{
fa[now][i]=fa[fa[now][i-1]][i-1];
}
for(int i=0; i<G[now].size(); i++)
{
int t=G[now][i].to;
if(t==last)
continue;
update(1,n,root[t],root[now],getid(G[now][i].w));
dfs(t,now);
}
}
int lca(int x,int y)
{
if(dep[x]>dep[y])
swap(x,y);
while(dep[x]!=dep[y])
{
if(lg[dep[y]-dep[x]]-1>=0)
y=fa[y][lg[dep[y]-dep[x]]-1];
else
y=fa[y][0];
}
if(x==y)
return x;
for(int i=lg[dep[y]]; i>=0; i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][0];
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
lg[i]=lg[i-1]+(1<<lg[i-1]==i);
for(int i=0; i<n-1; i++)
{
scanf("%d%d%d",&x,&y,&w);
G[x].push_back({y,w});
G[y].push_back({x,w});
v.push_back(w);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
dfs(1,0);
for(int i=1; i<=m; i++)
{
scanf("%d %d %d",&x,&y,&k);
int pos=upper_bound(v.begin(),v.end(),k)-v.begin();
if(pos)
{
int ans=query(1,n,root[1],root[x],pos)+query(1,n,root[1],root[y],pos)-2*query(1,n,root[1],root[lca(x,y)],pos);
printf("%d\n",ans);
}
else
printf("0\n");
}
return 0;
}