描述

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号奶牛为树根。 问对于每个奶牛来说,它的子树中有几个能力值比它大的。

输入

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

康复训练ing。。。

这题本蒟蒻用线段树合并水过去了。。。

暂时没想到其它的做法毕竟实力太弱了

代码;

#include<bits/stdc++.h>
#define N 100005
using namespace std;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
inline void write(int x){
    if(x>9)write(x/10);
    putchar((x%10)^48);
}
int ans[N],a[N],b[N],n,sig,first[N],tot=0,cnt=0,rt[N],siz[N*20],son[N*20][2];
struct edge{int v,next;}e[N<<1];
inline void add(int u,int v){e[++cnt].v=v,e[cnt].next=first[u],first[u]=cnt;}
inline void modify(int&p,int l,int r,int k){
    if(!p)p=++tot;
    if(l==r){siz[p]=1;return;}
    int mid=l+r>>1;
    if(k<=mid)modify(son[p][0],l,mid,k);
    else modify(son[p][1],mid+1,r,k);
    siz[p]=siz[son[p][0]]+siz[son[p][1]];
}
inline int merge(int x,int y,int l,int r){
    if(!x||!y)return x+y;
    if(l==r){siz[x]+=siz[y];return x;}
    int mid=l+r>>1;
    son[x][0]=merge(son[x][0],son[y][0],l,mid);
    son[x][1]=merge(son[x][1],son[y][1],mid+1,r);
    siz[x]=siz[son[x][0]]+siz[son[x][1]];
    return x;
}
inline int query(int p,int l,int r,int v){
    if(l==r)return siz[p];
    int mid=l+r>>1;
    if(v<=mid)return query(son[p][0],l,mid,v)+siz[son[p][1]];
    return query(son[p][1],mid+1,r,v);
}
inline void dfs(int p){
    for(int i=first[p];i;i=e[i].next){
        int v=e[i].v;
        dfs(v);
        rt[p]=merge(rt[p],rt[v],1,sig);
    }
    ans[p]=query(rt[p],1,sig,a[p])-1;
}
int main(){
    n=read();
    for(int i=1;i<=n;++i)a[i]=b[i]=read();
    sort(b+1,b+n+1),sig=unique(b+1,b+n+1)-b-1;
    for(int i=1;i<=n;++i)modify(rt[i],1,sig,(a[i]=(lower_bound(b+1,b+sig+1,a[i])-b)));
    for(int i=2;i<=n;++i)add(read(),i);
    dfs(1);
    for(int i=1;i<=n;++i)write(ans[i]),puts("");
    return 0;
}

最新文章

  1. springmvc 格式化使用Jackjson格式化报Failed to write HTTP message
  2. 对html与body的一些研究与理解
  3. Android中的动画机制
  4. .Net环境下的缓存技术介绍
  5. Two Sum I &amp; II
  6. ACM题目————二叉树最大宽度和高度
  7. android开发 eclipse alt+”/”自动提示失效
  8. c#等待所有子线程执行完毕方法
  9. poj 2586 Y2K Accounting Bug
  10. [Head First Python]6. summary
  11. LeetCode-001 Two Sum
  12. 条码的种类(types of barcode)
  13. VUE2.0+VUE-Router做一个图片上传预览的组件
  14. ExperDot的博客目录导航
  15. python正则表达式判断素数【厉害了】
  16. if判断,switch语句
  17. Django之Models(三)
  18. weblogic反序列化漏洞CVE-2018-2628-批量检测脚本
  19. (已解决)#warning:尚未配置[微信]URL Scheme:wx4868b35061f87884, 无法使用进行授权。
  20. 两table水平滚动条级联滚动(同步滚动)。 table1放标题,table2放内容。

热门文章

  1. apache中 MaxClients 与MaxRequestsPerChild
  2. Linux命令详解----iostat
  3. jdk免安装对应配置
  4. H5新标签
  5. 本地Facts
  6. Haskell语言学习笔记(41)Parsec(1)
  7. avalon做的抽奖效果
  8. Python中文件编码的检测
  9. python中库学习
  10. Interrupt handler