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 ...
随机推荐
- tomcat7设置usernamepassword
因为tomcat是绿色版.今天想在网页上管理项目,却发现没实username和password.打开tomcat-users.xml文件全都是凝视.如图: 将例如以下代码拷贝到tomcat-users ...
- C# 将long类型写入二进制文件用bw.Write(num);将其读出用long num= br.ReadInt64();
理由: 因为long类型是 System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方) 而long BinaryReader ...
- angular - 配置package.json -3
package.json 包含了所有的开发包以及全局包以及其它项目信息,我们这个项目需要用到 bootstrap,所以我们添加信息. 添加包信息以后,我们用 npm install 安装,npm包管理 ...
- 上传图片/文件到server
package yao.camera.util; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; impo ...
- 安装odoo 9实录
安装Ubuntu,省略 下载 odoo源码 使用 git 下载源码 mkdir /opt/openerp/server cd /opt/openerp/server git clone https:/ ...
- 面试之SQL(1)--选出选课数量>=2的学号
ID Course 1 AA 1 BB 2 AA 2 BB 2 CC 3 AA 3 BB 3 CC 3 DD 4 AA NULL NULL 选出选课数量>=2的学号 select di ...
- leetcode第一刷_Permutation Sequence
这道题还挺好的,假设你的思路是每次生成一个全排列,然后累计到k次,那么停下来吧.肯定超时了亲.. 微软今年的笔试题里有一道类似的,我之前已经提到过了.是唯独0和1的字符串,求第k个排列是什么样子的.这 ...
- Linaro/Yocto/Openwrt
http://en.wikipedia.org/wiki/Linaro Linaro From Wikipedia, the free encyclopedia This article ap ...
- 用redis实现跨服务器session(转)
这个月我们新开发了一个项目,由于使用到了4台机器做web,使用dns做负载均衡, 上面图上用户通过DNS的调度(一个域名对应多个ip)分别访问到VM2-VM5上,四台机器都访问VM1上的redis,两 ...
- LeetCode求能够装得最多的水
费了半天劲还是没想出来,然后跑到网上找答案,明白了是怎么回事儿了,就是不知道为啥自己没有想出来. 明天再搞~~~ 国庆快乐~~~ 一年了~~~~~ 明年今日~~我会在哪儿 =====2017.9.30 ...