线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数
题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写。。记得离散化
线段树合并版:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=(1e5)+;
int N,num_edge=,edge_head[maxn],W[maxn],lsh_cnt=,num_treenode=;
int root[maxn],ans[maxn],u;
struct A_{int id,data;}A[maxn];
inline bool cmp(const A_&a,const A_&b){return a.data<b.data;}
struct Edge{int to,nx;}edge[maxn];
inline void Add_edge(int from,int to){
edge[++num_edge].nx=edge_head[from];
edge[num_edge].to=to;
edge_head[from]=num_edge;
return;
}
struct Tree{int lc,rc,l,r,cnt;}t[maxn*];
inline void Build(int x,int l,int r,int q){
t[x].l=l;t[x].r=r;int mid=(l+r)>>;
if(l==r&&l==q){
t[x].cnt=;
return;
}
if(q<=mid)Build(t[x].lc=++num_treenode,l,mid,q);
else Build(t[x].rc=++num_treenode,mid+,r,q);
t[x].cnt=t[t[x].lc].cnt+t[t[x].rc].cnt;
return;
}
inline int Merge(int u,int v){
if(!u)return v;
if(!v)return u;
int l=t[u].l,r=t[u].r;
if(l==r){
t[u].cnt+=t[v].cnt;
return u;
}
t[u].lc=Merge(t[u].lc,t[v].lc);
t[u].rc=Merge(t[u].rc,t[v].rc);
t[u].cnt=t[t[u].lc].cnt+t[t[u].rc].cnt;
return u;
}
inline void Query(int g,int x,int ql,int qr){
int l=t[x].l,r=t[x].r,mid=(l+r)>>,lc=t[x].lc,rc=t[x].rc;
if(x==)return;
if(ql<=l&&r<=qr){
ans[g]+=t[x].cnt;
return;
}
if(ql<=mid)Query(g,lc,ql,qr);
if(qr>mid) Query(g,rc,ql,qr);
return;
}
inline void Dfs(int x){
for(int i=edge_head[x];i;i=edge[i].nx){
int y=edge[i].to;
Dfs(y);
root[x]=Merge(root[x],root[y]);
}
if(W[x]+>lsh_cnt)ans[x]=;
else Query(x,root[x],W[x]+,lsh_cnt);
return;
}
int main(){
scanf("%d",&N);
for(int i=;i<=N;i++){
scanf("%d",&A[i].data);
A[i].id=i;
}
sort(A+,A+N+,cmp);
W[A[].id]=++lsh_cnt;
for(int i=;i<=N;i++)
if(A[i].data!=A[i-].data)W[A[i].id]=++lsh_cnt;
else W[A[i].id]=lsh_cnt;
for(int i=;i<=N;i++){
scanf("%d",&u);
Add_edge(u,i);
}
for(int i=;i<=N;i++)Build(root[i]=++num_treenode,,lsh_cnt+,W[i]);
Dfs();
for(int i=;i<=N;i++)printf("%d\n",ans[i]);
return ;
}
树状数组版:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define re register
using namespace std;
inline int rd(){
int x=;char c=getchar();
while(c<''||c>'')c=getchar();
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x;
}
const int maxn=1e5;
int N,P[maxn+],cnt=,num_edge=,edge_head[maxn+],F,C[maxn+];
ll ans[maxn+];
struct Edge{
int to,nx;
}edge[maxn+];
struct Node{
int x,id;
}A[maxn+];
inline bool cmp(const Node&a,const Node&b){
if(a.x<b.x)return ;
return ;
}
inline int Find(int x){
re ll ans=;
for(;x<=cnt;x+=x&(-x))ans+=C[x];
return ans;
}
inline void Update(int x){
for(;x>;x-=x&(-x))C[x]++;
return;
}
inline void Dfs(int x){
ans[x]-=Find(P[x]);
for(re int i=edge_head[x];i;i=edge[i].nx) Dfs(edge[i].to);
ans[x]+=Find(P[x]);
Update(P[x]);
return;
}
int main(){
N=rd();
for(re int i=;i<=N;i++){
A[i].x=rd();
A[i].id=i;
}
sort(A+,A+N+,cmp);
for(re int i=;i<=N;i++){
if(i==||A[i].x!=A[i-].x)cnt++;
P[A[i].id]=cnt;
}
for(re int i=;i<=N;i++){
scanf("%d",&F);
edge[++num_edge].nx=edge_head[F];
edge[num_edge].to=i;
edge_head[F]=num_edge;
}
Dfs();
for(re int i=;i<=N;i++)printf("%lld\n",ans[i]);
return ;
}
By:AlenaNuna
线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数的更多相关文章
- bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】
思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)
BZOJ 洛谷 \(Description\) 给定一棵树,每次询问给定\(p,k\),求满足\(p,a\)都是\(b\)的祖先,且\(p,a\)距离不超过\(k\)的三元组\(p,a,b\)个数. ...
- LightOJ 1085(树状数组+离散化+DP,线段树)
All Possible Increasing Subsequences Time Limit:3000MS Memory Limit:65536KB 64bit IO Format: ...
随机推荐
- 解决eureka注册时使用ip而不是hostname
eureka的client注册到server时默认是使用hostname而不是ip,这就导致client在多台机器时,服务间相互调用时也会使用hostname进行调用,从而调用失败.这时候就需要使用i ...
- 通过groovy表达式拓展oval——实现根据同一实体中的其他属性值对某个字段进行校验
在java的参数校验中,开源验证框架OVAL基本能够满足所有需求,如下面通过简单的添加注解,就可实现对参数的非空和长度校验. @NotNull(message="计息周期月数不能为空&quo ...
- haproxy+keepalived(涵盖了lvs,nginx.haproxy比较)
文章转载自: haproxy+keepalived https://cloud.tencent.com/developer/article/1026385 网络四层和七层的区别 https: ...
- Linux 常见操作
grep -nr SEARCHTERM file1 file2 ...https://www.linuxnix.com/grep-command-usage-linux/ https://www.li ...
- 双网卡双线路DNS解析分析
在企业网络维护过程中我们经常会遇到这样或那样的奇怪问题,而很多问题需要有深厚的理论知识才能解决.而随着网络的飞速发展越来越多的中小企业开始尝试通过多条线路来保证网络的畅通,一方面双网卡下的双线接入可以 ...
- Content of "Essential Software Test Design"
Content of "Essential Software Test Design" 2015-11-16 PART I 7 TEST DESIGN TECHNIQUES: AN ...
- python 信息同时输出到控制台与文件
python编程中,往往需要将结果用print等输出,如果希望输出既可以显示到IDE的屏幕上,也能存到文件中(如txt)中,该怎么办呢? 方法1 可通过日志logging模块输出信息到文件或屏幕.但可 ...
- git 创建tag , 查看tag , 删除tag
简介 用git了很久了,也喜欢这个版本控制工具,今天来分享下,怎么用命令创建tag,查看tag,删除tag和把本地tag推到远程git服务器上 C:\Users\\WandaPuHuiProject ...
- Syncfusion SfDataGrid 导出Excel
var options = new ExcelExportingOptions { ExcelVersion = ExcelVersion.Excel2013, }; //不需要导出的字段 optio ...
- 【Python】 Numpy极简寻路
[Numpy] 先感叹下最近挖坑越来越多了.. 最近想不自量力地挑战下ML甚至DL.然而我也知道对于我这种半路出家,大学数学也只学了两个学期,只学了点最基本的高数还都忘光了的渣滓来说,难度估计有点大. ...