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

          

       在这里强烈鄙视队友给我翻译错误题意导致我根本不敢写~~~

       不过之后的话只要想到用树链剖分和可持续化的话其实这道题挺裸的。。没什么好说的。。

#include
using namespace std;
const int maxn = 100010;
#define bug cout<<"bug"< num[son[u]])
			son[u] = v, len[u] = ed[i].len;
	}
}
void getpos(int u, int sp,int w) {
	top[u] = sp;
	if (u != 1) {
		T[pos] = update(T[pos - 1], Hash(w), 1);
		nu[u] = pos++;
	}
	if (son[u] == -1)return;
	getpos(son[u], sp, len[u]);
	for (int i = head[u]; ~i; i = ed[i].ne) {
		int v = ed[i].v;
		if (v != son[u] && v != fa[u]) {
			getpos(v, v, ed[i].len);
		}
	}
}
//树链剖分初始化结束;
int build(int l, int r) {
	int root = tot++;
	c[root] = 0;
	if (l != r) {
		int mid = (l + r) >> 1;
		lson[root] = build(l, mid);
		rson[root] = build(mid + 1, r);
	}
	return root;
}
int update(int root, int pos, int val) {
	int newroot = tot++, tmp = newroot;
	c[newroot] = c[root] + val;
	int l = 1, r = m;
	while (l < r) {
		int mid = (l + r) >> 1;
		if (pos <= mid) {
			lson[newroot] = tot++; rson[newroot] = rson[root];
			newroot = lson[newroot]; root = lson[root];
			r = mid;
		}
		else {
			rson[newroot] = tot++; lson[newroot] = lson[root];
			newroot = rson[newroot]; root = rson[root];
			l = mid + 1;
		}
		c[newroot] = c[root] + val;
	}
	return tmp;
}
int query(int root, int l, int r, int k) {
	if (r <= k)return c[root];
	int mid = (l + r) >> 1;
	int sum = 0;
	sum += query(lson[root], l, mid, k);
	if (k > mid)
		sum += query(rson[root], mid + 1, r, k);
	return sum;
}
int find(int u, int v,int k) {
	int f1 = top[u], f2 = top[v];
	int tmp = 0;
	while (f1 != f2) {
		if (deep[f1] < deep[f2]) {
			swap(f1, f2);
			swap(u, v);
		}
		int a = query(T[nu[f1] - 1], 1, m, k);
		int b = query(T[nu[u]], 1, m, k);
		tmp += b - a;
		u = fa[f1]; f1 = top[u];	
	}
	if (u == v)return tmp;
	if (deep[u] > deep[v])swap(u, v);
	tmp += query(T[nu[v]], 1, m, k) - query(T[nu[u]], 1, m, k);
	return tmp;
}

int main() {
	int q; 
	scanf("%d%d", &n, &q);init(); 
	for (int i = 0; i < n - 1; i++) {
		scanf("%d%d%d", &e[i][0], &e[i][1], &e[i][2]);
		sot[ecnt++] = e[i][2];
		addedge(e[i][0], e[i][1], e[i][2]);
		addedge(e[i][1], e[i][0], e[i][2]);
	}	

	for (int i = 0; i < q; i++) {
		scanf("%d%d%d", &qu[i][0], &qu[i][1], &qu[i][2]);
		sot[ecnt++] = qu[i][2];
	}
	sort(sot + 1, sot + ecnt);
	m = unique(sot + 1, sot + ecnt) - sot - 1;
	T[0] = build(1, m); nu[0] = 0;
	T[1] = T[0]; nu[1] = 1;  
	dfs1(1, 0, 0); getpos(1, 1, 0);
	for (int i = 0; i < q; i++) {
		printf("%d\n", find(qu[i][0], qu[i][1], Hash(qu[i][2])));
	}
	return 0;
}

 

你可能感兴趣的:(图论,树形结构)