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. QBC检索和本地SQL检索

    细说QBC:QBC(Query By Criteria) 查询:这种方式比较面向对象方式,因为是面向对象,所以查询时参数名就是所查询的类的属性名并不是数据库的表的列名重点是有三个描述条件的对象:Res ...

  2. Android Handler 的使用

    Android UI 操作是线程不安全的.我们只能在UI线程或者说主线程中修改UI.试想多个Thread操作同一个UI,可能引起不一致.UI 线程的主要工作是:UI界面更新显示,各个控件的交互等等.一 ...

  3. Macbook pro 13" compile Apollo 2.5

    STEPS: 0. Install Homebrew 1.  Install 'Docker for Mac 18.03+',配置CPUs (n个CPUs,Bazel开n个线程编译), Memory ...

  4. 原型模式(Prototype Pattern)--对象的克隆

    定义:使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象; 原型类的核心在于如何实现克隆方法: 能够实现克隆的Java类必须实现一个标识接口Cloneable,表示这个类支持被复制; 通 ...

  5. django-网页视屏播放

    基本都基于第三方: -cc视频 -播放免费视频 -收费视频 -需要做认证,cc视频会给你发消息,你返回,携带数据 -在前端页面中添加响应的视屏框的代码 -功能实现,有相关接口文档,配置即可

  6. python-gitlab 模块

    安装:pip install python-gitlab import gitlab # 登录 gl = gitlab.Gitlab('http://127.0.0.1', private_token ...

  7. oracle建表设置主键自增

    首先创建一张表 create table member( memberId number primary key, memberMail )not null, memberName ) not nul ...

  8. html table表格列数太多添加横向滚动条

    HTML的table表格的列数如果太多或者某一列的内容太长,就会导致表格td的内容被挤压变形,对后台的使用体验非常不友好.比如下面的情况: 那么如何在表格列数较多的情况下添加横向滚动条?其实很简单,只 ...

  9. Linux系统——Raid磁盘阵列

    Raid磁盘阵列 作用:解决磁盘速度.安全问题 Raid原理 Raid0 写入速度极快,有几块硬盘,写入速度就近似几倍,但是安全性极差,只要一块盘坏了,所有盘的数据全部坏掉,最少两块硬盘组合 性价比最 ...

  10. 怎么创建Porlet项目

    首先找到你liferay安装的地址D:\liferay\liferay-bundles-6.1.1\plugins-sdk,找到portlets这个文件夹点击(shift+鼠标右键)进入命令行窗口,写 ...