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

线段树合并裸题。那种返回 int 的与传引用的 merge 都能过。不知别的题是不是这样。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+,M=N**;
int n,m,a[N],tp[N],rt[N],hd[N],xnt,to[N],nxt[N],ans[N];
int tot,ls[M],rs[M],siz[M];
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
void add(int x,int y)
{
to[++xnt]=y;nxt[xnt]=hd[x];hd[x]=xnt;
} void merge(int &x,int y)
{
if(!x){x=y;return;}
siz[x]+=siz[y];
if(ls[y])merge(ls[x],ls[y]);
if(rs[y])merge(rs[x],rs[y]);
}
/*
int merge(int x,int y)
{
if(!x||!y)return x+y;
int d=++tot;
siz[d]=siz[x]+siz[y];
ls[d]=merge(ls[x],ls[y]);
rs[d]=merge(rs[x],rs[y]);
return d;
}
*/
int query(int l,int r,int cr,int L,int R)
{
if(!cr)return ;if(l>=L&&r<=R)return siz[cr];
int mid=l+r>>,ret=;
if(mid>=L)ret=query(l,mid,ls[cr],L,R);
if(mid<R)ret+=query(mid+,r,rs[cr],L,R);
return ret;
}
void insert(int l,int r,int &cr,int p)
{
if(!cr)cr=++tot; siz[cr]++;
if(l==r)return; int mid=l+r>>;
if(mid>=p) insert(l,mid,ls[cr],p);
else insert(mid+,r,rs[cr],p);
}
void dfs(int cr)
{
for(int i=hd[cr],v;i;i=nxt[i])
dfs(v=to[i]),/*rt[cr]=*/merge(rt[cr],rt[v]);
ans[cr]=query(,m,rt[cr],a[cr],m);
insert(,m,rt[cr],a[cr]);
}
int main()
{
n=rdn(); for(int i=;i<=n;i++)a[i]=tp[i]=rdn();
sort(tp+,tp+n+); m=unique(tp+,tp+n+)-tp-;
for(int i=;i<=n;i++)a[i]=lower_bound(tp+,tp+n+,a[i])-tp;
for(int i=,d;i<=n;i++)
{
d=rdn(); add(d,i);
}
dfs();
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. ggplot2-为图形加入直线

    本文更新地址:http://blog.csdn.net/tanzuozhev/article/details/51112057 本文在 http://www.cookbook-r.com/Graphs ...

  2. Go语言中的单引号、双引号、反引号

    =Start= 搜索关键字: golang single quotes golang double quotes golang back quotes 参考结果: 结论写在最前:在Go语言中不倾向于使 ...

  3. DataTable和DataRow利用反射直接转换为Model对象的扩展方法类

    DataTable和DataRow利用反射直接转换为Model对象的扩展方法类   /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...

  4. 【Sprint3冲刺之前】项目完成时间表

    Sprint2冲刺成果——项目完成时间表 项目完成时间表 经过Sprint2小组总结会议的探讨,我们决定一起约定项目alpha版本,beta版本,release版本的发布时间,发布形式及预计完成效果 ...

  5. A20 Gate信号

    https://doc.docsou.com/ba8e6b0612d6a989b7cebeaae-5.html Gate A20 Option 功能:设置A20 地址线的控制模式 设定值:Fast 或 ...

  6. kubernetes之StatefulSet详解

    系列目录 概述 RC.Deployment.DaemonSet都是面向无状态的服务,它们所管理的Pod的IP.名字,启停顺序等都是随机的,而StatefulSet是什么?顾名思义,有状态的集合,管理所 ...

  7. 利用CH341A编程器刷新BIOS,恢复BIOS,妈妈再也不用担心BIOS刷坏了

    前几天,修电脑主析就捣鼓刷BIOS,结果刷完黑屏开不了机,立刻意识到完了,BIOS刷错了.就从网上查资料,各种方法试了个遍,什么用处都没有.终于功夫不负有心人,找到了编码器,知道了怎么用.下面看看具体 ...

  8. 谈谈 T 型人才

    谈谈 T 型人才   昨天的图片发模糊了,正好我把这个话题展开聊一聊吧.这个话题是关于复合型人才的,我把它称作 T 型人才. 「全栈」工程师 前一段时间,「全栈」工程师的概念很火,不过大多数时候,「全 ...

  9. 关于erlang反编译的东西

    在查阅了相关文档,想了解erlang反编译的东西.当然,源码可以打包成可以获取源码的,也可以保护源码的. 在ebin下,如果没有或者找不到源码,可以进行反编译,由beam文件得到erl文件. 可以通过 ...

  10. Eclipse-----Eclipse断点调试