【bzoj4756】[Usaco2017 Jan]Promotion Counting

来源:互联网 发布:数据库中union all 编辑:程序博客网 时间:2024/05/22 00:22

Description

The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent 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 man
ager 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 seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. 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号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
Input

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号奶牛的经理(树中的父亲)
Output

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大
Sample Input

5

804289384

846930887

681692778

714636916

957747794

1

1

2

3
Sample Output

2

0

1

0

0

题解
离散化+dfs序+可持久化线段树

代码

#include<bits/stdc++.h>#define mod 1000000009#define inf 10000000#define N 100015#define pa pair<long long,int>typedef long long ll;using namespace std;inline int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int Ls[N],p[N],sum[10000000],sz,Head[N];int Next[N],ret[N],tot,tim,l[N],r[N];int ls[10000000],rs[10000000],rt[N],n;inline void ins(int u,int v){ret[++tot]=v;Next[tot]=Head[u];Head[u]=tot;}void update(int k1,int &k2,int l,int r,int x){    k2=++sz;sum[k2]=sum[k1]+1;    if (l==r)return;    int mid=(l+r)>>1;    ls[k2]=ls[k1];rs[k2]=rs[k1];    if (x<=mid) update(ls[k1],ls[k2],l,mid,x);    else update(rs[k1],rs[k2],mid+1,r,x);}int query(int k1,int k2,int l,int r,int x){    if (l==r) return 0;    int mid=(l+r)>>1;    if (x<=mid) return query(ls[k1],ls[k2],l,mid,x)+sum[rs[k1]]-sum[rs[k2]];    else return query(rs[k1],rs[k2],mid+1,r,x);}void dfs(int u){    l[u]=++tim;update(rt[l[u]-1],rt[l[u]],1,n,p[u]);    for (int i=Head[u];i;i=Next[i]) dfs(ret[i]);    r[u]=tim;}int main(){    n=read();    for (int i=1;i<=n;i++) p[i]=Ls[i]=read();    sort(Ls+1,Ls+n+1);    for (int i=1;i<=n;i++) p[i]=lower_bound(Ls+1,Ls+n+1,p[i])-Ls;    for (int i=2;i<=n;i++)    {        int fa=read();        ins(fa,i);    }    dfs(1);    for (int i=1;i<=n;i++)        printf("%d\n",query(rt[r[i]],rt[l[i]],1,n,p[i]));    return 0;}
阅读全文
0 0
原创粉丝点击