题意

题目链接

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(线段树合并)的更多相关文章

  1. BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  2. bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...

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

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

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

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

  5. BZOJ4756:[USACO]Promotion Counting(线段树合并)

    Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...

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

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

  7. bzoj 4756 Promotion Counting —— 线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...

  8. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...

  9. bzoj4756 [Usaco2017 Jan]Promotion Counting

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

随机推荐

  1. Android开发 - 掌握ConstraintLayout(五)偏差(Bias)

    比如实现这样一个场景: "在屏幕宽度的1/4的地方放置一个View" 使用传统布局时,实现按照屏幕的宽度(高度),或者相对两个View之间距离的一个比例来进行布局,就显得非常麻烦, ...

  2. XCode 无法识别设备

    XCode 取消Unpair Device 后不能读取设备 1:退出XCode 2:断开设备连接 3:在终端执行‘sudo pkill usbmuxd’ 4:重启XCode 5:连接设备即可

  3. Android数据存储之SharedPreferences使用

    SharedPreferences是Android中一种轻型的数据存储类.本质上是基于XML文件进行存储Key-Value键值对的数据,生成的XML文件的目录在/data/data/包名/Shared ...

  4. jdk的安装和配置环境变量

    一.下载 JDK是个免费的东东,所以大家不要去百度啥破解版了,直接去官网下载最新版本吧,比较安全,官网地址:http://www.oracle.com/technetwork/java/index.h ...

  5. kaili Linux学习推荐网站

    kaili学习推荐网站 kail中文网:http://www.kali.org.cn/forum.php?gid=67 Kali官方教材:https://kali.training/ Kali Lin ...

  6. HoloLens开发手记 - 使用HoloLens模拟器 Using HoloLens emulator

    首先下载HoloLens模拟器 HoloLens模拟器运行在没有真机的情况下在你的PC上测试应用,属于HoloLens开发工具系列.模拟器使用了Hyper-V虚拟机.通常通过传感器获取的人体和环境输入 ...

  7. iOS-AFN Post JSON格式数据

    - (void)postRequest{ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // >>> ...

  8. 2-3 用组件改写Todolist案例

     编写组件来改写2-2的Todolist案例

  9. UFLDL 教程学习笔记(三)自编码与稀疏性

    UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...

  10. Linux编程 1 (文件系统路径说明, 目录结构说明)

    一. Linux文件系统路径说明 熟悉windows系统的,都知道文件路径表示,如C:\User\rich\Documnets\test.doc. 在linux中目录称为虚拟目录(virtual di ...