bzoj 4756 Promotion Counting —— 线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756
合并子树的权值线段树;
merge 返回 int 或者是 void 都可以。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((l+r)>>1)
using namespace std;
int const xn=1e5+,xm=xn*;//
int n,p[xn],b[xn],hd[xn],ct,to[xn<<],nxt[xn<<];
int cnt,rt[xn],ls[xm],rs[xm],sum[xm],ans[xn],tot;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
int gt[];
void wr(int x)
{
if(!x){puts(""); return;}
if(x<)putchar('-'),x=-x;
int t=;
while(x)gt[++t]=x%,x/=;
for(int i=t;i;i--)putchar(gt[i]+'');
puts("");
}
void add(int x,int y){to[++ct]=y; nxt[ct]=hd[x]; hd[x]=ct;}
void merge(int &x,int y)
{
if(!x){x=y; return;}
sum[x]+=sum[y];
if(ls[y])merge(ls[x],ls[y]);//if
if(rs[y])merge(rs[x],rs[y]);//if
}
/*
int merge(int x,int y,int l,int r)
{
if(!x||!y)return x+y;
sum[x]+=sum[y];
if(l==r)return x;
ls[x]=merge(ls[x],ls[y],l,mid);
rs[x]=merge(rs[x],rs[y],mid+1,r);
return x;
}
*/
void add(int &x,int l,int r,int pos)
{
if(!x)x=++cnt; sum[x]++;
if(l==r)return;
if(pos<=mid)add(ls[x],l,mid,pos);
else add(rs[x],mid+,r,pos);
}
int query(int x,int l,int r,int L,int R)
{
if(!x)return ;
if(l>=L&&r<=R)return sum[x];
int ret=;
if(mid>=L)ret+=query(ls[x],l,mid,L,R);
if(mid<R)ret+=query(rs[x],mid+,r,L,R);
return ret;
}
void dfs(int x)
{
for(int i=hd[x],u;i;i=nxt[i])
{
dfs(u=to[i]);
// rt[x]=merge(rt[x],rt[u],1,tot);
merge(rt[x],rt[u]);
}
ans[x]=query(rt[x],,tot,p[x],tot);
add(rt[x],,tot,p[x]);
}
int main()
{
n=rd();
for(int i=;i<=n;i++)p[i]=rd(),b[i]=p[i];
sort(b+,b+n+); tot=unique(b+,b+n+)-b-;
for(int i=;i<=n;i++)p[i]=lower_bound(b+,b+tot+,p[i])-b;
for(int i=,fa;i<=n;i++)fa=rd(),add(fa,i);
dfs();
for(int i=;i<=n;i++)wr(ans[i]);
return ;
}
bzoj 4756 Promotion Counting —— 线段树合并的更多相关文章
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- 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 ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- BZOJ.3545.[ONTAK2010]Peaks(线段树合并)
题目链接 \(Description\) 有n个座山,其高度为hi.有m条带权双向边连接某些山.多次询问,每次询问从v出发 只经过边权<=x的边 所能到达的山中,第K高的是多少. \(Solut ...
- bzoj 4631: 踩气球 线段树合并
4631: 踩气球 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 265 Solved: 136[Submit][Status][Discuss] ...
- BZOJ:5457: 城市(线段树合并)(尚待优化)
5457: 城市 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 18 Solved: 12[Submit][Status][Discuss] Des ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
随机推荐
- 51 NOD 1325 两棵树的问题
Discription 对于 100% 的数据, N<=50. solution: 发现N比较小,所以我们可以花O(N^2)的代价枚举两颗树的联通块的LCA分别是哪个点,然后现在问题就变成了:选 ...
- 蓝桥java 入门训练 Fibonacci数列
import java.util.Scanner; public class Main{ public static void main(String[] args) { int maxn=10000 ...
- 【kotlin】long转化为date类型 或者date字符串
1.方法体中的 package org.joda.time.DateTime(long类型) fun Long?.toDateTime() = if (null != this) DateTime(t ...
- win7下 安装 source code pro
转: http://my.oschina.net/yearnfar/blog/325107 source code pro 字体安装, 一. 去 https://github.com/adobe-fo ...
- poj 1694 An Old Stone Game 树形dp
//poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...
- python 学习笔记 13 -- 经常使用的时间模块之time
Python 没有包括相应日期和时间的内置类型.只是提供了3个相应的模块,能够採用多种表示管理日期和时间值: * time 模块由底层C库提供与时间相关的函数.它包括一些函数用于获取时钟时间和处 ...
- 小老虎CSDN博客流量分析
小老虎CSDN博客流量分析 一.分析的博客对象 http://blog.csdn.net/littletigerat 二.分析的时间节点 2014年7月10日星期四 三.PV.UV以及IP值 wa ...
- java cup占用高分析脚本
[was@dmgr ita-scripts]$ vi java_analys.sh PID=$1 ; ps -mp $PID -o THREAD,tid,time | awk -F " ...
- Here is the reason why Fengguang turns from ipmitool to freeipmi
http://ipmitool.sourceforge.net/ Last updated Thu Apr 26 09:08:52 PDT 2007 Revision 1.21 · Home· Dow ...
- vue 开发前准备工作
工欲善其事,必先利其器. 第一步,选择一个合适的编辑器: 看vue官网上,有推荐用HBuilder X这个编辑器,这个我在开发微信小程序的时候,就是用的这个编辑器,还可以,挺好用的,也可以用git做版 ...