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

//针对所有点先分别建立权值线段树,然后进行线段树的合并
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
int n,cnt;
int pre[maxn],son[maxn],now[maxn];
int val[maxn],rt[maxn],ans[maxn],tmp[maxn]; int read()
{
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+ch-'0';
return x*f;
} struct segment_tree
{ int tot;
int siz[maxn*20],ls[maxn*20],rs[maxn*20]; void update(int p)
{
siz[p]=siz[ls[p]]+siz[rs[p]];
} void build(int &p,int l,int r,int v)
//T.build(rt[i],1,n,val[i]);
{
if(!p)
p=++tot;
if(l==r)
{
siz[p]=1;//第p个结点的大小为1
return;
}
int mid=(l+r)>>1;
if(v<=mid)
build(ls[p],l,mid,v);
else
build(rs[p],mid+1,r,v);
update(p);
} int merge(int x,int y)
{
if(x==0||y==0)
return x+y;
ls[x]=merge(ls[x],ls[y]);
rs[x]=merge(rs[x],rs[y]);
update(x);
return x;
} int find(int p,int l,int r,int v)
{
if(p==0)
return 0;
int mid=(l+r)>>1;
if(v<=mid)
return find(ls[p],l,mid,v);
return siz[ls[p]]+find(rs[p],mid+1,r,v);
}
}T; void add(int a,int b)
{
pre[++cnt]=now[a];
now[a]=cnt,son[cnt]=b;
} int dfs(int u)
{
int root=0;
for(int p=now[u],v=son[p];p;p=pre[p],v=son[p])
root=T.merge(root,dfs(v));
//将以u为父亲的所有点的线段树进行合并,合并完了后再来统计
ans[u]=T.siz[root]-T.find(root,1,n,val[u]);
//用总结点个数减去小于等于val[u]的
return T.merge(root,rt[u]);
//将u与上面形成的线段树再合并 } int main()
{
n=read();
for(int i=1;i<=n;i++)
tmp[i]=val[i]=read();
sort(tmp+1,tmp+n+1);
int sum=unique(tmp+1,tmp+n+1)-tmp-1;
for(int i=1;i<=n;i++)
{
val[i]=lower_bound(tmp+1,tmp+sum+1,val[i])-tmp;
T.build(rt[i],1,n,val[i]);
//先针对每个点建立一个线段树出来
}
for(int i=2;i<=n;i++)
{
int f=read();
add(f,i);//i与其父亲f连边
}
dfs(1);
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}

  

[Usaco2017 Jan]Promotion Counting的更多相关文章

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

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

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

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

  3. 【bzoj 4756】[Usaco2017 Jan] Promotion Counting

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

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

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

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

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

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

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

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

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

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

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

  9. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

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

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

随机推荐

  1. python 反射、动态导入

    1. 反射 hasattr(obj,'name')            # 判断对象中是否含有字符串形式的方法名或属性名,返回True.False getattr(obj,'name',None)  ...

  2. 关于 html button 点击刷新页面的问题

    如果不想点击button 刷新页面的话,需要加个属性   type="button" 如下: <button class="layui-btn" type ...

  3. npm 全局安装和局部安装的区别

    上图是从网上找的webpack 安装的步骤,我们看到除了要全局安装之外,还需要本地安装,那么这两者有什么区别呢? 本文以Windows平台上做测试,以webpack为示例做教程 什么是全局安装? 安装 ...

  4. 【NOIP2016提高A组模拟8.23】函数

    题目 分析 观察这个是式子\(\sum_{d|n}f(n)=n\), 发现其实函数\(f()\)就是欧拉函数\(φ()\)(见http://blog.csdn.net/chen1352/article ...

  5. PROP_ENTRY_TYPE用法

    最近几天刚换了vs2019,然后各种水土不服 这不 刚建了一个ATL组件,发现添加了属性编译不过,提示: 错误 C4995 “PROP_ENTRY”: 名称被标记为 #pragma deprecate ...

  6. 使用ros_driver运行velodyne16线激光雷达

    一.使用ros_driver运行VLP16 推荐网址: http://blog.csdn.net/littlethunder/article/details/51920681 https://www. ...

  7. Android开源SlidingMenu的使用

    一.SlidingMenu简介 SlidingMenu是最常用的几个开源项目之一. GitHub上的开源项目Slidingmenu提供了最佳的实现:定制灵活.各种阴影和渐变以及动画的滑动效果都不错.不 ...

  8. javascript 链式调用+构造函数

    前几天面试,有一个问题是使用构造函数实现链式调用,后面查看了一些简单的资料,整理一下 首先,先说一下JS 中构造函数和普通函数的区别,主要分为以下几点 1.构造函数也是一个普通函数,创建方式和普通函数 ...

  9. [nginx] CORS配置多域名

    如下 server { listen 80; server_name www.your.com; root /data/web/www.your.com; access_log /var/log/ng ...

  10. linux系统/var目录的作用

    linux系统/var目录的作用 一.总结 一句话总结: 1.如果/usr是安装时会占用较大硬盘容量目录,那么/var就是在系统运行后才会渐渐占用硬盘容量的目录. 2.因为var目录主要针对常态性变动 ...