「USACO 2017 JAN」 Promotion Counting

题目描述

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its "parent" in the tree). Each cow ii has a distinct proficiency rating, p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a manager of a manager) of cow jj, then we say jj is a subordinate of ii.

Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow ii in the company, please count the number of subordinates jj where p(j)>p(i).

n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。

输入

The first line of input contains N

The next N lines of input contain the proficiency ratings p(1)…p(N) 

for the cows. Each is a distinct integer in the range 1…1,000,000,000

The next N-1 lines describe the manager (parent) for cows 2…N

Recall that cow 1 has no manager, being the president.

n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)

输出

Please print N lines of output. The ith line of output should tell the number of

subordinates of cow ii with higher proficiency than cow i.

共n行,每行输出奶牛i的下属中有几个能力值比i大

样例输入

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

样例输出

2
0
1
0
0

dfs序列+将权值倒过来之后bit

/**************************************************************

    Problem: 4756

    User: ictsing

    Language: C++

    Result: Accepted

    Time:308 ms

    Memory:7464 kb

****************************************************************/

 

//BZOJ[Usaco2017 Jan]Promotion Counting

/*

给出一棵带点权的树,要求查询某个节点的子树中有几个节点权值比它打

将权值倒过来,dfs序之后bit即可

*/

#include <iostream>

#include <cstdio>

#include <algorithm>

usingnamespacestd;

constintmt = 100000+5;

structpoint

{

    intval,id;

}cow[mt];

intans[mt];

inlineintread()

{

    intnum=0,flag=1;

    charch;

    do{

       ch=getchar();

       if(ch=='-') flag=-1;

    }while(ch<'0'||ch>'9');

    do{

       num=num*10+ch-'0';

       ch=getchar();

    }while(ch>='0'&&ch<='9');

    returnnum*flag;

}

intto[mt<<1],nxt[mt<<1],adj[mt];

intfa[mt];

intecnt=0;

voidadde(intu,intv)

{

    ecnt++,to[ecnt]=v,nxt[ecnt]=adj[u],adj[u]=ecnt;

}

boolcmp(point a,point b)

{

    returna.val>b.val;

}

intst[mt],ed[mt];

intdfn_clock=0;

voiddfs(intu)

{

    st[u]=++dfn_clock;

    for(inti=adj[u];i;i=nxt[i])

    {

        intv=to[i];

        if(v^fa[u]) dfs(v);

    }

    ed[u]=dfn_clock;

}

intn;

inttree[mt];

voidadd(intx,intval)

{

    while(x<=n)

    {

        tree[x]+=val;

        x+=(x&(-x));

    }

}

intsum(intx)

{

    intres=0;

    while(x)

    {

        res+=tree[x];

        x-=(x&(-x));

    }

    returnres;

}

intmain()

{

    n=read();

    for(inti=1;i<=n;i++)

    {

        cow[i].val=read();

        cow[i].id=i;

    }

    for(inti=2;i<=n;i++)

    {

        fa[i]=read();

        adde(fa[i],i);

        adde(i,fa[i]);

    }

    sort(cow+1,cow+n+1,cmp);

    dfs(1);

    for(inti=1;i<=n;i++)

    {

        intr=ed[cow[i].id];

        intl=st[cow[i].id]+1-1;

        ans[cow[i].id]=sum(r)-sum(l);

        add(l,1);

    }

    for(inti=1;i<=n;i++)

        printf("%d\n",ans[i]);

    return0;

}


2017-04-24 #dfs#bit#BZOJ#C++  

评论

热度(6)