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 ...
随机推荐
- js逻辑非同时两次使用 !!null
今天遇到了“!!null”的写法,百度没有找到直接的解释,翻书在<javascript高级设计>P44找到了相应的解释: 同时使用两个逻辑非操作符,实际上就会模拟Boolean()转型函数 ...
- vim 宏的使用
1. 基本使用 q[a-z] 开始录制宏 q 停止录制 @[a-z] 使用宏 @@ 调用最近使用的宏 22@[a-z] 多次重放宏 2. 宏的执行方式 串行方式:5@[a-z] 宏内包含向下一个目标 ...
- Zabbix 3.0安装
Server 1. rpm安装zabbix 3.0最新的epel源 rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-re ...
- Webkit二:RenderTree 创建
RenderObject 作为所有Render 树节点的基类,完全类似与DOM树中的Node基类,它是构成Render 树的基础,作用非比寻常,其中包含了构成Render 树所可能涉及到的一些基本属性 ...
- 剑指Offer面试题16(Java版):反转链表
题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点. 解决与链表相关的问题总是有大量的指针操作.而指针操作的代码总是easy出错的. 非常多的面试官喜欢出链表相关的问题,就是 ...
- Leetcode 002-Search Insert Position
#Given a sorted array and a target value, return the index if the target is found. If not, return th ...
- CGGeometry.h详解
本文转载至:http://blog.csdn.net/chengyingzhilian/article/details/7894195 这些是在CGGeometry.h里的 CGPoint.CGSi ...
- 2016/7/7 设置wamp2.5 mysql密码 重点是mysql版本
密码设置时要注意mysql版本.版本不同,效果不同. 方法/步骤 安装好wamp后,右击wamp->MySQl->MySql console(控制台) 提示输入密码,因为密码为 ...
- 九度OJ 1130:日志排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1265 解决:303 题目描述: 有一个网络日志,记录了网络中计算任务的执行情况,每个计算任务对应一条如下形式的日志记录: "hs ...
- 今日头条Go建千亿级微服务的实践
今日头条Go建千亿级微服务的实践_36氪 http://36kr.com/p/5073181.html