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

  1. 3 3
  2. 1 3 2
  3. 2 3 7
  4. 1 3 0
  5. 1 2 4
  6. 1 2 7

样例输出1

  1. 0
  2. 1
  3. 2

样例输入2

  1. 5 2
  2. 1 2 1000000000
  3. 1 3 1000000000
  4. 2 4 1000000000
  5. 3 5 1000000000
  6. 2 3 1000000000
  7. 4 5 1000000000

样例输出2

2

4

题意

给你一棵树,问两个点之间边权小与等于k的数。

题解

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

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

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define N 500050
  4. #define ll long long
  5. #define INF 123456789
  6. struct Query{int l,r,tt;}que[N];
  7. ];
  8. int totn,root[N];
  9. ];
  10. int last[N],tot;
  11. int n,m,a[N],w[N],num;
  12. int cnt,fa[N],d[N],size[N],son[N],kth[N],rk[N],top[N];
  13. void AddEdge(int x,int y,int z)
  14. {
  15. edges[++tot]=Edge{x,y,z,last[x]};
  16. last[x]=tot;
  17. }
  18. void dfs1(int u,int pre,int val)
  19. {
  20. d[u]=d[pre]+;
  21. fa[u]=pre;
  22. size[u]=;
  23. w[u]=val;
  24. for(int i=last[u];i;i=edges[i].s)
  25. {
  26. Edge &e=edges[i];
  27. if (e.to==pre)continue;
  28. dfs1(e.to,u,e.val);
  29. size[u]+=size[e.to];
  30. if (size[e.to]>size[son[u]])son[u]=e.to;
  31. }
  32. }
  33. void dfs2(int u,int y)
  34. {
  35. rk[u]=++cnt;
  36. kth[cnt]=u;
  37. top[u]=y;
  38. )return;
  39. dfs2(son[u],y);
  40. for(int i=last[u];i;i=edges[i].s)
  41. {
  42. Edge &e=edges[i];
  43. if (e.to==son[u]||e.to==fa[u])continue;
  44. dfs2(e.to,e.to);
  45. }
  46.  
  47. }
  48. void bt(int &x,int l,int r)
  49. {
  50. x=++totn;
  51. if (l==r)return ;
  52. ;
  53. bt(tr[x].ls,l,mid);
  54. bt(tr[x].rs,mid+,r);
  55. }
  56. void add(int &x,int last,int l,int r,int p)
  57. {
  58. x=++totn;
  59. tr[x]=tr[last];
  60. if (l==r){tr[x].sum++;return;}
  61. ;
  62. if (p<=mid) add(tr[x].ls,tr[last].ls,l,mid,p);
  63. ,r,p);
  64. tr[x].sum=tr[tr[x].ls].sum+tr[tr[x].rs].sum;
  65. }
  66. int ask(int ql,int qr,int l,int r,int kk)
  67. {
  68. <=l&&r<=kk)return tr[qr].sum-tr[ql].sum;
  69. ,ans=;
  70. <=mid)ans+=ask(tr[ql].ls,tr[qr].ls,l,mid,kk);
  71. ,r,kk);
  72. return ans;
  73. }
  74. int get_sum(int x,int y,int tt)
  75. {
  76. ;
  77. int fx=top[x],fy=top[y];
  78. while(fx!=fy)
  79. {
  80. if (d[fx]<d[fy])swap(x,y),swap(fx,fy);
  81. ans+=ask(root[rk[fx]-],root[rk[x]],,num,tt);//
  82. x=fa[fx];fx=top[x];
  83. }
  84. if(d[x]<d[y])swap(x,y);
  85. ans+=ask(root[rk[y]],root[rk[x]],,num,tt);
  86. return ans;
  87. }
  88. int main()
  89. {
  90. #ifndef ONLINE_JUDGE
  91. freopen("aa.in","r",stdin);
  92. #endif
  93. ios::sync_with_stdio(false);
  94. cin>>n>>m;
  95. ll x,y,z,id,ans;
  96. ;i<=n-;i++)
  97. {
  98. cin>>x>>y>>z;
  99. a[++num]=z;
  100. AddEdge(x,y,z);
  101. AddEdge(y,x,z);
  102. }
  103. ;i<=m;i++)
  104. {
  105. cin>>que[i].l>>que[i].r>>que[i].tt;
  106. a[++num]=que[i].tt;
  107. }
  108. sort(a+,a+num+);
  109. num=unique(a+,a+num+)-a-;
  110. dfs1(,,INF);
  111. ;i<=n;i++)w[i]=lower_bound(a+,a+num+,w[i])-a;
  112. dfs2(,);
  113. bt(root[],,num);
  114. ;i<=n;i++)
  115. add(root[i],root[i-],,num,w[kth[i]]);
  116. ;i<=m;i++)
  117. {
  118. que[i].tt=lower_bound(a+,a+num+,que[i].tt)-a;
  119. int ans=get_sum(que[i].l,que[i].r,que[i].tt);
  120. printf("%d\n",ans);
  121. }
  122. }

南昌网络赛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. mybatis 用法分享

    主题 这篇文章主要是记录这2个月我对mybatis的学习以后的一些感触和一些如果我是架构师会怎么在项目里使用mybatis的一些大胆的想法. 感想 1.首先根据之前的学习我已经知道了mybatis g ...

  2. Python基础:Python数据类型及逻辑判断语句

    Python代码需要严谨的缩进 # 导包 import random # ********************输入输出***************** # 输出 print("hell ...

  3. kali linux: 网卡监听及扫描网络

    一.网卡监听: kali不支持内置网卡,仅仅支持usb网卡.我用的虚拟机.首先在虚拟机的可移动设备里打开usb网卡的连接,执行ifconfig命令,如下: eth0接口是本地有线网卡信息,lo接口是本 ...

  4. 【CodeForces148D】Bag of mice

    题意 dragon和princess玩一个游戏.开始的时候袋子里有w个白老鼠和b个黑老鼠.两个人轮流从袋子里面往外摸老鼠.谁先拿到白老鼠谁先获胜.dragon每次抓出一只老鼠,剩下老鼠里面都会有一只跳 ...

  5. Quaternion.identity是什么意思?

    Quaternion.identity就是指Quaternion(0,0,0,0),

  6. yum基本用法与修改源

    一.yum简介 yum,是Yellow dog Updater Modified的简称,起初是由yellow dog这一发行版的开发者Terra Soft研发,用python写成,那时还叫做yup(y ...

  7. Linux系统巡检常用命令-乾颐堂

    Linux系统需要定期巡检,以检查服务器软硬件使用情况,相当于对人的体检,确保可以及时发现问题.解决问题,降低损失,常用的巡检命令如下: # uname -a # 查看内核/操作系统/CPU信息 # ...

  8. Linux编程实现蜂鸣器演奏康定情歌

    Linux编程实现蜂鸣器演奏康定情歌 摘自:https://blog.csdn.net/jiazhen/article/details/3490979   2008年12月10日 15:40:00 j ...

  9. zigbee初探

    什么是zigbee? 1.它是一种通信方式,一种通信协议: 2.其作用就是构建一个类似无线局域网的东西:如果这个局域网用于传感器的数据收集.监控,那么这个网络就叫做无线传感器网络. 应用领域:家居.工 ...

  10. Linux 添加新硬盘

    1.识别分区和硬盘 在 /dev/ 目录下找到新的硬盘,sda 为本地硬盘,sda1.sda2.. 为分区,sdb 就是新添加的硬盘,如: [root@wusuyuan ~]# ls -ltr /de ...