题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756

合并子树的权值线段树;

merge 返回 int 或者是 void 都可以。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((l+r)>>1)
using namespace std;
int const xn=1e5+,xm=xn*;//
int n,p[xn],b[xn],hd[xn],ct,to[xn<<],nxt[xn<<];
int cnt,rt[xn],ls[xm],rs[xm],sum[xm],ans[xn],tot;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
int gt[];
void wr(int x)
{
if(!x){puts(""); return;}
if(x<)putchar('-'),x=-x;
int t=;
while(x)gt[++t]=x%,x/=;
for(int i=t;i;i--)putchar(gt[i]+'');
puts("");
}
void add(int x,int y){to[++ct]=y; nxt[ct]=hd[x]; hd[x]=ct;}
void merge(int &x,int y)
{
if(!x){x=y; return;}
sum[x]+=sum[y];
if(ls[y])merge(ls[x],ls[y]);//if
if(rs[y])merge(rs[x],rs[y]);//if
}
/*
int merge(int x,int y,int l,int r)
{
if(!x||!y)return x+y;
sum[x]+=sum[y];
if(l==r)return x;
ls[x]=merge(ls[x],ls[y],l,mid);
rs[x]=merge(rs[x],rs[y],mid+1,r);
return x;
}
*/
void add(int &x,int l,int r,int pos)
{
if(!x)x=++cnt; sum[x]++;
if(l==r)return;
if(pos<=mid)add(ls[x],l,mid,pos);
else add(rs[x],mid+,r,pos);
}
int query(int x,int l,int r,int L,int R)
{
if(!x)return ;
if(l>=L&&r<=R)return sum[x];
int ret=;
if(mid>=L)ret+=query(ls[x],l,mid,L,R);
if(mid<R)ret+=query(rs[x],mid+,r,L,R);
return ret;
}
void dfs(int x)
{
for(int i=hd[x],u;i;i=nxt[i])
{
dfs(u=to[i]);
// rt[x]=merge(rt[x],rt[u],1,tot);
merge(rt[x],rt[u]);
}
ans[x]=query(rt[x],,tot,p[x],tot);
add(rt[x],,tot,p[x]);
}
int main()
{
n=rd();
for(int i=;i<=n;i++)p[i]=rd(),b[i]=p[i];
sort(b+,b+n+); tot=unique(b+,b+n+)-b-;
for(int i=;i<=n;i++)p[i]=lower_bound(b+,b+tot+,p[i])-b;
for(int i=,fa;i<=n;i++)fa=rd(),add(fa,i);
dfs();
for(int i=;i<=n;i++)wr(ans[i]);
return ;
}

bzoj 4756 Promotion Counting —— 线段树合并的更多相关文章

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

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

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

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

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

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

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

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

  5. BZOJ.3545.[ONTAK2010]Peaks(线段树合并)

    题目链接 \(Description\) 有n个座山,其高度为hi.有m条带权双向边连接某些山.多次询问,每次询问从v出发 只经过边权<=x的边 所能到达的山中,第K高的是多少. \(Solut ...

  6. bzoj 4631: 踩气球 线段树合并

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 265  Solved: 136[Submit][Status][Discuss] ...

  7. BZOJ:5457: 城市(线段树合并)(尚待优化)

    5457: 城市 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 18  Solved: 12[Submit][Status][Discuss] Des ...

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

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

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

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

随机推荐

  1. spark学习(五)总结及其demo

    RDD及其特点 1.RDD是Spark的核心数据模型,但是个抽象类,全称为Resillient Distributed Dataset,即弹性分布式数据集. 2.RDD在抽象上来说是一种元素集合,包含 ...

  2. 【Java TCP/IP Socket】基于NIO的TCP通信(含代码)

    NIO主要原理及使用 NIO采取通道(Channel)和缓冲区(Buffer)来传输和保存数据,它是非阻塞式的I/O,即在等待连接.读写数据(这些都是在一线程以客户端的程序中会阻塞线程的操作)的时候, ...

  3. Codeforces Round #266 (Div. 2) C. Number of Ways

    You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split ...

  4. Python奇技

    本文目录 1. 显示有限的接口到外部 2. with的魔力 3. filter的用法 4. 一行作判断 5. 装饰器之单例 6. staticmethod装饰器 7. property装饰器 8. i ...

  5. Linux系统调用(syscall)原理(转)

    引言:分析Android源码的过程中,要想从上至下完全明白一行代码,往往涉及app.framework.native一直到kernel,可能迷失到代码世界,明白了系统调用原理,或许能帮你峰回路转,找到 ...

  6. ruby rails

    http://www.zhihu.com/question/19552402   作者:陈振宇链接:http://www.zhihu.com/question/19552402/answer/1236 ...

  7. TinyXML:TiXmlNode

    TiXmlNode: 对应于DOM结构中的节点,是TinyXML中很重要的一个类,它继承自TiXmlBase TiXmlNode的友元类: friend class TiXmlDocument; fr ...

  8. 64位CentOs7源码安装mysql-5.6.35过程分享

    首先安装依赖包,避免在安装过程中出现问题 [root@bogon liuzhen]# yum -y install gcc gcc-c++[root@bogon liuzhen]# yum -y in ...

  9. firefox浏览器和IE

    http://blog.csdn.net/pipisorry/article/details/40899701 firefox浏览器插件 [下载地址add-ons for firefox]皮皮blog ...

  10. 文件读写&&内容替换

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...