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.

思路:主席树 + LCA

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int head[maxn], tot;
struct Edge {
	int u, v, w, next;
}edge[maxn];
int root[maxn];
struct Tree {
	int l, r;
	int sum;
}tree[maxn * 40];
int cnt;
int Hash[maxn], len;
int n, m;
int u1[maxn], v1[maxn], w1[maxn];
int u2[maxn], v2[maxn], w2[maxn];
int size[maxn], son[maxn], father[maxn], depth[maxn], top[maxn];
int L[maxn], R[maxn], Index;
void init() {
	memset(head, -1, sizeof(head));
	tot = 1;
}

void add(int u, int v, int w) {
	edge[++tot].u = u; edge[tot].v = v;
	edge[tot].w = w;
	edge[tot].next = head[u];
	head[u] = tot;
}

void updata(int &nowp, int last, int l, int r, int pos) {
    tree[++cnt] = tree[last];
    tree[cnt].sum++; 
    nowp = cnt;
    if(l == r) {
        return ;
    }
    int mid = (l + r) / 2;
    if(pos <= mid) {
        updata(tree[nowp].l, tree[last].l, l, mid, pos);
    } else {
        updata(tree[nowp].r, tree[last].r, mid + 1, r, pos);
    }
}

int query(int lt, int rt, int l, int r, int k) {
    if(l == r) {
        return tree[rt].sum - tree[lt].sum;
    }
    int mid = (l + r) / 2;
    int sum = tree[tree[rt].l].sum - tree[tree[lt].l].sum;
    if(mid >= k) {
        return query(tree[lt].l, tree[rt].l, l, mid, k);
    } else {
        return sum + query(tree[lt].r, tree[rt].r, mid + 1, r, k);
    }
}


void add(int u, int v) {
    edge[++tot].u = u; edge[tot].v = v;
    edge[tot].next = head[u]; 
    head[u] = tot;
}

void dfs1(int u, int fa) {
    size[u] = 1;
    son[u] = 0;
    father[u] = fa;
    depth[u] = depth[fa] + 1;
    int maxson = -1;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int to = edge[i].v;
        if (to == fa) {
            continue;
        }
        updata(root[to], root[u], 1, len, edge[i].w);
        dfs1(to, u);
        size[u] += size[to];
        if (size[to] > maxson) {
            maxson = size[to];
            son[u] = to;
        }
    }
}

void dfs2(int u, int topf) {
    top[u] = topf;
    L[u] = R[u] = ++Index;
    if (son[u]) {
        dfs2(son[u], topf);
    }
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int to = edge[i].v;
        if (L[to] || to == son[u]) {
            continue;
        }
        dfs2(to, to);
    }
    R[u] = Index;
}

int LCA(int x, int y) {
    while (top[x] != top[y]) {
        if (depth[top[x]] < depth[top[y]]) {
            swap(x, y);
        }
        x = father[top[x]];
    }
    if (depth[x] > depth[y]) {
        return y;
    }
    return x;
}

int main() {
	scanf("%d%d", &n, &m);
	init();
	for (int i = 1; i <= n - 1; i++) {
		scanf("%d%d%d", &u1[i], &v1[i], &w1[i]);
		Hash[++len] = w1[i];
	}
	for (int i = 1; i <= m; i++) {
		scanf("%d%d%d", &u2[i], &v2[i], &w2[i]);
		Hash[++len] = w2[i];
	}
	sort(Hash + 1, Hash + len + 1);
	len = unique(Hash + 1, Hash + len + 1) - Hash - 1;
	for (int i = 1; i <= n - 1; i++) {
		w1[i] = lower_bound(Hash + 1, Hash + len + 1, w1[i]) - Hash;
		add(u1[i], v1[i], w1[i]);
		add(v1[i], u1[i], w1[i]);
	}
	for (int i = 1; i <= m; i++) {
		w2[i] = lower_bound(Hash + 1, Hash + len + 1, w2[i]) - Hash;
	}
	dfs1(1, 0);
	dfs2(1, 1);
	for (int i = 1; i <= m; i++) {
		int L1 = LCA(u2[i], v2[i]);
		int x = query(root[L1], root[u2[i]], 1, len, w2[i]);
		int y = query(root[L1], root[v2[i]], 1, len, w2[i]);
		printf("%d\n",  x + y);
	}
	return 0;
}