传送门: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. php杂记——2(数组的使用)

    1.建立升序数组:range(); $numarr1 = range(1,4); //(1,2,3,4) $numarr2 = range(1,10,2); //(1,3,5,7,9) $letter ...

  2. Sql Server 2008 R2数据库中插入中文变成了问号

            通过Insert语句插入数据库中,结果中文都变成了乱码.原因是在数据库中有一个属性需要设置,可以通过Sql server manager studio来进行设置,也要可以通过代码来设置 ...

  3. MediaTypeListWidget->insertItem 添加的label没有填充单元格

    label没有填充满当前的item,但是主界面拉伸或者大小变化之后会填充当前的item 类似相关的问题我猜测都是因为子控件或者需要参考的控件的参考对象的大小在初始化的时候还没有完成最终的初始化,导致大 ...

  4. 27、理解js的继承机制(转载自阮一峰)

    Javascript继承机制的设计思想   作者: 阮一峰 日期: 2011年6月 5日 我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类&qu ...

  5. c++调用Python基础功能

    c++调用Python首先安装Python,以win7为例,Python路径为:c:\Python35\,通过mingw编译c++代码.编写makefile文件,首先要添加包含路径:inc_path ...

  6. 安装CentOS 5.x与多重引导小技巧

    不建议使用Virtualbox安装Linux来学习!本处是学习在计算机上安装Linux. 但现在条件有限,就先使用Virtualbox练习!

  7. android扁平化ProgressBar--progressWheel

    ProgressWheel是git是一个开源项目,为开发者提供一个扁平化的ProgressBar,并可对其进行深度定制   1,将ProgressWheel的源码拷贝到项目中 public class ...

  8. htm,html,xhtml,xml,xsl,dhtml,shtm和shtml的区分

    介绍一下htm,html,xhtml,xml,shtml的区分,以下内容来自百度后的知识整理. HTML和htm: HTML(Hypertext Markup Language)超文本传输语言,是ww ...

  9. TLS协议分析

    TLS协议分析 本文目标: 学习鉴赏TLS协议的设计,透彻理解原理和重点细节 跟进一下密码学应用领域的历史和进展 整理现代加密通信协议设计的一般思路 本文有门槛,读者需要对现代密码学有清晰而系统的理解 ...

  10. asp.net中的cookie

    一.cookie导读,理解什么是cookie 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4kb左右)存储到客户端的硬盘或内存.并且读可以取出来的一种技术. 2.当你浏览某网 ...