题面: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晋升者计数的更多相关文章

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

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

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

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

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

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

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  7. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

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

  8. BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)

    BZOJ 洛谷 \(Description\) 给定一棵树,每次询问给定\(p,k\),求满足\(p,a\)都是\(b\)的祖先,且\(p,a\)距离不超过\(k\)的三元组\(p,a,b\)个数. ...

  9. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

随机推荐

  1. 一步步教你轻松学主成分分析PCA降维算法

    一步步教你轻松学主成分分析PCA降维算法 (白宁超 2018年10月22日10:14:18) 摘要:主成分分析(英语:Principal components analysis,PCA)是一种分析.简 ...

  2. 关于Django Ajax CSRF 认证

    CSRF(Cross-site request forgery跨站请求伪造,也被称为“one click attack”或者session riding,通常缩写为CSRF或者XSRF,是一种对网站的 ...

  3. .net core 2.0+superui +Dapper.SimpleCRUD+mysql+NLog

    **_ .net core 2.0+superui +Dapper.SimpleCRUD+mysql+NLog _** 前端框架 superui http://www.supermgr.cn/ 1.组 ...

  4. Future、FutureTask实现原理浅析(源码解读)

    前言 最近一直在看JUC下面的一些东西,发现很多东西都是以前用过,但是真是到原理层面自己还是很欠缺. 刚好趁这段时间不太忙,回来了便一点点学习总结. 前言 最近一直在看JUC下面的一些东西,发现很多东 ...

  5. Protobuf3 序列化

    在message_lite.h中定义了SerializeToString ,SerializeToArray ,SerializeToCodedStream ,SerializeToZeroCopyS ...

  6. node出现 Error: listen EACCES 0.0.0.0:8080错误

    Error: listen EACCES 0.0.0.0:8080 at Object._errnoException (util.js:1022:11) at _exceptionWithHostP ...

  7. openfire课程

    https://blog.csdn.net/huwenfeng_2011/article/category/2874473/2 https://www.cnblogs.com/Fordestiny/p ...

  8. Win10添加右键在此处打开命令行

    通过添加注册表项,实现右击“在此处打开命令行功能” 注册表位置:HKEY_CLASSES_ROOT\Directory\Background\shell\ win10系统用标识右键菜单打开命令行的键, ...

  9. MySQL 批量写入数据报错:mysql_query:Lost connection to MySQL server during query

    场景: 批量往mysql replace写入数据时,报错. 解决方法: 1.增大mysql 数据库配置中 max_allowed_packet 的值 max_allowed_packet = 1G ( ...

  10. [转]谈NAND Flash的底层结构和解析

    这里我想以一个纯玩家的角度来谈谈关于NAND Flash的底层结构和解析,可能会有错误的地方,如果有这方面专家强烈欢迎指正. NAND Flash作为一种比较实用的固态硬盘存储介质,有自己的一些物理特 ...