Distance on the tree

题目链接

https://nanti.jisuanke.com/t/38229

Describe

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 nnn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n−1n-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 nnn nodes, which is numbered from 111 to nnn. Edge between each two adjacent vertexes uuu and vvv has a value w, you're asked to answer the number of edge whose value is no more than kkk during the path between uuu and vvv."" 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,mn,m, represent the number of vertexes on the tree and queries(2≤n≤10^5,1≤m≤10^5)

The next n−1n-1n−1 lines, each line contains three integers u,v,wu,v,wu,v,w, indicates there is an undirected edge between nodes uuu and vvv with value www. (1≤u,v≤n,1≤w≤10^9)

The next mmm lines, each line contains three integers u,v,ku,v,ku,v,k , be consistent with the problem given by the teacher above. (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.

样例输入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的数。

题解

智商不够,靠数据结构来凑,弱弱的我直接树剖,然后用主席树,感觉就是把两个板子结合一下。

第一次一遍AC,看了好几遍才确定自己没看错。

代码

 #include<bits/stdc++.h>
 using namespace std;
 #define N 500050
 #define ll long long
 #define INF 123456789
 struct Query{int l,r,tt;}que[N];
 ];
 int totn,root[N];
 ];
 int last[N],tot;
 int n,m,a[N],w[N],num;
 int cnt,fa[N],d[N],size[N],son[N],kth[N],rk[N],top[N];
 void AddEdge(int x,int y,int z)
 {
   edges[++tot]=Edge{x,y,z,last[x]};
   last[x]=tot;
 }
 void dfs1(int u,int pre,int val)
 {
   d[u]=d[pre]+;
   fa[u]=pre;
   size[u]=;
   w[u]=val;
   for(int i=last[u];i;i=edges[i].s)
     {
       Edge &e=edges[i];
       if (e.to==pre)continue;
       dfs1(e.to,u,e.val);
       size[u]+=size[e.to];
       if (size[e.to]>size[son[u]])son[u]=e.to;
     }
 }
 void dfs2(int u,int y)
 {
   rk[u]=++cnt;
   kth[cnt]=u;
   top[u]=y;
   )return;
   dfs2(son[u],y);
   for(int i=last[u];i;i=edges[i].s)
     {
       Edge &e=edges[i];
       if (e.to==son[u]||e.to==fa[u])continue;
       dfs2(e.to,e.to);
     }

 }
 void bt(int &x,int l,int r)
 {
     x=++totn;
     if (l==r)return ;
     ;
     bt(tr[x].ls,l,mid);
     bt(tr[x].rs,mid+,r);
 }
 void add(int &x,int last,int l,int r,int p)
 {
     x=++totn;
     tr[x]=tr[last];
     if (l==r){tr[x].sum++;return;}
     ;
     if (p<=mid) add(tr[x].ls,tr[last].ls,l,mid,p);
     ,r,p);
     tr[x].sum=tr[tr[x].ls].sum+tr[tr[x].rs].sum;
 }
 int ask(int ql,int qr,int l,int r,int kk)
 {
   <=l&&r<=kk)return tr[qr].sum-tr[ql].sum;
   ,ans=;
   <=mid)ans+=ask(tr[ql].ls,tr[qr].ls,l,mid,kk);
   ,r,kk);
   return ans;
 }
 int get_sum(int x,int y,int tt)
 {
   ;
   int fx=top[x],fy=top[y];
   while(fx!=fy)
     {
       if (d[fx]<d[fy])swap(x,y),swap(fx,fy);
       ans+=ask(root[rk[fx]-],root[rk[x]],,num,tt);//
       x=fa[fx];fx=top[x];
     }
   if(d[x]<d[y])swap(x,y);
   ans+=ask(root[rk[y]],root[rk[x]],,num,tt);
   return ans;
 }
 int main()
 {
 #ifndef ONLINE_JUDGE
   freopen("aa.in","r",stdin);
 #endif
   ios::sync_with_stdio(false);
   cin>>n>>m;
   ll x,y,z,id,ans;
   ;i<=n-;i++)
     {
       cin>>x>>y>>z;
       a[++num]=z;
       AddEdge(x,y,z);
       AddEdge(y,x,z);
     }
   ;i<=m;i++)
     {
       cin>>que[i].l>>que[i].r>>que[i].tt;
       a[++num]=que[i].tt;
     }
   sort(a+,a+num+);
   num=unique(a+,a+num+)-a-;
   dfs1(,,INF);
   ;i<=n;i++)w[i]=lower_bound(a+,a+num+,w[i])-a;
   dfs2(,);
   bt(root[],,num);
   ;i<=n;i++)
     add(root[i],root[i-],,num,w[kth[i]]);
   ;i<=m;i++)
     {
       que[i].tt=lower_bound(a+,a+num+,que[i].tt)-a;
       int ans=get_sum(que[i].l,que[i].r,que[i].tt);
       printf("%d\n",ans);
     }
 }

