原文地址: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. iOS 检测版本更新(02)

    iOS 检测版本更新 如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bu ...

  2. gzip,bzip2,xz压缩工具

    gzip,bzip2,xz压缩工具====================== gzip压缩工具 示例:[root@aminglinux yasuo]# ls1.txt 2.txt 3.txt[roo ...

  3. VM虚拟机里的Ubuntu系统怎么设置屏幕分辨率

    说白了就是安装VMWare tools工具,步骤如下: 1)在VMWare中启动ubuntu虚拟机 2)在VMWare中:右键单击启动虚拟机,点击[安装vmware tools] 3)在ubuntu中 ...

  4. (转)零基础学习 Hadoop 该如何下手?

    推荐一些Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Amb ...

  5. 在CentOS VPS上通过SSH安装 MySQL

    输入 yum install mysql-server 按Y继续 安装完成,设置开机启动Mysql,输入 chkconfig --levels 235 mysqld on 然后启动tomcat,输入s ...

  6. Git的基本命令介绍

    Git的安装 进入官网下载系统所需要的版本  官网地址:https://git-scm.com/downloads 点击下载按钮官方网站一般会根据操作系统的自动下载所需要的Git版本. 下载完成后,点 ...

  7. python3 练习题100例 (十九)

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- """练习十九:计算1-2+3...+99中除了88以外所有数的和" ...

  8. python系列3之内置函数和文件操作

    目录 自定义函数 内置函数 文件的操作 练习题 一. 自定义函数 1. 函数的创建 函数的创建 1.def关键字 2.函数名+() 3.冒号 4.缩进 5. return返回值,可以不写,默认的返回值 ...

  9. C++基础 const

    1. C中的const C中const变量只是只读变量,有自己存储空间.可能被存放在 栈.堆.数据段,所以可以修改. 2. C++中const 可能分配空间,也可能不分配空间. 当 const 为全局 ...

  10. 把SmartForm转换成PDF

    摘要:将SmartForm转换为PDF的过程包括3个简单步骤. 调用智能窗体,然后返回OTF数据. 使用“转换”功能模块将OTF数据转换为所需格式. 下载文件 呈现宏“code”时出错:为参数“lan ...