题目描述

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  (), 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 has a distinct proficiency rating, , which describes how good she is at her job. If cow  is an ancestor (e.g., a manager of a manager of a manager) of cow , then we say  is a subordinate of .

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  in the company, please count the number of subordinates  where .

奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者!

为了方便,把奶牛从  编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。所有的第  头牛都有一个不同的能力指数 ,描述了她对其工作的擅长程度。如果奶牛  是奶牛  的祖先节点(例如,上司的上司的上司),那么我们我们把奶牛  叫做  的下属。

不幸地是,奶牛们发现经常发生一个上司比她的一些下属能力低的情况,在这种情况下,上司应当考虑晋升她的一些下属。你的任务是帮助奶牛弄清楚这是什么时候发生的。简而言之,对于公司的中的每一头奶牛 ,请计算其下属  的数量满足 

输入输出格式

输入格式:

The first line of input contains .

The next  lines of input contain the proficiency ratings  for the cows. Each is a distinct integer in the range .

The next  lines describe the manager (parent) for cows . Recall that cow 1 has no manager, being the president.

输入的第一行包括一个整数 

接下来的  行包括奶牛们的能力指数 . 保证所有数互不相同,在区间  之间。

接下来的  行描述了奶牛  的上司(双亲节点)的编号。再次提醒,1 号奶牛作为总裁,没有上司。

输出格式:

Please print  lines of output. The th line of output should tell the number of subordinates of cow  with higher proficiency than cow .

输出包括  行。输出的第  行应当给出有多少奶牛  的下属比奶牛  能力高。

输入输出样例

输入样例#1:

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
输出样例#1:

2
0
1
0
0 题解:
1.先读入每个权值然后离散化编号.
2.然后就是遍历这颗树,然后一边修改一边统计.
3.关键:左子树会影响到右子树的答案,那么我们就在进入右子树之前,先统计一边(答案记为Y),出来之后再统计一遍(答案记为X)那么该点的答案就为X-Y
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int gi(){
int str=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-,ch=getchar();
return str;
}
int val[N],b[N],id[N],Tree[N*],n;
#define ls (node<<1)
#define rs (node<<1|1)
void updata(int node){
Tree[node]=Tree[ls]+Tree[rs];
}
void change(int l,int r,int node,int p)
{
if(l>p || r<p)return ;
if(l==p && r==p)
{
Tree[node]++;
return ;
}
int mid=(l+r)>>;
change(l,mid,ls,p);
change(mid+,r,rs,p);
updata(node);
}
int getsum(int l,int r,int node,int sa,int se)
{
if(l>se || r<sa)return ;
if(sa<=l && r<=se)return Tree[node];
int mid=(l+r)>>;
return getsum(l,mid,ls,sa,se)+getsum(mid+,r,rs,sa,se);
}
int mt(int x)
{
int l=,r=n,mid;
while(l<=r)
{
mid=(l+r)>>;
if(b[mid]==x)return mid;
if(b[mid]>x)r=mid-;
else l=mid+;
}
return -;
}
int head[N],num=,ans[N];
struct Lin{
int next,to;
}a[N];
void init(int x,int y){
a[++num].next=head[x];
a[num].to=y;
head[x]=num;
}
void dfs(int x)
{
int tmp=getsum(,n,,id[x]+,n);
for(int i=head[x];i;i=a[i].next)dfs(a[i].to);
ans[x]=getsum(,n,,id[x]+,n)-tmp;
change(,n,,id[x]);
}
int main()
{
n=gi();
int x;
for(int i=;i<=n;i++)val[i]=b[i]=gi();
sort(b+,b+n+);
for(int i=;i<=n;i++)id[i]=mt(val[i]);
for(int i=;i<=n;i++)x=gi(),init(x,i);
dfs();
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}

 

【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化的更多相关文章

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

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

  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. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

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

  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. BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数

    Description The cows have once again tried to form a startup company, failing to remember from past ...

随机推荐

  1. 张旭升20162329 2006-2007-2 《Java程序设计》第一周学习总结

    20162329 2006-2007-2 <Java程序设计>第一周学习总结 教材学习内容总结 通过打书上的代码熟悉了Java编程的基本过程 教材学习中的问题和解决过程 1.因为我的虚拟机 ...

  2. C程序第二次作业

    2-1删除字符串中数字字符 1.设计思路 (1)主要描述题目算法 第一步:遍历指针s所指的s数组. 第二步:如果 * (s+i)在0至9之间的话,则跳过此 * (s+i). 第三步:如果* (s+i) ...

  3. python每日一函数 - divmod数字处理函数

    python每日一函数 - divmod数字处理函数 divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: ...

  4. 【Swift】iOS导航栏错乱的原因

    #iOS开发高级技巧#导航栏错乱,也就是导航栏的显示效果与内容区不匹配,引发原因很多,其中最重要的有两个原因: 1.在viewwillappear,viewwilldisappear两个函数中,设置导 ...

  5. Scrum 冲刺 第五日

    目录 要求 项目链接 燃尽图 问题 今日任务 明日计划 成员贡献量 要求 各个成员今日完成的任务(如果完成的任务为开发或测试任务,需给出对应的Github代码签入记录截图:如果完成的任务为调研任务,需 ...

  6. CentOS7安装配置iptables防火墙

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50779761 CentOS7默认的防火墙不是iptables,而是firewall ...

  7. Vim 中文社区:期待你的加入

    我们的愿景 Vim 中文社区一直比较零散,缺少凝聚力,现有的一些群经常也是水的可以的,讨论各种无关紧要的内容,于是导致很大一部分人,将这些群丢入了群助手,渐渐地他们也淡出了 vim 中文社区. 而我理 ...

  8. C# 启动 SQL Server 服务

    //首先要添加 System.ServiceProcess.dll 引用 ServiceController sc = new ServiceController("MSSQLSERVER& ...

  9. 清除session信息

    session.removeAttribute("sessionname")是清除SESSION里的某个属性.     session.invalidate()是让SESSION失 ...

  10. GNU/Linux需要特别注意的目录

    /bin         存放大多数系统命令,如cat.mkdir.mv.cp.tar.chmod等 /boot       存放开机所需要的文件,开机时载入开机管理程序(bootloader),并映 ...