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

本题考查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. 洛谷 P3004 [USACO10DEC]宝箱Treasure Chest

    P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 Bessie and Bonnie have found a treasure chest full of marvel ...

  2. CLLocationManagerDelegate的解说

    1.//新的方法.登陆成功之后(旧的方法就无论了) - (void)locationManager:(CLLocationManager *)manager      didUpdateLocatio ...

  3. log4net写日志的时间附带时区信息

    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss.fffzzz} [%thread] %-5level %logger - %me ...

  4. open Command window here

    http://www.sevenforums.com/tutorials/134831-open-command-window-here-add-remove.html 按照教程里面,下载一个脚本 需 ...

  5. JavaScript:Browser 对象

    ylbtech-JavaScript:Browser 对象 1.  Window 对象返回顶部 1. Window 对象 Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框 ...

  6. ServletContextAware、ServletRequestAware、ServletResponseAware、SessionAware

    转自:ServletContextAware.ServletRequestAware.ServletResponseAware.SessionAware Struts 2提供了Aware接口.Awar ...

  7. [javaEE] Eclipse 默认设置的换行长度

    eclipse 默认设置的换行长度, 格式化代码后,经常换行,非常难看. 1.Java代码 打开Eclipse的Window菜单,然后 Preferences->Java->Code St ...

  8. Redis List 命令技巧

    1.实现栈的功能(先进后出) lpush + lpop = stack > lpush mylist (integer) > lpop mylist " > lpop my ...

  9. Codeforces Round #198 (Div. 2)E题解

    E. Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all ...

  10. [Offer收割]编程练习赛36

    逃离单身节 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector&g ...