[USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)
题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数
本题考查dfs的性质
离散+树状数组求逆序对
先离散
我们发现,求逆序对时,某节点的兄弟节点会干扰答案
所以,我们在递推时统计一次答案,递归时再统计一次答案,两者的差值就是最终结果
#include <bits/stdc++.h>
#define dd double
#define N 100100
using namespace std; int n,cnt,ma,lst;
int a[N],head[N],s[N],ans[N];
struct EDGE{
int to,nxt;
}edge[N*];
struct node{
int og,mx,id;
}d[N];
int cmp1(node a,node b) {return a.og<b.og;}
int cmp2(node a,node b) {return a.id<b.id;}
void update(int x,int p)
{
for(int i=x;i<=ma;i+=(i&(-i)))
{
s[i] += p;
}
}
int query(int x)
{
int ans=;
for(int i=x;i>;i-=(i&(-i)))
{
ans += s[i];
}
return ans;
}
void edge_add(int u,int v)
{
cnt++;
edge[cnt].to = v;
edge[cnt].nxt= head[u];
head[u] = cnt;
}
void discrete()
{
sort(d+,d+n+,cmp1);
for(int i=;i<=n;i++)
{
if(d[i].og==d[i-].og)
{
d[i].mx=d[i-].mx;
}else{
d[i].mx=++ma;
}
}
sort(d+,d+n+,cmp2);
for(int i=;i<=n;i++) a[i] = d[i].mx;
}
void dfs(int x,int fa)
{
for(int j=head[x];j!=-;j=edge[j].nxt)
{
int v=edge[j].to;
if(v==fa) continue;
int s1=query(ma)-query(a[x]);
dfs(v,x);
int s2=query(ma)-query(a[x]);
ans[x] += s2-s1;
}
update(a[x],);
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) {scanf("%d",&d[i].og);d[i].id=i;}
memset(head,-,sizeof(head));
int x;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
edge_add(i,x);
edge_add(x,i);
}
discrete();
dfs(,-);
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}
[USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)的更多相关文章
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...
- [USACO17JAN]Promotion Counting晋升者计数
题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...
- 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数
Description The cows have once again tried to form a startup company, failing to remember from past ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...
- P3605 [USACO17JAN]Promotion Counting晋升者计数
思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...
随机推荐
- RubyMine快捷键
RubyMine快捷键 ctrl+shift+up/down 向上/向下移动代码段 alt+shift+up/down 向上/向下移动代码行 ctrl+y 删除一行 ctrl+shift+f10 运行 ...
- Mybatis结合Spring注解自己主动扫描源代码分析
作为一个想做架构师的程序猿,必须是一个优秀的程序猿.在引入某一个框架的时候,必需要研究源代码,将新的开源框架的风险变为可控性. 1.Spring结合Mybatis最经常使用的配置. <!--理论 ...
- sqlalchemy.orm.exc.flusherror:错误解决
使用sqlalchemy创建model 初次代码: class UserModel(db.Model): __tablename__ = "users" id = db.Colum ...
- 框架-Java:Spring Cloud
ylbtech-框架-Java:Spring Cloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册. ...
- js获取验证码 秒表效果(原创)
<script src="http://code.jquery.com/jquery-latest.js"></script> <input type ...
- html5之文件操作
用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据.到目前文职,只有FF3.6+和Chrome6 ...
- Service不完全解析
本篇的内容并不是介绍service使用方法和生命周期的,而是对其中的一些要点进行记录和分析. 我们都知道,Service是一个在后台执行的应用组件,用于在后台进行长期操作,例如进行网络事务,播放背景音 ...
- rem简单实现移动端适配
rem:移动web开发 默认字体大小是16px 在<html>中设置字体大小 与em的区别: em是在父级设置字体大小受影响 移动端适配 首先获取屏幕的宽度 计算当前屏幕宽度和640的比例 ...
- Web移动端常见问题
一.按钮点击时出现黑色背景 解决方法: .class { -webkit-tap-highlight-color:rgba(0,0,0,0);} .class { -webkit-appearance ...
- 基于S3C2440数码相框
[参考]韦东山 教学笔记 1. 程序框架1.1 触摸屏: 主按线程,通过socket发给显示进程 --------------------------- 封装事件:ts线程 按键线程 -------- ...