题目链接: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?
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)
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
题意:
给你一棵n(n<=1e5)个节点的带权(权值<=1e9)树,然后q(q<=2e5)次询问,每次询问两点(u,v)的路径中权值<=k的边的条数。
思路:
貌似这题有十分相似的原题。。。
如果是给一个数组,查询区间里[l,r]中a[i]<=k的数的个数,我们可以用主席树或者离线树状数组/线段树来做。
我是用的树状数组:把询问和边权一起从小到大排序,每次遇到询问就求query(r)-query(l-1),遇到边权就add(i,1)。
既然是在树上,那就再加个树剖就行了。把边权化为点权,只需要把这个点和它父亲的点相连的边的权值当做这个点的权值就可以了。根节点置为inf。
其实并不算难,也难怪过了辣么多人。。。
代码:
#include
#define ll long long
#define inf 0x3f3f3f3f
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
#define mp(x,y) make_pair(x,y)
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int N = 100005;
const int maxn = 100005;
int n,m;
int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N]; //top 近期的重链父节点
int num;
vector > v[N];
int a[N];
struct nod
{
int id,v,fg;
int x,y;
bool operator<(nod aa)const
{
return v dep[v]) swap(u, v);
ans += query(id[v])-query(id[son[u]]-1);
//cout<<"OOOOK"<