【题目链接】

点击打开链接

【算法】

离散化 + dfs + 树状数组

【代码】

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAXN 100000
  4.  
  5. int N,i,lth,x,pos;
  6. int a[MAXN+],tmp[MAXN+],rank[MAXN+],ans[MAXN+];
  7. vector<int> E[MAXN+];
  8.  
  9. template <typename T> inline void read(T &x) {
  10. int f = ; x = ;
  11. char c = getchar();
  12. for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
  13. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  14. x *= f;
  15. }
  16.  
  17. template <typename T> inline void write(T x) {
  18. if (x < ) { putchar('-'); x = -x; }
  19. if (x > ) write(x/);
  20. putchar(x%+'');
  21. }
  22.  
  23. template <typename T> inline void writeln(T x) {
  24. write(x);
  25. puts("");
  26. }
  27.  
  28. struct BinaryIndexedTree {
  29. int bit[MAXN+];
  30. int lowbit(int x) { return x & -x; }
  31. inline void modify(int pos) {
  32. int i;
  33. for (i = pos; i <= N; i += lowbit(i)) bit[i]++;
  34. }
  35. inline int query(int pos) {
  36. int i,ret = ;
  37. for (i = pos; i; i -= lowbit(i)) ret += bit[i];
  38. return ret;
  39. }
  40. } BIT;
  41.  
  42. inline void dfs(int x) {
  43. int i;
  44. BIT.modify(rank[x]);
  45. ans[x] = -BIT.query(rank[x]-);
  46. for (i = ; i < E[x].size(); i++) dfs(E[x][i]);
  47. ans[x] += BIT.query(rank[x]-);
  48. }
  49.  
  50. int main() {
  51.  
  52. read(N);
  53. for (i = ; i <= N; i++) {
  54. read(a[i]);
  55. tmp[i] = a[i];
  56. }
  57.  
  58. sort(tmp+,tmp+N+);
  59. for (i = ; i <= N; i++) {
  60. if (tmp[i] != tmp[i-])
  61. tmp[++lth] = tmp[i];
  62. }
  63.  
  64. for (i = ; i <= N; i++) {
  65. pos = lower_bound(tmp+,tmp+lth+,a[i]) - tmp;
  66. rank[i] = N - pos + ;
  67. }
  68.  
  69. for (i = ; i <= N; i++) {
  70. read(x);
  71. E[x].push_back(i);
  72. }
  73.  
  74. dfs();
  75.  
  76. for (i = ; i <= N; i++) writeln(ans[i]);
  77.  
  78. return ;
  79.  
  80. }

【USACO2017JAN】 Promotion Counting的更多相关文章

  1. 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化

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

  2. 【模板】【P3605】【USACO17JAN】Promotion Counting 晋升者计数——动态开点和线段树合并(树状数组/主席树)

    (题面来自Luogu) 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1⋯N(1≤N≤100,000) 编号,把公司组织成一棵树 ...

  3. 【LeetCode】338. Counting Bits (2 solutions)

    Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num  ...

  4. 【SP26073】DIVCNT1 - Counting Divisors 题解

    题目描述 定义 \(d(n)\) 为 \(n\) 的正因数的个数,比如 \(d(2) = 2, d(6) = 4\). 令 $ S_1(n) = \sum_{i=1}^n d(i) $ 给定 \(n\ ...

  5. 【计数】【UVA11401】 Triangle Counting

    传送门 Description 把1……n这n个数中任取3个数,求能组成一个三角形的方案个数 Input 多组数据,对于每组数据,包括: 一行一个数i,代表前i个数. 输入结束标识为i<3. O ...

  6. 【hdu3518】Boring counting

    题意:找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). 后缀数组 枚举字串长度h,对于每一次的h,利用height数组,找出连续的height大于等于h的里面最左端和最右端得为之l ...

  7. 【leetcode】338 .Counting Bits

    原题 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate t ...

  8. 【JZOJ6342】Tiny Counting

    description analysis 首先不管\(a,b,c,d\)重复的情况方案数是正逆序对之积 如果考虑\(a,b,c,d\)有重复,只有四种情况,下面括号括起来表示该位置重复 比如\(\{a ...

  9. 【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目描述 Given a non negati ...

随机推荐

  1. [Bzoj3676][Apio2014]回文串(后缀自动机)(parent树)(倍增)

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 3396  Solved: 1568[Submit][Statu ...

  2. MySQL、Hive以及MySQL Connector/J安装过程

    MySQL安装 ①官网下载mysql-server(yum安装) wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch. ...

  3. C#使用PrintDocument打印 多页 打印预览

    PrintDocument实例所有的订阅事件如下: 创建一个PrintDocument的实例.如下: System.Drawing.Printing.PrintDocument docToPrint ...

  4. 【mac】mac上安装JDK

    安装步骤就是在Oracle官网下载你想要的JDK版本下载,下载的时候同意协议即可 这里就给出jdk安装的位置 还有一点要注意的是,在指定JVM的位置的时候,需要指定到home目录下

  5. 【IntelliJ IDEA】2017.3.4版本永久破解

    [本版本软件包和破解jar在网盘上有    我的网盘--技术--idea破解所需要的] 1.idea官网下载 历史版本 选择2017.3.4版本下载 https://www.jetbrains.com ...

  6. PythonCookbook读书笔记

    第一章 数据结构和算法 1.1 将序列分解为单独的变量 适用于元组.列表.字符串等.只要是可迭代的对象,都可以执行分解操作.唯一的要求是变量的总数和结构要与序列相同. 1.2 从任意长度的可迭代对象中 ...

  7. Delphi GDI对象之绘制文本

    转载:http://www.cnblogs.com/pchmonster/archive/2012/07/06/2579185.html 基本绘图操作(Basic Drawing Operations ...

  8. 查询公司外网ip方法

    curl -s "http://checkip.dyndns.org/"|cut -f 6 -d" "|cut -f 1 -d"<" ...

  9. BUPT复试专题—众数(2014)

    题目描述 有一个长度为N的非降数列,求数列中出现最多的数,若答案不唯一输出最小的数 输入 第一行T表示测试数据的组数(T<100) 对于每组测试数据: 第一行是一个正整数N表示数列长度 第二行有 ...

  10. mysql 同样内容的字段合并为一条的方法

    从两个表中内联取出的数据,当中category_name字段有同样内容,想将具有同样内容的字段进行合并,将amount字段进行加法运算,变成下表中的内容 url=http%3A%2F%2Fdev.my ...