传送门: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. 读取hbase数据到mysql

    先写一个自己的MyRecordWriter类 extends RecordWriter package calllog; import java.io.IOException; import java ...

  2. log报错: Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.

    报错: 解决方式: 1.登录数据库查看错误原因 结果发现账号无法正常登录出现账号被锁定的错误. 2.如何账号解锁? 用sys系统管理员账号登录数据库 SQL> alter user 用户名 ac ...

  3. Ajax请求被缓存的几种处理方式

    Ajax请求被缓存的几种处理方式 我们都知道IE会针对ajax请求的地址缓存请求结果,直到缓存过期之前,针对相同地址发出的请求,只有第一次会请求会真正发送到服务端.在某种情况下,这种缓存机制确实能提高 ...

  4. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

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

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

  6. HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description There are N points in total. Every point moves in certain direction and certain speed. W ...

  7. 牛客网/LeetCode/七月在线/HelloWorld114

    除了知乎,还有这些网站与offer/内推/秋招/春招相关. 其中HelloWorld114更是囊括许多IT知识. 当然,我们可以拓宽思考的维度,既然课堂上的老师讲不好,我们可以自己找资源啊= => ...

  8. C++STL——list

    一.相关定义 list 链表,分配的内存不连续 可以高效地进行插入/删除元素 不可随机访问,访问速度慢 特征 只能通过迭代器来访问list中的元素 在头和尾都可以插入元素 二.list [前提条件] ...

  9. qemu的配置

    qemu的配置: buildroot的配置不需要多做配置,对了,设置下生成的文件系统是rootfs.ext2 内核打开virtio qemu脚本会在后面 疑问: 1)为什么qemu启动起来之后,没有e ...

  10. 【题解】CQOI2015任务查询系统

    主席树,操作上面基本上是一样的.每一个时间节点一棵树,一个树上的每个节点代表一个优先级的节点.把开始和结束时间点离散,在每一棵树上进行修改.注意因为一个时间节点可能会有多个修改,但我们要保证都在同一棵 ...