传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756

【题解】

dsu on tree,树状数组直接上

O(nlog^2n)

# include <vector>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 2e5 + , N = 4e5 + ;
const int mod = 1e9+; # define RG register
# define ST static int n, a[M];
vector<int> ps;
int ans[M]; int head[M], nxt[M], to[M], tot=;
inline void add(int u, int v) {
++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v;
} # define lb(x) (x&(-x))
struct BIT {
int c[N];
inline void edt(int x, int d) {
for (; x<=n; x+=lb(x)) c[x] += d;
}
inline int sum(int x) {
int ret = ;
for (; x; x-=lb(x)) ret += c[x];
return ret;
}
inline int sum(int x, int y) {
if(x>y) return ;
return sum(y) - sum(x-);
}
}T;
# undef lb int sz[M];
inline void dfssize(int x) {
sz[x] = ;
for (int i=head[x]; i; i=nxt[i]) {
dfssize(to[i]);
sz[x] += sz[to[i]];
}
} bool big[M];
inline void dfsans(int x) {
T.edt(a[x], );
for (int i=head[x]; i; i=nxt[i])
if(!big[to[i]]) dfsans(to[i]);
} inline void delans(int x) {
T.edt(a[x], -);
for (int i=head[x]; i; i=nxt[i])
if(!big[to[i]]) delans(to[i]);
} inline void dsu(int x, bool iskep) {
int mx = -, bigc = -;
for (int i=head[x]; i; i=nxt[i])
if(sz[to[i]] > mx) mx = sz[to[i]], bigc = to[i];
for (int i=head[x]; i; i=nxt[i])
if(to[i] != bigc) dsu(to[i], );
if(bigc != -) big[bigc] = , dsu(bigc, );
dfsans(x);
ans[x] = T.sum(a[x]+, n);
if(bigc != -) big[bigc] = ;
if(!iskep) delans(x);
} int main() {
cin >> n;
for (int i=; i<=n; ++i) {
scanf("%d", &a[i]);
ps.push_back(a[i]);
} sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end()); for (int i=; i<=n; ++i) a[i] = lower_bound(ps.begin(), ps.end(), a[i]) - ps.begin() + ; for (int i=, fa; i<=n; ++i) {
scanf("%d", &fa);
add(fa, i);
} dfssize();
dsu(, ); for (int i=; i<=n; ++i) printf("%d\n", ans[i]); return ;
}

bzoj4756 [Usaco2017 Jan]Promotion Counting的更多相关文章

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

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

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

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

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

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

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

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

  5. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

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

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

  7. 【bzoj 4756】[Usaco2017 Jan] Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...

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

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

  9. 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)

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

随机推荐

  1. 操作Excel的宏

          有时候在Excel中,需要循环的算每一列的值,而这一列的值是某几列的求和或者某种运算后的结果,比如如下的C4=C3+B4      可以用一个宏来实现,宏代码如下: Dim i As In ...

  2. JDBC剖析篇(2):JDBC之PreparedStatement

    一次有人问我为什么要使用JDBC中的PreparedStatement,我说可以“防止SQL注入”,其他的却不能说出个一二三,现在来看看其中的秘密 参考文章: http://www.jb51.net/ ...

  3. Java 图像处理框架-Marvin

    网上看到,摘录过来的,暂时还没涉足这方面的东西 Marvin 1.4.5 的插件接口支持处理多个图像作为输入,新的插件可通过多个图片来确认背景,新的插件可使用多个图片来合并相同场景. Marvin 是 ...

  4. 关于Vue脚手架写法的问题

    问题描述: main.js import Vue from 'vue' import App from './App' /* eslint-disable no-new */ new Vue({ el ...

  5. Visual Studio Code 配置Go 开发环境最简单的方法!!!

    由于大家都知道的原因,在国内如果想访问go等各种资源,都会遇到某种不可预知的神奇问题.导致在VS Code中安装 go 各种插件都会失败. 于是乎,网上就出现了各种各样的解决方案:什么手动git cl ...

  6. 【Dataset】Goodbooks-10k: 图书推荐数据

    当前推荐领域一些公开的据集都是关于电影和音乐的(比如Netflix.Movielens等),没有关于图书推荐的数据.本文将要介绍的就是一份用于图书推荐的数据集,该数据来源于goodreads网站,包含 ...

  7. MySQL训练营02

    一.表操作: 1.MySQL表的数据类型: MySQL的数据类型分为3种: 数值 时间/日期 字符/字符串 (1)数值类型: 包括:TinyInt.SmallInt.MediumInt.Int.Big ...

  8. Week9 Teamework from Z.XML-Scenario testing

    关于场景测试 About Scenario testing   一.关于用户(About Personas) 1 我们如何预期我们的用户对我们的软件的使用 (How do we expect diff ...

  9. @section script{}的使用

    1,MVC视图中,JS代码被放在下面的Razor代码中(@section script{}) 2,这样做的好处是:在视图进行JS编码时是一个很好 的实践,在共享视图(_layout.cshtml),存 ...

  10. 集显也能硬件编码:Intel SDK && 各种音视频编解码学习详解

    http://blog.sina.com.cn/s/blog_4155bb1d0100soq9.html INTEL MEDIA SDK是INTEL推出的基于其内建显示核心的编解码技术,我们在播放高清 ...