题意

题目链接

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. cobub razor 安装及使用

    server端安装及配置 apache2 + Mysql5.7 + php7 + redis 参见:http://docs.cobub.com/pages/viewpage.action?pageId ...

  2. [Mac]如何让两个窗口各占半个屏幕

    OS X中的拆分视图El Capitan或更高版本允许您使用两个应用程序填充Mac屏幕,而无需手动移动和调整窗口大小. 进入拆分视图 按住 窗口左上角的全屏按钮  . 当您按住按钮时,窗口会缩小,您可 ...

  3. 人生苦短:Python里的17个“超赞操作

    人生苦短,我选Python”.那么,你真的掌握了Python吗?   1. 交换变量 有时候,当我们要交换两个变量的值时,一种常规的方法是创建一个临时变量,然后用它来进行交换.比如: # 输入 a = ...

  4. MySQL:binlog 和 redo log

    [参考文章]:MySQL中Redo与Binlog顺序一致性问题? [参考文章]:极客时间 1. 数据更新时的日志处理流程 1.1 redo log(prepare状态) 此时SQL已经成功执行了,已经 ...

  5. java后端导入excel模板和导入excel文件去读数据

    模板转载地址:https://www.cnblogs.com/zhangyangtao/p/9802948.html 直接上代码(我是基于ssm写的demo,导入文件目前只能读取.xls后缀的exce ...

  6. Dispatch Queue 之 dispatch_async

     可以看到,在queue里的任务,不仅仅是一个函数,也可以是一个其他的queue. 下面是一个例子.  全局队列的处理 一般一个全局队列有多个线程,这些个线程会遍历并处理链表里的任务. 对于全局队 ...

  7. 机器学习技法笔记:05 Kernel Logistic Regression

    Roadmap Soft-Margin SVM as Regularized Model SVM versus Logistic Regression SVM for Soft Binary Clas ...

  8. LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy

    要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离) Given a binary tree, find its maximum depth. The max ...

  9. C++Primer笔记之复制控制

    复制控制这一节需要注意的地方不多,主要有以下几点: 1.定义自己的复制构造函数 什么时候需要定义自己的复制构造函数,而不用系统提供的,主要遵循以下的经验说明: 某些类必须对复制对象时发生的事情加以控制 ...

  10. 分布式作业 Elastic-Job 快速上手指南,从理论到实战一文搞定!

    Elastic-Job支持 JAVA API 和 Spring 配置两种方式配置任务,这里我们使用 JAVA API 的形式来创建一个简单的任务入门,现在都是 Spring Boot 时代了,所以不建 ...