【BZOJ】4756: [Usaco2017 Jan]Promotion Counting
【题意】带点权树,统计每个结点子树内点权比它大的结点数。
【算法】线段树合并
【题解】对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计。
注意三个量tot,cnt,tots,细心查错。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
int n,first[maxn],cnt,tot,tots,root[maxn],a[maxn],b[maxn],ans[maxn];
struct edge{int v,from;}e[maxn*];
void ins(int u,int v){tots++;e[tots].v=v;e[tots].from=first[u];first[u]=tots;}
struct cyc{int l,r,sum;}t[maxn*];
void insert(int l,int r,int &x,int y){
if(!x)x=++cnt;t[x].sum++;
if(l==r)return;//
int mid=(l+r)>>;
if(y<=mid)insert(l,mid,t[x].l,y);
else insert(mid+,r,t[x].r,y);
}
int merge(int x,int y){
if(!x||!y)return x^y;//
t[x].l=merge(t[x].l,t[y].l);
t[x].r=merge(t[x].r,t[y].r);//
t[x].sum=t[t[x].l].sum+t[t[x].r].sum;//
return x;
}
int ask(int left,int right,int k,int l,int r){
if(l<=left&&right<=r)return t[k].sum;
else{
int mid=(left+right)>>,sum=;
if(l<=mid)sum=ask(left,mid,t[k].l,l,r);
if(r>mid)sum+=ask(mid+,right,t[k].r,l,r);
return sum;
}
}
void dfs(int x){
insert(,tot,root[x],a[x]);
for(int i=first[x];i;i=e[i].from){
dfs(e[i].v);
root[x]=merge(root[x],root[e[i].v]);
}
if(a[x]<tot)ans[x]=ask(,tot,root[x],a[x]+,tot);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
for(int i=;i<=n;i++){
int fa;scanf("%d",&fa);
ins(fa,i);
}
sort(b+,b+n+);tot=n;
tot=unique(b+,b+tot+)-b-;
for(int i=;i<=n;i++)a[i]=lower_bound(b+,b+tot+,a[i])-b;
dfs();
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}
【BZOJ】4756: [Usaco2017 Jan]Promotion Counting的更多相关文章
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面: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 都能过.不知别的题是不是这 ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]
[题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...
- bzoj4756 [Usaco2017 Jan]Promotion Counting
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...
- 【BZOJ】1576 [Usaco2009 Jan]安全路经Travel
[算法]最短路树+(树链剖分+线段树)||最短路树+并查集 [题解] 两种方法的思想是一样的,首先题目限制了最短路树唯一. 那么建出最短路树后,就是询问对于每个点断掉父边后重新找路径的最小值,其它路径 ...
随机推荐
- erlang转化中文为url
今天使用http get 方法时,参量中有中文而导致出错. 例如http://abc.com/abc?arg=中文,在erlang使用http:request方法失败. 后来查了url的规范,url中 ...
- Spring+Netty4实现的简单通信框架
参考:http://cpjsjxy.iteye.com/blog/1587601 Spring+Netty4实现的简单通信框架,支持Socket.HTTP.WebSocket_Text.WebSock ...
- 设计模式--Restful笔记(一)
一.REST基础概念 首先REST是 Representational State Transfer 的缩写,如果一个架构符合REST原则,它就是RESTful架构. 在REST中的一切都被认为是一种 ...
- 【final】140字互评②
按照产品发布顺序 nice!----约吧 我们的团队展示相对于上次的手足无措,有了一定进步.但是整体还是不那么流畅总结起来的缺点是: 1.发布时,摄像头不清晰 且抖动 我们没有把摄像头固定,并且为了让 ...
- 分享几个IP获取地理位置的API接口(最全面的了)
转载;https://cloud.tencent.com/developer/article/1152362 全网首发,最全的IP接口,不服来辩!博主找了几个小时的资料,又手动抓取到了几个接口补充进来 ...
- Struts访问序号的设置
- 转换成json字符串,与json字符串转换成java类型都要先转换成json对象
转换成json字符串,与json字符串转换成java类型都要先转换成json对象
- innerHTML与innerText的区别: 前者获取的是dom对象内的所有html元素 后者获取的是dom对象里面的纯文本元素
- shell脚本学习—正则表达式
正则表达式概念.特点 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤辑. 给定一个 ...
- BZOJ3622 已经没有什么好害怕的了(动态规划+容斥原理)
显然可以转化为一个阶梯状01矩阵每行每列取一个使权值和为k的方案数.直接做不可做,考虑设f[i][j]为前i行权值和至少为j,即在其中固定了j行选1的方案数.设第i行从1~a[i]列都是1且a[i]+ ...