【题意】带点权树,统计每个结点子树内点权比它大的结点数。

【算法】线段树合并

【题解】对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计。

注意三个量tot,cnt,tots,细心查错。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
int n,first[maxn],cnt,tot,tots,root[maxn],a[maxn],b[maxn],ans[maxn];
struct edge{int v,from;}e[maxn*];
void ins(int u,int v){tots++;e[tots].v=v;e[tots].from=first[u];first[u]=tots;}
struct cyc{int l,r,sum;}t[maxn*];
void insert(int l,int r,int &x,int y){
if(!x)x=++cnt;t[x].sum++;
if(l==r)return;//
int mid=(l+r)>>;
if(y<=mid)insert(l,mid,t[x].l,y);
else insert(mid+,r,t[x].r,y);
}
int merge(int x,int y){
if(!x||!y)return x^y;//
t[x].l=merge(t[x].l,t[y].l);
t[x].r=merge(t[x].r,t[y].r);//
t[x].sum=t[t[x].l].sum+t[t[x].r].sum;//
return x;
}
int ask(int left,int right,int k,int l,int r){
if(l<=left&&right<=r)return t[k].sum;
else{
int mid=(left+right)>>,sum=;
if(l<=mid)sum=ask(left,mid,t[k].l,l,r);
if(r>mid)sum+=ask(mid+,right,t[k].r,l,r);
return sum;
}
}
void dfs(int x){
insert(,tot,root[x],a[x]);
for(int i=first[x];i;i=e[i].from){
dfs(e[i].v);
root[x]=merge(root[x],root[e[i].v]);
}
if(a[x]<tot)ans[x]=ask(,tot,root[x],a[x]+,tot);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
for(int i=;i<=n;i++){
int fa;scanf("%d",&fa);
ins(fa,i);
}
sort(b+,b+n+);tot=n;
tot=unique(b+,b+tot+)-b-;
for(int i=;i<=n;i++)a[i]=lower_bound(b+,b+tot+,a[i])-b;
dfs();
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

  7. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

    [题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...

  8. bzoj4756 [Usaco2017 Jan]Promotion Counting

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

  9. 【BZOJ】1576 [Usaco2009 Jan]安全路经Travel

    [算法]最短路树+(树链剖分+线段树)||最短路树+并查集 [题解] 两种方法的思想是一样的,首先题目限制了最短路树唯一. 那么建出最短路树后,就是询问对于每个点断掉父边后重新找路径的最小值,其它路径 ...

随机推荐

  1. Jquery获取属性值

    jq获取某个标签内的属性值:$("#TeamPerformanceYearUl li:eq(0)").attr('data') jq获取li或者td第一个属性(索引值从零开始)$( ...

  2. Linux安装weblogic

    一.软件安装 1. 安装前的准备工作 1.1 首先请确认您要安装的WebLogic版本所在的平台已通过了BEA的认证,完整的认证平台列表请参考 http://e-docs.bea.com/wls/ce ...

  3. ZOJ 2060 A-Fibonacci Again

    https://vjudge.net/contest/67836#problem/A There are another kind of Fibonacci numbers: F(0) = 7, F( ...

  4. Python2 获取docx/doc文件内容

    整体思路: 下载文件并修改后缀为zip文件,解压zip文件,所要获取的内容在固定的文件夹下:work/temp/word/document.xml 所用包,全部是python自带,不需要额外下载安装. ...

  5. 查询出menupath字段中 出现 “- "(横杆)大于3次的 记录

  6. 10个linux网络和监控命令

    我下面列出来的10个基础的每个linux用户都应该知道的网络和监控命令.网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nsloo ...

  7. this.$http & vue

    this.$http & vue https://github.com/pagekit/vue-resource Alias axios to Vue.prototype.$http http ...

  8. Building simple plug-ins system for ASP.NET Core(转)

    Recently I built plug-ins support to my TemperatureStation IoT solution web site. The code for .NET ...

  9. Centos7安装完毕后联网-设置ip地址(VMware虚拟机)

    VMware虚拟机中安装了Centos7,为了让Centos能够访问外网及设置固定的ip地址以方便本地通过SSH访问Centos,做以下几步.本文来自osfipin note. 1.确认虚拟机网络链接 ...

  10. C++解析(14):静态成员变量与静态成员函数

    0.目录 1.静态成员变量 2.静态成员函数 3.小结 1.静态成员变量 成员变量的回顾: 通过对象名能够访问public成员变量 每个对象的成员变量都是专属的 成员变量不能在对象之间共享 新的需求: ...