BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意
Sol
线段树合并板子题
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 400000, SS = MAXN * 21;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, p[MAXN], fa[MAXN], root[MAXN], tot, ans[MAXN];
vector<int> v[MAXN];
int ls[SS], rs[SS], sum[SS];
void insert(int &k, int l, int r, int p, int v) {
if(!k) k = ++tot;
sum[k]++;
if(l == r) return ;
int mid = l + r >> 1;
if(p <= mid) insert(ls[k], l, mid, p, v);
else insert(rs[k], mid + 1, r, p, v);
}
int Query(int k, int l, int r, int ql, int qr) {
if(ql <= l && r <= qr) return sum[k];
int mid = l + r >> 1;
if(ql > mid) return Query(rs[k], mid + 1, r, ql, qr);
else if(qr <= mid) return Query(ls[k], l, mid, ql, qr);
else return Query(ls[k], l, mid, ql, qr) + Query(rs[k], mid + 1, r, ql, qr);
}
int Merge(int x, int y) {
if(!x || !y) return x ^ y;
ls[x] = Merge(ls[x], ls[y]);
rs[x] = Merge(rs[x], rs[y]);
sum[x] += sum[y];
return x;
}
void dfs(int x) {
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i]; dfs(to);
root[x] = Merge(root[x], root[to]);
}
ans[x] = Query(root[x], 1, N, p[x] + 1, N);
insert(root[x], 1, N, p[x], 1);
}
void Des() {
static int date[MAXN], num = 0;
for(int i = 1; i <= N; i++) date[++num] = p[i];
sort(date + 1, date + num + 1);
num = unique(date + 1, date + num + 1) - date - 1;
for(int i = 1; i <= N; i++) p[i] = lower_bound(date + 1, date + N + 1, p[i]) - date;
}
int main() {
N = read();
for(int i = 1; i <= N; i++) p[i] = read();
Des();
for(int i = 2; i <= N; i++) fa[i] = read(), v[fa[i]].push_back(i);
dfs(1);
for(int i = 1; i <= N; i++) printf("%d\n", ans[i]);
return 0;
}
/*
*/
BZOJ4756: [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 ...
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)
传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...
- 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 ...
- bzoj4756 [Usaco2017 Jan]Promotion Counting
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...
随机推荐
- Linux合上笔记本不进入休眠模式
最近一个问题困扰了我很久,入职之前和人事说过工作中会用自己的电脑,但是人事还是坚持要给我发一个电脑,没办法,公司没有补贴,那就领了吧,索性将这个笔记本配置成了Fedora系统,用来当测试机,但是一 ...
- Apache Drill - join HBase and RDBMs
HBase作为Nosql的常用系统之一,在很多大数据应用/平台中广泛使用.例如通过Spark统计后将结果存放到HBase中.通常统计结果还需要进一步和元数据或者字典表关联从而得到最终结果显示,这意味着 ...
- 9.indicate、xutils、json
json数据 页签详情页数据 public class TabData { public int retcode; public TabDetail data;//不是数组的话类型就是这个 pu ...
- Swift5 语言指南(十九) 错误处理
错误处理是响应程序中的错误条件并从中恢复的过程.Swift为在运行时抛出,捕获,传播和操纵可恢复的错误提供了一流的支持. 某些操作无法保证始终完成执行或生成有用的输出.Optionals用于表示缺少值 ...
- Lucene 4.X 全套教程
http://www.cnblogs.com/forfuture1978/category/300665.html Lucene 4.X 倒排索引原理与实现: (3) Term Dictionary和 ...
- 第一篇:服务的注册与发现Eureka(Finchley版本)
一.创建服务注册中心(Eureka) 1. 首先创建一个maven主工程 创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Clo ...
- C# 获取Header中的token值
public CurrentUser currentUser { get { CurrentUser result = new CurrentUser(); //jwt 解密token IJsonSe ...
- linux下 /usr/bin/ld: 找不到 -ldhnetsdk的解决方法
linux下使用Qt编译程序的时候,安装了程序自带的链接库之后,仍然上报这个错误, 发现系统上报这个错误: /usr/bin/ld: 找不到 -ldhnetsdk 经过仔细的定位,终于解决了,这里把思 ...
- 使用speex动态链接库过程中遇到问题及解决方法
本以为speex的应用程序很容易就能跑起来,可是,实际操作中才发现,这里面暴露 的问题还真不少.看来以后不能眼高手低了,知行合一,这个一定要牢记在心中. speex安装成功后,可以一直无法调用动态链接 ...
- mysql 开发进阶篇系列 41 mysql日志之慢查询日志
一.概述 慢查询日志记录了所有的超过sql语句( 超时参数long_query_time单位 秒),获得表锁定的时间不算作执行时间.慢日志默认写入到参数datadir(数据目录)指定的路径下.默认文件 ...