原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html


题目描述

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its "parent" in the tree). Each cow ii has a distinct proficiency rating, p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a manager of a manager) of cow jj, then we say jj is a subordinate of ii.
Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow ii in the company, please count the number of subordinates jj where p(j)>p(i).
n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。

输入

The first line of input contains N
The next N lines of input contain the proficiency ratings p(1)…p(N) 
for the cows. Each is a distinct integer in the range 1…1,000,000,000
The next N-1 lines describe the manager (parent) for cows 2…N 
Recall that cow 1 has no manager, being the president.
n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)

输出

Please print N lines of output. The ith line of output should tell the number of 
subordinates of cow ii with higher proficiency than cow i.
共n行,每行输出奶牛i的下属中有几个能力值比i大

样例输入

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

样例输出

2
0
1
0
0


题解

离散化+树状数组

先将数据离散化,然后在树上dfs。

搜子树之前求一下比w[x]小的数的个数,搜子树之后再求一下比w[x]小的数的个数,作差即为子树中比w[x]小的数的个数。最后把w[x]加入到树状数组中。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
using namespace std;
struct data
{
int w , id;
}a[N];
int n , v[N] , pos[N] , f[N] , ans[N] , head[N] , to[N] , next[N] , cnt;
void add(int x , int y)
{
to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
bool cmp(data a , data b)
{
return a.w < b.w;
}
void update(int x , int a)
{
int i;
for(i = x ; i <= n ; i += i & -i) f[i] += a;
}
int query(int x)
{
int i , ans = 0;
for(i = x ; i ; i -= i & -i) ans += f[i];
return ans;
}
void dfs(int x)
{
int i;
ans[x] -= query(v[x]);
for(i = head[x] ; i ; i = next[i]) dfs(to[i]);
ans[x] += query(v[x]);
update(v[x] , 1);
}
int main()
{
int i , x;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &a[i].w) , a[i].id = i;
sort(a + 1 , a + n + 1 , cmp);
for(i = 1 ; i <= n ; i ++ ) v[a[i].id] = n - i + 1;
for(i = 2 ; i <= n ; i ++ ) scanf("%d" , &x) , add(x , i);
dfs(1);
for(i = 1 ; i <= n ; i ++ ) printf("%d\n" , ans[i]);
return 0;
}

【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组的更多相关文章

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

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

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

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

  3. 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

    调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...

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

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  5. bzoj4756 [Usaco2017 Jan]Promotion Counting

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

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

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

  7. bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...

  8. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  9. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

随机推荐

  1. sqlite3基本操作

    在移动设备上进行高性能高效率的大量数据存储,我们一般用得时sqlite这款轻巧型的数据库,这里介绍其增删改查基本功能 在ios开发中我们需要先导入"libsqlite3.dylib" ...

  2. 阿里云OSS图片上传plupload.js结合jq-weui 图片上传的插件

    项目中用到了oss上传,用的plupload,奈何样式上不敢恭维,特别是放在移动端上使用.于是自己把它移植到了jq weui的上传图片组件上. 更改:选择照片后确认即及时上传至oss服务器,不限制上传 ...

  3. 腾讯云负载均衡CLB

    负载均衡 使用场景: ①购买一个负载均衡LB实例 ②一级.二级域名都解析到VIP上 ③创建HTTP/HTTPS监听器 ④绑定云主机 在nginx中只需要配置好伪静态和相关设置就ok了

  4. pwn的一些环境搭建

    <1>pwntools库安装 pwntools是一个CTF框架和漏洞利用开发库,用Python开发,由rapid设计,旨在让使用者简单快速的编写exploit. 本文将基于KUbuntu ...

  5. 给树莓派Raspbian stretch版本修改软件源

    树莓派最新的系统版本是stretch,试了阿里和网易的软件源都不行,最后试了清华的可以 deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbia ...

  6. python-读写文件的方式

    open(path, flag[, encoding][, errors]) path:要打开文件的路径 flag:打开方式 r 以只读的方式打开文件,文件的描述符放在文件的开头 rb 以二进制格式打 ...

  7. webmin纯web界面管理linux系统

    关键字: 摘要:从Windows环境的管理转到Linux环境的管理时所面临的挑战之一是,您需要去学习利用新的工具.作为一个管理员,您希望理解操作系统的细节以发挥它的最大功效.但是,当您还处在学习阶段时 ...

  8. static作用域

    当一个函数完成时,它的所有变量通常都会被删除.然而,有时候您希望某个局部变量不要被删除. 要做到这一点,请在您第一次声明变量时使用 static 关键字: <?php function myTe ...

  9. 准备篇(三)Makefile

    Makefile 也是蛮多的, 嵌入式的Makefile也是很重要的,所以单独开一个分支.

  10. Description POJ1703

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...