Counting Offspring

HDU - 3887

问你对于每个节点,它的子树上标号比它小的点有多少个

  1. /*
  2. 子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它的两个时间戳的区间之间,所以我们只需要从小到大考虑,它的区间里有多少个点已经放了,然后再把它放进去。很容易的解决了
  3. 每行的最后一个数后面不要出输出多余的空格,否则会PE
  4. */
  5. #include<iostream>
  6. #include<cstdio>
  7. #include<cstring>
  8. #define maxn 100010
  9. using namespace std;
  10. int n,p,num,head[maxn],xu[maxn],sz[maxn],opx,opl,opr;
  11. struct node{int to,pre;}e[maxn*];
  12. struct Node{int l,r,v;}tr[maxn<<];
  13. void Insert(int from,int to){
  14. e[++num].to=to;
  15. e[num].pre=head[from];
  16. head[from]=num;
  17. }
  18. void build(int l,int r,int k){
  19. tr[k].l=l;tr[k].r=r;tr[k].v=;
  20. if(l==r)return;
  21. int mid=(l+r)>>;
  22. build(l,mid,k<<);
  23. build(mid+,r,k<<|);
  24. }
  25. int id;
  26. void dfs(int now,int father){
  27. xu[now]=++id;
  28. sz[now]=;
  29. for(int i=head[now];i;i=e[i].pre){
  30. int to=e[i].to;
  31. if(to==father)continue;
  32. dfs(to,now);
  33. sz[now]+=sz[to];
  34. }
  35. }
  36. int query(int l,int r,int k){
  37. if(l>=opl&&r<=opr){return tr[k].v;}
  38. int mid=(l+r)>>;
  39. int res=;
  40. if(opl<=mid)res+=query(l,mid,k<<);
  41. if(opr>mid)res+=query(mid+,r,k<<|);
  42. return res;
  43. }
  44. void change(int l,int r,int k){
  45. if(l==r){tr[k].v++;return;}
  46. int mid=(l+r)>>;
  47. if(opx<=mid)change(l,mid,k<<);
  48. else change(mid+,r,k<<|);
  49. tr[k].v=tr[k<<].v+tr[k<<|].v;
  50. }
  51. int main(){
  52. freopen("Cola.txt","r",stdin);
  53. while(){
  54. scanf("%d%d",&n,&p);
  55. if(n==&&p==)return ;
  56. memset(head,,sizeof(head));
  57. memset(e,,sizeof(e));num=;
  58. id=;build(,n,);
  59. int x,y;
  60. for(int i=;i<n;i++){
  61. scanf("%d%d",&x,&y);
  62. Insert(x,y);Insert(y,x);
  63. }
  64. dfs(p,);
  65. for(int i=;i<=n;i++){
  66. opl=xu[i],opr=xu[i]+sz[i]-;
  67. if(i!=n)printf("%d ",query(,n,));
  68. else printf("%d",query(,n,));
  69. opx=xu[i];
  70. change(,n,);
  71. }
  72. puts("");
  73. }
  74. }

hdu3887 Counting Offspring的更多相关文章

  1. HDU3887 Counting Offspring [2017年6月计划 树上问题03]

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. Counting Offspring(hdu3887)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. HDU 3887 Counting Offspring(DFS序+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  6. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  7. Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg

    这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y     以节点x的权值修改为y. 2.Q x ...

  8. 杭电 3887 Counting Offspring

    根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...

  9. HDU 3887 Counting Offspring (树状数组+人工模拟栈)

    对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...

随机推荐

  1. python MLP 神经网络使用 MinMaxScaler 没有 StandardScaler效果好

    MLP 64,2  preprocessing.MinMaxScaler().fit(X)                               test confusion_matrix:[[ ...

  2. JSP的一个增删改查例子和总结

    总结的几点: 1.在jsp中注意<%! %>声明代码块中的变量只会在项目开始的时候第一次运行jsp的时候执行一遍,有点类似于java类中的static代码块,所以如果是会改变的值不应该声明 ...

  3. BEC listen and translation exercise 35

    高中听力: At five o'clock, we have afternoon tea, but we don't have it in the kitchen. Father's Day is t ...

  4. list dict set comprehension 列表推导式 (字典推导式,集合推导式)

    从一个list生成新的list [ word.upper() for word in 'hellO worlD!' ] 简单的语法,如果不用list comprehension, 则要用更长的代码. ...

  5. OpenCV-Python sift/surf特征匹配与显示

    import cv2 import numpy as np def drawMatchesKnn_cv2(img1_gray,kp1,img2_gray,kp2,goodMatch): h1, w1 ...

  6. 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...

  7. Mybatis generator配置文件及说明

    项目采用sring mvc + mybatis 组合,这里简单介绍下mybatis的应用: 我的IDE是STS(Spring + Tool + Suite), 安装Mybatis Generator插 ...

  8. redis的五种数据类型及应用场景

    前言 redis是用键值对的形式来保存数据,键类型只能是String,但是值类型可以有String.List.Hash.Set.Sorted Set五种,来满足不同场景的特定需求. 本博客中的示例不是 ...

  9. 安装DCOS,关于docker异常引发的调查

    入门DCOS,刚开始安装,碰到了一个异常: Bind for 0.0.0.0:9000 failed: port is already allocated. 调试这个问题花费了好长时间,因为无法通过n ...

  10. python构造一个http请求

    我们经常会用python来进行抓包,模拟登陆等等, 势必要构造http请求包. http的request通常有4个方法get,post,put,delete,分别对应于查询,更新,添加,删除.我们经常 ...