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
 
线段树合并
代码:
 #include<iostream>
#include<cstdio>
#include<algorithm>
#define M 100010
#define ls ch[node][0]
#define rs ch[node][1]
using namespace std; int n,m,num,cnt;
int head[M],a[M],b[M],ans[M],rt[M];
int v[M<<],ch[M<<][];
struct point{int to,nxt;}e[M<<]; void add(int from,int to)
{
e[++num].nxt=head[from];
e[num].to=to;
head[from]=num;
} void insert(int &node,int l,int r,int x)
{
if(!node) node=++cnt;v[node]++;
if(l==r) return;
int mid=(l+r)/;
if(x<=mid) insert(ls,l,mid,x);
else insert(rs,mid+,r,x);
} int query(int node,int l,int r,int l1,int r1)
{
if(!node) return ;
if(l1<=l&&r1>=r) return v[node];
if(l1>r||r1<l) return ;
int mid=(l+r)/;
return query(ls,l,mid,l1,r1)+query(rs,mid+,r,l1,r1);
} int merge(int x,int y)
{
if(!x||!y) return x+y;
int node=++cnt;
v[node]=v[x]+v[y];
ls=merge(ch[x][],ch[y][]);
rs=merge(ch[x][],ch[y][]);
return node;
} void dfs(int x)
{
for(int i=head[x];i;i=e[i].nxt)
{
int to=e[i].to;
dfs(to);
rt[x]=merge(rt[x],rt[to]);
}
ans[x]=query(rt[x],,m,a[x]+,m);
insert(rt[x],,m,a[x]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),b[++m]=a[i];
sort(b+,b++m); m=unique(b+,b++m)-b-;
for(int i=;i<=n;i++) a[i]=lower_bound(b+,b++m,a[i])-b;
for(int i=;i<=n;i++)
{
int x;scanf("%d",&x);
add(x,i);
}
dfs();
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}

[BZOJ4756]Promotion Counting的更多相关文章

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

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

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

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

  3. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu 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. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

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

  6. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

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

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

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

  8. BZOJ4756:[USACO]Promotion Counting(线段树合并)

    Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...

  9. bzoj4756 [Usaco2017 Jan]Promotion Counting

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

随机推荐

  1. Hadoop伪分布安装详解(四)

    目录: 1.修改主机名和用户名 2.配置静态IP地址 3.配置SSH无密码连接 4.安装JDK1.7 5.配置Hadoop 6.安装Mysql 7.安装Hive 8.安装Hbase 9.安装Sqoop ...

  2. 在nginx启动后,如果我们要操作nginx,要怎么做呢 别增加无谓的上下文切换 异步非阻塞的方式来处理请求 worker的个数为cpu的核数 红黑树

    nginx平台初探(100%) — Nginx开发从入门到精通 http://ten 众所周知,nginx性能高,而nginx的高性能与其架构是分不开的.那么nginx究竟是怎么样的呢?这一节我们先来 ...

  3. ES6中的let和const

    let和const let 用来声明变量,但是所声明的变量只在let命令所在的代码块内有效 { let a=12 alert(a)//12 } alert(a)//报错 找不到 let不像var那样会 ...

  4. 自建YUM仓库

    YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系. 要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库),这个 ...

  5. POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan

    该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...

  6. 13.Query for Null or Missing Fields-官方文档摘录

    1 插入数据 db.inventory.insertMany([ { _id: 1, item: null }, { _id: 2 } ]) 2 查询null值 db.inventory.find({ ...

  7. uchome android 开发记录

    一.uchome 1.无法转移临时图片到服务器指定目录 cp_upload.php----------- function.cp.php ---------mobile_picture_tempora ...

  8. Powerdesigner显示列名

    设置要修改的列 点击ok即可.

  9. 360UI 界面框架 即QUI框架与EXT比较

    EXTJS框架是非常全面和成熟的,这是因为它发展的年头久远,并且有全世界的EXTJS爱好者为其出谋献策,它的组件库尤其是DataGrid组件无人能出其右.我在最初也考虑过使用EXTJS来做项目,学习了 ...

  10. flatpickr功能强大的日期时间选择器插件

    flatpickr日期时间选择器支持移动手机,提供多种内置的主题效果,并且提供对中文的支持.它的特点还有: 使用SVG作为界面的图标. 兼容jQuery. 支持对各种日期格式的解析. 轻量级,高性能, ...