Description

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号奶牛为树根。

问对于每个奶牛来说,它的子树中有几个能力值比它大的。

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<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int N=1e5+;
int n,cnt,tot;
int id[N],p[N],fa[N],first[N],ans[N],root[N];
int ls[N*],rs[N*],tr[N*];
struct edge{int to,next;}e[N];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void ins(int u,int v){e[++tot]=(edge){v,first[u]};first[u]=tot;}
void insert(int l,int r,int& pos,int num,int w)
{
pos=++cnt;tr[pos]+=w;
if(l==r)return;
int mid=(l+r)>>;
if(num<=mid)insert(l,mid,ls[pos],num,w);
else insert(mid+,r,rs[pos],num,w);
}
int merge(int now,int last)
{
if(!now||!last)return now^last;//子树为空就直接并上去
ls[now]=merge(ls[now],ls[last]);
rs[now]=merge(rs[now],rs[last]);
tr[now]=tr[ls[now]]+tr[rs[now]];
return now;
}
int query(int l,int r,int pos,int L,int R)
{
if(L<=l&&R>=r)return tr[pos];
int sum=,mid=(l+r)>>;
if(L<=mid)sum+=query(l,mid,ls[pos],L,R);
if(R>mid)sum+=query(mid+,r,rs[pos],L,R);
return sum;
}
void dfs(int x)
{
insert(,n,root[x],id[x],);
for(int i=first[x];i;i=e[i].next)dfs(e[i].to);
for(int i=first[x];i;i=e[i].next)root[x]=merge(root[x],root[e[i].to]);
ans[x]=query(,n,root[x],id[x]+,n);
}
int main()
{
n=read();
for(int i=;i<=n;i++)id[i]=p[i]=read();
sort(p+,p+n+);
for(int i=;i<=n;i++)id[i]=lower_bound(p+,p+n+,id[i])-p;
for(int i=;i<=n;i++)fa[i]=read(),ins(fa[i],i);
dfs();
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}

【bzoj 4756】[Usaco2017 Jan] Promotion Counting的更多相关文章

  1. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...

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

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

  3. BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...

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

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

  5. 【BZOJ】4756: [Usaco2017 Jan]Promotion Counting

    [题意]带点权树,统计每个结点子树内点权比它大的结点数. [算法]线段树合并 [题解]对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计. 注意三个量tot,cnt,tots,细心 ...

  6. bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】

    思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...

  7. bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...

  8. 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

    调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...

  9. BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并

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

随机推荐

  1. shell中的source和直接执行sh的区别

    首先我们知道我们执行shell有这么几种方法 1. sh/bash使用其内置的命令集来执行一些命令,例如如下 sh demo.sh bash demo.sh 2. 使用./或者/$SHELLPATH/ ...

  2. 牛客小白月赛12 F 华华开始学信息学 (分块+树状数组)

    链接:https://ac.nowcoder.com/acm/contest/392/F来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语言65536K ...

  3. HDU--4417 Super Mario (主席树模版题)

    题目链接 题目让求 L R区间 不大于H 的数有多少 数据太大需要离散化 #include<bits/stdc++.h> using namespace std; #define maxn ...

  4. Nginx宣布正式支持gRPC,附示例代码

    原创 2018-03-20 薛命灯 聊聊架构 作者|Owen Garrett编辑|薛命灯 NGINX 官方博客正式宣布 NGINX 支持原生的 gPRC,现在就可以从代码仓库拉取快照版本.该特性将会被 ...

  5. Home School Books美国家庭学校教育小学初中高中全套美语教材

    加州的资料总共买过三次: ①优妈妈儿童教育,买过美国加州小学一.二年级的语文及相应的练习册,并买了纸版资料. (这是自己学习用的) ②美国加州原版小学教材Reading Wonders 2014新版语 ...

  6. MacBook上使用ssh localhost被拒绝

    最开始以为没有装sshd呢,实际上不是.又查了些方法,有些人居然把Linux上访问失败的方法直接照搬,让我建立公私钥.都没有成功.最后找到了解决方法,原来是由于苹果的安全限制,限制了这个功能. sud ...

  7. A1099. Build A Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  8. adb导出安卓 把手机内存文件导入到电脑里 adb安装软件

    记得先找对路劲adb shellls 最上面的ls: ./ 打头的没有权限.而下面的这些acct sdcard等 都有权限. 然后cd sdcardls 看下目录,发现gxm文件夹在sdcard下面. ...

  9. c#文件管理

    Directory类-------目录管理 Directory.CreateDirectory(_Path); bool IsExit=Directory.Exists(_Path); Directo ...

  10. SpringCloud之Hystrix断路器以及dashboard 属性详解

    1.自定义hystrixCommand: https://blog.csdn.net/u012702547/article/details/78032191?utm_source=tuicool&am ...