n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
Input
n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
Output
共n行,每行输出奶牛i的下属中有几个能力值比i大
Sample Input
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
2
0
1
0
0

  1. //针对所有点先分别建立权值线段树,然后进行线段树的合并
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. const int maxn=1e5+5;
  6. int n,cnt;
  7. int pre[maxn],son[maxn],now[maxn];
  8. int val[maxn],rt[maxn],ans[maxn],tmp[maxn];
  9.  
  10. int read()
  11. {
  12. int x=0,f=1;char ch=getchar();
  13. for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
  14. for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+ch-'0';
  15. return x*f;
  16. }
  17.  
  18. struct segment_tree
  19. {
  20.  
  21. int tot;
  22. int siz[maxn*20],ls[maxn*20],rs[maxn*20];
  23.  
  24. void update(int p)
  25. {
  26. siz[p]=siz[ls[p]]+siz[rs[p]];
  27. }
  28.  
  29. void build(int &p,int l,int r,int v)
  30. //T.build(rt[i],1,n,val[i]);
  31. {
  32. if(!p)
  33. p=++tot;
  34. if(l==r)
  35. {
  36. siz[p]=1;//第p个结点的大小为1
  37. return;
  38. }
  39. int mid=(l+r)>>1;
  40. if(v<=mid)
  41. build(ls[p],l,mid,v);
  42. else
  43. build(rs[p],mid+1,r,v);
  44. update(p);
  45. }
  46.  
  47. int merge(int x,int y)
  48. {
  49. if(x==0||y==0)
  50. return x+y;
  51. ls[x]=merge(ls[x],ls[y]);
  52. rs[x]=merge(rs[x],rs[y]);
  53. update(x);
  54. return x;
  55. }
  56.  
  57. int find(int p,int l,int r,int v)
  58. {
  59. if(p==0)
  60. return 0;
  61. int mid=(l+r)>>1;
  62. if(v<=mid)
  63. return find(ls[p],l,mid,v);
  64. return siz[ls[p]]+find(rs[p],mid+1,r,v);
  65. }
  66. }T;
  67.  
  68. void add(int a,int b)
  69. {
  70. pre[++cnt]=now[a];
  71. now[a]=cnt,son[cnt]=b;
  72. }
  73.  
  74. int dfs(int u)
  75. {
  76. int root=0;
  77. for(int p=now[u],v=son[p];p;p=pre[p],v=son[p])
  78. root=T.merge(root,dfs(v));
  79. //将以u为父亲的所有点的线段树进行合并,合并完了后再来统计
  80. ans[u]=T.siz[root]-T.find(root,1,n,val[u]);
  81. //用总结点个数减去小于等于val[u]的
  82. return T.merge(root,rt[u]);
  83. //将u与上面形成的线段树再合并
  84.  
  85. }
  86.  
  87. int main()
  88. {
  89. n=read();
  90. for(int i=1;i<=n;i++)
  91. tmp[i]=val[i]=read();
  92. sort(tmp+1,tmp+n+1);
  93. int sum=unique(tmp+1,tmp+n+1)-tmp-1;
  94. for(int i=1;i<=n;i++)
  95. {
  96. val[i]=lower_bound(tmp+1,tmp+sum+1,val[i])-tmp;
  97. T.build(rt[i],1,n,val[i]);
  98. //先针对每个点建立一个线段树出来
  99. }
  100. for(int i=2;i<=n;i++)
  101. {
  102. int f=read();
  103. add(f,i);//i与其父亲f连边
  104. }
  105. dfs(1);
  106. for(int i=1;i<=n;i++)
  107. printf("%d\n",ans[i]);
  108. return 0;
  109. }

  

[Usaco2017 Jan]Promotion Counting的更多相关文章

  1. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

  2. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  3. 【bzoj 4756】[Usaco2017 Jan] Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...

  4. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

  5. BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  6. 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)

    描述 The cows have once again tried to form a startup company, failing to remember from past experienc ...

  7. 【BZOJ】4756: [Usaco2017 Jan]Promotion Counting

    [题意]带点权树,统计每个结点子树内点权比它大的结点数. [算法]线段树合并 [题解]对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计. 注意三个量tot,cnt,tots,细心 ...

  8. BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...

  9. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

  10. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...

随机推荐

  1. windows环境下 RabbitMQ 安装时创建用户流程命令

    https://blog.csdn.net/xiaojieblog/article/details/70332469

  2. Vue项目的痛点

    前言 用Vue做项目,难免会有痛点,移动端还是PC端,我也总结了下关于问题: 进入详情页的传参问题. 服务器接口跨域 axios封装请求 UI库的按需加载 如何只在当前页面中覆盖ui库中组件的样式 定 ...

  3. default关键字

    default关键字在JDK8中有两个用处. 1.在switch语句的时候使用default int gender = 3; String genderString; switch (gender) ...

  4. JAVA笔记16-生产者消费者问题

    http://www.cnblogs.com/happyPawpaw/archive/2013/01/18/2865957.html import java.util.*; public class ...

  5. 【leetcode】1224. Maximum Equal Frequency

    题目如下: Given an array nums of positive integers, return the longest possible length of an array prefi ...

  6. SQL复杂筛选

    SELECT A.MATERIALID,A.MATERIALNAME,ISNULL(A.COMPIDSEQ,'') COMPIDSEQ,ISNULL(A.SUPPLYID,'') SUPPLYID,S ...

  7. 【BZOJ4570】 [Scoi2016]妖怪

    Description 邱老师是妖怪爱好者,他有n只妖怪,每只妖怪有攻击力atk和防御力dnf两种属性.邱老师立志成为妖怪大师,于 是他从真新镇出发,踏上未知的旅途,见识不同的风景.环境对妖怪的战斗力 ...

  8. StringTokenizer工具类的使用

    package stringtokenizer.java; import java.util.StringTokenizer; public class stringtokenizer { publi ...

  9. 自定义springmvc参数解析器

    实现spring HandlerMethodArgumentResolver接口 通过使用@JsonArg自定义注解来解析json数据(通过fastjson的jsonPath),支持多个参数(@Req ...

  10. Android学习笔记之数据的Sdcard存储方法及操作sdcard的工具类

    FileService.java也就是操作sdcard的工具类: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...