浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4756

对于每个结点用一棵值域线段树维护子树内结点的信息,然后该查询查询该合并合并就好了。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(nlogn)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=1e5+5; int n,tot,cnt;
int now[maxn],pre[maxn],son[maxn];
int p[maxn],tmp[maxn],rt[maxn],ans[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} void add(int a,int b) {
pre[++tot]=now[a];
now[a]=tot,son[tot]=b;
} struct segment_tree {
int tot;
int sum[maxn*20],ls[maxn*20],rs[maxn*20]; void update(int p) {
sum[p]=sum[ls[p]]+sum[rs[p]];
} void change(int &p,int l,int r,int pos) {
if(!p)p=++tot;
if(l==r) {sum[p]++;return;}
int mid=(l+r)>>1;
if(pos<=mid)change(ls[p],l,mid,pos);
else change(rs[p],mid+1,r,pos);
update(p);
} int query(int p,int l,int r,int pos) {
if(l==r)return 0;
int mid=(l+r)>>1,res=0;
if(pos<=mid)res=sum[rs[p]]+query(ls[p],l,mid,pos);
else res=query(rs[p],mid+1,r,pos);
return res;
} int merge(int a,int b) {
if(!a||!b)return a+b;
if(!ls[a]&&!rs[a]&&!ls[b]&&!rs[b]) {
sum[a]+=sum[b];return a;
}
ls[a]=merge(ls[a],ls[b]);
rs[a]=merge(rs[a],rs[b]);
update(a);return a;
}
}T; void dfs(int fa,int u) {
int tmp=0;
for(int P=now[u],v=son[P];P;P=pre[P],v=son[P])
dfs(u,v),tmp=T.merge(tmp,rt[v]);
ans[u]=T.query(tmp,1,cnt,p[u]);
rt[u]=T.merge(rt[u],tmp);
} int main() {
n=read();
for(int i=1;i<=n;i++)
tmp[i]=p[i]=read();
sort(tmp+1,tmp+n+1);
cnt=unique(tmp+1,tmp+n+1)-tmp-1;
for(int i=1;i<=n;i++) {
p[i]=lower_bound(tmp+1,tmp+cnt+1,p[i])-tmp;
T.change(rt[i],1,cnt,p[i]);
}
for(int i=2;i<=n;i++) {
int f=read();add(f,i);
}
dfs(0,1);
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}

BZOJ4756:[USACO2017JAN]Promotion Counting的更多相关文章

  1. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

  2. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

  3. 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

    调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...

  4. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

  5. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  6. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  7. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  8. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

    P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...

  9. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

    [题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...

随机推荐

  1. Spring标签@Aspect-实现面向方向编程(@Aspect的多数据源自动加载)——SKY

    从Spring 2.0开始,可以使用基于schema及@AspectJ的方式来实现AOP.由于@Aspect是基于注解的,因此要求支持注解的5.0版本以上的JDK. 环境要求:    1. mybit ...

  2. 栈 堆 stack heap

    点餐 做菜 Stack and Heap 堆和栈的区别 - Grandyang - 博客园 https://www.cnblogs.com/grandyang/p/4933011.html 在和计算机 ...

  3. JVM虚拟机参数

    追踪参数: 打印GC简要信息 -XX:+PrintGC 打印GC详细信息 -XX:+PrintGCDetails 打印CG发生的时间戳 -XX:+PrintGCTimeStamps 指定GC log的 ...

  4. Django自定义对象转成json字符串

    1.定义转换函数:def convert_to_builtin_type(obj): print 'default(', repr(obj), ')' # 把MyObj对象转换成dict类型的对象 d ...

  5. SQL的优化1

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  6. django 异步任务实现及Celery beat实现定时/轮询任务

    Celery定时任务 requirements celery==3.1.25 异步任务 django-celery==3.2.2 定时任务管理包 redis==2.10.6 django-redis- ...

  7. Windows存储管理之磁盘结构详解

    Windows磁盘结构: Windows的主流磁盘结构分为MBR和GPT两种.MBR是早期Windows的唯一选择,但是随着物理磁盘的容量不断增大.GPT结构成为目前的主流,最大支持超过2TB的容量, ...

  8. 关于树莓派Pi2通过UART连接攀藤G5传感器的python

    1.准备工作:树莓派Pi2板子,攀藤G5传感器 关于树莓派40pin口网上很多,我们只了解与攀藤G5连接的问题 (1)攀藤G5pin1(VCC5v)要注意是5V,有很多板子接的是3V,而树莓派的pin ...

  9. 316python 基础之计算机基础、Python简介、变量、注释、基础数据类型初识、if、while、语句

    一.计算机基础. cpu:相当于人的大脑,运算与控制中心. 速度 飞机 内存:4G,8G,16G....暂时存储,供给cpu数据. 速度 高铁.成本高,断电即消失. 硬盘:相当于你电脑的数据库,存储着 ...

  10. 单链表(C++实现)

    单链表的结构有多种 这里介绍的链表有头结点.有尾节点并且尾节点指向头结点 单链表的每个结点的地址存放在其直接前驱结点的指针域中.其中第一个结点没有前驱结点,因此需要一个头指针指向第一个节点,便于我们对 ...