南昌网络赛J. Distance on the tree 树链剖分+主席树的更多相关文章

  1. 南昌网络赛J. Distance on the tree 树链剖分

    Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...

  2. 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树

    边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...

  3. 2019南昌网络赛 J Distance on the tree 主席树+lca

    题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k​\) 分析 在树的每个点上建\(1​\)到\(i​\)的权值线段树,查询的时候同时跑\(u,v,lca ...

  4. 2019南昌邀请赛网络赛:J distance on the tree

    1000ms 262144K   DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...

  5. 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解

    题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...

  6. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  7. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  8. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  9. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

随机推荐

  1. C++11之nullptr

    [C++11空指针] 1.NULL的问题 class Test { public: void TestWork(int index) { std::cout << "TestWo ...

  2. 安装kali linux 2017.1 【二、安装VMware-tools 以及相关问题处理】

    一.基本步骤: 1.VMware Workstation菜单栏中,选择“虚拟机”,”安装VMware Tools...“. 2.挂载VMware Tools安装程序到/mnt/cdrom/. mkdi ...

  3. ubuntu tensorflow cpu Faster-RCNN配置参考

    https://blog.csdn.net/qq_36652619/article/details/85006559     (参考) https://blog.csdn.net/zcy0xy/art ...

  4. python学习——查找计算机中文件位置

    有时想查找某个文件时,却忘记了文件在计算机中存放的位置,这是一个经常遇到的问题. 当然如果你使用windows 7的话,可以直接用右上角的搜索框来搜索. 最近在学习python,正好拿这个来练练手,写 ...

  5. 清北学堂 day6 花

    1.花( flower.cpp/c/pas)[ 问题描述]商店里出售 n 种不同品种的花.为了装饰桌面,你打算买 m 支花回家.你觉得放两支一样的花很难看,因此每种品种的花最多买 1 支.求总共有几种 ...

  6. sql语句表连接

    "Persons" 表: Id_P LastName FirstName Address City 1 Adams John Oxford Street London 2 Bush ...

  7. 白盒测试实践-任务进度-Day04

    所使用静态代码检查工具 阿里巴巴Java开发代码检测IDE插件 小组成员 华同学.郭同学.覃同学.刘同学.穆同学.沈同学 任务进度 对大家完成各自任务情况进行询问后,以下是对小组成员完成任务进度的情况 ...

  8. javascript总结22: javascript的对象--面向对象编程

    1 对象:JavaScript 中的所有事物都是对象:字符串.数值.数组.函数. 对象与程序的关系: 程序 = 基于对象操作的算法 + 以对象为最小单位的数据结构 此外: 面向对象的本质就是让对象有多 ...

  9. 一款基于uploadify扩展的多文件上传插件,完全适用于Html5

    http://www.uploadify.com/documentation/  官网里面有两个插件,一个是要使用flash插件才能文件上传的插件,另外一个是不需要使用要flash插件的文件上传插件完 ...

  10. .NET基础 (07)异常的处理

    异常的处理1 如何针对不同的异常进行捕捉2 如何使用Conditional特性3 如何避免类型转换时的异常 异常的处理 1 如何针对不同的异常进行捕捉 C#中一个try块可以有多个catch块,每个c ...