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 ...
随机推荐
- MyBatis 源码分析 - 内置数据源
1.简介 本篇文章将向大家介绍 MyBatis 内置数据源的实现逻辑.搞懂这些数据源的实现,可使大家对数据源有更深入的认识.同时在配置这些数据源时,也会更清楚每种属性的意义和用途.因此,如果大家想知其 ...
- Java基本数据类型总结、类型转换、常量的声明规范,final关键字的用法
1 Java 基本数据类型 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此,通过 ...
- JAVA基础语法——标识符、修饰符、关键字(个人整理总结)
JAVA基础语法——标识符.修饰符.关键字 一 . 标识符 1.1 什么是标识符 就是程序员在定义java程序时,自定义的一些名字,例如helloworld 程序里关键字class 后跟的Dem ...
- [git] 常用配置
基本配置 对git进行配置时使用 git config 命令进行操作 1. git config 的作用域,缺省等于 local git config --local #只针对某个仓库有效 git ...
- iOS-UIImage图片绘制颜色
- (UIImage *)dtk_setImageColor:(UIColor *)imageColor{ //获取画布 UIGraphicsBeginImageContextWithOptions( ...
- 【LeetCode】13. 罗马数字转整数
题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为 ...
- Linux编程 7 (实时监测进程 top, 结束进程kill,killall)
一. 实时监测进程 top 在一篇里讲到ps命令在收集进程信息时非常有用,但它只能显示某个特定时间点的信息.想要观察那些频繁换进换出的内存进程趋势,用top命令是合适的.使用top命令如下图所示: 在 ...
- java-Timer类使用方法
Timer: public Timer()创建一个新计时器.相关的线程不 作为守护程序运行. 方法摘要: void schedule(TimerTask task, Date time) ...
- 从零开始学 Web 之 Vue.js(三)Vue实例的生命周期
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- Hadoop2源码分析-YARN 的服务库和事件库
1.概述 在<Hadoop2源码分析-YARN RPC 示例介绍>一文当中,给大家介绍了YARN 的 RPC 机制,以及相关代码的演示,今天我们继续去学习 YARN 的服务库和事件库,分享 ...