Description

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

Input

n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)

Output

共n行,每行输出奶牛i的下属中有几个能力值比i大

Sample Input

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

Sample Output

2
0
1
0
0

Solution

线段树合并模板题,$DFS$一遍,然后把儿子的线段树合并到自己身上,查询比自己能力值大的有多少个。

Code

 #include<iostream>
#include<cstdio>
#define N (100009)
#define INF (1000000000)
using namespace std; struct Sgt{int ls,rs,val;}Segt[N<<];
struct Edge{int to,next;}edge[N<<];
int n,x,sgt_num,a[N],ans[N],Root[N];
int head[N],num_edge; void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Update(int &now,int l,int r,int x)
{
if (!now) now=++sgt_num;
Segt[now].val++;
if (l==r) return;
int mid=(l+r)>>;
if (x<=mid) Update(Segt[now].ls,l,mid,x);
else Update(Segt[now].rs,mid+,r,x);
} int Query(int now,int l,int r,int l1,int r1)
{
if (l>r1 || r<l1) return ;
if (l1<=l && r<=r1) return Segt[now].val;
int mid=(l+r)>>,ls=Segt[now].ls,rs=Segt[now].rs;
return Query(ls,l,mid,l1,r1)+Query(rs,mid+,r,l1,r1);
} void Merge(int &x,int y)
{
if (!x || !y) {x|=y; return;}
Segt[x].val+=Segt[y].val;
Merge(Segt[x].ls,Segt[y].ls);
Merge(Segt[x].rs,Segt[y].rs);
} void DFS(int x,int fa)
{
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].to!=fa)
{
DFS(edge[i].to,x);
Merge(Root[x],Root[edge[i].to]);
}
ans[x]=Query(Root[x],,INF,a[x]+,INF);
} int main()
{
scanf("%d",&n);
for (int i=; i<=n; ++i)
{
scanf("%d",&a[i]);
Update(Root[i],,INF,a[i]);
}
for (int i=; i<=n; ++i)
{
scanf("%d",&x);
add(i,x); add(x,i);
}
DFS(,);
for (int i=; i<=n; ++i)
printf("%d\n",ans[i]);
}

BZOJ4756:[USACO]Promotion Counting(线段树合并)的更多相关文章

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

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

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

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

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

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

  4. bzoj 4756 Promotion Counting —— 线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...

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

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

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

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

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

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

  8. 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)

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

  9. [模板]BZOJ4756线段树合并

    题面 Solution: 板子不解释 #include <iostream> #include <algorithm> #include <cstdio> #inc ...

随机推荐

  1. Go 中包导入声明

    Go中的程序由软件包组成.通常,软件包依赖于其他软件包,或者内置于标准库或第三方的软件包.包需要先导入才能使用其导出的标识符.本文将翻译一篇国外的文章,用于介绍包导入的原理以及几种常用的导入方式. & ...

  2. Python 两个list合并成一个字典

    方法一:list1 = ['k1','k2','k3'] list2 = ['v1','v2','v3'] dic = dict(map(lambda x,y:[x,y],list1,list2)) ...

  3. 微信网页授权获取code

    <script> var code = GetQueryString('code'); var callback = 'personal.html'; var appId = " ...

  4. setInterval和setTimeout的区别以及setInterval越来越快问题的解决方法

    setInterval()和setTimeout()方法都是js原生的定时方法,当然它们两个的作用也是不同的,并且最近在做上下滚动公告栏的时候,发现了setInterval()非常令人抓狂的问题,那就 ...

  5. Spring Boot—18Redis

    pom.xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-ja ...

  6. 行动学习方法----PARR

  7. oracle 导入导出 dmp 的三种方式

    1.命令行参数 比如:exp scott/tiger@orcl tables=emp file=D:\test.dmp 2.交互提示符 比如:C:\Users\Administrator>exp ...

  8. Pig是轻类型的

    总体来说Pig是“强类型”的,但Pig又允许用户不指定输入数据的类型,而可以自己根据用户的使用方式进行推测. 称Pig是“轻类型”的更合适,它确实对类型有严格的要求,但是如果没有明确定义类型也是可以处 ...

  9. Django 多表查询 聚合查询 分组查询 F查询 Q查询

    # -------------------------------------------------------------------------------------------------- ...

  10. CSS 小结笔记之选择器

    Css选择器主要分为以下几类 类选择器 ID选择器 通配符选择器 标签选择器 伪类选择器 复合选择器 1.类选择器:通过.classname 来选择 例如 .color2 { color: rebec ...