传送门: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. ORB-SLAM (四)tracking跟踪解析

    初始化完成后,对于相机获取当前图像mCurrentFrame,通过跟踪匹配上一帧mLastFrame特征点的方式,可以获取一个相机位姿的初始值:为了兼顾计算量和跟踪鲁棒性,处理了三种模型: 1. Tr ...

  2. svn 服务器搭建 (Linux)

    1.svn目前在程序开发工程汇总应用非常普遍,所以学习svn的环境搭建还是很有必要的 2.本次安装的服务是Subversion(svn)针对的环境是Linux,Subversion(SVN) 是一个开 ...

  3. web框架与爬虫

    所有的web框架 http://www.cnblogs.com/wupeiqi/articles/5341480.html 爬虫技术 http://www.cnblogs.com/wupeiqi/ar ...

  4. 利尔达NB-IOT模块对接移动onenet平台步骤

    1. 首先登陆浙江移动onenet网站,http://openiot.zj.chinamobile.com/,进入右上角的开发者中心,然后才能看到创建产品 2. 填写产品的信息,其他信息按照个人实际填 ...

  5. nexys4-DDR开发板温度传感器ADT7420U

    1. 用这个板子做个什么功能来学习?板子上有个温度传感器,看下官方是否有例程,板子售价1780元,相当的贵,下面是I2C接口, 看下芯片的引脚图 2. 资料下载地址,得注册账号 https://ref ...

  6. Linux/CentOS防CC攻击脚本

    #!/bin/sh cd /var/log/httpd/ cat access_log|awk > a cp /dev/null access_log cp /dev/null error_lo ...

  7. ASP.NET Web API 2 返回 Json格式

    最近在学习ASP.NET的Web API,刚刚开始以为会有些复杂,结果却非常简单. 学习的地址:http://www.asp.net/web-api/overview/getting-started- ...

  8. mysql字段名与关键字重复解决办法

    mysql 关键字与字段名相同,插入或者修改里会报错 解决办法: 1.改字段名,如果库里面表结构关系不复杂,修改字段名就解决 2.在插入或者修改字段时,字段名加上  ` 包上,注意:这里不是引号,是英 ...

  9. Floatingip

    浮动IP相关功能点: 模块 功能 描述 备注 FloatingIP 创建浮动IP 指定带宽大小创建单个/多个浮动IP   指定子网.指定IP创建浮动IP   绑定浮动IP,修改带宽 绑定浮动IP到指定 ...

  10. 5.爬虫 requests库讲解 高级用法

    0.文件上传 import requests files = {'file': open('favicon.ico', 'rb')} response = requests.post("ht ...