传送门

此题很有意思,有多种解法

1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞

2.按dfs序建立主席树

3.线段树的合并

前两个都会,于是学习一下线段树的合并。。

道理用文字解释不清。。。直接看代码就能看懂。。

可以脑补出,合并的操作复杂度是logn的,总时间复杂度nlogn

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100001 int n, m, cnt, size;
int head[N], to[N << 1], next[N << 1], root[N], sum[N * 20], ls[N * 20], rs[N * 20], ans[N], a[N], b[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void insert(int &now, int l, int r, int x)
{
now = ++size;
if(l == r)
{
sum[now] = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) insert(ls[now], l, mid, x);
else insert(rs[now], mid + 1, r, x);
sum[now] = sum[ls[now]] + sum[rs[now]];
} inline void merge(int &x, int y)
{
if(!x || !y)
{
x += y;
return;
}
sum[x] += sum[y];
merge(ls[x], ls[y]);
merge(rs[x], rs[y]);
} inline int query(int now, int l, int r, int x, int y)
{
if(x <= l && r <= y) return sum[now];
int mid = (l + r) >> 1, ret = 0;
if(x <= mid) ret += query(ls[now], l, mid, x, y);
if(mid < y) ret += query(rs[now], mid + 1, r, x, y);
return ret;
} inline void dfs(int u)
{
int i, v;
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
dfs(v);
merge(root[u], root[v]);
}
ans[u] = query(root[u], 1, m, a[u] + 1, m);
} int main()
{
int i, x;
n = read();
memset(head, -1, sizeof(head));
for(i = 1; i <= n; i++) a[i] = b[i] = read();
for(i = 2; i <= n; i++)
{
x = read();
add(x, i);
}
std::sort(b + 1, b + n + 1);
m = std::unique(b + 1, b + n + 1) - b - 1;
for(i = 1; i <= n; i++)
a[i] = std::lower_bound(b + 1, b + m + 1, a[i]) - b;
for(i = 1; i <= n; i++)
insert(root[i], 1, m, a[i]);
dfs(1);
for(i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
}

  

[BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)的更多相关文章

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

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

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

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

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

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

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

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

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

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

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

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

  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. rhythmbox插件开发笔记2:背景知识学习 D-Bus&VFS&Gio& Python GTK+ 3

    这次主要简单介绍下相关的背景知识 D-Bus&VFS&Gio& Python GTK+ 3  D-Bus D-Bus是开源的进程通信(IPC)系统,它允许多个进程进行实时通信. ...

  2. 【原创】最有效解决IE8 position兼容性问题

    看了网上的的帖子真是水的一塌糊涂,完全没有解决我和广大网友们的关于ie8下position兼容性问题. 网上有的技术我就不说了 ,大家自行搜索,我想说的重点是 ie8不支持html5的新标签.这是重点 ...

  3. vue-awesome-swiper 插件

    Swiper 版本区分了组件和普通版本 (1)npm install vue-awesome-swiper –save (2)在 main,js 里引入(全局): import VueAwesomeS ...

  4. js中声明函数的三种方式

    己亥年  庚午月 癸巳日  宜入宅 忌婚嫁 函数声明方式 声明 : function first(){}: 调用:first() 函数表达式声明方式   声明: var second=function ...

  5. CAS (Compare and Swap)

    synchronized是悲观锁 注意:实现了CAS的有原子类(AtomicInteger,AtomicLong,等等原子类) CAS 是乐观锁,一种高效实现线程安全性的方法 1.支持原子更新操作,适 ...

  6. 利用javascript实现二维数组的筛选

    背景: 在项目中,我需要对解析后的nc文件数据进行筛选,选出符合要求的点,绘制相应的polygon. 代码:ajax获取某一时刻的数据 var url ="/api/config/PostC ...

  7. c++ 递归求一个数的阶乘

    #include <iostream> using namespace std; long factorial(int value); int main() { int value; co ...

  8. jdk环境变量配置(参考自《疯狂java讲义》)

    做个记录,免得每次配环境都要百度 环境变量的配置 path环境变量配置的作用:程序的执行需要使用外部指令javac, 但是javac指令仅仅能在JDK安装目录下的bin目录下使用,因此程序只能写入bi ...

  9. PAT 乙级 1037

    题目 题目地址:PAT 乙级 1037 题解 本题有两个版本的代码,初版因为种种问题写得比较繁琐,具体的分析见后文,更新的之后的版本相对来说要好很多,代码也比较清晰简洁. 初版的代码主要有如下几方面的 ...

  10. python 发送附件

    #!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: sksendmail.py #@time: 2018/8/20 13:3 ...