1. //树形DP+树状数组 HDU 5877 Weak Pair
  2. // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值
  3. // 这道题要离散化
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define LL long long
  8. typedef pair<int,int> pii;
  9. const double inf = 123456789012345.0;
  10. const LL MOD =100000000LL;
  11. const int N = 2e5+;
  12. const int maxx = ;
  13. #define clc(a,b) memset(a,b,sizeof(a))
  14. const double eps = 1e-;
  15. void fre() {freopen("in.txt","r",stdin);}
  16. void freout() {freopen("out.txt","w",stdout);}
  17. inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;}
  18.  
  19. map<LL,LL> ma;
  20. LL a[N];
  21. LL c[N],b[N];
  22. LL in[N];
  23. vector<LL> g[N];
  24. LL lowbit(LL x){ return x&(-x);}
  25. LL add(LL x,int t){
  26. while(x>){
  27. c[x]+=t;
  28. x-=lowbit(x);
  29. }
  30. }
  31. LL Sum(LL x){
  32. LL sum=;
  33. while(x<maxx){
  34. sum+=c[x];
  35. x+=lowbit(x);
  36. }
  37. return sum;
  38. }
  39.  
  40. LL ans=;
  41. LL n,k;
  42. void dfs(LL rt){
  43. for(LL i=;i<(int)g[rt].size();i++){
  44. LL v=g[rt][i];
  45. ans+=Sum(ma[a[v]]);
  46. if(a[v]==) add(maxx,);
  47. else add(ma[k/a[v]],);
  48. dfs(v);
  49. if(a[v]==) add(maxx,-);
  50. else add(ma[k/a[v]],-);
  51. }
  52. }
  53. int main(){
  54. int T;
  55. scanf("%d",&T);
  56. while(T--){
  57. ma.clear();
  58. memset(c,,sizeof(c));
  59. scanf("%I64d%I64d",&n,&k);
  60. for(int i=;i<=n;i++){
  61. scanf("%I64d",&a[i]);
  62. b[i*-]=a[i];
  63. if(a[i]!=) b[i*-]=k/a[i];
  64. g[i].clear();
  65. in[i]=;
  66. }
  67. sort(b,b+*n);
  68. int K=unique(b,b+*n)-b;
  69. int cxt=;
  70. for(int i=;i<K;i++){
  71. ma[b[i]]=++cxt;
  72. }
  73. for(LL i=;i<n-;i++){
  74. LL u,v;
  75. scanf("%I64d%I64d",&u,&v);
  76. g[u].push_back(v);
  77. in[v]++;
  78. }
  79. LL rt;
  80. for(LL i=;i<=n;i++){
  81. if(in[i]==){
  82. rt=i;
  83. break;
  84. }
  85. }
  86. ans=;
  87. if(a[rt]==) add(maxx,);
  88. else add(ma[k/a[rt]],);
  89. dfs(rt);
  90. printf("%I64d\n",ans);
  91. }
  92. return ;
  93. }

树形DP+树状数组 HDU 5877 Weak Pair的更多相关文章

  1. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  2. 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...

  3. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  4. HDU 5877 Weak Pair(弱点对)

    HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Jav ...

  5. HDU 5877 Weak Pair(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...

  6. HDU 5877 Weak Pair(树状数组+dfs+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...

  7. HDU - 5877 Weak Pair (dfs+树状数组)

    题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...

  8. hdu 5877 Weak Pair dfs序+树状数组+离散化

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  9. HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化

    http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...

随机推荐

  1. parent children

    class parent{ protected static int count=0; public parent() { count++; } } public class child extend ...

  2. 256. Paint House

    题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...

  3. Vim的可视模式

    可视模式可以看到选中的字符串, 并对其进行操作 v:进入字符选择模式 V:进入行选择模式 ctrl-v(Window是ctrl-q):进入block选择模式 o:移动光标到选择的另一端 O:移动光标到 ...

  4. Ubuntu 12.04搭建MTK 6577 安卓开发环境

    Ubuntu 12.04搭建 MTK 6577安卓开发环境 1.       下载并安装Vmware虚拟机: 2.       下载并在虚拟机上安装Ubuntu 12.04 iso 安装包:下载地址: ...

  5. SQLiteParameter不能将TableName作为参数

    http://stackoverflow.com/questions/1274432/sqlite-parameters-not-allowing-tablename-as-parameter Gen ...

  6. TeeChart的X轴为时间,多个Y轴的显示

    最后上代码 public partial class Test : Form { private TChart tChart = new TChart(); ; public Test() { Ini ...

  7. WPF中的Drawing

    以前在用WinForm的时候,可以通过GDI+接口在窗体上动态绘制自定义的图形.在WPF中有没有对应的API呢,最近项目中用到了这个,在这里总结一下. WPF中的Drawing主要提供了几类API: ...

  8. HDU 1672 Cuckoo Hashing

    Cuckoo Hashing Description One of the most fundamental data structure problems is the dictionary pro ...

  9. bzoj1876: [SDOI2009]SuperGCD

    更相减损数. 上手就debug了3个小时,直接给我看哭了. 3个函数都写错了是什么感受? 乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数. 除2函数要从后往前除,这样前面的数借来的位不 ...

  10. sql2005主从数据库同步配置

    网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库.主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据的 ...