Description

The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent 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 man
ager 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 seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. 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号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
 

Input

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号奶牛的经理(树中的父亲)

Output

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大

Sample Input

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

Sample Output

2
0
1
0
0

一道水题,本来可以用dfs+树状数组两次查询的方式水过的,这里为了练习线段树合并又自己yy了一个,结果还是不错的

因为RE了一回,本地又卡系统栈特意又去查了下空间复杂度,原来每次合并最多会新开logN个节点,所以就开到NlogN就好了

不过要是实在蒙圈的话我认为开到内存上限也不失为良策,只不过要千万小心的计算内存啊。。。

 #include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,m,p[N],t[N],ans[N];
int h[N],to[N],nxt[N],etop;
void add(int u,int v){to[++etop]=v;nxt[etop]=h[u];h[u]=etop;}
int data[N*],ls[N*],rs[N*],tot;
int newtree(int l,int r,int x){
data[++tot]=;
if(l==r)return tot;
int node=tot,mid=(l+r)>>;
if(x<=mid)ls[node]=newtree(l,mid,x);
else rs[node]=newtree(mid+,r,x);
return node;
}
int merge(int l,int r,int u,int v){
if(!u||!v)return u+v;
if(l==r){
data[++tot]=data[u]+data[v];
return tot;
}
int mid=(l+r)>>,node=++tot;
ls[node]=merge(l,mid,ls[u],ls[v]);
rs[node]=merge(mid+,r,rs[u],rs[v]);
data[node]=data[ls[node]]+data[rs[node]];
return node;
}
int query(int l,int r,int L,int R,int u){
if(!u)return ;
if(L>R)return ;
if(l==L&&r==R)return data[u];
int mid=(l+r)>>;
if(R<=mid)return query(l,mid,L,R,ls[u]);
else if(L>mid)return query(mid+,r,L,R,rs[u]);
else return query(l,mid,L,mid,ls[u])+query(mid+,r,mid+,R,rs[u]);
}
int dfs(int u){
int node=newtree(,n,p[u]),v;
for(int k=h[u];k;k=nxt[k]){
v=dfs(to[k]);
node=merge(,n,node,v);
}
ans[u]=query(,n,p[u]+,n,node);
return node;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&p[i]),t[i]=p[i];
sort(t+,t++n);
m=unique(t+,t++n)-t-;
for(int i=;i<=n;i++)p[i]=lower_bound(t+,t++m,p[i])-t;
for(int i=,fff;i<=n;i++){
scanf("%d",&fff);
add(fff,i);
}
dfs();
for(int i=;i<=n;i++)
printf("%d\n",ans[i]);
}

BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数的更多相关文章

  1. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...

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

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

  3. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

    P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...

  4. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  5. [USACO17JAN]Promotion Counting晋升者计数

    题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...

  6. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

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

  7. luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...

  8. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

  9. [USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)

    题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数 本题考查dfs的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计 ...

随机推荐

  1. [bzoj4154][Ipsc2015]Generating Synergy_KD-Tree_dfs序

    Generating Synergy bzoj-4154 Ipsc-2015 题目大意:给定一棵n个节点树,m个操作,支持:将一个点周围所有距该点距离不超过l的子结点的颜色改成另一种颜色:查询单点颜色 ...

  2. 机器学习1k近邻

    自己一直学习计算机视觉方面的东西,现在想学习一下数据挖掘跟搜索引擎,自己基础也有点薄弱,看朱明的那本数据挖掘,只能片面的了解这个数据挖掘.不过最近有一本书 机器学习实战,于是乎通过实战的形式了解一下基 ...

  3. iOS分组通讯录效果+側滑菜单(MMDrawerController)

    前言的废话-能够忽略 自从学会了使用Cocoapod,就欲罢不能了!由于太简单太赞了,不用再把源代码粘到project里了! 參见戴维营博客中的解说:Cocoapod 安装以及使用 先上一下效果图,请 ...

  4. python 003 os模块 example

    import os for tmpdir in ('/tmp',r'C:/Users/Administrator/PycharmProjects/'): if os.path.isdir(tmpdir ...

  5. 查询结果多个合并一个GROUP_CONCAT(EmployeeName)

    一个课程多个教师,查询结果单条显示,其中课程与教师关系是一一对应存入表中

  6. 64位BASM学习随笔(一)

     64位BASM学习随笔(一) Delphi的BASM一直是我最喜爱的内嵌汇编语言,同C/C++的内联汇编相比,它更方便,更具灵活性,由于C/C++的内联汇编仅仅能是或插入式的汇编代码,函数花括号 ...

  7. 最经典的SDK程序结构 HelloWin

    程序运行效果:在创建窗口的时候,播放一个声音.且在窗口的客户区中央画一句文字:Hello, Windows 98!,无论程序怎么移动.最大化,文字始终在程序的中央部位. 程序总共分为六个步骤:定义,注 ...

  8. poj--1753--Flip Game(dfs好题)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37201   Accepted: 16201 Descr ...

  9. spring 监听器 IntrospectorCleanupListener简介

    转自:https://blog.csdn.net/ywb201314/article/details/51144256 其中JavaBeans Introspector是一个类,位置在Java.bea ...

  10. Newtonsoft.Json 序列化日期问题解决

    上代码 其中的使用方法和UserInfo实体对象就不贴代码了. /// <summary> /// 把对象转成json字符串 /// </summary> /// <pa ...