bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756
线段树合并裸题。那种返回 int 的与传引用的 merge 都能过。不知别的题是不是这样。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+,M=N**;
int n,m,a[N],tp[N],rt[N],hd[N],xnt,to[N],nxt[N],ans[N];
int tot,ls[M],rs[M],siz[M];
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
void add(int x,int y)
{
to[++xnt]=y;nxt[xnt]=hd[x];hd[x]=xnt;
} void merge(int &x,int y)
{
if(!x){x=y;return;}
siz[x]+=siz[y];
if(ls[y])merge(ls[x],ls[y]);
if(rs[y])merge(rs[x],rs[y]);
}
/*
int merge(int x,int y)
{
if(!x||!y)return x+y;
int d=++tot;
siz[d]=siz[x]+siz[y];
ls[d]=merge(ls[x],ls[y]);
rs[d]=merge(rs[x],rs[y]);
return d;
}
*/
int query(int l,int r,int cr,int L,int R)
{
if(!cr)return ;if(l>=L&&r<=R)return siz[cr];
int mid=l+r>>,ret=;
if(mid>=L)ret=query(l,mid,ls[cr],L,R);
if(mid<R)ret+=query(mid+,r,rs[cr],L,R);
return ret;
}
void insert(int l,int r,int &cr,int p)
{
if(!cr)cr=++tot; siz[cr]++;
if(l==r)return; int mid=l+r>>;
if(mid>=p) insert(l,mid,ls[cr],p);
else insert(mid+,r,rs[cr],p);
}
void dfs(int cr)
{
for(int i=hd[cr],v;i;i=nxt[i])
dfs(v=to[i]),/*rt[cr]=*/merge(rt[cr],rt[v]);
ans[cr]=query(,m,rt[cr],a[cr],m);
insert(,m,rt[cr],a[cr]);
}
int main()
{
n=rdn(); for(int i=;i<=n;i++)a[i]=tp[i]=rdn();
sort(tp+,tp+n+); m=unique(tp+,tp+n+)-tp-;
for(int i=;i<=n;i++)a[i]=lower_bound(tp+,tp+n+,a[i])-tp;
for(int i=,d;i<=n;i++)
{
d=rdn(); add(d,i);
}
dfs();
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}
bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并的更多相关文章
- 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 ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...
- bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】
思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...
- 【BZOJ】4756: [Usaco2017 Jan]Promotion Counting
[题意]带点权树,统计每个结点子树内点权比它大的结点数. [算法]线段树合并 [题解]对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计. 注意三个量tot,cnt,tots,细心 ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- bzoj 4756 Promotion Counting —— 线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...
随机推荐
- Highcharts常用的最核心的参数选项配置详细说明
Highcharts提供大量的选项配置参数,您可以轻松定制符合用户要求的图表,目前官网只提供英文版的开发配置说明文档,而中文版的文档网上甚少,且零散不全.这里,我把Highcharts常用的最核心的参 ...
- myBatis-plus异常提示For input string: "{0=null}"
异常信息 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Per ...
- 小贝_mysql select连接查询
select连接查询 简要: 一.union联合查询 二.左右内连接 一.union联合查询 作用: 把2次或多次查询结果合并起来 具体: (表1查询结果) union (表2查询结果) 运行: 先算 ...
- 系统安全-SElinux
Selinux是[security-Enhanced Linux]的简称,是美国国家安全局[NSA]和[SCC]开发的Linux的一个扩张强制访问控制安全模块. 因为企业的业务平台的服务器上存储着大量 ...
- Django-mysq数据库链接问题
Django链接MySQL数据库有可能会报no model named MySQLdb, 解决办法: 首先安装pymysql 然后进入到项目目录,找到__init__.py文件,在里面添加 impor ...
- kubernetes容器编排之定义环境变量以及通过downwardapi把pod信息作为环境变量传入容器内
系列目录 在学习docker的时候,大家可能经常看到不少示例在docker run的时候指定环境变量(比如wordpress的docker示例就是通过环境变量传入账户和密码到容器内).这里之所以经常用 ...
- jsp 下拉框首字母定位可检索
实现效果如图: 页面部分: (1)js中: (2)body中: JAVA代码部分: 控制器Controller中 写一个页面js中调用的方法: 引入jar包:
- STL_算法_查找算法(find、find_if)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...
- 【iOS开发】---- UIView动画
iOS 动画UIView动画 原文:http://www.cocoachina.com/bbs/read.php?tid=110168 1.概述 UIKit直接将动画集成到UIView类中,实现简 ...
- VLC RTP Over TCP
在RTSP协议请求数据时,让VLC以TCP的方式获取服务器发来的RTP数据 不为别的,下次回复直接用博客链接就能回复大家了! 操作:工具 -> 首选项 然后: 搞定! ------------- ...