BZOJ 4756 主席树+dfs

Promotion Counting

Time Limit: 10 Sec   Memory Limit: 128 MB
[ Submit][ Status][ Discuss]

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出每个点序和每个点的子树的最大序,然后用主席树处理下,最后询问n次。

复杂度n*logn*logn


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<deque>
#include<map>
using namespace std;
int a[200010],b[200010],lc[2000005],rc[2000005],root[200005],sum[2000005],cnt=0,head[200005],tot=0,c[100005],vis[100005],dt=1,dp[100005];  
void add(int l,int r,int x,int &cur,int cur1){  
    cur=++cnt;  
    lc[cur]=lc[cur1];  
    rc[cur]=rc[cur1];  
    sum[cur]=sum[cur1]+1;  
    if(l==r)return;  
    int mid=l+r>>1;  
    if(x<=mid)add(l,mid,x,lc[cur],lc[cur1]);  
    else add(mid+1,r,x,rc[cur],rc[cur1]);  
}  
int query(int l,int r,int cur,int cur1,int k){  
    if(l==r)return l;  
    int mid=l+r>>1;  
    int t=sum[lc[cur]]-sum[lc[cur1]];  
    if(t>=k)return query(l,mid,lc[cur],lc[cur1],k);  
    else return query(mid+1,r,rc[cur],rc[cur1],k-t);  
}  
struct node{
	int to,nex;
}edge[200005];
void adds(int u,int v){
	edge[tot].to=v;
	edge[tot].nex=head[u];
	head[u]=tot++;
}
int dfs(int t){
	int i;
	for(i=head[t];~i;i=edge[i].nex){
		int v=edge[i].to;
		if(!vis[v]){
			vis[v]=++dt;
			dp[v]=dt;
			a[dt]=c[v];
			b[dt]=c[v];
			vis[t]=max(dfs(v),vis[t]);
		}
	}
	return vis[t];
}
int main(){  
    int i,j,n,m;  
    memset(head,-1,sizeof(head));
    scanf("%d",&n);  
    for(i=1;i<=n;i++){  
        scanf("%d",&c[i]);
    }  
    int ds;
    for(i=2;i<=n;i++){
    	scanf("%d",&ds);
    	adds(ds,i);
    	adds(i,ds);
    }
    dp[1]=1;
    vis[1]=1;
    a[dt]=c[1];
    b[dt]=a[dt];
    dfs(1);
    sort(b+1,b+1+n);  
    for(i=1;i<=n;i++){  
        a[i]=lower_bound(b+1,b+1+n,a[i])-b;  
        add(1,n,a[i],root[i],root[i-1]);  
    }  
    for(i=1;i<=n;i++){
		int l=1,r=vis[i]-dp[i]+1;
		while(l+1<r){//二分查找当前的数在其子树区间第几大
			int mid=l+r>>1;
			if(b[query(1,n,root[vis[i]],root[dp[i]-1],mid)]<=c[i]){
				l=mid;
			}
			else r=mid;
		}
        if(b[query(1,n,root[vis[i]],root[dp[i]-1],r)]==c[i])printf("%d\n",vis[i]-dp[i]+1-r);
        else printf("%d\n",vis[i]-dp[i]+1-l);
    }  
    return 0;  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值