bzoj 4756 Promotion Counting —— 线段树合并
题目: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 —— 线段树合并的更多相关文章
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- BZOJ.3545.[ONTAK2010]Peaks(线段树合并)
题目链接 \(Description\) 有n个座山,其高度为hi.有m条带权双向边连接某些山.多次询问,每次询问从v出发 只经过边权<=x的边 所能到达的山中,第K高的是多少. \(Solut ...
- bzoj 4631: 踩气球 线段树合并
4631: 踩气球 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 265 Solved: 136[Submit][Status][Discuss] ...
- BZOJ:5457: 城市(线段树合并)(尚待优化)
5457: 城市 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 18 Solved: 12[Submit][Status][Discuss] Des ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
随机推荐
- Linux出现cannot create temp file for here-document: No space left on device的问题解决
在终端输入:cd /ho 按tab键时,显示错误: bash: cannot create temp file for here-document: No space left on device 这 ...
- PHP实现INT型,SHORT型,STRING转换成BYTE数组
实现PHP实现INT型,SHORT型,STRING转换成BYTE数组的转化: class Bytes { public static function integerToBytes($val) { $ ...
- 【spring boot】mybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper' in your configuration. 【Mapper类不能被找到】@Mapper 和@MapperScan注解的区别
启动报错: 2018-05-16 17:22:58.161 ERROR 4080 --- Disconnected from the target VM, address: '127.0.0.1:50 ...
- ASP.NET Core 依赖注入(构造函数注入,属性注入等)
原文:ASP.NET Core 依赖注入(构造函数注入,属性注入等) 如果你不熟悉ASP.NET Core依赖注入,先阅读文章: 在ASP.NET Core中使用依赖注入 构造函数注入 构造函数注 ...
- weexpack 的 Login.vue 及 vue 的 Login.vue
1.登录页 weexpack Login.vue <!-- 登录页 --> <template> <div class="wrapper"> ...
- 【转载】C#中的泛型
1.1 C#中的泛型 .Net 1.1版本最受诟病的一个缺陷就是没有提供对泛型的支持.通过使用泛型,我们可以极大地提高代码的重用度,同时还可以获得强类型的支持,避免了隐式的装箱.拆箱,在一定程度上提升 ...
- C++再论单例模式
#include <iostream> #include <windows.h> #include <mutex> std::mutex gmutex; using ...
- dubbo springCloud比较
1.dubbo只是专注于服务之间的治理,配置中心.分布式跟踪等这些内容都需要自己集成 2.dubbo核心功能: a.远程通讯 b.集群容错 c.自动发现 Dubbo SpringCloud 服务注册中 ...
- delete from inner join
Update Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,这个在表间关联来做更新和删除操作 ...
- linux下提示command not found
首先就要考虑root 的$PATH里是否已经包含了这些环境变量. 主要是这四个:/bin ,/usr/bin,/sbin,/usr/sbin. 四个主要存放的东东: ./bin: bin为binary ...