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. Winform 常用的方法

    一,Winform 如何内嵌窗体 1,判断窗体中是否以还有内嵌窗体 private void ClosePreForm() { foreach (Control item in this.spCont ...

  2. GDC NEC单机自动化设置

    GDC NEC 单机自动化设置 进入播放列表   进入设置,进入登陆,请选择维修员登陆,输入密码257910   选择“一般选项”中的“自动化” 在进入的新菜单中选择“设备”,添加一个新的名称,默认的 ...

  3. 532 -数组中的K-diff对

    例1: 输入: [3,1,4,1,5],k = 2  输出: 2 说明:阵列中有两个2-diff对,(1,3)和(3,5). 虽然我们在输入中有两个1,但我们应该只返回唯一对的数量. 例2: 输入: ...

  4. HDFS要点剖析

    谈到大数据,不得不提的一个名词是"HDFS".它是一种分布式文件存储系统,其系统架构图如下图所示: 从图中可以了解到的几个关键概念 元数据(MetaData) 机架(Rock) 块 ...

  5. Java虚拟机_运行时数据区

    Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域. 这些区域都有各自的用途.各自的创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有些区域则是依赖用户线程启动 ...

  6. Java 使用new Thread和线程池的区别

    本文转至:https://www.cnblogs.com/cnmenglang/p/6273761.html , 孟凡柱的专栏 的博客,在此谢谢博主! 1.new Thread的弊端执行一个异步任务你 ...

  7. POJ1426(KB1-E 暴力搜索)

    Find The Multiple   Description Given a positive integer n, write a program to find out a nonzero mu ...

  8. Android手动显示和隐藏软键盘

    1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Contex ...

  9. OpenGL学习—04--彩色立方体

        1.tutorial04.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> ...

  10. android设计的布局在阿拉伯语下界面错乱的解决方法

    (1)正在AndroidManifest.xml声明文件的application元素中,增加” android:supportsRtl=true” (2)建] androidの设计的布局在阿拉伯语下界 ...