BZOJ4756:[USACO2017JAN]Promotion Counting
浅谈线段树合并: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的更多相关文章
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- bzoj4756 [Usaco2017 Jan]Promotion Counting
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)
传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...
- 线段树合并 || 树状数组 || 离散化 || 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 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]
[题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...
随机推荐
- 九度OJ 1198:a+b (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6745 解决:2320 题目描述: 实现一个加法器,使其能够输出a+b的值. 输入: 输入包括两个数a和b,其中a和b的位数不超过1000位 ...
- 【python】-- 类的继承(新式类/经典类)、多态
继承 之前我们说到了类的公有属性和类的私有属性,其实就是类的封装,现在准备随笔的 是继承,是面向对象的第二大特性. 面向对象编程 (OOP) 语言的一个主要功能就是“继承”.继承是指这样一种能力:它可 ...
- C++学习笔记30,指针的引用(2)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/guang_jing/article/details/32910093 能够创建不论什么类型的引用,包 ...
- 11.23 Eclipse
一.Eclipse Workspace File-->Switch Workspace 编译环境:Window -- Preferences – Java - Compiler 运行环境:Win ...
- 【leetcode刷题笔记】Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- cat echo 打印菜单
cat << END ============================= 1.apple 2.pear 3.banana ...
- javascript数字时钟
<html> <head> <script type="text/javascript"> function startTime() { var ...
- 利用Tkinter做的自动生成JSONSchema的小工具
前面讲到可以使用JSONSchema做json数据校验, 但是每个接口数据都手动写jsonschema太痛苦了, 就写了个小脚本,可以直接复制接口文档的mock数据然后生成一个简单的jsonschem ...
- 机器学习——支持向量机(SVM)
机器学习--支持向量机(SVM) 支持向量机(Support Vector Machine)广泛地应用于分类问题,回归问题和异常检测问题.支持向量机一个很好的性质是其与凸优化问题相对应,局部最优解就是 ...
- NET 平台下的WebService 简单使用
一句话理解:提供可供外部访问的方法,实现跨平台访问 注意: 在客户端是添加“服务引用”,而不是引用 当服务端更新了服务之后,在客户端,一定也要“更新服务” 当要执行异常调用时,要在前台.aspx的头部 ...