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. 关于LegacyExchangeDN的问题

    IMCEAEX i NDR This problem is not common but quite annoying and usually hard to understood by users. ...

  2. Android 查看自己的keystore的别名及相关信息

    1.在DOS窗口下进入自己的keystore所在位置,输入 keytool -list  -v -keystore xxxx.keystore -storepass 密码 xxxx.keystore是 ...

  3. Linux(5)- MariaDB、mysql主从复制、初识redis

    一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...

  4. 前端 Dom 直接选择器

    文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...

  5. mongoDb,下载及启动

     mongoDb下载 https://www.mongodb.com/download-center 可视化工具Robomongo下载 https://robomongo.org/download m ...

  6. windows平台tensorboard的配置及使用

    由于官网和其他教程里面都是以Linux为平台演示tensorboard使用的,而在Windows上与Linux上会有一些差别,因此我将学习的过程记录下来与大家分享(基于tensorflow1.2.1版 ...

  7. C的指针疑惑:C和指针8数组

    ]; ]; 上面申明两个数组,不能进行以下赋值 b = a; 你不能使用赋值符把一个数组的所有元素复制给另一个数组,必须使用一个循环,每次复制一个元素 数组和指针 ]; int *b; 声明一个数组, ...

  8. PAT 1121 Damn Single[简单]

    1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...

  9. SpringMVC-SimpleDEMO

    本博文主要将如何配置一个简单的SpringMVC的DEMO,由上一讲的SpringMVC工作流程来看,配置一个SpringMVC的步骤是简单而清晰的. 一.引入SpringMVC所需依赖   < ...

  10. RF的优缺点

    随机森林有什么优点,如: a. 对于很多数据集表现良好,精确度比较高: b. 不容易过拟合: c. 可以得到变量的重要性排序: d. 既能处理离散型数据,也能处理连续型数据,且不需要进行归一化处理: ...