[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 < ...
随机推荐
- 洛谷 P1463 [SDOI2005]反素数ant
P1463 [SDOI2005]反素数ant 题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i< ...
- LeetCode之RemoveElement
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- 关于double类型数字相加位数发生变化的问题
因为计算机内部存贮本身的缺陷,导致double类型的数字相加.得到的结果有非常多位,比方 774.23 750.0 2638.66 4162.889999999999 看到这个是不是非常晕 当然 ...
- bootstrap checkbox
在使用bootstrap库中的checkboxlistrow时,我想要依据checkbox是否至少选中了一个选项来确定页面的跳转,即须要在js中操作checkbox.这样就存在一个问题,一般的chec ...
- [Cypress] Find and Test Focused Input with Chrome’s DevTools in Cypress
In this lesson, we’ll add tests that finds a focused input. We’ll use Chrome’s dev tools from inside ...
- Getting started with ASP.NET Core MVC and Visual Studio
This tutorial will teach you the basics of building an ASP.NET Core MVC web app using Visual Studio ...
- HTML5动态时钟
实现效果 源码可以去github下载 地址:https://github.com/feifeiliu/jsBlock 参考:慕课网动态时钟
- ubuntu-设置分辨率
xrandr -s 1440x900 -r 60 前提是,分辨率选项中有对应的设置选项.
- Delete, drop table, truncate之间的区别
Delete, drop table, truncate有什么区别? delete 删除表中数据,可以删除一条或多条记录,可以回滚,记录操作日记,是DML truncate table,一次性清空表中 ...
- 浅析CLR的事件
文章目录: 1.C#(.net framework框架)中的事件以及特点 2.事件的组成部分 3.编辑器如何实现事件的 4.显式实现事件 1.C#(.net framework ...