题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数

本题考查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)的更多相关文章

  1. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  2. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

    P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...

  3. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...

  4. [USACO17JAN]Promotion Counting晋升者计数

    题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...

  5. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  6. BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数

    Description The cows have once again tried to form a startup company, failing to remember from past ...

  7. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  8. luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...

  9. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

随机推荐

  1. JAVA版本号微信公众账号开源项目版本号公布-jeewx1.0(捷微)

    JeeWx, 敏捷微信开发,简称"捷微". 捷微是一款免费开源的微信公众账号开发平台. 平台介绍: 一.简单介绍 jeewx是一个开源,高效.敏捷的微信开发平台採用JAVA语言,它 ...

  2. eclipse怎样开启/关闭代码提示功能

    把以下红色框中的勾勾选上就是使用代码提示功能,不选就是关闭.

  3. C# - Generics

    Generics were added to version 2.0 of the C# language and the common language runtime (CLR). Generic ...

  4. formValidator向struts2 ajax传參验证

    话不多说,直接上代码 页面head里面的formvalidator验证配置 <script type="text/javascript"> $(document).re ...

  5. selenium实例学习地址

    一个完整的maven配置selenium webdriver工程实例   http://www.spasvo.com/ceshi/open/kygncsgj/Selenium/201312209580 ...

  6. python spark 随机森林入门demo

    class pyspark.mllib.tree.RandomForest[source] Learning algorithm for a random forest model for class ...

  7. [POJ 3345] Bribing FIPA

    [题目链接] http://poj.org/problem?id=3345 [算法] 树形背包 [代码] #include <algorithm> #include <bitset& ...

  8. 【POJ 2891】 Strange Way to Express Integers

    [题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...

  9. hammer教程

    一.前言 移动端框架当前还处在初级阶段,但相对于移动端的应用来说已经有很长时间了.虽然暂时还没有PC端开发的需求量大,但移动端的Web必然是一种趋势,在接触移动端脚本的过程中,最开始想到的是juqer ...

  10. NOIP2013 D1T3 货车运输

    [NOIP2013T3]货车运输 背景 noip2013day1 描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重 量限制,简称限重.现在有 q 辆货 ...