【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
[算法]最短路树+(树链剖分+线段树)||最短路树+并查集 [题解] 两种方法的思想是一样的,首先题目限制了最短路树唯一. 那么建出最短路树后,就是询问对于每个点断掉父边后重新找路径的最小值,其它路径 ...
随机推荐
- lintcode-397-最长上升连续子序列
397-最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 注意事 ...
- PAT 1085 PAT单位排行 (Microsoft_zzt)
https://pintia.cn/problem-sets/994805260223102976/problems/994805260353126400 每次 PAT 考试结束后,考试中心都会发布一 ...
- 使用TestNG 和 CSV文件进行数据驱动
package testNGPractice; import java.io.BufferedReader; import java.io.FileInputStream; import java.i ...
- IF与SWITCH
今晚刚刚看了一点儿<大话设计模式>这本书,看到它示例的第一个程序,好像有点理解IF与SWITCH的区别了.大致的思考了总结出来. IF适用于每个条件都必须判断,就是IF语句中的判断是不同类 ...
- idea导出包含main函数的jar
1.首先打开File->project stucture->Artifacts 2.按照下图方式: 3.选择面main函数的所在的类,选择MAINFEST.MF问的生成路径 这里一定选择 ...
- App流量测试--使用安卓自身提供的TCP收发长度统计功能
在Linux系统有3个地方保存流量统计文件,对于Android系统同样也适用: (1)在/proc/net/dev下可以查看各个网络接口的收发流量 (等同adb shell cat /proc/pi ...
- apache server-status配置
引言 自己配置LAMP服务器时(xwamp),获取状态信息出现错误: You don't have permission to access /server-status on this server ...
- Centos7安装完毕后联网-设置ip地址(VMware虚拟机)
VMware虚拟机中安装了Centos7,为了让Centos能够访问外网及设置固定的ip地址以方便本地通过SSH访问Centos,做以下几步.本文来自osfipin note. 1.确认虚拟机网络链接 ...
- 转---Post/Redirect/Get pattern
今天重新认识了Post/Redirect/Get pattern, 感谢hip-hop的session, 一下帮助我理清了概念和思路. 谈到pattern,首先要清楚它为了什么而产生: PRG (参见 ...
- hbase 安装笔记
1.安装 在官方镜像站点下载hbase2.0,地址:https://www.apache.org/dyn/closer.lua/hbase/ 解压tar xzvf hbase-2.0.4-bin.ta ...